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

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


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


public ObjectMapper setDateFormat(DateFormat dateFormat) 

Source Link


Method for configuring the default DateFormat to use when serializing time values as Strings, and deserializing from JSON Strings.


From source file:lab.mage.rate.example.NewsServer.java

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

    // configure resource
    final ResourceConfig resourceConfig = new ResourceConfig();

    // set news service
    final NewsService newsService = new NewsService();

    // set Jackson as JSON provider
    final ObjectMapper mapper = new ObjectMapper();
    mapper.setDateFormat(new SimpleDateFormat(ISO_8601_DATE_PATTERN));

    final JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
    provider.setMapper(mapper);/*from   ww  w  .j  a va  2  s  . co m*/

    // create Grizzly instance and add handler
    final HttpHandler handler = ContainerFactory.createContainer(GrizzlyHttpContainer.class, resourceConfig);
    final URI uri = new URI("http://localhost:4711/");
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri);
    final ServerConfiguration config = server.getServerConfiguration();
    config.addHttpHandler(handler, "/api");

    // start

From source file:com.ibm.dgaasx.utils.JSONUtils.java

public static String writeValue(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(object);

From source file:org.fcrepo.transform.http.responses.JsonObjectProvider.java

private static ObjectMapper createDefaultMapper() {
    final ObjectMapper mapper = new ObjectMapper();

    return mapper;

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);
    }// w w w  .  j  a  v a 2  s . c  o  m
    mapper.setDateFormat(new SimpleDateFormat(DEFAULT_DATEFORMAT));
    return mapper;

From source file:net.solarnetwork.node.reactor.io.json.JsonReactorSerializationService.java

private static ObjectMapper defaultObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'Z'");
    return mapper;

From source file:de.avpptr.umweltzone.utils.ContentProvider.java

@SuppressWarnings("unchecked") // for Collections.EMPTY_LIST
@NonNull// w  w w.j a v a 2s  .com
private static <T> List<T> getContent(final Context context, final String fileName, final String folderName,
        Class<T> contentType) {
    int rawResourceId = getResourceId(context, fileName, folderName);

    InputStream inputStream = context.getResources().openRawResource(rawResourceId);
    SimpleModule module = new SimpleModule();
    module.addDeserializer(Circuit.class, new CircuitDeserializer());
    ObjectMapper objectMapper = new ObjectMapper();
    String datePattern = context.getString(R.string.config_zone_number_since_date_format);
    objectMapper.setDateFormat(new SimpleDateFormat(datePattern, Locale.getDefault()));
    try {
        TypeFactory typeFactory = objectMapper.getTypeFactory();
        CollectionType collectionType = typeFactory.constructCollectionType(List.class, contentType);
        return objectMapper.readValue(inputStream, collectionType);
    } catch (IOException e) {
        // TODO Aware that app will crash when JSON is mis-structured.
    Log.e(ContentProvider.class.getName(), "Failure parsing zone data for: " + fileName);
    return Collections.EMPTY_LIST;

From source file:com.evrythng.java.wrapper.util.JSONUtils.java

 * Creates a pre-configured {@link ObjectMapper}.
 *//* ww w . jav a  2s  .  c  om*/
public static ObjectMapper createObjectMapper() {

    ObjectMapper mapper = new ObjectMapper();

    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
    mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
    mapper.setDateFormat(new ISO8601DateFormat());

    mapper.registerModule(new CoreModule());

    return mapper;

From source file:org.emfjson.jackson.module.EMFModule.java

 * Returns a pre configured mapper with the EMF module.
 * @return mapper/*from  w  w  w.j  ava2 s.com*/
public static ObjectMapper setupDefaultMapper() {
    final ObjectMapper mapper = new ObjectMapper();
    // same as emf
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);

    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    mapper.registerModule(new EMFModule());

    return mapper;

From source file:com.redhat.lightblue.util.JsonUtils.java

 * <p>//from   w w  w.j  a  va2  s .  c  o  m
 * Returns an object mapper to parse JSON text.</p>
 * <p>
 * <b>NOTE:</b> {@link ObjectMapper} should not be shared among threads.</p>
public static ObjectMapper getObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
    return mapper;

From source file:com.unboundid.scim2.common.utils.MapperFactory.java

 * Creates a custom SCIM compatible Jackson ObjectMapper. Creating new
 * ObjectMapper instances are expensive so instances should be shared if
 * possible. This can be used to set the factory used to build new instances
 * of the object mapper used by the SCIM 2 SDK.
 * @return an Object Mapper with the correct options set for serializing
 *     and deserializing SCIM JSON objects.
 *///from  w  w  w .  j a v a2  s.c  om
public static ObjectMapper createObjectMapper() {
    ObjectMapper mapper = new ObjectMapper(new ScimJsonFactory());

    // Don't serialize POJO nulls as JSON nulls.
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

    // Only use ISO8601 format for dates.
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.setDateFormat(new ScimDateFormat());

    // Do not care about case when de-serializing POJOs.
    mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

    // Use the case-insensitive JsonNodes.
    mapper.setNodeFactory(new ScimJsonNodeFactory());

    for (DeserializationFeature feature : deserializationCustomFeatures.keySet()) {
        mapper.configure(feature, deserializationCustomFeatures.get(feature));

    for (JsonGenerator.Feature feature : jsonGeneratorCustomFeatures.keySet()) {
        mapper.configure(feature, jsonGeneratorCustomFeatures.get(feature));

    for (JsonParser.Feature feature : jsonParserCustomFeatures.keySet()) {
        mapper.configure(feature, jsonParserCustomFeatures.get(feature));

    for (MapperFeature feature : mapperCustomFeatures.keySet()) {
        mapper.configure(feature, mapperCustomFeatures.get(feature));

    for (SerializationFeature feature : serializationCustomFeatures.keySet()) {
        mapper.configure(feature, serializationCustomFeatures.get(feature));

    return mapper;