下面列出了com.fasterxml.jackson.databind.ObjectMapper#readerFor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testDeserializationCaseInsensitiveEnabledOnValue() throws Throwable
{
ObjectMapper mapper = newMapperBuilder()
.withConfigOverride(LocalDate.class, o -> o.setFormat(JsonFormat.Value
.forPattern("dd-MMM-yyyy")
.withFeature(Feature.ACCEPT_CASE_INSENSITIVE_VALUES))
)
.build();
ObjectReader reader = mapper.readerFor(LocalDate.class);
String[] jsons = new String[] { quote("01-Jan-2000"), quote("01-JAN-2000"),
quote("01-jan-2000")};
for(String json : jsons) {
expectSuccess(reader, LocalDate.of(2000, Month.JANUARY, 1), json);
}
}
@Test
public void testLenientDeserializeFromEmptyString() throws Exception {
String key = "zoneId";
ObjectMapper mapper = newMapper();
ObjectReader objectReader = mapper.readerFor(MAP_TYPE_REF);
String valueFromNullStr = mapper.writeValueAsString(asMap(key, null));
Map<String, ZoneId> actualMapFromNullStr = objectReader.readValue(valueFromNullStr);
ZoneId actualDateFromNullStr = actualMapFromNullStr.get(key);
assertNull(actualDateFromNullStr);
String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, ""));
Map<String, ZoneId> actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr);
ZoneId actualDateFromEmptyStr = actualMapFromEmptyStr.get(key);
assertEquals("empty string failed to deserialize to null with lenient setting", null, actualDateFromEmptyStr);
}
public void testStrictDeserializeFromEmptyString() throws Exception {
final String key = "zoneId";
final ObjectMapper mapper = mapperBuilder()
.withCoercionConfig(LogicalType.DateTime,
cfg -> cfg.setCoercion(CoercionInputShape.EmptyString, CoercionAction.Fail))
.build();
final ObjectReader objectReader = mapper.readerFor(MAP_TYPE_REF);
String valueFromNullStr = mapper.writeValueAsString(asMap(key, null));
Map<String, ZoneId> actualMapFromNullStr = objectReader.readValue(valueFromNullStr);
assertNull(actualMapFromNullStr.get(key));
String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, ""));
try {
objectReader.readValue(valueFromEmptyStr);
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "Cannot coerce empty String");
verifyException(e, ZoneId.class.getName());
}
}
@Test
public void testStrictDeserializeFromEmptyString() throws Exception {
final String key = "zoneOffset";
final ObjectMapper mapper = mapperBuilder()
.withCoercionConfig(LogicalType.DateTime,
cfg -> cfg.setCoercion(CoercionInputShape.EmptyString, CoercionAction.Fail))
.build();
final ObjectReader objectReader = mapper.readerFor(MAP_TYPE_REF);
String valueFromNullStr = mapper.writeValueAsString(asMap(key, null));
Map<String, ZoneOffset> actualMapFromNullStr = objectReader.readValue(valueFromNullStr);
assertNull(actualMapFromNullStr.get(key));
String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, ""));
try {
objectReader.readValue(valueFromEmptyStr);
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "Cannot coerce empty String");
verifyException(e, ZoneOffset.class.getName());
}
}
@Test
public void testLenientDeserializeFromEmptyString() throws Exception {
String key = "duration";
ObjectMapper mapper = newMapper();
ObjectReader objectReader = mapper.readerFor(MAP_TYPE_REF);
String dateValAsNullStr = null;
String dateValAsEmptyStr = "";
String valueFromNullStr = mapper.writeValueAsString(asMap(key, dateValAsNullStr));
Map<String, Duration> actualMapFromNullStr = objectReader.readValue(valueFromNullStr);
Duration actualDateFromNullStr = actualMapFromNullStr.get(key);
assertNull(actualDateFromNullStr);
String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, dateValAsEmptyStr));
Map<String, Duration> actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr);
Duration actualDateFromEmptyStr = actualMapFromEmptyStr.get(key);
assertEquals("empty string failed to deserialize to null with lenient setting", null, actualDateFromEmptyStr);
}
@Test
public void testLenientDeserializeFromEmptyString() throws Exception {
String key = "zoneDateTime";
ObjectMapper mapper = newMapper();
ObjectReader objectReader = mapper.readerFor(MAP_TYPE_REF);
String valueFromNullStr = mapper.writeValueAsString(asMap(key, null));
Map<String, ZonedDateTime> actualMapFromNullStr = objectReader.readValue(valueFromNullStr);
ZonedDateTime actualDateFromNullStr = actualMapFromNullStr.get(key);
assertNull(actualDateFromNullStr);
String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, ""));
Map<String, ZonedDateTime> actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr);
ZonedDateTime actualDateFromEmptyStr = actualMapFromEmptyStr.get(key);
assertEquals("empty string failed to deserialize to null with lenient setting", null, actualDateFromEmptyStr);
}
@Test ( expected = MismatchedInputException.class)
public void testStrictDeserializeFromEmptyString() throws Exception {
final String key = "instant";
final ObjectMapper mapper = mapperBuilder()
.withConfigOverride(Instant.class,
o -> o.setFormat(JsonFormat.Value.forLeniency(false)))
.build();
final ObjectReader objectReader = mapper.readerFor(MAP_TYPE_REF);
String valueFromNullStr = mapper.writeValueAsString(asMap(key, null));
Map<String, Instant> actualMapFromNullStr = objectReader.readValue(valueFromNullStr);
assertNull(actualMapFromNullStr.get(key));
String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, ""));
objectReader.readValue(valueFromEmptyStr);
}
public V2_35_2__Update_data_sync_job_parameters_with_system_setting_value()
{
ObjectMapper mapper = new ObjectMapper();
mapper.activateDefaultTyping( BasicPolymorphicTypeValidator.builder().allowIfBaseType( JobParameters.class ).build() );
mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );
JavaType resultingJavaType = mapper.getTypeFactory().constructType( JobParameters.class );
reader = mapper.readerFor( resultingJavaType );
writer = mapper.writerFor( resultingJavaType );
}
public static void main(String[] args) throws IOException {
SchemaGenerator gen = new SchemaGenerator();
ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.readerFor(SchemaGenConfig.class);
SchemaGenConfig extension = reader.readValue(new String(Base64.getDecoder().decode(args[1])));
File outputDirectory = new File(args[0]);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
gen.run(extension, classLoader, outputDirectory);
}
public static StyleConfiguration load(StyleResource mainConfig, StyleResource... overrides) {
ObjectMapper mapper = createObjectMapper();
ObjectReader reader = mapper.readerFor(StyleConfiguration.class);
StyleConfiguration styleConfiguration = readConfig(reader, mainConfig);
for (StyleResource override : overrides) {
StyleConfiguration overrideConfig = readConfig(reader, override);
styleConfiguration.merge(overrideConfig);
}
return styleConfiguration;
}
@Test
public void testDeserializationCaseInsensitiveEnabled() throws Throwable
{
final ObjectMapper mapper = newMapperBuilder()
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES, true)
.withConfigOverride(LocalDate.class, o -> o.setFormat(
JsonFormat.Value.forPattern("dd-MMM-yyyy")))
.build();
ObjectReader reader = mapper.readerFor(LocalDate.class);
String[] jsons = new String[] { quote("01-Jan-2000"), quote("01-JAN-2000"),
quote("01-jan-2000")};
for(String json : jsons) {
expectSuccess(reader, LocalDate.of(2000, Month.JANUARY, 1), json);
}
}
@Before
public void setUp() {
final ObjectMapper log4jObjectMapper = newObjectMapper();
writer = log4jObjectMapper.writer();
reader = log4jObjectMapper.readerFor(Log4jMarker.class);
MarkerManager.clear();
}
public JacksonSerDe(Class<MSG> clazz) {
ObjectMapper mapper = new ObjectMapper();
writer = mapper.writerFor(clazz);
reader = mapper.readerFor(clazz);
}
public static ObjectReader readerForType(ObjectMapper mapper, Type type) {
JavaType javaType = mapper.getTypeFactory().constructType(type);
return mapper.readerFor(javaType);
}
public JacksonModelSerializer(ObjectMapper mapper, TypeReference type)
{
reader = mapper.readerFor(type);
writer = mapper.writerFor(type);
}
JWTParser(ObjectMapper mapper) {
addDeserializers(mapper);
this.payloadReader = mapper.readerFor(Payload.class);
this.headerReader = mapper.readerFor(Header.class);
}
public static <T> ObjectReader createReader(Class<T> type) {
ObjectMapper mapper = createMapper();
return mapper.readerFor(type);
}
public JacksonSerializer(final ObjectMapper mapper, final Class<T> klazz) {
this.reader = mapper.readerFor(klazz);
this.writer = mapper.writer();
}
protected AbstractJacksonLogEventParser(final ObjectMapper objectMapper) {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.objectReader = objectMapper.readerFor(Log4jLogEvent.class);
}
/**
* Creates an object of supplied type from supplied JSON string.
*
* @param inJsonRepresentation JSON representation from which to create object(s).
* @param inDestinationType Type of the (root) object to create.
* @param <T> Destination type.
* @return Object(s) created from JSON representation.
* @throws IOException If error occurs creating object(s).
*/
public static <T> T jsonToObject(final String inJsonRepresentation,
final Class<T> inDestinationType)
throws IOException {
final ObjectMapper theJsonObjectMapper = createAndConfigureJsonObjectMapper();
final ObjectReader theJsonObjectReader = theJsonObjectMapper.readerFor(inDestinationType);
return theJsonObjectReader.readValue(inJsonRepresentation);
}