Search in sources :

Example 1 with Jackson2JsonObjectMapper

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());
}
Also used : Jackson2JsonObjectMapper(org.springframework.integration.support.json.Jackson2JsonObjectMapper) Jackson2JsonObjectMapper(org.springframework.integration.support.json.Jackson2JsonObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BoonJsonObjectMapper(org.springframework.integration.support.json.BoonJsonObjectMapper) Test(org.junit.Test)

Example 2 with Jackson2JsonObjectMapper

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));
}
Also used : Jackson2JsonObjectMapper(org.springframework.integration.support.json.Jackson2JsonObjectMapper) Jackson2JsonObjectMapper(org.springframework.integration.support.json.Jackson2JsonObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with Jackson2JsonObjectMapper

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\""));
}
Also used : Pattern(java.util.regex.Pattern) Jackson2JsonObjectMapper(org.springframework.integration.support.json.Jackson2JsonObjectMapper) Matcher(java.util.regex.Matcher) Matchers.containsString(org.hamcrest.Matchers.containsString) Jackson2JsonObjectMapper(org.springframework.integration.support.json.Jackson2JsonObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BoonJsonObjectMapper(org.springframework.integration.support.json.BoonJsonObjectMapper) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Test (org.junit.Test)3 Jackson2JsonObjectMapper (org.springframework.integration.support.json.Jackson2JsonObjectMapper)3 BoonJsonObjectMapper (org.springframework.integration.support.json.BoonJsonObjectMapper)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1