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

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

Introduction

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

Prototype

public void writeValue(Writer w, Object value)
        throws IOException, JsonGenerationException, JsonMappingException 

Source Link

Document

Method that can be used to serialize any Java value as JSON output, using Writer provided.

Usage

From source file:org.wso2.carbon.apimgt.impl.GZIPUtils.java

public static File constructZippedResponse(Object data) throws APIManagementException {
    String tmpSourceFileName = System.currentTimeMillis() + APIConstants.JSON_FILE_EXTENSION;
    String tmpDestinationFileName = System.currentTimeMillis() + APIConstants.JSON_GZIP_FILENAME_EXTENSION;
    String sourcePath = System.getProperty(APIConstants.JAVA_IO_TMPDIR) + File.separator + tmpSourceFileName;
    String destinationPath = System.getProperty(APIConstants.JAVA_IO_TMPDIR) + File.separator
            + tmpDestinationFileName;//from w  w  w  .j a  va  2 s  .  c  o m
    File zippedResponse;

    File file = new File(sourcePath);
    FileWriter fileWriter = null;
    try {
        file.createNewFile();
        fileWriter = new FileWriter(file);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(fileWriter, data);
        compressFile(sourcePath, destinationPath);
        zippedResponse = new File(destinationPath);
    } catch (IOException e) {
        throw new APIManagementException("Error while constructing zipped response..", e);
    } finally {
        IOUtils.closeQuietly(fileWriter);
    }
    return zippedResponse;
}

From source file:helper.SerializationHelper.java

public static String serializeGridTopology(ActorTopology topology) {
    ObjectMapper mapper = new ObjectMapper();
    Writer strWriter = new StringWriter();
    try {//from   ww w .  j  a  v  a  2s.  c  om
        mapper.writeValue(strWriter, topology);
    } catch (Exception e) {
        System.out.println(e);
    }
    return strWriter.toString();
}

From source file:com.boundary.sdk.util.UnixTimeSerializerTest.java

public static void write(OutputStream out, MyDate measurement) {
    ObjectMapper mapper = new ObjectMapper();
    try {//ww  w .j  av a 2  s  .  com
        mapper.writeValue(out, measurement);
    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:AIR.Common.Json.JsonHelper.java

public static <T> String serialize(T obj) throws JsonGenerationException, JsonMappingException, IOException {
    if (obj == null)
        return null;

    ObjectMapper mapper = new ObjectMapper();

    String json = null;/*from  w  ww  .  ja  v  a 2s  .c o m*/

    StringWriter sw = new StringWriter();
    mapper.writeValue(sw, obj);

    json = sw.toString();
    sw.close();

    return json;
}

From source file:com.boundary.sdk.metric.MeasurementTest.java

public static void write(OutputStream out, Measurement measurement) {
    ObjectMapper mapper = new ObjectMapper();
    try {/*from  www  .  j av a2  s  .c  o  m*/
        mapper.writeValue(out, measurement);
    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:za.co.bronkode.jwtbroker.Tokenizer.java

public static String EncodeToken(Token token) {
    try (StringWriter hwriter = new StringWriter(); StringWriter cwriter = new StringWriter();) {
        ObjectMapper hmapper = new ObjectMapper();

        hmapper.writeValue(hwriter, token.getHeader());
        String header = hwriter.toString();
        hmapper.writeValue(cwriter, token.getClaim(claimClass));
        String claim = cwriter.toString();
        hwriter.close();/*from w  w  w  .j  ava  2s.c  o m*/
        cwriter.close();
        String h64 = Base64.getEncoder().encodeToString(header.getBytes());
        String c64 = Base64.getEncoder().encodeToString(claim.getBytes());
        String signature = GetSignature(h64, c64);
        StringBuilder sb = new StringBuilder(h64);
        sb.append(".");
        sb.append(c64);
        sb.append(".");
        sb.append(signature);
        return sb.toString();
    } catch (IOException ex) {
        Logger.getLogger(Tokenizer.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}

From source file:org.apache.taverna.activities.interaction.InteractionUtils.java

public static String objectToJson(final Object o) throws IOException {
    final ObjectMapper mapper = new ObjectMapper();
    final StringWriter sw = new StringWriter();
    mapper.writeValue(sw, o);
    final String theString = sw.toString();
    return theString;
}

From source file:helper.SerializationHelper.java

public static String serializeObject(Object object) {
    ObjectMapper mapper = new ObjectMapper();
    Writer strWriter = new StringWriter();
    try {/*from w  ww . j  a  v  a  2s  . c  o m*/
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        mapper.writeValue(strWriter, object);
    } catch (Exception e) {
        System.out.println(e);
    }
    return strWriter.toString();
}

From source file:bakuposter.gcm.server.POST2GCMessage.java

public static void post(String apiKey, Content content) {

    try {// ww  w.  j  a v  a  2  s.  com
        URL url = new URL("https://android.googleapis.com/gcm/send");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key=" + apiKey);
        conn.setDoOutput(true);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(content.getRegistration_ids().get(0));
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        mapper.writeValue(wr, content);
        wr.flush();
        wr.close();
        int responseCode = conn.getResponseCode();
        System.out.println("responseCode = " + responseCode);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.undercouch.bson4jackson.serializers.BsonSerializersTest.java

private static Object generateAndParse(Object data) throws Exception {
    Map<String, Object> m = new LinkedHashMap<String, Object>();
    m.put("data", data);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    ObjectMapper om = new ObjectMapper(new BsonFactory());
    om.registerModule(new BsonModule());
    om.writeValue(baos, m);

    byte[] r = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(r);

    BSONDecoder decoder = new BasicBSONDecoder();
    BSONObject bo = decoder.readObject(bais);

    return bo.get("data");
}