下面列出了com.fasterxml.jackson.databind.ObjectMapper#disable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static byte[] convertObjectToJsonBytes( Object object ) throws IOException
{
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );
objectMapper.configure( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false );
objectMapper.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false );
objectMapper.configure( SerializationFeature.WRAP_EXCEPTIONS, true );
objectMapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );
objectMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
objectMapper.configure( DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true );
objectMapper.configure( DeserializationFeature.WRAP_EXCEPTIONS, true );
objectMapper.disable( MapperFeature.AUTO_DETECT_FIELDS );
objectMapper.disable( MapperFeature.AUTO_DETECT_CREATORS );
objectMapper.disable( MapperFeature.AUTO_DETECT_GETTERS );
objectMapper.disable( MapperFeature.AUTO_DETECT_SETTERS );
objectMapper.disable( MapperFeature.AUTO_DETECT_IS_GETTERS );
return objectMapper.writeValueAsBytes( object );
}
private static ObjectMapper createObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());
mapper.registerModule(new JavaTimeModule());
mapper.registerModule(new ParameterNamesModule());
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
return mapper;
}
@BeforeClass
public static void initClassStubData() {
mapper = new ObjectMapper();
mapper.disable(MapperFeature.AUTO_DETECT_GETTERS);
mapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
mapper.disable(MapperFeature.AUTO_DETECT_FIELDS);
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
treeMapSerializer = new TreeMapSerializer();
}
public static ObjectMapper createJSONMapper() {
ObjectMapper mapper = new ObjectMapper();
// SerializationFeature for changing how JSON is written
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// DeserializationFeature for changing how JSON is read as POJOs:
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
return mapper;
}
@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"));
}
@Bean
public static SdkFactory httpClientSdkFactory(
List<OkHttp3ClientFactory> okHttpClientFactories,
Environment environment,
Provider<Registry> registry) {
OkHttpClientConfigurationProperties okHttpClientProperties =
Binder.get(environment)
.bind("ok-http-client", Bindable.of(OkHttpClientConfigurationProperties.class))
.orElse(new OkHttpClientConfigurationProperties());
OkHttpMetricsInterceptorProperties okHttpMetricsInterceptorProperties =
Binder.get(environment)
.bind(
"ok-http-client.interceptor", Bindable.of(OkHttpMetricsInterceptorProperties.class))
.orElse(new OkHttpMetricsInterceptorProperties());
List<OkHttp3ClientFactory> factories = new ArrayList<>(okHttpClientFactories);
OkHttp3MetricsInterceptor okHttp3MetricsInterceptor =
new OkHttp3MetricsInterceptor(registry, okHttpMetricsInterceptorProperties.skipHeaderCheck);
factories.add(new DefaultOkHttp3ClientFactory(okHttp3MetricsInterceptor));
OkHttp3ClientConfiguration config =
new OkHttp3ClientConfiguration(okHttpClientProperties, okHttp3MetricsInterceptor);
// TODO(rz): It'd be nice to make this customizable, but I'm not sure how to do that without
// bringing Jackson into the Plugin SDK (quite undesirable).
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new Jdk8Module());
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new KotlinModule());
objectMapper.disable(READ_DATE_TIMESTAMPS_AS_NANOSECONDS);
objectMapper.disable(WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
objectMapper.disable(FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.disable(FAIL_ON_EMPTY_BEANS);
objectMapper.setSerializationInclusion(NON_NULL);
return new HttpClientSdkFactory(
new CompositeOkHttpClientFactory(factories), environment, objectMapper, config);
}
public JsonMapper(Include include) {
mapper = new ObjectMapper();
// 设置输出时包含属性的风格
if (include != null) {
mapper.setSerializationInclusion(include);
}
// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
@Test
public void testIgnore(){
Map<String,String> map = new HashMap<>();
map.put("ST","31");
map.put("Flag","N");
map.put("CP","&&k=v&&");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.addMixIn(Data.class,DataDeserializationMixin.class);
Data data = objectMapper.convertValue(map, Data.class);
assertNull(data.getDataFlag());
}
public static DataRequest fromJson(String json) {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
DataRequest dataRequest = mapper.readValue(json, DataRequest.class);
return dataRequest;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public ObjectMapper build() {
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
registerQuantitySerializer(mapper);
mapper.registerModules(new MoneyModule(), new JavaTimeModule(), new Jackson2HalModule());
return mapper;
}
public static Ranker get(Reader reader, Config override, int featLength) {
String algorithm;
ObjectMapper mapper = new ObjectMapper();
mapper.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
try {
Map model = mapper.readValue(reader, Map.class);
algorithm = ((String)model.get("algorithm")).toLowerCase();
reader.reset();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
return get(algorithm, reader, override, featLength);
}
@Override
public ObjectMapper getObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
// objectMapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.setTimeZone(TimeZone.getDefault());
return objectMapper;
}
private static synchronized void buildDefaultObjectMapper() {
objectMapper = new ObjectMapper();
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
// objectMapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.setTimeZone(TimeZone.getDefault());
}
public TimedJsonStreamParser(List<TblColRef> allColumns, Map<String, String> properties) {
this.allColumns = allColumns;
if (properties == null) {
properties = StreamingParser.defaultProperties;
}
tsColName = properties.get(PROPERTY_TS_COLUMN_NAME);
tsParser = properties.get(PROPERTY_TS_PARSER);
separator = properties.get(PROPERTY_EMBEDDED_SEPARATOR);
strictCheck = Boolean.parseBoolean(properties.get(PROPERTY_STRICT_CHECK));
if (!StringUtils.isEmpty(tsParser)) {
try {
Class clazz = Class.forName(tsParser);
Constructor constructor = clazz.getConstructor(Map.class);
streamTimeParser = (AbstractTimeParser) constructor.newInstance(properties);
} catch (Exception e) {
throw new IllegalStateException("Invalid StreamingConfig, tsParser " + tsParser + ", parserProperties " + properties + ".", e);
}
} else {
throw new IllegalStateException("Invalid StreamingConfig, tsParser " + tsParser + ", parserProperties " + properties + ".");
}
mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
mapper.enable(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY);
for (TblColRef col : allColumns) {
colLowerCaseMap.put(col.getName(), col.getName().toLowerCase(Locale.ROOT));
}
}
private Graph readGraphFromFile(String filePath) throws URISyntaxException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapper.readValue(
OLSHttpClientTest.getFile(filePath),
Graph.class);
}
public ObjectMapper newSerializerMapper() {
final ObjectMapper mapper = new ObjectMapper();
// JDK8+ Time and Date handling / JSR-310
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.disable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);
mapper.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
return mapper;
}
@Test
public void privateTempTestToJSON() {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.enable(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY);
File tempOut = new File("temp.json");
// Test Integer:
SourceDataTagValue valueToWrite = sdt3.update(new ValueUpdate(1, "test", System.currentTimeMillis()));
mapper.writeValue(tempOut, valueToWrite);
SourceDataTagValue readValue = mapper.readValue(tempOut, SourceDataTagValue.class);
assertTrue(readValue.equals(valueToWrite));
// Test IntegerArray
Integer[] integerArray = { 7, 8, 9 };
valueToWrite = sdt3.update(new ValueUpdate(integerArray, "test", System.currentTimeMillis()));
mapper.writeValue(tempOut, valueToWrite);
readValue = mapper.readValue(tempOut, SourceDataTagValue.class);
// Object Test
SourceDataTagValue objectValue = sdt1.update(new ValueUpdate("Tach", "test2", System.currentTimeMillis()));
valueToWrite = sdt3.update(new ValueUpdate(objectValue, "test", System.currentTimeMillis()));
mapper.writeValue(tempOut, valueToWrite);
readValue = mapper.readValue(tempOut, SourceDataTagValue.class);
String blub = mapper.writeValueAsString(123456L);
}
catch (IOException e) {
e.printStackTrace();
}
}
public static DataRequestResponse fromJson(String json) {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
DataRequestResponse dataRequestResponse = mapper.readValue(json, DataRequestResponse.class);
return dataRequestResponse;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
public ObjectMapper onCreated(BeanCreatedEvent<ObjectMapper> event) {
final ObjectMapper mapper = event.getBean();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return mapper;
}
private static ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return mapper;
}