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

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

Introduction

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

Prototype

@SuppressWarnings("unchecked")
    public <T> T convertValue(Object fromValue, JavaType toValueType) throws IllegalArgumentException 

Source Link

Usage

From source file:com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.java

public static String getMessageBody(Message message) {
    try {/*from   w  w  w .  j a va2  s . c  o m*/
        if (message instanceof MapMessage) {
            MapMessage mm = (MapMessage) message;
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode root = mapper.createObjectNode();

            @SuppressWarnings("unchecked")
            Enumeration<String> e = mm.getMapNames();
            while (e.hasMoreElements()) {
                String field = e.nextElement();
                root.set(field, mapper.convertValue(mm.getObject(field), JsonNode.class));
            }
            return mapper.writer().writeValueAsString(root);
        } else if (message instanceof TextMessage) {
            TextMessage tm = (TextMessage) message;
            return tm.getText();
        } else if (message instanceof BytesMessage) {
            BytesMessage bm = (BytesMessage) message;
            byte[] bytes = new byte[(int) bm.getBodyLength()];
            if (bm.readBytes(bytes) == bm.getBodyLength()) {
                return new String(bytes);
            }
        } else {
            log.log(Level.SEVERE, "Unsupported message type:\n" + formatMessage(message));
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Unhandled exception retrieving message body:\n" + formatMessage(message), e);
    }

    return "";
}

From source file:com.yahoo.elide.utils.coerce.converters.FromMapConverter.java

/**
 * Convert value to Enum./*from  w  w w. j a  v  a2  s  .co m*/
 *
 * @param cls class to convert to
 * @param value value to convert
 * @param <T> object type
 * @return converted object
 */
@Override
public <T> T convert(Class<T> cls, Object value) {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.convertValue(value, cls);
}

From source file:com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.java

public static String formatMessage(Message message) {
    StringBuilder sb = new StringBuilder();

    try {/*from   w  w  w  .j  av a 2  s .  c  o m*/
        String headers = formatHeaders(message);
        if (headers.length() > 0) {
            sb.append("Message Headers:\n");
            sb.append(headers);
        }

        sb.append("Message Properties:\n");
        @SuppressWarnings("unchecked")
        Enumeration<String> propNames = message.getPropertyNames();
        while (propNames.hasMoreElements()) {
            String propertyName = propNames.nextElement();
            sb.append("  ");
            sb.append(propertyName);
            sb.append(": ");
            if (message.getObjectProperty(propertyName) != null) {
                sb.append(message.getObjectProperty(propertyName).toString());
            }
            sb.append("\n");
        }

        sb.append("Message Content:\n");
        if (message instanceof TextMessage) {
            sb.append(((TextMessage) message).getText());
        } else if (message instanceof MapMessage) {
            MapMessage mm = (MapMessage) message;
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode root = mapper.createObjectNode();

            @SuppressWarnings("unchecked")
            Enumeration<String> e = mm.getMapNames();
            while (e.hasMoreElements()) {
                String field = e.nextElement();
                root.put(field, mapper.convertValue(mm.getObject(field), JsonNode.class));
            }
            sb.append(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root));
        } else if (message instanceof BytesMessage) {
            BytesMessage bm = (BytesMessage) message;
            bm.reset();
            byte[] bytes = new byte[(int) bm.getBodyLength()];
            if (bm.readBytes(bytes) == bm.getBodyLength()) {
                sb.append(new String(bytes));
            }
        } else {
            sb.append("  Unhandled message type: " + message.getJMSType());
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Unable to format message:", e);
    }

    return sb.toString();
}

From source file:sample.todosapp.vertx.simplified.TodoDataVerticle.java

private JsonObject toJson(Object pojo) {
    ObjectMapper mapper = new ObjectMapper();
    Map map = mapper.convertValue(pojo, HashMap.class);
    return new JsonObject(map);
}

From source file:eu.trentorise.game.repo.NotificationPersistence.java

public Notification toNotification() {
    ObjectMapper mapper = new ObjectMapper();
    try {/*from  w w w  .j  a  va 2s .c o m*/
        return mapper.convertValue(getObj(), (Class<? extends Notification>) Thread.currentThread()
                .getContextClassLoader().loadClass(getType()));
    } catch (Exception e) {
        LogHub.error(null, logger, "Problem to load class {}", getType(), e);
        return null;
    }
}

From source file:com.metamx.emitter.core.LoggingEmitterConfigTest.java

@Test
public void testDefaults() {
    final Properties props = new Properties();
    final ObjectMapper objectMapper = new ObjectMapper();
    final LoggingEmitterConfig config = objectMapper.convertValue(Emitters.makeLoggingMap(props),
            LoggingEmitterConfig.class);

    Assert.assertEquals(LoggingEmitter.class.getName(), config.getLoggerClass());
    Assert.assertEquals("debug", config.getLogLevel());
}

From source file:com.metamx.emitter.core.LoggingEmitterConfigTest.java

@Test
public void testSettingEverything() {
    final Properties props = new Properties();
    props.setProperty("com.metamx.emitter.logging.class", "Foo");
    props.setProperty("com.metamx.emitter.logging.level", "INFO");

    final ObjectMapper objectMapper = new ObjectMapper();
    final LoggingEmitterConfig config = objectMapper.convertValue(Emitters.makeLoggingMap(props),
            LoggingEmitterConfig.class);

    Assert.assertEquals("Foo", config.getLoggerClass());
    Assert.assertEquals("INFO", config.getLogLevel());
}

From source file:com.metamx.emitter.core.HttpEmitterConfigTest.java

@Test
public void testDefaults() {
    final Properties props = new Properties();
    props.put("com.metamx.emitter.http.url", "http://example.com/");

    final ObjectMapper objectMapper = new ObjectMapper();
    final HttpEmitterConfig config = objectMapper.convertValue(Emitters.makeHttpMap(props),
            HttpEmitterConfig.class);

    Assert.assertEquals(60000, config.getFlushMillis());
    Assert.assertEquals(300, config.getFlushCount());
    Assert.assertEquals("http://example.com/", config.getRecipientBaseUrl());
    Assert.assertEquals(null, config.getBasicAuthentication());
    Assert.assertEquals(BatchingStrategy.ARRAY, config.getBatchingStrategy());
    Assert.assertEquals(5 * 1024 * 1024, config.getMaxBatchSize());
    Assert.assertEquals(250 * 1024 * 1024, config.getMaxBufferSize());
}

From source file:inti.ws.spring.resource.CommonMinifyableConfig.java

@SuppressWarnings("unchecked")
@Override//from w w w  . ja  v  a 2  s. c  o  m
public Map<String, WebResource> instanceWebResources(ObjectMapper mapper, JsonNode node) throws Exception {
    return parseJs(mapper.convertValue(node, Map.class));
}

From source file:com.metamx.emitter.core.HttpEmitterConfigTest.java

@Test
public void testSettingEverything() {
    final Properties props = new Properties();
    props.setProperty("com.metamx.emitter.flushMillis", "1");
    props.setProperty("com.metamx.emitter.flushCount", "2");
    props.setProperty("com.metamx.emitter.http.url", "http://example.com/");
    props.setProperty("com.metamx.emitter.http.basicAuthentication", "a:b");
    props.setProperty("com.metamx.emitter.http.batchingStrategy", "newlines");
    props.setProperty("com.metamx.emitter.http.maxBatchSize", "4");
    props.setProperty("com.metamx.emitter.http.maxBufferSize", "8");

    final ObjectMapper objectMapper = new ObjectMapper();
    final HttpEmitterConfig config = objectMapper.convertValue(Emitters.makeHttpMap(props),
            HttpEmitterConfig.class);

    Assert.assertEquals(1, config.getFlushMillis());
    Assert.assertEquals(2, config.getFlushCount());
    Assert.assertEquals("http://example.com/", config.getRecipientBaseUrl());
    Assert.assertEquals("a:b", config.getBasicAuthentication());
    Assert.assertEquals(BatchingStrategy.NEWLINES, config.getBatchingStrategy());
    Assert.assertEquals(4, config.getMaxBatchSize());
    Assert.assertEquals(8, config.getMaxBufferSize());
}