com.fasterxml.jackson.datatype.joda.JodaModule Java Examples

The following examples show how to use com.fasterxml.jackson.datatype.joda.JodaModule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Serializer.java    From omise-java with MIT License 8 votes vote down vote up
private Serializer() {
    dateTimeFormatter = ISODateTimeFormat.dateTimeNoMillis();
    localDateFormatter = ISODateTimeFormat.date();

    objectMapper = new ObjectMapper()
            .registerModule(new JodaModule()
                    .addSerializer(DateTime.class, new DateTimeSerializer()
                            .withFormat(new JacksonJodaDateFormat(dateTimeFormatter), 0)
                    )
                    .addSerializer(LocalDate.class, new LocalDateSerializer()
                            .withFormat(new JacksonJodaDateFormat(localDateFormatter), 0)
                    )
            )

            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true)
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); // TODO: Deprecate in vNext
}
 
Example #2
Source File: JacksonConfig.java    From openapi-generator with Apache License 2.0 7 votes vote down vote up
public JacksonConfig() throws Exception {
    this.objectMapper = new ObjectMapper();
    
    this.objectMapper.registerModule(new JodaModule());

    // sample to convert any DateTime to readable timestamps
    //this.objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
}
 
Example #3
Source File: OnlineProcessTool.java    From ipst with Mozilla Public License 2.0 6 votes vote down vote up
private OnlineProcessParameters readParamsFile(String filename, PrintStream out)
        throws JsonParseException, JsonMappingException, IOException {
    Path procFile = Paths.get(filename);
    if (Files.exists(procFile)) {
        InputStream is = new FileInputStream(procFile.toFile());
        String json = IOUtils.toString(is);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JodaModule());
        objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
                false);
        return objectMapper.readValue(json, OnlineProcessParameters.class);
    } else {
        out.println("File not found: " + filename);
    }
    return null;
}
 
Example #4
Source File: JacksonConfig.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
public JacksonConfig() throws Exception {
    this.objectMapper = new ObjectMapper();
    
    this.objectMapper.registerModule(new JodaModule());

    // sample to convert any DateTime to readable timestamps
    //this.objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
}
 
Example #5
Source File: DiscoveryApi.java    From sdk-java with MIT License 6 votes vote down vote up
public DiscoveryApi(String apiKey, DiscoveryApiConfiguration configuration) {
  Preconditions.checkNotNull(apiKey, "The API key is mandatory");
  this.apiKey = apiKey;
  this.configuration = configuration;
  this.mapper = new ObjectMapper() //
      .registerModule(new JodaModule()) //
      .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

  this.client = new OkHttpClient.Builder()
               .readTimeout(configuration.getSocketTimeout(), TimeUnit.MILLISECONDS)
               .connectTimeout(configuration.getSocketConnectTimeout(), TimeUnit.MILLISECONDS)
               .build();

  this.pathByType = new HashMap<>();
  this.pathByType.put(Event.class, "events");
  this.pathByType.put(Attraction.class, "attractions");
  this.pathByType.put(Venue.class, "venues");

  this.apiKeyQueryParam=configuration.getApiKeyQueryParam();
}
 
Example #6
Source File: PollingState.java    From autorest-clientruntime-for-java with MIT License 6 votes vote down vote up
/**
 * Initializes an object mapper.
 *
 * @param mapper the mapper to initialize
 * @return the initialized mapper
 */
private static ObjectMapper initMapper(ObjectMapper mapper) {
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true)
            .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)
            .registerModule(new JodaModule())
            .registerModule(ByteArraySerializer.getModule())
            .registerModule(Base64UrlSerializer.getModule())
            .registerModule(DateTimeSerializer.getModule())
            .registerModule(DateTimeRfc1123Serializer.getModule())
            .registerModule(HeadersSerializer.getModule());
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
    return mapper;
}
 
Example #7
Source File: JacksonAdapter.java    From autorest-clientruntime-for-java with MIT License 6 votes vote down vote up
/**
 * Initializes an instance of JacksonMapperAdapter with default configurations
 * applied to the object mapper.
 *
 * @param mapper the object mapper to use.
 */
