Search in sources :

Example 46 with JsonParser

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

the class TrailingCommasTest method testArrayTrailingCommasTriple.

@Test
public void testArrayTrailingCommasTriple() throws Exception {
    String json = "[\"a\", \"b\",,,]";
    JsonParser p = createParser(factory, mode, json);
    assertEquals(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals("a", p.getText());
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals("b", p.getText());
    // ALLOW_TRAILING_COMMA takes priority over ALLOW_MISSING_VALUES
    if (features.contains(Feature.ALLOW_MISSING_VALUES) && features.contains(Feature.ALLOW_TRAILING_COMMA)) {
        assertToken(JsonToken.VALUE_NULL, p.nextToken());
        assertToken(JsonToken.VALUE_NULL, p.nextToken());
        assertToken(JsonToken.END_ARRAY, p.nextToken());
        assertEnd(p);
    } else if (features.contains(Feature.ALLOW_MISSING_VALUES)) {
        assertToken(JsonToken.VALUE_NULL, p.nextToken());
        assertToken(JsonToken.VALUE_NULL, p.nextToken());
        assertToken(JsonToken.VALUE_NULL, p.nextToken());
        assertToken(JsonToken.END_ARRAY, p.nextToken());
        assertEnd(p);
    } else {
        assertUnexpected(p, ',');
    }
    p.close();
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) UTF8DataInputJsonParser(com.fasterxml.jackson.core.json.UTF8DataInputJsonParser) BaseTest(com.fasterxml.jackson.core.BaseTest) Test(org.junit.Test)

Example 47 with JsonParser

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

the class TestJDKSerializability method _copyJson.

protected void _copyJson(JsonFactory f, String json, JsonGenerator g) throws IOException {
    JsonParser p = f.createParser(json);
    while (p.nextToken() != null) {
        g.copyCurrentEvent(p);
    }
    p.close();
    g.close();
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 48 with JsonParser

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

the class TestJDKSerializability method testParseException.

public void testParseException() throws Exception {
    JsonFactory jf = new JsonFactory();
    JsonParser p = jf.createParser("  { garbage! }");
    JsonParseException exc = null;
    try {
        p.nextToken();
        p.nextToken();
        fail("Should not get here");
    } catch (JsonParseException e) {
        exc = e;
    }
    p.close();
    byte[] stuff = jdkSerialize(exc);
    JsonParseException result = jdkDeserialize(stuff);
    assertNotNull(result);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 49 with JsonParser

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

the class DeserializeTest method simpleDeserializeTest.

@Test
public void simpleDeserializeTest() throws IOException, JSONException {
    final int intValue = 25;
    final int integerValue = 37;
    final float floatValue = 1.0f;
    final float floatObjValue = 2.0f;
    final String stringValue = "hello world\r\n\'\"";
    final List<Integer> integerList = Lists.newArrayList(1, 2, 3, 4);
    final ArrayList<Integer> integerArrayList = 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);
    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.FLOAT_FIELD_NAME).value(floatValue).key(SimpleParseUUT.FLOAT_OBJ_FIELD_NAME).value(floatObjValue).key(SimpleParseUUT.STRING_FIELD_NAME).value(stringValue).key(SimpleParseUUT.INTEGER_LIST_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Integer integer : integerList) {
                writer.value(integer);
            }
        }
    }).endArray().key(SimpleParseUUT.INTEGER_ARRAY_LIST_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Integer integer : integerList) {
                writer.value(integer);
            }
        }
    }).endArray().key(SimpleParseUUT.INTEGER_QUEUE_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Integer integer : integerQueue) {
                writer.value(integer);
            }
        }
    }).endArray().key(SimpleParseUUT.INTEGER_SET_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Integer integer : integerSet) {
                writer.value(integer);
            }
        }
    }).endArray().key(SimpleParseUUT.SUBOBJECT_FIELD_NAME).object().key(SimpleParseUUT.SubobjectParseUUT.INT_FIELD_NAME).value(subIntValue).endObject().key(SimpleParseUUT.SUBENUM_FIELD_NAME).value(subEnum.toString()).key(SimpleParseUUT.SUBENUM_LIST_FIELD_NAME).array().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (SimpleParseUUT.SubenumUUT enumValue : subEnumList) {
                writer.value(enumValue.toString());
            }
        }
    }).endArray().endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    SimpleParseUUT uut = SimpleParseUUT__JsonHelper.parseFromJson(jp);
    assertTrue(new SimpleParseUUT__JsonHelper() instanceof JsonHelper);
    assertSame(intValue, uut.intField);
    assertSame(integerValue, uut.integerField.intValue());
    assertEquals(Float.valueOf(floatValue), Float.valueOf(uut.floatField));
    assertEquals(Float.valueOf(floatObjValue), uut.FloatField);
    assertEquals(stringValue, uut.stringField);
    assertEquals(integerList, uut.integerListField);
    assertEquals(integerArrayList, uut.integerArrayListField);
    // NOTE: this is because ArrayDeque hilariously does not implement .equals()/.hashcode().
    assertEquals(Lists.newArrayList(integerQueue), Lists.newArrayList(uut.integerQueueField));
    assertEquals(integerSet, uut.integerSetField);
    assertSame(subIntValue, uut.subobjectField.intField);
    assertSame(subEnum, uut.subenumField);
    assertEquals(subEnumList, uut.subenumFieldList);
}
Also used : SimpleParseUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT__JsonHelper) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JSONException(org.json.JSONException) SimpleParseUUT(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT) AlternateFieldUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.AlternateFieldUUT__JsonHelper) SimpleParseUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT__JsonHelper) CustomParseContainerUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.CustomParseContainerUUT__JsonHelper) StrictListParseUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.StrictListParseUUT__JsonHelper) MapUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.MapUUT__JsonHelper) EnumUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.EnumUUT__JsonHelper) FormatterUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.FormatterUUT__JsonHelper) ExactMappingUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.ExactMappingUUT__JsonHelper) PostprocessingUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.PostprocessingUUT__JsonHelper) JsonHelper(com.instagram.common.json.JsonHelper) StringWriter(java.io.StringWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 50 with JsonParser

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

the class DeserializeTest method enumTest.

@Test
public void enumTest() throws IOException, JSONException {
    final EnumUUT.EnumType value = EnumUUT.EnumType.VALUE2;
    StringWriter stringWriter = new StringWriter();
    JSONWriter writer = new JSONWriter(stringWriter);
    writer.object().key(EnumUUT.ENUM_FIELD_NAME).value(value.toString()).endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    EnumUUT uut = EnumUUT__JsonHelper.parseFromJson(jp);
    assertSame(value, uut.enumField);
}
Also used : ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JSONWriter(org.json.JSONWriter) StringWriter(java.io.StringWriter) EnumUUT(com.instagram.common.json.annotation.processor.uut.EnumUUT) JsonFactory(com.fasterxml.jackson.core.JsonFactory) 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