Search in sources :

Example 31 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project pinpoint by naver.

the class LinkSerializerTest method testSerialize.

@Test
public void testSerialize() throws Exception {
    AgentHistogramList list = new AgentHistogramList();
    AgentHistogram histogram = new AgentHistogram(new Application("test", ServiceType.STAND_ALONE));
    list.addAgentHistogram(histogram);
    Node node1 = new Node(new Application("test1", ServiceType.STAND_ALONE));
    Node node2 = new Node(new Application("test1", ServiceType.STAND_ALONE));
    Link link = new Link(CreateType.Source, node1, node2, new Range(0, 1));
    ObjectWriter objectWriter = MAPPER.writerWithDefaultPrettyPrinter();
    String s = objectWriter.writeValueAsString(link);
    logger.debug(s);
}
Also used : Node(com.navercorp.pinpoint.web.applicationmap.Node) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) AgentHistogramList(com.navercorp.pinpoint.web.applicationmap.rawdata.AgentHistogramList) Range(com.navercorp.pinpoint.web.vo.Range) Application(com.navercorp.pinpoint.web.vo.Application) Link(com.navercorp.pinpoint.web.applicationmap.Link) AgentHistogram(com.navercorp.pinpoint.web.applicationmap.rawdata.AgentHistogram) Test(org.junit.Test)

Example 32 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project pinpoint by naver.

the class PluginDisableIT method test.

@Test
public void test() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "jackson");
    mapper.writeValueAsString(map);
    mapper.writeValueAsBytes(map);
    ObjectWriter writer = mapper.writer();
    writer.writeValueAsString(map);
    writer.writeValueAsBytes(map);
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    verifier.verifyTraceCount(0);
}
Also used : HashMap(java.util.HashMap) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 33 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project spring-framework by spring-projects.

the class Jackson2JsonEncoder method encodeValue.

private DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType type, Map<String, Object> hints) {
    TypeFactory typeFactory = this.mapper.getTypeFactory();
    JavaType javaType = typeFactory.constructType(type.getType());
    if (type.isInstance(value)) {
        javaType = getJavaType(type.getType(), null);
    }
    ObjectWriter writer;
    Class<?> jsonView = (Class<?>) hints.get(AbstractJackson2Codec.JSON_VIEW_HINT);
    if (jsonView != null) {
        writer = this.mapper.writerWithView(jsonView);
    } else {
        writer = this.mapper.writer();
    }
    if (javaType != null && javaType.isContainerType()) {
        writer = writer.forType(javaType);
    }
    Boolean sse = (Boolean) hints.get(ServerSentEventHttpMessageWriter.SSE_CONTENT_HINT);
    SerializationConfig config = writer.getConfig();
    if (Boolean.TRUE.equals(sse) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        writer = writer.with(this.ssePrettyPrinter);
    }
    DataBuffer buffer = bufferFactory.allocateBuffer();
    OutputStream outputStream = buffer.asOutputStream();
    try {
        writer.writeValue(outputStream, value);
    } catch (IOException ex) {
        throw new CodecException("Error while writing the data", ex);
    }
    return buffer;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) OutputStream(java.io.OutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) CodecException(org.springframework.core.codec.CodecException) IOException(java.io.IOException) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 34 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project retrofit by square.

the class JacksonConverterFactory method requestBodyConverter.

@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
    JavaType javaType = mapper.getTypeFactory().constructType(type);
    ObjectWriter writer = mapper.writerFor(javaType);
    return new JacksonRequestBodyConverter<>(writer);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Example 35 with ObjectWriter

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

the class TestJDKSerialization method testEnumHandlers.

// for [databind#899]
public void testEnumHandlers() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    // ensure we have serializers and/or deserializers, first
    String json = mapper.writerFor(EnumPOJO.class).writeValueAsString(new EnumPOJO());
    EnumPOJO result = mapper.readerFor(EnumPOJO.class).readValue(json);
    assertNotNull(result);
    // and then use JDK serialization to freeze/thaw objects
    byte[] bytes = jdkSerialize(mapper);
    ObjectMapper mapper2 = jdkDeserialize(bytes);
    assertNotNull(mapper2);
    bytes = jdkSerialize(mapper.readerFor(EnumPOJO.class));
    ObjectReader r = jdkDeserialize(bytes);
    assertNotNull(r);
    /* 14-Aug-2015, tatu: Looks like pre-loading JsonSerializer is problematic
         *    at this point; comment out for now. Try to fix later on.
         */
    bytes = jdkSerialize(mapper.writerFor(EnumPOJO.class));
    ObjectWriter w = jdkDeserialize(bytes);
    assertNotNull(w);
    // plus, ensure objects are usable:
    String json2 = w.writeValueAsString(new EnumPOJO());
    assertEquals(json, json2);
    EnumPOJO result2 = r.readValue(json2);
    assertNotNull(result2);
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)42 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)18 Test (org.junit.Test)12 JavaType (com.fasterxml.jackson.databind.JavaType)7 IOException (java.io.IOException)7 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)4 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)4 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)4 OutputStream (java.io.OutputStream)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)3 FileOutputStream (java.io.FileOutputStream)3 Writer (java.io.Writer)3 JCommander (com.beust.jcommander.JCommander)2 ParameterException (com.beust.jcommander.ParameterException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)2 RateLimiter (com.google.common.util.concurrent.RateLimiter)2 Application (com.navercorp.pinpoint.web.vo.Application)2 Range (com.navercorp.pinpoint.web.vo.Range)2