use of org.springframework.integration.support.json.Jackson2JsonObjectMapper in project spring-integration by spring-projects.
the class JsonToObjectTransformerTests method objectPayloadWithCustomMapper.
@Test
public void objectPayloadWithCustomMapper() throws Exception {
ObjectMapper customMapper = new ObjectMapper();
customMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, Boolean.TRUE);
customMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, Boolean.TRUE);
JsonToObjectTransformer transformer = new JsonToObjectTransformer(TestPerson.class, new Jackson2JsonObjectMapper(customMapper));
String jsonString = "{firstName:'John', lastName:'Doe', age:42, address:{number:123, street:'Main Street'}}";
Message<?> message = transformer.transform(new GenericMessage<String>(jsonString));
TestPerson person = (TestPerson) message.getPayload();
assertEquals("John", person.getFirstName());
assertEquals("Doe", person.getLastName());
assertEquals(42, person.getAge());
assertEquals("123 Main Street", person.getAddress().toString());
}
use of org.springframework.integration.support.json.Jackson2JsonObjectMapper in project spring-integration by spring-projects.
the class ObjectToMapTransformerTests method testCustomMapperSupport_DisableTimestampFlag_SerializesDateAsString.
@Test
public void testCustomMapperSupport_DisableTimestampFlag_SerializesDateAsString() throws Exception {
Employee employee = buildEmployee();
ObjectMapper customMapper = new ObjectMapper();
customMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
Map<String, Object> transformedMap = new ObjectToMapTransformer(new Jackson2JsonObjectMapper(customMapper)).transformPayload(employee);
assertThat(transformedMap.get("listOfDates[0][0]"), instanceOf(String.class));
assertThat(transformedMap.get("listOfDates[0][1]"), instanceOf(String.class));
assertThat(transformedMap.get("listOfDates[1][0]"), instanceOf(String.class));
assertThat(transformedMap.get("listOfDates[1][1]"), instanceOf(String.class));
}
use of org.springframework.integration.support.json.Jackson2JsonObjectMapper in project spring-integration by spring-projects.
the class ObjectToJsonTransformerTests method objectPayloadWithCustomObjectMapper.
@Test
public void objectPayloadWithCustomObjectMapper() throws Exception {
ObjectMapper customMapper = new ObjectMapper();
customMapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, Boolean.FALSE);
ObjectToJsonTransformer transformer = new ObjectToJsonTransformer(new Jackson2JsonObjectMapper(customMapper));
TestPerson person = new TestPerson("John", "Doe", 42);
person.setAddress(new TestAddress(123, "Main Street"));
String result = (String) transformer.transform(new GenericMessage<TestPerson>(person)).getPayload();
assertTrue(result.contains("firstName:\"John\""));
assertTrue(result.contains("lastName:\"Doe\""));
assertTrue(result.contains("age:42"));
Pattern addressPattern = Pattern.compile("(address:\\{.*?\\})");
Matcher matcher = addressPattern.matcher(result);
assertTrue(matcher.find());
String addressResult = matcher.group(1);
assertTrue(addressResult.contains("number:123"));
assertTrue(addressResult.contains("street:\"Main Street\""));
}
Aggregations