Example usage for com.fasterxml.jackson.databind ObjectMapper writeValueAsBytes

List of usage examples for com.fasterxml.jackson.databind ObjectMapper writeValueAsBytes

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper writeValueAsBytes.

Prototype

@SuppressWarnings("resource")
public byte[] writeValueAsBytes(Object value) throws JsonProcessingException 

Source Link

Document

Method that can be used to serialize any Java value as a byte array.

Usage

From source file:com.metamx.datatypes.ExampleWriteNewLineJson.java

public static void main(String[] args) throws Exception {
    final MmxAuctionSummary sampleAuction1 = MmxAuctionSummary.builder()
            .timestamp(new DateTime("2014-01-01T00:00:00.000Z")).auctionType(2).build();
    final MmxAuctionSummary sampleAuction2 = MmxAuctionSummary.builder()
            .timestamp(new DateTime("2014-01-01T01:00:00.000Z")).auctionType(1).build();

    List<MmxAuctionSummary> auctionList = Arrays.asList(sampleAuction1, sampleAuction2);
    final String separator = "\n";

    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    final OutputStream outStream = new ByteArrayOutputStream();

    for (MmxAuctionSummary auction : auctionList) {
        outStream.write(objectMapper.writeValueAsBytes(auction));
        outStream.write(separator.getBytes());
    }// w  w  w  .  jav a2 s  . co  m
    System.out.println(outStream.toString());
}

From source file:it.jugtorino.one.msvc.way.rabbit.reference.utils.JSONUtils.java

public static byte[] toBytes(Map<String, Object> map) {
    ObjectMapper mapper = new ObjectMapper();
    try {/*from w ww. j  a v a  2  s .  c  o  m*/
        return mapper.writeValueAsBytes(map);
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.sitewhere.hbase.common.MarshalUtils.java

/**
 * Marshal an object to a JSON string./*w  w w .  j  a v a 2s  . c om*/
 * 
 * @param object
 * @return
 * @throws SiteWhereException
 */
public static byte[] marshalJson(Object object) throws SiteWhereException {
    ObjectMapper mapper = new ObjectMapper();
    try {
        return mapper.writeValueAsBytes(object);
    } catch (JsonProcessingException e) {
        throw new SiteWhereException("Could not marshal device as JSON.", e);
    }
}

From source file:com.github.lburgazzoli.hazelcast.serialization.json.JsonEnvelope.java

public static <T> JsonEnvelope fromObject(ObjectMapper mapper, T object) throws IOException {
    return new JsonEnvelope(mapper.writeValueAsBytes(object));
}

From source file:util.TestUtil.java

/**
 * Converts an object to json (as bytes).
 *
 * @param object the object to convert//w  w  w . j av  a  2s.c  om
 * @return JSON as bytes.
 * @throws JsonProcessingException thrown by jackson objectmapper
 */
public static byte[] toJson(BaseEntity object) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsBytes(object);
}

From source file:it.reply.orchestrator.util.TestUtil.java

/**
 * Convert Object To a Json byte array./*from w  ww.  j  av  a2  s . c om*/
 * 
 * @return byte[]
 */
public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper.writeValueAsBytes(object);
}

From source file:com.elearning.rest1.config.TestUtils.java

public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper.writeValueAsBytes(object);
}

From source file:am.ik.sendgrid.util.JsonCodec.java

static <T> ByteBuf encode(ByteBufAllocator allocator, ObjectMapper objectMapper, T source) {
    try {/*from   www.  jav  a2  s.com*/
        return allocator.directBuffer().writeBytes(objectMapper.writeValueAsBytes(source));
    } catch (JsonProcessingException e) {
        throw Exceptions.propagate(e);
    }
}

From source file:fi.helsinki.opintoni.web.WebTestUtils.java

public static byte[] toJsonBytes(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.registerModule(new JavaTimeModule());
    return mapper.writeValueAsBytes(object);
}

From source file:org.obiba.mica.web.rest.TestUtil.java

/**
 * Convert an object to JSON byte array.
 *
 * @param object the object to convert//from   ww w .  j ava2 s. com
 * @return the JSON byte array
 * @throws IOException
 */
public static byte[] convertObjectToJsonBytes(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper.writeValueAsBytes(object);
}