Search in sources :

Example 1 with SimpleParseUUT

use of com.instagram.common.json.annotation.processor.uut.SimpleParseUUT in project ig-json-parser by Instagram.

the class SerializeTest method nullObject.

@Test
public void nullObject() throws IOException {
    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 int subIntValue = 30;
    SimpleParseUUT source = new SimpleParseUUT();
    source.intField = intValue;
    // intentionally do not set integerField
    source.stringField = stringValue;
    source.integerListField = integerList;
    source.subobjectField = new SimpleParseUUT.SubobjectParseUUT();
    source.subobjectField.intField = subIntValue;
    String serialized = SimpleParseUUT__JsonHelper.serializeToJson(source);
    SimpleParseUUT parsed = SimpleParseUUT__JsonHelper.parseFromJson(serialized);
    assertSame(source.intField, parsed.intField);
    assertNull(parsed.integerField);
    assertEquals(source.stringField, parsed.stringField);
    assertEquals(source.integerListField, parsed.integerListField);
    assertSame(source.subobjectField.intField, parsed.subobjectField.intField);
}
Also used : SimpleParseUUT(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT) Test(org.junit.Test)

Example 2 with SimpleParseUUT

use of com.instagram.common.json.annotation.processor.uut.SimpleParseUUT 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 3 with SimpleParseUUT

use of com.instagram.common.json.annotation.processor.uut.SimpleParseUUT 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 4 with SimpleParseUUT

use of com.instagram.common.json.annotation.processor.uut.SimpleParseUUT in project ig-json-parser by Instagram.

the class SerializeTest method stringSerializeTest.

@Test
public void stringSerializeTest() throws IOException {
    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;
    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;
    String serialized = SimpleParseUUT__JsonHelper.serializeToJson(source);
    SimpleParseUUT parsed = SimpleParseUUT__JsonHelper.parseFromJson(serialized);
    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);
}
Also used : SimpleParseUUT(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT) Test(org.junit.Test)

Example 5 with SimpleParseUUT

use of com.instagram.common.json.annotation.processor.uut.SimpleParseUUT 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)

Aggregations

SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)12 Test (org.junit.Test)12 JsonFactory (com.fasterxml.jackson.core.JsonFactory)8 StringWriter (java.io.StringWriter)8 JsonParser (com.fasterxml.jackson.core.JsonParser)7 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)6 JSONException (org.json.JSONException)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 JsonHelper (com.instagram.common.json.JsonHelper)1 TypeFormatterImportsContainerUUT__JsonHelper (com.instagram.common.json.annotation.processor.dependent.TypeFormatterImportsContainerUUT__JsonHelper)1 AlternateFieldUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.AlternateFieldUUT__JsonHelper)1 CustomParseContainerUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.CustomParseContainerUUT__JsonHelper)1 EnumUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.EnumUUT__JsonHelper)1 ExactMappingUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.ExactMappingUUT__JsonHelper)1 FormatterUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.FormatterUUT__JsonHelper)1 ImportsUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.ImportsUUT__JsonHelper)1 MapUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.MapUUT__JsonHelper)1 PostprocessingUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.PostprocessingUUT__JsonHelper)1 SimpleParseUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT__JsonHelper)1 StrictListParseUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.StrictListParseUUT__JsonHelper)1