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

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

Introduction

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

Prototype

@SuppressWarnings("resource")
public String writeValueAsString(Object value) throws JsonProcessingException 

Source Link

Document

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

Usage

From source file:org.lable.rfc3881.auditlogger.serialization.ObjectMapperFactoryTest.java

@Test
public void objectMapperTest() throws JsonProcessingException {
    ObjectMapper objectMapper = getObjectMapper();

    TestObject testObject = new TestObject("a", null);

    System.out.println(objectMapper.writeValueAsString(testObject));
}

From source file:org.mayocat.store.rdbms.dbi.argument.MapAsJsonArgumentFactory.java

@Override
public Argument build(Class<?> expectedType, final Map value, StatementContext ctx) {
    try {/*from   w  w  w .j  a v  a 2  s  .  c om*/
        final PGobject jsonObject = new PGobject();
        final ObjectMapper mapper = new ObjectMapper();

        jsonObject.setType("json");
        jsonObject.setValue(mapper.writeValueAsString(value));

        return new Argument() {
            @Override
            public void apply(int position, PreparedStatement statement, StatementContext ctx)
                    throws SQLException {
                statement.setObject(position, jsonObject);
            }
        };
    } catch (JsonProcessingException | SQLException e) {
        throw new RuntimeException("Failed to convert map argument to JSON", e);
    }
}

From source file:com.shampan.db.codec.CountriesCodec.java

@Override
public void encode(BsonWriter writer, CountriesDAO countryList, EncoderContext encoderContext) {
    ObjectMapper mapper = new ObjectMapper();
    String json = "";
    try {/*from  w  w w . j  a  v  a2s  . c o m*/
        json = mapper.writeValueAsString(countryList);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    documentCodec.encode(writer, Document.parse(json), encoderContext);
}

From source file:com.shampan.db.codec.ReligionsCodec.java

@Override
public void encode(BsonWriter writer, ReligionsDAO religionList, EncoderContext encoderContext) {
    ObjectMapper mapper = new ObjectMapper();
    String json = "";
    try {//from   w  w w .j  a  v  a  2 s  .  c  o  m
        json = mapper.writeValueAsString(religionList);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    documentCodec.encode(writer, Document.parse(json), encoderContext);
}

From source file:io.klerch.alexa.tellask.model.wrapper.AlexaRequestStreamHandlerTest.java

private InputStream convertToStream(final SpeechletRequestEnvelope envelope) throws JsonProcessingException {
    final ObjectMapper mapper = new ObjectMapper();
    final String jsonEnvelope = mapper.writeValueAsString(envelope);
    return new ByteArrayInputStream(jsonEnvelope.getBytes(StandardCharsets.UTF_8));
}

From source file:volker.streaming.music.Track.java

@Override
public String toString() {
    ObjectMapper mapper = new ObjectMapper();
    String result;/*from www  . jav a2s  . c  o m*/
    try {
        result = mapper.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        LOG.error("Failed to print this Track to a JSON String", e);
        result = super.toString();
    }
    return result;
}

From source file:com.shampan.db.codec.LoginAttemptCodec.java

@Override
public void encode(BsonWriter writer, LoginAttemptDAO loginAttempt, EncoderContext encoderContext) {
    ObjectMapper mapper = new ObjectMapper();
    String json = "";
    try {/*  w  ww  .  j  a va 2 s .  co m*/
        json = mapper.writeValueAsString(loginAttempt);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    documentCodec.encode(writer, Document.parse(json), encoderContext);
}

From source file:com.shampan.db.codec.MessageCodec.java

@Override
public void encode(BsonWriter writer, MessageDAO message, EncoderContext encoderContext) {
    ObjectMapper mapper = new ObjectMapper();
    String json = "";
    try {/* www .  java  2  s  .  c o m*/
        json = mapper.writeValueAsString(message);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    Document doc = new Document();
    documentCodec.encode(writer, Document.parse(json), encoderContext);
}

From source file:de.stadtrallye.rallyesoft.net.NfcCallback.java

@Override
@TargetApi(14)//from w w  w.  j a  v  a  2s  .co m
public NdefMessage createNdefMessage(NfcEvent event) {
    ObjectMapper mapper = Serialization.getJsonInstance();
    String text = null;
    try {
        text = mapper.writeValueAsString(server.exportLogin());
    } catch (JsonProcessingException e) {
        Log.e(THIS, "Could not export Login", e);
    }
    NdefMessage msg = new NdefMessage(new NdefRecord[] {
            new NdefRecord(NdefRecord.TNF_MIME_MEDIA, Std.APP_MIME.getBytes(Charset.forName("US-ASCII")),
                    new byte[0], text.getBytes(Charset.forName("US_ASCII"))),
            NdefRecord.createApplicationRecord("de.stadtrallye.rallyesoft") });
    return msg;
}

From source file:com.hanuor.sapphire.hub.SapphireIntentHandler.java

public void saveIntent(String keyTag, Intent intent) throws IOException {
    ObjectMapper mapper = new ObjectMapper();

    String jsonString = mapper.writeValueAsString(intent);
    Log.d("SappHeya", "" + jsonString);
    //Intent bj = mapper.readValue(jsonString, Intent.class);
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("a", "aa");
    hashMap.put("b", "lalala");
    ArrayList<String> m = new ArrayList<String>();
    m.add("frost");
    m.add("girl");
    m.add("Bumblebee");
    hashMap.put("c", m.toString());
    Log.d("SappTestTag", "" + mapper.writeValueAsString(hashMap));

    //  String saveIntentthroughString = ToStringBuilder.reflectionToString(intent);
    // suggestionTempDBHandler.insertData(keyTag, saveIntentthroughString);
}