Search in sources :

Example 26 with JsonFactory

use of org.codehaus.jackson.JsonFactory in project OpenRefine by OpenRefine.

the class JsonImporter method createParserUIInitializationData.

@Override
public JSONObject createParserUIInitializationData(ImportingJob job, List<JSONObject> fileRecords, String format) {
    JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
    if (fileRecords.size() > 0) {
        try {
            JSONObject firstFileRecord = fileRecords.get(0);
            File file = ImportingUtilities.getFile(job, firstFileRecord);
            JsonFactory factory = new JsonFactory();
            JsonParser parser = factory.createJsonParser(file);
            PreviewParsingState state = new PreviewParsingState();
            Object rootValue = parseForPreview(parser, state);
            if (rootValue != null) {
                JSONUtilities.safePut(options, "dom", rootValue);
            }
        } catch (IOException e) {
            logger.error("Error generating parser UI initialization data for JSON file", e);
        }
    }
    return options;
}
Also used : JSONObject(org.json.JSONObject) JsonFactory(org.codehaus.jackson.JsonFactory) JSONObject(org.json.JSONObject) IOException(java.io.IOException) File(java.io.File) JsonParser(org.codehaus.jackson.JsonParser)

Example 27 with JsonFactory

use of org.codehaus.jackson.JsonFactory in project OpenRefine by OpenRefine.

the class Row method loadStreaming.

public static Row loadStreaming(String s, Pool pool) throws Exception {
    JsonFactory jsonFactory = new JsonFactory();
    JsonParser jp = jsonFactory.createJsonParser(s);
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        return null;
    }
    List<Cell> cells = new ArrayList<Cell>();
    boolean starred = false;
    boolean flagged = false;
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldName = jp.getCurrentName();
        jp.nextToken();
        if (STARRED.equals(fieldName)) {
            starred = jp.getBooleanValue();
        } else if (FLAGGED.equals(fieldName)) {
            flagged = jp.getBooleanValue();
        } else if ("cells".equals(fieldName)) {
            if (jp.getCurrentToken() != JsonToken.START_ARRAY) {
                return null;
            }
            while (jp.nextToken() != JsonToken.END_ARRAY) {
                Cell cell = Cell.loadStreaming(jp, pool);
                cells.add(cell);
            }
        }
    }
    return (cells.size() > 0) ? new Row(cells, flagged, starred) : new Row(0);
}
Also used : JsonFactory(org.codehaus.jackson.JsonFactory) ArrayList(java.util.ArrayList) JsonParser(org.codehaus.jackson.JsonParser)

Example 28 with JsonFactory

use of org.codehaus.jackson.JsonFactory in project OpenRefine by OpenRefine.

the class Recon method loadStreaming.

public static Recon loadStreaming(String s, Pool pool) throws Exception {
    JsonFactory jsonFactory = new JsonFactory();
    JsonParser jp = jsonFactory.createJsonParser(s);
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        return null;
    }
    return loadStreaming(jp, pool);
}
Also used : JsonFactory(org.codehaus.jackson.JsonFactory) JsonParser(org.codehaus.jackson.JsonParser)

Example 29 with JsonFactory

use of org.codehaus.jackson.JsonFactory in project OpenRefine by OpenRefine.

the class ReconCandidate method loadStreaming.

public static ReconCandidate loadStreaming(String s) throws Exception {
    JsonFactory jsonFactory = new JsonFactory();
    JsonParser jp = jsonFactory.createJsonParser(s);
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        return null;
    }
    return loadStreaming(jp);
}
Also used : JsonFactory(org.codehaus.jackson.JsonFactory) JsonParser(org.codehaus.jackson.JsonParser)

Example 30 with JsonFactory

use of org.codehaus.jackson.JsonFactory in project jstorm by alibaba.

the class JSONUtil method formatPOJO2JSON.

/**
	 * POJO对象转换为JSON
	 * @param pojo
	 * @return
	 */
public String formatPOJO2JSON(Object pojo) {
    StringWriter stringWriter = new StringWriter();
    String json = "";
    try {
        JsonGenerator gen = new JsonFactory().createJsonGenerator(stringWriter);
        MAPPER.writeValue(gen, pojo);
        gen.close();
        json = stringWriter.toString();
    } catch (Exception e) {
        LOGGER.error(pojo.getClass().getName() + "转json出错", e);
    }
    return json;
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(org.codehaus.jackson.JsonFactory) JsonGenerator(org.codehaus.jackson.JsonGenerator) IOException(java.io.IOException) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Aggregations

JsonFactory (org.codehaus.jackson.JsonFactory)38 JsonGenerator (org.codehaus.jackson.JsonGenerator)16 JsonParser (org.codehaus.jackson.JsonParser)13 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)13 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 File (java.io.File)5 GenericRecord (org.apache.avro.generic.GenericRecord)4 JsonNode (org.codehaus.jackson.JsonNode)4 OutputStream (java.io.OutputStream)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 BufferedWriter (java.io.BufferedWriter)2 DataOutputStream (java.io.DataOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2