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:pushandroid.POST2GCM.java

public static void post(Content content) {

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

        // 1. URL
        URL url = new URL(URL);

        // 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;
        StringBuilder response = new StringBuilder();

        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:edu.brandeis.cs.json.JsonProxy.java

public static String obj2json(Object obj) throws Exception {
    ObjectWriter ow = new ObjectMapper().writer();
    String json = null;/* www .  j  a v a  2  s.  co  m*/
    try {
        json = ow.writeValueAsString(obj);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        throw new Exception(e);
    }
    return json;
}

From source file:org.craftercms.social.migration.util.scripting.ScriptUtils.java

public static String toJson(Object o) throws JsonProcessingException {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.getSerializationConfig().without(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.getSerializationConfig().with(SerializationFeature.WRITE_NULL_MAP_VALUES,
            SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
    return mapper.writeValueAsString(o);
}

From source file:com.blacklocus.jres.strings.ObjectMappers.java

static ObjectMapper newConfiguredObjectMapper() {
    return new ObjectMapper()
            // ElasticSearch is usually more okay with absence of properties rather than presence with null values.
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)
            // Allow empty JSON objects where they might occur.
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            // Not all properties need to be mapped back into POJOs.
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

From source file:com.kinglcc.spring.jms.utils.JsonUtils.java

private static ObjectMapper createMapper(boolean ignoreUnkownProps) {
    ObjectMapper mapper = new ObjectMapper();
    if (ignoreUnkownProps) {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }/*from w  w w  . jav  a2  s .  co m*/
    mapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATEFORMAT));
    return mapper;
}

From source file:com.hollowsoft.library.utility.utility.JsonParser.java

/**
 *
 * @param entity//from  w w  w.ja  v a2 s.  com
 * @return
 * @throws JsonProcessingException
 */
public static <T> String toJson(final T entity) throws JsonProcessingException {
    if (entity == null) {
        throw new IllegalArgumentException("The entity cannot be null.");
    }

    return new ObjectMapper().writer().writeValueAsString(entity);
}

From source file:svnserver.ext.gitlab.mapping.GitLabHookEvent.java

@NotNull
public static GitLabHookEvent parseEvent(@NotNull Reader reader) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(reader, GitLabHookEvent.class);
}

From source file:com.spectralogic.ds3autogen.Ds3NameMapperParserImpl.java

private static ObjectMapper initDs3NameMapper() {
    final ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new GuavaModule());
    return objectMapper;
}

From source file:com.codemacro.jcm.util.JsonUtil.java

public static <T> T fromString(String str, Class<T> clazz) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.readValue(str, clazz);
}

From source file:org.axway.grapes.commons.utils.JsonUtils.java

/**
 * Un-serialize a Json into Organization
 * @param organization String/* w w w  . ja v  a 2 s  . c  o m*/
 * @return Organization
 * @throws IOException
 */
public static Organization unserializeOrganization(final String organization) throws IOException {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS);
    return mapper.readValue(organization, Organization.class);
}