private static ObjectMapper initializeObjectMapper(ObjectMapper mapper) {
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true)
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)
            .registerModule(new JodaModule())
            .registerModule(ByteArraySerializer.getModule())
            .registerModule(Base64UrlSerializer.getModule())
            .registerModule(DateTimeSerializer.getModule())
            .registerModule(DateTimeRfc1123Serializer.getModule())
            .registerModule(HeadersSerializer.getModule());
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
    return mapper;
}
 
Example #8
Source File: JacksonFactory.java    From pardot-java-client with MIT License 6 votes vote down vote up
/**
 * Creates properly configured Jackson XML Mapper instances.
 * @return XmlMapper instance.
 */
public static XmlMapper newInstance() {
    if (instance != null) {
        return instance;
    }

    // Create new mapper
    final JacksonXmlModule module = new JacksonXmlModule();
    module.setDefaultUseWrapper(false);
    module.addDeserializer(ProspectCustomFieldValue.class, new ProspectCustomFieldDeserializer());

    final XmlMapper mapper = new XmlMapper(module);

    // Configure it
    mapper
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
        .setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
        .registerModule(new JodaModule())
        .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

    return mapper;
}
 
Example #9
Source File: JsonMetadataWriter.java    From navigator-sdk with Apache License 2.0 5 votes vote down vote up
private ObjectMapper newMapper() {
  ObjectMapper mapper = new ObjectMapper();
  SimpleModule module = new SimpleModule("MetadataSerializer");
  if (config.getApiVersion() < 9) {
    module.addSerializer(new EntitySerializer(registry));
  } else {
    module.addSerializer(new EntityV9Serializer(registry));
  }
  module.addSerializer(new RelationSerializer(registry));
  mapper.registerModule(module);
  mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  mapper.configure(DeserializationFeature.WRAP_EXCEPTIONS, false);
  mapper.registerModule(new JodaModule());
  return mapper;
}
 
Example #10
Source File: RestJsonConverter.java    From sdk-rest with MIT License 5 votes vote down vote up
/**
 * Creates the ObjectMapper that serializes json to entity. Wraps the root (most often "data").
 * 
 * See @JsonRootName on the RestEntities
 * 
 * @return
 */
private ObjectMapper createObjectMapperWithRootUnWrap() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
    mapper.addHandler(new CustomDeserializationProblemHandler());
   
    
    mapper.registerModule(new JodaModule());
    return mapper;
}
 
Example #11
Source File: AbstractMetricsDispatcher.java    From gradle-metrics-plugin with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
public static ObjectMapper getObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.registerModule(new JodaModule());
    registerEnumModule(mapper);
    return mapper;
}
 
Example #12
Source File: DynamoDocumentStoreTemplate.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
public DynamoDocumentStoreTemplate(final DatabaseSchemaHolder databaseSchemaHolder) {
    super(databaseSchemaHolder);
    mapper = new ObjectMapper();
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.registerModule(new JodaModule());
}
 
Example #13
Source File: JsonConfig.java    From nakadi with MIT License 5 votes vote down vote up
@Bean
@Primary
public ObjectMapper jacksonObjectMapper() {
    final ObjectMapper objectMapper = new ObjectMapper()
            .setPropertyNamingStrategy(SNAKE_CASE);

    objectMapper.registerModule(enumModule());
    objectMapper.registerModule(new Jdk8Module());
    objectMapper.registerModule(new ProblemModule());
    objectMapper.registerModule(new JodaModule());
    objectMapper.configure(WRITE_DATES_AS_TIMESTAMPS , false);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    return objectMapper;
}
 
Example #14
Source File: TestUtil.java    From ServiceCutter with Apache License 2.0 5 votes vote down vote up
/**
 * Convert an object to JSON byte array.
 *
 * @param object
 *            the object to convert
 * @return the JSON byte array
 * @throws IOException
 */
public static byte[] convertObjectToJsonBytes(Object object)
        throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    JodaModule module = new JodaModule();
    module.addSerializer(DateTime.class, new CustomDateTimeSerializer());
    module.addSerializer(LocalDate.class, new CustomLocalDateSerializer());
    mapper.registerModule(module);
    return mapper.writeValueAsBytes(object);
}
 
