Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#writeTree()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#writeTree() . 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: MessagePackJsonConverter.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@Override
public ServiceResponse<String> toJson(byte bytes[]) {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        // read message pack
        ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
        JsonNode node = objectMapper.readTree(bytes);

        // write json
        JsonFactory jsonFactory = new JsonFactory();
        JsonGenerator jsonGenerator = jsonFactory.createGenerator(bos);

        objectMapper.writeTree(jsonGenerator, node);
    } catch (IOException | NullPointerException e) {
        LOGGER.error("Exception converting message pack to JSON", e);
        return ServiceResponseBuilder.<String>error().build();
    }

    return ServiceResponseBuilder.<String>ok().withResult(new String(bos.toByteArray())).build();

}
 
Example 2
Source File: MessagePackJsonConverter.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
@Override
public ServiceResponse<byte[]> fromJson(String json) {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        // read json
        ObjectMapper objectMapper = new ObjectMapper(new JsonFactory());
        JsonNode node = objectMapper.readTree(json);

        // write message pack
        MessagePackFactory messagePackFactory = new MessagePackFactory();
        JsonGenerator jsonGenerator = messagePackFactory.createGenerator(bos);

        objectMapper.writeTree(jsonGenerator, node);
    } catch (IOException e) {
        LOGGER.error("Exception converting message pack to JSON", e);
        return ServiceResponseBuilder.<byte[]>error().build();
    }

    return ServiceResponseBuilder.<byte[]>ok().withResult(bos.toByteArray()).build();

}
 
Example 3
Source File: SampleTweet.java    From trident-tutorial with Apache License 2.0 6 votes vote down vote up
public SampleTweet() throws IOException {
    ObjectMapper om = new ObjectMapper();
    JsonFactory factory = new JsonFactory();
    ImmutableList.Builder<String> b = ImmutableList.builder();

    InputStreamReader reader = new InputStreamReader(this.getClass().getResourceAsStream("sample_tweet.json"));
    try {
        String tweetArray = CharStreams.toString(reader);
        ArrayNode parsed = (ArrayNode)om.readTree(tweetArray);
        for (JsonNode tweet : parsed) {
            StringWriter sw = new StringWriter();
            om.writeTree(factory.createGenerator(sw), tweet);
            b.add(sw.toString());
        }
        sampleTweet = Iterators.cycle(b.build());
    } finally {
        reader.close();
    }
}
 
Example 4
Source File: JacksonJsonDataFormatWriter.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
public void writeToWriter(Writer writer, Object input) {
  final ObjectMapper objectMapper = dataFormat.getObjectMapper();
  final JsonFactory factory = objectMapper.getFactory();

  try {
    JsonGenerator generator = factory.createGenerator(writer);
    objectMapper.writeTree(generator, (JsonNode) input);
  }
  catch (IOException e) {
    throw LOG.unableToWriteJsonNode(e);
  }

}