Search in sources :

Example 76 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory 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 : JSONWriter(org.json.JSONWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) 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)

Example 77 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory 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) TypeFormatterImportsContainerUUT__JsonHelper(com.instagram.common.json.annotation.processor.dependent.TypeFormatterImportsContainerUUT__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) JsonHelper(com.instagram.common.json.JsonHelper) SimpleParseUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT__JsonHelper) EnumUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.EnumUUT__JsonHelper) FormatterUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.FormatterUUT__JsonHelper) ImportsUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.ImportsUUT__JsonHelper) ExactMappingUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.ExactMappingUUT__JsonHelper) PostprocessingUUT__JsonHelper(com.instagram.common.json.annotation.processor.uut.PostprocessingUUT__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 78 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project ig-json-parser by Instagram.

the class DeserializeTest method postprocessTest.

@Test
public void postprocessTest() throws IOException, JSONException {
    final int value = 25;
    StringWriter stringWriter = new StringWriter();
    JSONWriter writer = new JSONWriter(stringWriter);
    writer.object().key(PostprocessingUUT.FIELD_NAME).value(value).endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    PostprocessingUUT uut = PostprocessingUUT__JsonHelper.parseFromJson(jp);
    assertSame(value + 1, uut.getValue());
}
Also used : JSONWriter(org.json.JSONWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) PostprocessingUUT(com.instagram.common.json.annotation.processor.uut.PostprocessingUUT) StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 79 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project ig-json-parser by Instagram.

the class DeserializeTest method valueExtractTest.

@Test
public void valueExtractTest() throws IOException, JSONException {
    final int encodedValue = 25;
    final int deserializedValue = 40;
    StringWriter stringWriter = new StringWriter();
    JSONWriter writer = new JSONWriter(stringWriter);
    writer.object().key(FormatterUUT.VALUE_FORMATTER_FIELD_NAME).value(encodedValue).endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    FormatterUUT uut = FormatterUUT__JsonHelper.parseFromJson(jp);
    assertSame(deserializedValue, uut.getValueFormatter());
}
Also used : JSONWriter(org.json.JSONWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) StringWriter(java.io.StringWriter) FormatterUUT(com.instagram.common.json.annotation.processor.uut.FormatterUUT) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 80 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project ig-json-parser by Instagram.

the class DeserializeTest method malformedArrayEntry.

@Test
public void malformedArrayEntry() throws IOException, JSONException {
    final List<Integer> integerList = Lists.newArrayList(1, null, 3, 4);
    final int subIntValue = 30;
    StringWriter stringWriter = new StringWriter();
    ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
    writer.object().key(StrictListParseUUT.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(StrictListParseUUT.SUBOBJECT_LIST_FIELD_NAME).object().key(StrictListParseUUT.SubobjectParseUUT.INT_FIELD_NAME).value(subIntValue).endObject().endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    StrictListParseUUT uut = StrictListParseUUT__JsonHelper.parseFromJson(jp);
    assertEquals(3, uut.integerListField.size());
    assertEquals(1, uut.integerListField.get(0).intValue());
    assertEquals(3, uut.integerListField.get(1).intValue());
    assertEquals(4, uut.integerListField.get(2).intValue());
}
Also used : StringWriter(java.io.StringWriter) StrictListParseUUT(com.instagram.common.json.annotation.processor.uut.StrictListParseUUT) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)266 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)102 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)91 JsonParser (com.fasterxml.jackson.core.JsonParser)78 Test (org.junit.Test)65 IOException (java.io.IOException)62 StringWriter (java.io.StringWriter)60 Map (java.util.Map)27 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)25 JsonNode (com.fasterxml.jackson.databind.JsonNode)21 List (java.util.List)18 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)16 JsonToken (com.fasterxml.jackson.core.JsonToken)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)14 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 InputStreamReader (java.io.InputStreamReader)9 TypeReference (com.fasterxml.jackson.core.type.TypeReference)8 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8