Example #15
Source File: JacksonConfiguration.java    From ServiceCutter with Apache License 2.0 5 votes vote down vote up
@Bean
public JodaModule jacksonJodaModule() {
    JodaModule module = new JodaModule();
    module.addSerializer(DateTime.class, new CustomDateTimeSerializer());
    module.addDeserializer(DateTime.class, new CustomDateTimeDeserializer());
    module.addSerializer(LocalDate.class, new CustomLocalDateSerializer());
    module.addDeserializer(LocalDate.class, new ISO8601LocalDateDeserializer());
    return module;
}
 
Example #16
Source File: TestUtil.java    From angularjs-springboot-bookstore with MIT License 5 votes vote down vote up
/**
 * Convert an object to JSON byte array.
 *
 * @param object
 *            the object to convert
 * @return the JSON byte array
 * @throws IOException
 */
public static byte[] convertObjectToJsonBytes(Object object)
        throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    JodaModule module = new JodaModule();
    DateTimeFormatterFactory formatterFactory = new DateTimeFormatterFactory();
    formatterFactory.setIso(DateTimeFormat.ISO.DATE);
    module.addSerializer(DateTime.class, new DateTimeSerializer(
        new JacksonJodaFormat(formatterFactory.createDateTimeFormatter()
            .withZoneUTC())));
    mapper.registerModule(module);
    return mapper.writeValueAsBytes(object);
}
 
Example #17
Source File: JacksonConfiguration.java    From angularjs-springboot-bookstore with MIT License 5 votes vote down vote up
@Bean
public JodaModule jacksonJodaModule() {
    JodaModule module = new JodaModule();
    DateTimeFormatterFactory formatterFactory = new DateTimeFormatterFactory();
    formatterFactory.setIso(DateTimeFormat.ISO.DATE);
    module.addSerializer(DateTime.class, new DateTimeSerializer(
            new JacksonJodaFormat(formatterFactory.createDateTimeFormatter()
                    .withZoneUTC())));
    return module;
}
 
Example #18
Source File: ObjectMapperProvider.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
public ObjectMapperProvider() {
    objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JodaModule());
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.setSerializationInclusion(Include.NON_NULL);

    // Swagger codegen generates the following code, but it seems to be redundant
    // as the Java model files for enums have @JsonValue on the toString() methods
    // objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
    // objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
}
 
Example #19
Source File: JsonCacheTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {
    // NOTE: we want each test to have its own pristine cache instance.
    root = JsonCache.Factory.instance.create();
    ObjectMapper mapper = root.getMapper();
    mapper.registerModule(new JavaTimeModule());
    mapper.registerModule(new JodaModule());
    mapper.registerModule(new ThreeTenModule());
    cache = root.child("/JsonCacheTest");
    reload();
}
 
Example #20
Source File: JsonDocumentSearchResponseUnmarshaller.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
public JsonDocumentSearchResponseUnmarshaller() {
    mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    mapper.setPropertyNamingStrategy(new LowerCasePropertyNamingStrategy());
}
 
Example #21
Source File: JsonDocumentUpdateMarshaller.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
private JsonDocumentUpdateMarshaller() {
    mapper = new ObjectMapper();
    final SimpleModule module = new SimpleModule();
    module.addSerializer(Boolean.class, new BooleanLiteralSerializer());
    mapper.registerModule(module);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.registerModule(new JodaModule());
    mapper.setPropertyNamingStrategy(new LowerCasePropertyNamingStrategy());
}
 
Example #22
Source File: JacksonXML.java    From dropwizard-xml with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link com.fasterxml.jackson.dataformat.xml.XmlMapper} using Woodstox
 * with Logback and Joda Time support.
 * Also includes all {@link io.dropwizard.jackson.Discoverable} interface implementations.
 *
 * @return XmlMapper
 */
public static XmlMapper newXMLMapper(JacksonXmlModule jacksonXmlModule) {

    final XmlFactory woodstoxFactory = new XmlFactory(new WstxInputFactory(), new WstxOutputFactory());
    final XmlMapper mapper = new XmlMapper(woodstoxFactory, jacksonXmlModule);

    mapper.registerModule(new GuavaModule());
    mapper.registerModule(new GuavaExtrasModule());
    mapper.registerModule(new JodaModule());
    mapper.registerModule(new FuzzyEnumModule());
    mapper.setPropertyNamingStrategy(new AnnotationSensitivePropertyNamingStrategy());
    mapper.setSubtypeResolver(new DiscoverableSubtypeResolver());

    return mapper;
}
 
