Java Code Examples for com.fasterxml.jackson.databind.ObjectWriter#writeValueAsBytes()

The following examples show how to use com.fasterxml.jackson.databind.ObjectWriter#writeValueAsBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObjectReaderIT.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteValue() throws Exception {
    __POJO pojo = new __POJO();
    pojo.setName("Jackson");
    
    ObjectWriter writer = mapper.writer();

    String jsonStr = writer.writeValueAsString(pojo);
    byte[] jsonByte = writer.writeValueAsBytes(pojo);

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();

    Method writeval1 = ObjectWriter.class.getMethod("writeValueAsString", Object.class);
    Method writeval2 = ObjectWriter.class.getMethod("writeValueAsBytes", Object.class);

    verifier.verifyTrace(event("JACKSON", writeval1, annotation("jackson.json.length", jsonStr.length())));
    verifier.verifyTrace(event("JACKSON", writeval2, annotation("jackson.json.length", jsonByte.length)));

    verifier.verifyTraceCount(0);
}
 
Example 2
Source File: ObjectReaderJDK7IT.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteValue() throws Exception {
    __POJO pojo = new __POJO();
    pojo.setName("Jackson");
    
    ObjectWriter writer = mapper.writer();

    String jsonStr = writer.writeValueAsString(pojo);
    byte[] jsonByte = writer.writeValueAsBytes(pojo);

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();

    Method writeval1 = ObjectWriter.class.getMethod("writeValueAsString", Object.class);
    Method writeval2 = ObjectWriter.class.getMethod("writeValueAsBytes", Object.class);

    verifier.verifyTrace(event("JACKSON", writeval1, annotation("jackson.json.length", jsonStr.length())));
    verifier.verifyTrace(event("JACKSON", writeval2, annotation("jackson.json.length", jsonByte.length)));

    verifier.verifyTraceCount(0);
}
 
Example 3
Source File: CamelKPublishHandler.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private String extractIntegrationJson(Integration fullIntegration, boolean prettyPrint) {
    Integration integration = resourceManager.sanitize(fullIntegration);
    ObjectWriter writer = JsonUtils.writer();
    try {
        return prettyPrint
            ? new String(writer.with(writer.getConfig().getDefaultPrettyPrinter()).writeValueAsBytes(integration), UTF_8)
            : new String(writer.writeValueAsBytes(integration), UTF_8);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Cannot convert integration " + integration.getName() + " to JSON: " + e,e);
    }
}
 
Example 4
Source File: ClientSideState.java    From syndesis with Apache License 2.0 5 votes vote down vote up
static byte[] serialize(final Object value) {
    final ObjectWriter writer = MAPPER.writerFor(value.getClass());

    try {
        return writer.writeValueAsBytes(value);
    } catch (final JsonProcessingException e) {
        throw new IllegalArgumentException("Unable to serialize given value: " + value, e);
    }
}
 
Example 5
Source File: JsonHelper.java    From emodb with Apache License 2.0 5 votes vote down vote up
private static byte[] asUtf8Bytes(Object value, ObjectWriter writer) {

        try {
            return writer.writeValueAsBytes(value);
        } catch (IOException e) {
            // Shouldn't get I/O errors writing to a string.
            throw Throwables.propagate(e);
        }
    }
 
Example 6
Source File: PrepareProxyInterceptor.java    From moon-api-gateway with MIT License 4 votes vote down vote up
private byte[] getBytesByObjectNode(ObjectNode bodyObjectNode) throws Exception {
    ObjectWriter writer = JsonUtil.getObjectMapper().writer();
    return writer.writeValueAsBytes(bodyObjectNode);
}
 
Example 7
Source File: ObjectMapperIT.java    From pinpoint with Apache License 2.0 3 votes vote down vote up
@Test
public void testWriteValue() throws Exception {
    __POJO pojo = new __POJO();
    pojo.setName("Jackson");

    String jsonStr = mapper.writeValueAsString(pojo);
    byte[] jsonByte = mapper.writeValueAsBytes(pojo);
    
    ObjectWriter writer = mapper.writer();

    writer.writeValueAsString(pojo);
    writer.writeValueAsBytes(pojo);


    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();

    Method mapperWriteValueAsString = ObjectMapper.class.getMethod("writeValueAsString", Object.class);
    Method mapperWriteValueAsBytes = ObjectMapper.class.getMethod("writeValueAsBytes", Object.class);
    Method writerWriteValueAsString = ObjectWriter.class.getMethod("writeValueAsString", Object.class);
    Method writerWriteValueAsBytes = ObjectWriter.class.getMethod("writeValueAsBytes", Object.class);


    verifier.verifyTrace(event(SERVICE_TYPE, mapperWriteValueAsString, annotation(ANNOTATION_KEY, jsonStr.length())));
    verifier.verifyTrace(event(SERVICE_TYPE, mapperWriteValueAsBytes, annotation(ANNOTATION_KEY, jsonByte.length)));

    verifier.verifyTrace(event(SERVICE_TYPE, writerWriteValueAsString, annotation(ANNOTATION_KEY, jsonStr.length())));
    verifier.verifyTrace(event(SERVICE_TYPE, writerWriteValueAsBytes, annotation(ANNOTATION_KEY, jsonByte.length)));

    verifier.verifyTraceCount(0);
}
 
Example 8
Source File: ObjectMapperJDK7IT.java    From pinpoint with Apache License 2.0 3 votes vote down vote up
@Test
public void testWriteValue() throws Exception {
    __POJO pojo = new __POJO();
    pojo.setName("Jackson");

    String jsonStr = mapper.writeValueAsString(pojo);
    byte[] jsonByte = mapper.writeValueAsBytes(pojo);
    
    ObjectWriter writer = mapper.writer();

    writer.writeValueAsString(pojo);
    writer.writeValueAsBytes(pojo);


    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();

    Method mapperWriteValueAsString = ObjectMapper.class.getMethod("writeValueAsString", Object.class);
    Method mapperWriteValueAsBytes = ObjectMapper.class.getMethod("writeValueAsBytes", Object.class);
    Method writerWriteValueAsString = ObjectWriter.class.getMethod("writeValueAsString", Object.class);
    Method writerWriteValueAsBytes = ObjectWriter.class.getMethod("writeValueAsBytes", Object.class);


    verifier.verifyTrace(event(SERVICE_TYPE, mapperWriteValueAsString, annotation(ANNOTATION_KEY, jsonStr.length())));
    verifier.verifyTrace(event(SERVICE_TYPE, mapperWriteValueAsBytes, annotation(ANNOTATION_KEY, jsonByte.length)));

    verifier.verifyTrace(event(SERVICE_TYPE, writerWriteValueAsString, annotation(ANNOTATION_KEY, jsonStr.length())));
    verifier.verifyTrace(event(SERVICE_TYPE, writerWriteValueAsBytes, annotation(ANNOTATION_KEY, jsonByte.length)));

    verifier.verifyTraceCount(0);
}