Search in sources :

Example 6 with ExtensibleJSONWriter

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

the class DeserializeTest method testMapNullValue.

@Test
public void testMapNullValue() throws Exception {
    StringWriter stringWriter = new StringWriter();
    ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
    final String keyWithNullValue = "key_with_null_value";
    writer.object().key(MapUUT.STRING_STRING_MAP_FIELD_NAME).object().key(keyWithNullValue).value(null).endObject().endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    MapUUT uut = MapUUT__JsonHelper.parseFromJson(jp);
    assertTrue(uut.stringStringMapField.containsKey(keyWithNullValue));
    assertNull(uut.stringStringMapField.get(keyWithNullValue));
}
Also used : StringWriter(java.io.StringWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) MapUUT(com.instagram.common.json.annotation.processor.uut.MapUUT) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 7 with ExtensibleJSONWriter

use of com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter 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 8 with ExtensibleJSONWriter

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

the class DeserializeTest method testMap.

@Test
public void testMap() throws Exception {
    final String stringValue = "hello world\r\n\'\"";
    final int integerValue = 37;
    final Map<String, Integer> stringIntegerMap = Maps.newHashMap();
    stringIntegerMap.put(stringValue, integerValue);
    final Map<String, String> stringStringMap = Maps.newHashMap();
    stringStringMap.put(stringValue, "value");
    final Map<String, Long> stringLongMap = Maps.newHashMap();
    stringLongMap.put("key_min_value", Long.MIN_VALUE);
    stringLongMap.put("key_max_value", Long.MAX_VALUE);
    final Map<String, Double> stringDoubleMap = Maps.newHashMap();
    stringDoubleMap.put("key_min_value", Double.MIN_VALUE);
    stringDoubleMap.put("key_max_value", Double.MAX_VALUE);
    final Map<String, Float> stringFloatMap = Maps.newHashMap();
    stringFloatMap.put("key_min_value", Float.MIN_VALUE);
    stringFloatMap.put("key_max_value", Float.MAX_VALUE);
    final Map<String, Boolean> stringBooleanMap = Maps.newHashMap();
    stringBooleanMap.put("true", Boolean.TRUE);
    stringBooleanMap.put("false", Boolean.FALSE);
    final Map<String, MapUUT.MapObject> stringObjectMap = Maps.newHashMap();
    MapUUT.MapObject mapObject = new MapUUT.MapObject();
    mapObject.subclassInt = integerValue;
    final HashMap<String, String> stringStringHashMap = Maps.newHashMap();
    stringStringHashMap.put(stringValue, "value");
    StringWriter stringWriter = new StringWriter();
    ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
    writer.object().key(MapUUT.STRING_INTEGER_MAP_FIELD_NAME).object().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Map.Entry<String, Integer> entry : stringIntegerMap.entrySet()) {
                writer.key(entry.getKey());
                writer.value(entry.getValue());
            }
        }
    }).endObject().key(MapUUT.STRING_STRING_MAP_FIELD_NAME).object().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
                writer.key(entry.getKey());
                writer.value(entry.getValue());
            }
        }
    }).endObject().key(MapUUT.STRING_LONG_MAP_FIELD_NAME).object().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Map.Entry<String, Long> entry : stringLongMap.entrySet()) {
                writer.key(entry.getKey());
                writer.value(entry.getValue());
            }
        }
    }).endObject().key(MapUUT.STRING_DOUBLE_MAP_FIELD_NAME).object().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Map.Entry<String, Double> entry : stringDoubleMap.entrySet()) {
                writer.key(entry.getKey());
                writer.value(entry.getValue());
            }
        }
    }).endObject().key(MapUUT.STRING_FLOAT_MAP_FIELD_NAME).object().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Map.Entry<String, Float> entry : stringFloatMap.entrySet()) {
                writer.key(entry.getKey());
                writer.value(entry.getValue());
            }
        }
    }).endObject().key(MapUUT.STRING_BOOLEAN_MAP_FIELD_NAME).object().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Map.Entry<String, Boolean> entry : stringBooleanMap.entrySet()) {
                writer.key(entry.getKey());
                writer.value(entry.getValue());
            }
        }
    }).endObject().key(MapUUT.STRING_OBJECT_MAP_FIELD_NAME).object().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Map.Entry<String, MapUUT.MapObject> entry : stringObjectMap.entrySet()) {
                writer.key(entry.getKey()).object().key(MapUUT.MapObject.INT_KEY).value(entry.getValue().subclassInt).endObject();
            }
        }
    }).endObject().endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    MapUUT uut = MapUUT__JsonHelper.parseFromJson(jp);
    assertEquals(stringIntegerMap, uut.stringIntegerMapField);
    assertEquals(stringStringMap, uut.stringStringMapField);
    assertEquals(stringLongMap, uut.stringLongMapField);
    assertEquals(stringDoubleMap, uut.stringDoubleMapField);
    assertEquals(stringFloatMap, uut.stringFloatMapField);
    assertEquals(stringBooleanMap, uut.stringBooleanMapField);
    assertEquals(stringObjectMap, uut.stringObjectMapField);
}
Also used : MapUUT(com.instagram.common.json.annotation.processor.uut.MapUUT) JsonFactory(com.fasterxml.jackson.core.JsonFactory) StringWriter(java.io.StringWriter) JsonParser(com.fasterxml.jackson.core.JsonParser) JSONException(org.json.JSONException) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) Test(org.junit.Test)

Example 9 with ExtensibleJSONWriter

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

the class DeserializeTest method testAlternateFieldNameHelper.

private void testAlternateFieldNameHelper(String fieldName, String value) throws Exception {
    StringWriter stringWriter = new StringWriter();
    ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
    writer.object().key(fieldName).value(value).endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    AlternateFieldUUT uut = AlternateFieldUUT__JsonHelper.parseFromJson(jp);
    assertEquals(value, uut.getNameField());
}
Also used : StringWriter(java.io.StringWriter) ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) AlternateFieldUUT(com.instagram.common.json.annotation.processor.uut.AlternateFieldUUT) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 10 with ExtensibleJSONWriter

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

the class MalformedJsonTest method scalarInsteadOfObject.

@Test
public void scalarInsteadOfObject() 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 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).array().extend(new ExtensibleJSONWriter.Extender() {

        @Override
        public void extend(ExtensibleJSONWriter writer) throws JSONException {
            for (Integer integer : integerList) {
                writer.value(integer);
            }
        }
    }).endArray().key(SimpleParseUUT.SUBOBJECT_FIELD_NAME).value(subIntValue).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);
    assertEquals(integerList, uut.integerListField);
    assertNull(uut.subobjectField);
}
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

ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)12 StringWriter (java.io.StringWriter)12 JsonFactory (com.fasterxml.jackson.core.JsonFactory)11 JsonParser (com.fasterxml.jackson.core.JsonParser)11 Test (org.junit.Test)10 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)6 JSONException (org.json.JSONException)4 MapUUT (com.instagram.common.json.annotation.processor.uut.MapUUT)2 JsonHelper (com.instagram.common.json.JsonHelper)1 AlternateFieldUUT (com.instagram.common.json.annotation.processor.uut.AlternateFieldUUT)1 AlternateFieldUUT__JsonHelper (com.instagram.common.json.annotation.processor.uut.AlternateFieldUUT__JsonHelper)1 CustomParseContainerUUT (com.instagram.common.json.annotation.processor.uut.CustomParseContainerUUT)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 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 (com.instagram.common.json.annotation.processor.uut.StrictListParseUUT)1