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

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

Introduction

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

Prototype

public ObjectMapper() 

Source Link

Document

Default constructor, which will construct the default JsonFactory as necessary, use SerializerProvider as its SerializerProvider , and BeanSerializerFactory as its SerializerFactory .

Usage

From source file:com.mylife.hbase.mapper.serialization.json.JsonSerializer.java

private static ObjectMapper GET_OBJECT_MAPPER() {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    return objectMapper;
}

From source file:fr.feedreader.websocket.UpdateFeed.java

public static void notifyUpdateFeed(Map<Feed, List<FeedItem>> newFeedItem, Map<Feed, Long> countUnread) {
    LogManager.getLogger().info("notification de mise  jour de flux");
    ObjectMapper mapper = new ObjectMapper();
    FeedUpdateWrapper feedUpdateWrapper = new FeedUpdateWrapper(newFeedItem, countUnread);
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        mapper.writeValue(baos, feedUpdateWrapper);
        String jsonResponse = baos.toString("UTF-8");
        availableSession.iterator();//from   w  w w .  j  a  v a 2s  .  co m
        for (Iterator<Session> it = availableSession.iterator(); it.hasNext();) {
            Session session = it.next();
            try {
                session.getBasicRemote().sendText(jsonResponse);
                LogManager.getLogger().info("Envoyer  \"" + session.getId() + "\"");
            } catch (ClosedChannelException ex) {
                LogManager.getLogger().error(
                        "Session \"" + session.getId() + "\" Fermer. Exclusion de la liste des session active");
                it.remove();
            } catch (IOException ex) {
                LogManager.getLogger().error("Erreur dans l'envoi  la websocket : " + session.getId(), ex);
            }
        }
    } catch (IOException ioe) {
        LogManager.getLogger().error("Impossible de convertir les flux mis  jour en json", ioe);
    }
}

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

/**
 * Unmarshal a JSON string to an object.
 * //from   w  w w .j av a 2s.c o m
 * @param json
 * @param type
 * @return
 * @throws SiteWhereException
 */
public static <T> T unmarshalJson(byte[] json, Class<T> type) throws SiteWhereException {
    ObjectMapper mapper = new ObjectMapper();
    try {
        return mapper.readValue(json, type);
    } catch (Throwable e) {
        throw new SiteWhereException("Unable to parse JSON.", e);
    }
}

From source file:com.github.tomakehurst.wiremock.common.Json.java

public static <T> String write(T object) {
    try {/*w ww.j av  a2  s .co m*/
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
    } catch (IOException ioe) {
        throw new RuntimeException("Unable to generate JSON from object. Reason: " + ioe.getMessage(), ioe);
    }
}

From source file:org.openmhealth.schema.configuration.JacksonConfiguration.java

public static ObjectMapper newObjectMapper() {

    ObjectMapper objectMapper = new ObjectMapper();

    // we represent JSON numbers as Java BigDecimals
    objectMapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);

    // we serialize dates, date times, and times as strings, not numbers
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

    // we preserve time zone offsets when deserializing
    objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);

    // we default to the ISO8601 format for JSR-310 and support Optional
    objectMapper.registerModule(new JavaTimeModule());
    objectMapper.registerModule(new Jdk8Module());

    // but we have to explicitly support the RFC3339 format over ISO8601 to make JSON Schema happy, specifically to
    // prevent the truncation of zero second fields
    SimpleModule rfc3339Module = new SimpleModule("rfc3339Module");
    rfc3339Module.addSerializer(new Rfc3339OffsetDateTimeSerializer(OffsetDateTime.class));
    objectMapper.registerModule(rfc3339Module);

    return objectMapper;
}

From source file:test.SemanticConverter3.java

