Search in sources :

Example 56 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project jackson-module-afterburner by FasterXML.

the class TestJvmDeserPerf method testDeser.

protected int testDeser(JsonFactory jf, byte[] input, int reps) throws Exception {
    MediaItem item = null;
    for (int i = 0; i < reps; ++i) {
        JsonParser jp = jf.createParser(input);
        item = MediaItem.deserialize(jp);
        jp.close();
    }
    // just to get some non-optimizable number
    return item.hashCode();
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 57 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project ig-json-parser by Instagram.

the class MalformedJsonTest method scalarInsteadOfArray.

@Test
public void scalarInsteadOfArray() throws IOException, JSONException {
    final int intValue = 25;
    final int integerValue = 37;
    final String stringValue = "hello world\r\n\'\"";
    final int subIntValue = 30;
    StringWriter stringWriter = new StringWriter();
    ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
    writer.object().key(SimpleParseUUT.INT_FIELD_NAME).value(intValue).key(SimpleParseUUT.INTEGER_FIELD_NAME).value(integerValue).key(SimpleParseUUT.STRING_FIELD_NAME).value(stringValue).key(SimpleParseUUT.INTEGER_LIST_FIELD_NAME).value(intValue).key(SimpleParseUUT.SUBOBJECT_FIELD_NAME).object().key(SimpleParseUUT.SubobjectParseUUT.INT_FIELD_NAME).value(subIntValue).endObject().endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    SimpleParseUUT uut = SimpleParseUUT__JsonHelper.parseFromJson(jp);
    assertSame(intValue, uut.intField);
    assertSame(integerValue, uut.integerField.intValue());
    assertEquals(stringValue, uut.stringField);
    assertNull(uut.integerListField);
    assertSame(subIntValue, uut.subobjectField.intField);
}
Also used : StringWriter(java.io.StringWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) SimpleParseUUT(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 58 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project ig-json-parser by Instagram.

the class SerializeTest method simpleSerializeTest.

@Test
public void simpleSerializeTest() throws IOException, JSONException {
    final int intValue = 25;
    final int integerValue = 37;
    final String stringValue = "hello world\r\n\'\"";
    final List<Integer> integerList = Lists.newArrayList(1, 2, 3, 4);
    final Queue<Integer> integerQueue = Queues.newArrayDeque(Arrays.asList(1, 2, 3, 4));
    final Set<Integer> integerSet = Sets.newHashSet(1, 2, 3, 4);
    final int subIntValue = 30;
    final SimpleParseUUT.SubenumUUT subEnum = SimpleParseUUT.SubenumUUT.A;
    final List<SimpleParseUUT.SubenumUUT> subEnumList = Lists.newArrayList(SimpleParseUUT.SubenumUUT.A, SimpleParseUUT.SubenumUUT.B);
    SimpleParseUUT source = new SimpleParseUUT();
    source.intField = intValue;
    source.integerField = integerValue;
    source.stringField = stringValue;
    source.integerListField = integerList;
    source.integerQueueField = integerQueue;
    source.integerSetField = integerSet;
    source.subobjectField = new SimpleParseUUT.SubobjectParseUUT();
    source.subobjectField.intField = subIntValue;
    source.subenumField = subEnum;
    source.subenumFieldList = subEnumList;
    StringWriter stringWriter = new StringWriter();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);
    SimpleParseUUT__JsonHelper.serializeToJson(jsonGenerator, source, true);
    jsonGenerator.close();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    SimpleParseUUT parsed = SimpleParseUUT__JsonHelper.parseFromJson(jp);
    assertSame(source.intField, parsed.intField);
    assertEquals(source.integerField, parsed.integerField);
    assertEquals(source.stringField, parsed.stringField);
    assertEquals(source.integerListField, parsed.integerListField);
    // NOTE: this is because ArrayDeque hilariously does not implement .equals()/.hashcode().
    assertEquals(Lists.newArrayList(source.integerQueueField), Lists.newArrayList(parsed.integerQueueField));
    assertEquals(source.integerSetField, parsed.integerSetField);
    assertSame(source.subobjectField.intField, parsed.subobjectField.intField);
    assertSame(source.subenumField, parsed.subenumField);
    assertEquals(source.subenumFieldList, parsed.subenumFieldList);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) SimpleParseUUT(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 59 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 60 with JsonParser

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

the class TestJSON method parseToStreamUTFString.

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

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)144 IOException (java.io.IOException)43 Test (org.junit.Test)35 JsonFactory (com.fasterxml.jackson.core.JsonFactory)26 StringWriter (java.io.StringWriter)17 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 JsonToken (com.fasterxml.jackson.core.JsonToken)14 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 UTF8DataInputJsonParser (com.fasterxml.jackson.core.json.UTF8DataInputJsonParser)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonParseException (com.fasterxml.jackson.core.JsonParseException)9 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