Search in sources :

Example 76 with JsonFactory

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

the class TestJDKSerializability method testGenerationException.

public void testGenerationException() throws Exception {
    JsonFactory jf = new JsonFactory();
    JsonGenerator g = jf.createGenerator(new ByteArrayOutputStream());
    JsonGenerationException exc = null;
    g.writeStartObject();
    try {
        g.writeNumber(4);
        fail("Should not get here");
    } catch (JsonGenerationException e) {
        exc = e;
    }
    g.close();
    byte[] stuff = jdkSerialize(exc);
    JsonGenerationException result = jdkDeserialize(stuff);
    assertNotNull(result);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 77 with JsonFactory

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

the class TestGeneratorUsingMapper method testPojoWritingFailing.

public void testPojoWritingFailing() throws IOException {
    // regular factory can't do it, without a call to setCodec()
    JsonFactory jf = new JsonFactory();
    try {
        StringWriter sw = new StringWriter();
        JsonGenerator gen = jf.createGenerator(sw);
        gen.writeObject(new Pojo());
        gen.close();
        fail("Expected an exception: got sw '" + sw.toString() + "'");
    } catch (IllegalStateException e) {
        verifyException(e, "No ObjectCodec defined");
    }
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 78 with JsonFactory

use of com.fasterxml.jackson.core.JsonFactory in project graylog2-server by Graylog2.

the class JsonProcessingExceptionMapperTest method testToResponse.

@Test
public void testToResponse() throws Exception {
    final ExceptionMapper<JsonProcessingException> mapper = new JsonProcessingExceptionMapper();
    final JsonParser jsonParser = new JsonFactory().createParser("");
    final JsonMappingException exception = new JsonMappingException(jsonParser, "Boom!", new RuntimeException("rootCause"));
    final Response response = mapper.toResponse(exception);
    assertEquals(response.getStatusInfo(), Response.Status.BAD_REQUEST);
    assertEquals(response.getMediaType(), MediaType.APPLICATION_JSON_TYPE);
    assertTrue(response.hasEntity());
    assertTrue(response.getEntity() instanceof ApiError);
    final ApiError responseEntity = (ApiError) response.getEntity();
    assertTrue(responseEntity.getMessage().startsWith("Boom!"));
}
Also used : JsonProcessingExceptionMapper(org.graylog2.shared.rest.exceptionmappers.JsonProcessingExceptionMapper) Response(javax.ws.rs.core.Response) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ApiError(org.graylog2.plugin.rest.ApiError) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 79 with JsonFactory

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

the class DeserializeTest method testCustomParse.

@Test
public void testCustomParse() throws Exception {
    String value = "hey there";
    StringWriter stringWriter = new StringWriter();
    ExtensibleJSONWriter writer = new ExtensibleJSONWriter(stringWriter);
    writer.object().key(CustomParseContainerUUT.INNER_FIELD_NAME).object().key(CustomParseContainerUUT.CustomParseUUT.STRING_FIELD_NAME).value(value).endObject().endObject();
    String inputString = stringWriter.toString();
    JsonParser jp = new JsonFactory().createParser(inputString);
    jp.nextToken();
    CustomParseContainerUUT uut = CustomParseContainerUUT__JsonHelper.parseFromJson(jp);
    assertEquals(value, uut.getCustomParseUUT().getStringField());
}
Also used : CustomParseContainerUUT(com.instagram.common.json.annotation.processor.uut.CustomParseContainerUUT) StringWriter(java.io.StringWriter) 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 80 with JsonFactory

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

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)115 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)45 Test (org.junit.Test)37 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)35 StringWriter (java.io.StringWriter)34 JsonParser (com.fasterxml.jackson.core.JsonParser)26 IOException (java.io.IOException)16 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 Map (java.util.Map)14 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)9 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8 List (java.util.List)8 Reader (java.io.Reader)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 RemoteSession (org.apache.jackrabbit.oak.remote.RemoteSession)5 FileInputStream (java.io.FileInputStream)4 JSONWriter (org.json.JSONWriter)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 BaseJsonHttpResponseHandler (com.loopj.android.http.BaseJsonHttpResponseHandler)3