Example #23
Source File: DocumentSearchInternalUtils.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private static ObjectMapper initializeObjectMapper() {
    ObjectMapper jsonMapper = new ObjectMapper();
    jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    jsonMapper.registerModule(new JodaModule());

    return jsonMapper;
}
 
Example #24
Source File: JsonUtil.java    From cm_ext with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link ObjectMapper} instance with the certain default
 * behavior: (1) sorting properties alphabetically, and (2) support for Joda
 * time format.
 * <p/>
 */
public static ObjectMapper createObjectMapper() {
  ObjectMapper newMapper = new ObjectMapper();
  newMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
  newMapper.registerModule(new JodaModule());
  return newMapper;
}
 
Example #25
Source File: Handler.java    From billow with Apache License 2.0 5 votes vote down vote up
public Handler(MetricRegistry registry, AWSDatabaseHolder dbHolder, long maxDBAgeInMs) {
    this.mapper = new ObjectMapper();
    this.mapper.addMixInAnnotations(DBInstance.class, DBInstanceMixin.class);
    this.mapper.addMixInAnnotations(PendingModifiedValues.class, PendingModifiedValuesMixin.class);
    this.mapper.addMixInAnnotations(DBInstanceStatusInfo.class, DBInstanceStatusInfoMixin.class);
    this.mapper.registerModule(new JodaModule());
    this.mapper.registerModule(new GuavaModule());
    this.registry = registry;
    this.dbHolder = dbHolder;
    this.maxDBAgeInMs = maxDBAgeInMs;
}
 
Example #26
Source File: JacksonDateUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSerializingJodaTime_thenCorrect() throws JsonProcessingException {
    final DateTime date = new DateTime(2014, 12, 20, 2, 30, DateTimeZone.forID("Europe/London"));

    final ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

    final String result = mapper.writeValueAsString(date);
    assertThat(result, containsString("2014-12-20T02:30:00.000Z"));
}
 
Example #27
Source File: ApiClient.java    From docusign-java-client with MIT License 5 votes vote down vote up
public ApiClient() {
  objectMapper = new ObjectMapper();
  objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
  objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
  objectMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
  objectMapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
  objectMapper.registerModule(new JodaModule());
  httpClient = buildHttpClient(debugging);
  objectMapper.setDateFormat(ApiClient.buildDefaultDateFormat());

  dateFormat = ApiClient.buildDefaultDateFormat();

  // Set default User-Agent.
  setUserAgent("Swagger-Codegen/3.6.0/java");

  // Setup authentications (key: authentication name, value: authentication).
  authentications = new HashMap<String, Authentication>();
  authentications.put("docusignAccessCode", new OAuth());

  // Derive the OAuth base path from the Rest API base url
  this.deriveOAuthBasePathFromRestBasePath();

  rebuildHttpClient();
}
 
Example #28
Source File: JSON.java    From docusign-java-client with MIT License 5 votes vote down vote up
public JSON() {
  mapper = new ObjectMapper();
  mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
  mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
  mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
  mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
  mapper.setDateFormat(new RFC3339DateFormat());
  mapper.registerModule(new JodaModule());
}
 
Example #29
Source File: JodaJsonDataFormatConfigurator.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(JacksonJsonDataFormat dataFormat) {
  ObjectMapper objectMapper = dataFormat.getObjectMapper();

  objectMapper.registerModule(new JodaModule());
  objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
      WRITE_DATES_AS_TIMESTAMPS , false);
}
 
Example #30
Source File: CsvStringSerializer.java    From pocket-etl with Apache License 2.0 5 votes vote down vote up
private void createObjectWriter() {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.registerModule(new JodaModule());
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    CsvSchema schema = mapper.schemaFor(classToSerialize).withColumnSeparator(getColumnSeparator()).withoutQuoteChar();
    writer = mapper.writer(schema);

    if (getWriteHeaderRow()) {
        schema = schema.withHeader();
    }

    firstRowWriter = mapper.writer(schema);
}