下面列出了com.fasterxml.jackson.databind.ser.std.StdSerializer#com.fasterxml.jackson.datatype.joda.JodaModule 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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
}
/**
* 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;
}
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;
}
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();
}
/**
* 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;
}
/**
* 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;
}
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);
}
@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();
}
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);
}
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);
}
public JacksonJsonProvider() {
ObjectMapper objectMapper = new ObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.registerModule(new JodaModule())
.setDateFormat(new RFC3339DateFormat());
setMapper(objectMapper);
}
private static ObjectMapper create() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new GuavaModule());
mapper.registerModule(new JodaModule());
mapper.registerModule(new Jdk8Module());
mapper.registerModule(new JSR310Module());
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, false);
mapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, true);
mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper;
}
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 JavaTimeModule());
mapper.registerModule(new JodaModule());
}
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 JavaTimeModule());
mapper.registerModule(new JodaModule());
}
public JacksonJsonProvider() {
ObjectMapper objectMapper = new ObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.registerModule(new JodaModule())
.setDateFormat(new RFC3339DateFormat());
setMapper(objectMapper);
}
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);
}
public ProcessApiServiceImpl(ProcessDBUtils utils) {
this.utils = Objects.requireNonNull(utils);
objectMapper = new ObjectMapper();
objectMapper.registerModule(new JodaModule());
objectMapper.configure(WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"));
}
public JacksonJsonProvider() {
ObjectMapper objectMapper = new ObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.registerModule(new JodaModule())
.setDateFormat(new RFC3339DateFormat());
setMapper(objectMapper);
}
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);
}
private DeprecatedAlertPolicy readJson(String alertPolicyJson) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
mapper.setDateFormat(sdf);
return mapper.readValue(alertPolicyJson, DeprecatedAlertPolicy.class);
}
private IntegrationConfig readJson(String entityJson) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
mapper.setDateFormat(sdf);
return mapper.readValue(entityJson, IntegrationConfig.class);
}
private PolicyWithTeamInfo readJson(String policyJson) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
mapper.setDateFormat(sdf);
return mapper.readValue(policyJson, PolicyWithTeamInfo.class);
}
private String serializeMetrics(MetricQueryResults metricQueryResults) throws Exception {
SimpleModule module = new JodaModule();
module.addSerializer(new MetricNameSerializer(MetricName.class));
module.addSerializer(new MetricKeySerializer(MetricKey.class));
module.addSerializer(new MetricResultSerializer(MetricResult.class));
objectMapper.registerModule(module);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
// need to register a filter as soon as @JsonFilter annotation is specified.
// So specify an pass through filter
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.serializeAll();
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.addFilter("committedMetrics", filter);
objectMapper.setFilterProvider(filterProvider);
String result;
try {
result = objectMapper.writeValueAsString(metricQueryResults);
} catch (JsonMappingException exception) {
if ((exception.getCause() instanceof UnsupportedOperationException)
&& exception.getCause().getMessage().contains("committed metrics")) {
filterProvider.removeFilter("committedMetrics");
filter = SimpleBeanPropertyFilter.serializeAllExcept("committed");
filterProvider.addFilter("committedMetrics", filter);
result = objectMapper.writeValueAsString(metricQueryResults);
} else {
throw exception;
}
}
return result;
}
@Inject
public LocalDatabaseFrozenStateManager(final ApplicationDirectories applicationDirectories) {
this.frozenMarkerFile = new File(applicationDirectories.getWorkDirectory("db"), FROZEN_MARKER);
objectMapper.registerModule(new JodaModule());
// make sure jackson doesn't try to coax the FreezeRequest#timestamp into system time zone.
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
}
@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;
}
/**
* 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);
}
@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;
}
/**
* 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);
}
@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;
}
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;
}