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.codenvy.eclipse.core.CodenvyProjectMetadata.java

public static void create(IProject project, CodenvyProjectMetadata projectMetadata) {
    PROJECT_METADATA_CACHE.put(project, projectMetadata);

    final IFile projectMetadataFile = project.getFolder(CODENVY_FOLDER_NAME)
            .getFile(PROJECT_METADATA_FILE_NAME);
    final ObjectMapper mapper = new ObjectMapper();
    try {//from  w  w w  .  ja v  a  2  s.co m

        final byte[] projectMetadataBytes = mapper.writeValueAsBytes(projectMetadata);
        if (!projectMetadataFile.exists()) {
            projectMetadataFile.create(new ByteArrayInputStream(projectMetadataBytes), true,
                    new NullProgressMonitor());
        } else {
            projectMetadataFile.setContents(new ByteArrayInputStream(projectMetadataBytes), IResource.FORCE,
                    new NullProgressMonitor());
        }

    } catch (JsonProcessingException | CoreException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.raythos.sentilexo.persistence.cql.PersistedEntity.java

public static byte[] toBinaryJSon(PersistedEntity item) {
    try {//from w  w  w .  j a va 2 s. co m
        SmileFactory f = new SmileFactory();
        f.configure(SmileParser.Feature.REQUIRE_HEADER, true);
        ObjectMapper mapper = new ObjectMapper(f);
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        byte[] result = mapper.writeValueAsBytes(item);
        return result;
    } catch (JsonProcessingException ex) {
        Logger.getLogger(QueryResultItemMapper.class.getName()).log(Level.SEVERE, null, ex);
        return null;

    }
}

From source file:org.apache.ode.jacob.examples.helloworld.HelloWorld.java

public static JacksonExecutionQueueImpl loadAndRestoreQueue(ObjectMapper mapper, JacksonExecutionQueueImpl in)
        throws Exception {
    byte[] json = mapper.writeValueAsBytes(in);
    // print json
    // System.out.println(new String(json));
    JacksonExecutionQueueImpl q2 = mapper.readValue(json, JacksonExecutionQueueImpl.class);
    return q2;/*from  w ww  . j av a2  s  .co  m*/
}

From source file:org.bremersee.sms.ExtensionUtils.java

/**
 * Transforms a JSON map into an object.
 * /*from w  ww . j  a  v a  2s.co  m*/
 * @param map
 *            the JSON map
 * @param valueType
 *            the class of the target object
 * @param objectMapper
 *            the JSON object mapper (can be null)
 * @return the target object
 * @throws IOException
 *             if transformation fails
 */
public static <T> T jsonMapToObject(Map<String, Object> map, Class<T> valueType, ObjectMapper objectMapper)
        throws IOException {
    if (map == null) {
        return null;
    }
    Validate.notNull(valueType, "valueType must not be null");
    if (objectMapper == null) {
        objectMapper = DEFAULT_OBJECT_MAPPER;
    }
    return objectMapper.readValue(objectMapper.writeValueAsBytes(map), valueType);
}

From source file:org.bremersee.pagebuilder.PageBuilderUtils.java

/**
 * Transforms a JSON map to an object.//from  w  w w  .  j a  v  a 2  s  .  c  om
 *
 * @param map          the JSON map
 * @param valueType    the class of the target object
 * @param objectMapper the JSON object mapper (optional)
 * @param <T>          value type
 * @return the target object
 * @throws IOException if transformation fails
 */
public static <T> T jsonMapToObject(final Map<String, Object> map, final Class<T> valueType,
        final ObjectMapper objectMapper) throws IOException {
    if (map == null) {
        return null;
    }
    Validate.notNull(valueType, "valueType must not be null");
    if (objectMapper == null) {
        return DEFAULT_OBJECT_MAPPER.readValue(DEFAULT_OBJECT_MAPPER.writeValueAsBytes(map), valueType);
    }
    return objectMapper.readValue(objectMapper.writeValueAsBytes(map), valueType);
}

From source file:org.seedstack.seed.rest.hal.HalRepresentationTest.java

@Test
public void test_hal_link_serialization() throws Exception {
    HalRepresentation halRepresentation = new HalRepresentation();
    halRepresentation.link("objects", "/pok");
    halRepresentation.embedded("objects", Lists.newArrayList(new Person("toto")));

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ObjectMapper objectMapper = new ObjectMapper();
    outputStream.write(objectMapper.writeValueAsBytes(halRepresentation));
    outputStream.flush();//from   w ww .ja v  a2  s  .  co m

    Assertions.assertThat(outputStream.toString()).isEqualTo(EXPECTED);
}

From source file:org.seedstack.seed.rest.internal.hal.HalMessageBodyWriter.java

@Override
public void writeTo(HalRepresentation halRepresentation, Class<?> type, Type genericType,
        Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
        OutputStream entityStream) throws IOException, WebApplicationException {
    ObjectMapper objectMapper = new ObjectMapper();
    try {//from  www.j  av a  2  s.  c o m
        entityStream.write(objectMapper.writeValueAsBytes(halRepresentation));
        entityStream.flush();
    } catch (JsonProcessingException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.couchbase.lite.support.JsonDocumentTest.java

public void testStringFragment() throws Exception {
    String fragment = "01234567890";
    ObjectMapper mapper = new ObjectMapper();
    byte[] json = mapper.writeValueAsBytes(fragment);
    JsonDocument jsdoc = new JsonDocument(json);
    assertEquals(fragment, jsdoc.jsonObject());
}

From source file:com.couchbase.lite.support.JsonDocumentTest.java

public void testBooleanFragment() throws Exception {
    Boolean fragment = true;//w  w  w .  ja  va2 s .co m
    ObjectMapper mapper = new ObjectMapper();
    byte[] json = mapper.writeValueAsBytes(fragment);
    JsonDocument jsdoc = new JsonDocument(json);
    assertEquals(fragment, jsdoc.jsonObject());
}

From source file:com.couchbase.lite.support.JsonDocumentTest.java

public void testIntegerFragment() throws Exception {
    Integer fragment = 5;/*from   w w  w .  j  av  a 2 s  . c o  m*/
    ObjectMapper mapper = new ObjectMapper();
    byte[] json = mapper.writeValueAsBytes(fragment);
    JsonDocument jsdoc = new JsonDocument(json);
    assertEquals(fragment, jsdoc.jsonObject());
}