public static void main(String[] args) throws JAXBException {

    SemanticConverter3 ri = new SemanticConverter3();

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    RegisterContextRequest rcr = new RegisterContextRequest();
    try {//from  ww  w  .  ja  v  a2 s. co  m
        rcr = objectMapper.readValue(ri.NGSI_FILE, RegisterContextRequest.class);
    } catch (Exception e) {

    }
    rcr.setTimestamp(Instant.now());
    ri.createJenaModel(rcr);

    //        //hMap.put("class", "OnDeviceResource"); 
    ////        hMap.put("class", new String[]{"ResourceService"});
    //        hMap.put("class", new String[]{"VirtualEntity"});
    ////        hMap.put("hasID", new String[]{"Resource_1"});
    //        hMap.put("hasID", new String[]{"VirtualEntity_1"});
    //        hMap.put("hasAttributeType", new String[]{"http://purl.oclc.org/NET/ssnx/qu/quantity#temperature"});     
    ////        hMap.put("isHostedOn", "PloggBoard_49_BA_01_light");
    ////        hMap.put("hasType", "Sensor");
    ////        hMap.put("hasName", "lightsensor49_BA_01");
    ////        hMap.put("isExposedThroughService", "http://www.surrey.ac.uk/ccsr/ontologies/ServiceModel.owl#49_BA_01_light_sensingService");
    ////        hMap.put("hasTag", "light sensor 49,1st,BA,office");
    //        hMap.put("hasLatitude", new String[]{"51.243455"});
    ////        hMap.put("hasGlobalLocation", "http://www.geonames.org/2647793/");
    ////        hMap.put("hasResourceID", "Resource_53_BA_power_sensor");
    ////        hMap.put("hasLocalLocation", "http://www.surrey.ac.uk/ccsr/ontologies/LocationModel.owl#U49");
    //        hMap.put("hasAltitude", new String[]{""});
    //        hMap.put("hasLongitude", new String[]{"-0.588088"});
    ////        hMap.put("hasTimeOffset", "20");
    //
    //        //ri.ONT_URL = "http://www.surrey.ac.uk/ccsr/ontologies/ServiceModel.owl#";
    //        //ri.ONT_URL = "http://www.surrey.ac.uk/ccsr/ontologies/VirtualEntityModel.owl#";
    //        //ri.ONT_FILE = "web/IoTA-Models/VirtualEntityModel.owl";
    //        ri.createJenaModel(hMap);
}

From source file:org.pathirage.freshet.utils.ExpressionSerde.java

public static Expression deserialize(String expression) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    return objectMapper.readValue(expression, Expression.class);
}

From source file:com.ant.myteam.gcm.POST2GCM.java

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

    try {/*  w  ww  .ja v  a  2  s .co  m*/

        // 1. URL
        URL url = new URL("https://android.googleapis.com/gcm/send");

        // 2. Open connection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // 3. Specify POST method
        conn.setRequestMethod("POST");

        // 4. Set the headers
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key=" + apiKey);

        conn.setDoOutput(true);

        // 5. Add JSON data into POST request body

        //`5.1 Use Jackson object mapper to convert Contnet object into JSON
        ObjectMapper mapper = new ObjectMapper();

        // 5.2 Get connection output stream
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());

        // 5.3 Copy Content "JSON" into
        mapper.writeValue(wr, content);

        // 5.4 Send the request
        wr.flush();

        // 5.5 close
        wr.close();

        // 6. Get the response
        int responseCode = conn.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // 7. Print result
        System.out.println(response.toString());

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.cinovo.cloudconductor.api.lib.helper.MapperFactory.java

/**
 * @return the object mapper/* w w w. ja v  a  2  s.  c  o m*/
 */
public static ObjectMapper createDefault() {
    ObjectMapper m = new ObjectMapper();
    m.registerModule(new JodaModule());
    m.registerModule(new GuavaModule());
    m.setSerializationInclusion(Include.NON_NULL);
    m.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    m.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
    m.enable(MapperFeature.AUTO_DETECT_GETTERS);
    return m;
}

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

public static Emitter create(Properties props, HttpClient httpClient, Lifecycle lifecycle) {
    return create(props, httpClient, new ObjectMapper(), lifecycle);
}