Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#readerFor()
The following examples show how to use
com.fasterxml.jackson.databind.ObjectMapper#readerFor() .
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: ZoneIdDeserTest.java From jackson-modules-java8 with Apache License 2.0 | 7 votes |
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()); } }
Example 2
Source File: InstantDeserTest.java From jackson-modules-java8 with Apache License 2.0 | 6 votes |
@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); }
Example 3
Source File: InstantDeserTest.java From jackson-modules-java8 with Apache License 2.0 | 6 votes |
@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); }
Example 4
Source File: ZonedDateTimeDeserTest.java From jackson-modules-java8 with Apache License 2.0 | 6 votes |
@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); }
Example 5
Source File: LocalDateDeserTest.java From jackson-modules-java8 with Apache License 2.0 | 6 votes |
@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); } }
Example 6
Source File: ZoneOffsetDeserTest.java From jackson-modules-java8 with Apache License 2.0 | 6 votes |
@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()); } }
Example 7
Source File: ZoneIdDeserTest.java From jackson-modules-java8 with Apache License 2.0 | 6 votes |
@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); }
Example 8
Source File: SchemaGenerator.java From gradle-plugins with Apache License 2.0 | 5 votes |
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); }
Example 9
Source File: V2_35_2__Update_data_sync_job_parameters_with_system_setting_value.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
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 ); }
Example 10
Source File: StyleConfiguration.java From depgraph-maven-plugin with Apache License 2.0 | 5 votes |
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; }
Example 11
Source File: LocalDateDeserTest.java From jackson-modules-java8 with Apache License 2.0 | 5 votes |
@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); } }
Example 12
Source File: AbstractMarkerMixInTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Before public void setUp() { final ObjectMapper log4jObjectMapper = newObjectMapper(); writer = log4jObjectMapper.writer(); reader = log4jObjectMapper.readerFor(Log4jMarker.class); MarkerManager.clear(); }
Example 13
Source File: JacksonModelSerializer.java From curator with Apache License 2.0 | 4 votes |
public JacksonModelSerializer(ObjectMapper mapper, TypeReference type) { reader = mapper.readerFor(type); writer = mapper.writerFor(type); }
Example 14
Source File: JWTParser.java From java-jwt with MIT License | 4 votes |
JWTParser(ObjectMapper mapper) { addDeserializers(mapper); this.payloadReader = mapper.readerFor(Payload.class); this.headerReader = mapper.readerFor(Header.class); }
Example 15
Source File: JacksonUtils.java From native-navigation with MIT License | 4 votes |
public static ObjectReader readerForType(ObjectMapper mapper, Type type) { JavaType javaType = mapper.getTypeFactory().constructType(type); return mapper.readerFor(javaType); }
Example 16
Source File: Jackson.java From metanome-algorithms with Apache License 2.0 | 4 votes |
public static <T> ObjectReader createReader(Class<T> type) { ObjectMapper mapper = createMapper(); return mapper.readerFor(type); }
Example 17
Source File: JacksonSerializer.java From dremio-oss with Apache License 2.0 | 4 votes |
public JacksonSerializer(final ObjectMapper mapper, final Class<T> klazz) { this.reader = mapper.readerFor(klazz); this.writer = mapper.writer(); }
Example 18
Source File: AbstractJacksonLogEventParser.java From logging-log4j2 with Apache License 2.0 | 4 votes |
protected AbstractJacksonLogEventParser(final ObjectMapper objectMapper) { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); this.objectReader = objectMapper.readerFor(Log4jLogEvent.class); }
Example 19
Source File: ControlTunnel.java From Bats with Apache License 2.0 | 4 votes |
public JacksonSerDe(Class<MSG> clazz) { ObjectMapper mapper = new ObjectMapper(); writer = mapper.writerFor(clazz); reader = mapper.readerFor(clazz); }
Example 20
Source File: JsonConverter.java From rest-example with Apache License 2.0 | 3 votes |
/** * 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); }