Search in sources :

Example 6 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project opentsdb by OpenTSDB.

the class TestJSON method parseToStreamASCIIByte.

@Test
public void parseToStreamASCIIByte() throws Exception {
    JsonParser jp = JSON.parseToStream("{\"utf\":\"aeriennes\",\"ascii\":\"aariennes\"}".getBytes());
    HashMap<String, String> map = this.parseToMap(jp);
    assertEquals("aeriennes", map.get("utf"));
    assertEquals("aariennes", map.get("ascii"));
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 7 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project opentsdb by OpenTSDB.

the class TestJSON method parseToMap.

/** Helper to parse an input stream into a map */
private HashMap<String, String> parseToMap(final InputStream is) throws Exception {
    JsonParser jp = JSON.parseToStream(is);
    HashMap<String, String> map = new HashMap<String, String>();
    String field = "";
    String value;
    while (jp.nextToken() != null) {
        if (jp.getCurrentToken() == JsonToken.FIELD_NAME && jp.getCurrentName() != null) {
            field = jp.getCurrentName();
        } else if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
            value = jp.getText();
            map.put(field, value);
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 8 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project fastjson by alibaba.

the class JacksonPageModelParser method parse.

/**
     * @param content
     * @throws JsonParseException
     * @throws IOException
     */
public PageInstance parse(String content) throws JsonParseException, IOException {
    JsonFactory f = new JsonFactory();
    JsonParser parser = f.createJsonParser(content);
    // move to the start of the
    JsonToken current = parser.nextToken();
    // object
    // get instanceId
    String instanceId = getNextTextValue("sid", parser);
    // get pageId
    String pageId = getNextTextValue("cid", parser);
    // move to field: segments
    current = parser.nextToken();
    assertExpectedFiled(parser.getCurrentName(), "segments", parser.getCurrentLocation());
    PageInstance pageInstance = new PageInstance();
    pageInstance.setCid(pageId);
    pageInstance.setSid(Long.valueOf(instanceId));
    pageInstance.setSegments(parseSegments(parser));
    return pageInstance;
// 构建组件树,用于递归渲染
// pageInstance.buildComponentTree();
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParserHelper.assertExpectedJsonToken(com.alibaba.json.test.performance.JacksonPageModelParser.JsonParserHelper.assertExpectedJsonToken) PageInstance(com.alibaba.json.test.entity.pagemodel.PageInstance) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 9 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project immutables by immutables.

the class BsonEncoding method unwrapJsonable.

public static DBObject unwrapJsonable(String json) {
    try {
        JsonParser parser = JSON_FACTORY.createParser(json);
        parser.nextToken();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BsonGenerator generator = BSON_FACTORY.createGenerator(outputStream);
        generator.copyCurrentStructure(parser);
        generator.close();
        parser.close();
        byte[] data = outputStream.toByteArray();
        return (DBObject) new LazyDBCallback(null).createObject(data, 0);
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}
Also used : BsonGenerator(de.undercouch.bson4jackson.BsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) LazyDBCallback(com.mongodb.LazyDBCallback) DBObject(com.mongodb.DBObject) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 10 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project rest.li by linkedin.

the class JacksonDataCodec method stringToList.

@Override
public DataList stringToList(String input) throws IOException {
    final Parser parser = new Parser();
    JsonParser jsonParser = null;
    try {
        jsonParser = _jsonFactory.createParser(input);
        return parser.parse(jsonParser, DataList.class);
    } catch (IOException e) {
        throw e;
    } finally {
        closeJsonParserQuietly(jsonParser);
    }
}
Also used : IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)137 IOException (java.io.IOException)37 Test (org.junit.Test)35 JsonFactory (com.fasterxml.jackson.core.JsonFactory)24 StringWriter (java.io.StringWriter)17 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)12 SqlNullable (com.facebook.presto.spi.function.SqlNullable)11 SqlType (com.facebook.presto.spi.function.SqlType)11 BaseTest (com.fasterxml.jackson.core.BaseTest)11 JsonToken (com.fasterxml.jackson.core.JsonToken)11 UTF8DataInputJsonParser (com.fasterxml.jackson.core.json.UTF8DataInputJsonParser)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonParseException (com.fasterxml.jackson.core.JsonParseException)8 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)7 PrestoException (com.facebook.presto.spi.PrestoException)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)5