Search in sources :

Example 6 with SimpleParseUUT

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

the class SerializeTest method nullArrayEntry.

@Test
public void nullArrayEntry() 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, null);
    final int subIntValue = 30;
    SimpleParseUUT source = new SimpleParseUUT();
    source.intField = intValue;
    source.integerField = integerValue;
    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);
    assertEquals(source.integerField, parsed.integerField);
    assertEquals(source.stringField, parsed.stringField);
    assertEquals(source.integerListField.size() - 1, parsed.integerListField.size());
    for (int ix = 0; ix < source.integerListField.size() - 1; ix++) {
        assertEquals(source.integerListField.get(ix), parsed.integerListField.get(ix));
    }
    assertSame(source.subobjectField.intField, parsed.subobjectField.intField);
}
Also used : SimpleParseUUT(com.instagram.common.json.annotation.processor.uut.SimpleParseUUT) Test(org.junit.Test)

Example 7 with SimpleParseUUT

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

the class SerializeTest method serializeOrderTest.

@Test
public void serializeOrderTest() throws IOException {
    final int intValue = 25;
    final int integerValue = 37;
    final float floatValue = 1f;
    final float floatObjectValue = 5f;
    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);
    SimpleParseUUT source = new SimpleParseUUT();
    source.intField = intValue;
    source.integerField = integerValue;
    source.floatField = floatValue;
    source.FloatField = floatObjectValue;
    source.stringField = stringValue;
    source.integerListField = integerList;
    source.integerArrayListField = integerArrayList;
    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();
    // Test that fields appear in the order specified in the class
    for (int i = 0; i < FIELD_DECLARATION_ORDER.length - 1; i++) {
        assertTrue(inputString.indexOf("\"" + FIELD_DECLARATION_ORDER[i] + "\"") < inputString.indexOf("\"" + FIELD_DECLARATION_ORDER[i + 1] + "\""));
    }
}
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) Test(org.junit.Test)

Example 8 with SimpleParseUUT

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

the class SerializeTest method nullArray.

@Test
public void nullArray() throws IOException {
    final int intValue = 25;
    final int integerValue = 37;
    final String stringValue = "hello world\r\n\'\"";
    final int subIntValue = 30;
    SimpleParseUUT source = new SimpleParseUUT();
    source.intField = intValue;
    source.integerField = integerValue;
    source.stringField = stringValue;
    // intentionally do not set integerListField
    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);
    assertNull(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 9 with SimpleParseUUT

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

the class DeserializeTest method nullString.

@Test
public void nullString() throws IOException, JSONException {
    StringWriter stringWriter = new StringWriter();
    ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
    writer.object().key(SimpleParseUUT.STRING_FIELD_NAME).value(null).endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    SimpleParseUUT uut = SimpleParseUUT__JsonHelper.parseFromJson(jp);
    assertNull(uut.stringField);
}
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 10 with SimpleParseUUT

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

the class MalformedJsonTest method arrayInsteadOfScalar.

@Test
public void arrayInsteadOfScalar() throws IOException, JSONException {
    StringWriter stringWriter = new StringWriter();
    ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
    writer.object().key(SimpleParseUUT.STRING_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.INT_FIELD_NAME).value(intValue).key(SimpleParseUUT.INTEGER_FIELD_NAME).value(integerValue).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());
    assertNull(uut.stringField);
}
Also used : StringWriter(java.io.StringWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JSONException(org.json.JSONException) 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