Search in sources :

Example 51 with JsonParser

use of com.fasterxml.jackson.core.JsonParser 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 : ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JSONWriter(org.json.JSONWriter) 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 52 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project ig-json-parser by Instagram.

the class DeserializeTest method fieldAssignmentTest.

@Test
public void fieldAssignmentTest() throws IOException, JSONException {
    final int encodedValue = 25;
    final int deserializedValue = -encodedValue;
    StringWriter stringWriter = new StringWriter();
    JSONWriter writer = new JSONWriter(stringWriter);
    writer.object().key(FormatterUUT.FIELD_ASSIGNMENT_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.getFieldAssignmentFormatter());
}
Also used : ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JSONWriter(org.json.JSONWriter) 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 53 with JsonParser

use of com.fasterxml.jackson.core.JsonParser 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)

Example 54 with JsonParser

use of com.fasterxml.jackson.core.JsonParser 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 : ExtensibleJSONWriter(com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter) JSONWriter(org.json.JSONWriter) 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 55 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project jackson-databind by FasterXML.

the class ExceptionSerializationTest method testSimpleOther.

// to double-check [databind#1413]
public void testSimpleOther() throws Exception {
    JsonParser p = MAPPER.getFactory().createParser("{ }");
    InvalidFormatException exc = InvalidFormatException.from(p, "Test", getClass(), String.class);
    String json = MAPPER.writeValueAsString(exc);
    p.close();
    assertNotNull(json);
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)144 IOException (java.io.IOException)43 Test (org.junit.Test)35 JsonFactory (com.fasterxml.jackson.core.JsonFactory)26 StringWriter (java.io.StringWriter)17 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 JsonToken (com.fasterxml.jackson.core.JsonToken)14 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)12 SqlNullable (com.facebook.presto.spi.function.SqlNullable)11 SqlType (com.facebook.presto.spi.function.SqlType)11 BaseTest (com.fasterxml.jackson.core.BaseTest)11 UTF8DataInputJsonParser (com.fasterxml.jackson.core.json.UTF8DataInputJsonParser)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonParseException (com.fasterxml.jackson.core.JsonParseException)9 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)7 PrestoException (com.facebook.presto.spi.PrestoException)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)5