Search in sources :

Example 11 with ExtensibleJSONWriter

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

Example 12 with ExtensibleJSONWriter

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

the class MalformedJsonTest method dictInsteadOfScalar.

@Test
public void dictInsteadOfScalar() throws IOException, JSONException {
    StringWriter stringWriter = new StringWriter();
    ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
    writer.object().key(SimpleParseUUT.STRING_FIELD_NAME).object().key("garbage").value("123").endObject().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) 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