Search in sources :

Example 1 with BoonJsonObjectMapper

use of org.springframework.integration.support.json.BoonJsonObjectMapper in project spring-integration by spring-projects.

the class JsonToObjectTransformerTests method testBoonJsonObjectMapper.

@Test
public void testBoonJsonObjectMapper() throws Exception {
    JsonToObjectTransformer transformer = new JsonToObjectTransformer(TestPerson.class, new BoonJsonObjectMapper());
    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 : BoonJsonObjectMapper(org.springframework.integration.support.json.BoonJsonObjectMapper) Test(org.junit.Test)

Example 2 with BoonJsonObjectMapper

use of org.springframework.integration.support.json.BoonJsonObjectMapper in project spring-integration by spring-projects.

the class JsonTransformersSymmetricalTests method testBoonObjectToJson_JsonToObject.

@Test
public void testBoonObjectToJson_JsonToObject() {
    TestPerson person = new TestPerson("John", "Doe", 42);
    person.setAddress(new TestAddress(123, "Main Street"));
    List<TestPerson> payload = new ArrayList<TestPerson>();
    payload.add(person);
    ObjectToJsonTransformer objectToJsonTransformer = new ObjectToJsonTransformer(new BoonJsonObjectMapper());
    Message<?> jsonMessage = objectToJsonTransformer.transform(new GenericMessage<Object>(payload));
    JsonToObjectTransformer jsonToObjectTransformer = new JsonToObjectTransformer(new BoonJsonObjectMapper());
    Object result = jsonToObjectTransformer.transform(jsonMessage).getPayload();
    assertThat(result, Matchers.instanceOf(List.class));
    assertEquals(person, ((List<?>) result).get(0));
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) BoonJsonObjectMapper(org.springframework.integration.support.json.BoonJsonObjectMapper) Test(org.junit.Test)

Example 3 with BoonJsonObjectMapper

use of org.springframework.integration.support.json.BoonJsonObjectMapper in project spring-integration by spring-projects.

the class ObjectToJsonTransformerTests method testBoonJsonObjectMapper.

@Test
public void testBoonJsonObjectMapper() throws Exception {
    ObjectToJsonTransformer transformer = new ObjectToJsonTransformer(new BoonJsonObjectMapper());
    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) Matcher(java.util.regex.Matcher) Matchers.containsString(org.hamcrest.Matchers.containsString) BoonJsonObjectMapper(org.springframework.integration.support.json.BoonJsonObjectMapper) Test(org.junit.Test)

Example 4 with BoonJsonObjectMapper

use of org.springframework.integration.support.json.BoonJsonObjectMapper in project spring-integration by spring-projects.

the class ObjectToJsonTransformerTests method testBoonJsonObjectMapper_toNode.

@Test
public void testBoonJsonObjectMapper_toNode() throws Exception {
    ObjectToJsonTransformer transformer = new ObjectToJsonTransformer(new BoonJsonObjectMapper(), ObjectToJsonTransformer.ResultType.NODE);
    TestPerson person = new TestPerson("John", "Doe", 42);
    person.setAddress(new TestAddress(123, "Main Street"));
    Object payload = transformer.transform(new GenericMessage<TestPerson>(person)).getPayload();
    assertThat(payload, instanceOf(Map.class));
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expression = parser.parseExpression("firstName + ': ' + address.street");
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.addPropertyAccessor(new MapAccessor());
    String value = expression.getValue(evaluationContext, payload, String.class);
    assertEquals("John: Main Street", value);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) MapAccessor(org.springframework.context.expression.MapAccessor) Matchers.containsString(org.hamcrest.Matchers.containsString) BoonJsonObjectMapper(org.springframework.integration.support.json.BoonJsonObjectMapper) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 BoonJsonObjectMapper (org.springframework.integration.support.json.BoonJsonObjectMapper)4 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 MapAccessor (org.springframework.context.expression.MapAccessor)1 Expression (org.springframework.expression.Expression)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1