INT-1026 Added initial implementation of JSON transformers.

This commit is contained in:
Mark Fisher
2010-09-14 17:31:12 -07:00
parent 2d5a9dde49
commit 46c52f1b81
4 changed files with 367 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.json;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
import org.springframework.util.Assert;
/**
* Transformer implementation that converts a JSON string payload into an instance of the provided target Class.
*
* @author Mark Fisher
* @since 2.0
*/
public class JsonToObjectTransformer<T> extends AbstractPayloadTransformer<String, T> {
private final Class<T> targetClass;
private final ObjectMapper objectMapper;
public JsonToObjectTransformer(Class<T> targetClass) {
this(targetClass, null);
}
public JsonToObjectTransformer(Class<T> targetClass, ObjectMapper objectMapper) {
Assert.notNull(targetClass, "targetClass must not be null");
this.targetClass = targetClass;
this.objectMapper = (objectMapper != null) ? objectMapper : new ObjectMapper();
}
protected T transformPayload(String payload) throws Exception {
return this.objectMapper.readValue(payload, this.targetClass);
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.json;
import java.io.StringWriter;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
import org.springframework.util.Assert;
/**
* Transformer implementation that converts a payload instance into a JSON string representation.
*
* @author Mark Fisher
* @since 2.0
*/
public class ObjectToJsonTransformer extends AbstractPayloadTransformer<Object, String> {
private final ObjectMapper objectMapper;
public ObjectToJsonTransformer(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "objectMapper must not be null");
this.objectMapper = objectMapper;
}
public ObjectToJsonTransformer() {
this.objectMapper = new ObjectMapper();
}
protected String transformPayload(Object payload) throws Exception {
StringWriter writer = new StringWriter();
this.objectMapper.writeValue(writer, payload);
return writer.toString();
}
}

View File

@@ -0,0 +1,131 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.json;
import static org.junit.Assert.assertEquals;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;
/**
* @author Mark Fisher
* @since 2.0
*/
public class JsonToObjectTransformerTests {
@Test
public void objectPayload() throws Exception {
JsonToObjectTransformer<TestPerson> transformer = new JsonToObjectTransformer<TestPerson>(TestPerson.class);
String jsonString = "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"age\":42,\"address\":{\"number\":123,\"street\":\"Main Street\"}}";
TestPerson person = transformer.transformPayload(jsonString);
assertEquals("John", person.getFirstName());
assertEquals("Doe", person.getLastName());
assertEquals(42, person.getAge());
assertEquals("123 Main Street", person.getAddress().toString());
}
@Test
public void objectPayloadWithCustomMapper() throws Exception {
ObjectMapper customMapper = new ObjectMapper();
customMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, Boolean.TRUE);
customMapper.configure(Feature.ALLOW_SINGLE_QUOTES, Boolean.TRUE);
JsonToObjectTransformer<TestPerson> transformer = new JsonToObjectTransformer<TestPerson>(TestPerson.class, customMapper);
String jsonString = "{firstName:'John', lastName:'Doe', age:42, address:{number:123, street:'Main Street'}}";
TestPerson person = transformer.transformPayload(jsonString);
assertEquals("John", person.getFirstName());
assertEquals("Doe", person.getLastName());
assertEquals(42, person.getAge());
assertEquals("123 Main Street", person.getAddress().toString());
}
@SuppressWarnings("unused")
private static class TestPerson {
private String firstName;
private String lastName;
private int age;
private TestAddress address;
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setAddress(TestAddress address) {
this.address = address;
}
public TestAddress getAddress() {
return this.address;
}
@Override
public String toString() {
return "name=" + this.firstName + " " + this.lastName
+ ", age=" + this.age + ", address=" + this.address;
}
}
@SuppressWarnings("unused")
private static class TestAddress {
private int number;
private String street;
public void setNumber(int number) {
this.number = number;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return this.number + " " + this.street;
}
}
}

View File

@@ -0,0 +1,131 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.json;
import static org.junit.Assert.assertEquals;
import org.codehaus.jackson.JsonGenerator.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;
/**
* @author Mark Fisher
* @since 2.0
*/
public class ObjectToJsonTransformerTests {
@Test
public void simpleStringPayload() throws Exception {
ObjectToJsonTransformer transformer = new ObjectToJsonTransformer();
String result = transformer.transformPayload("foo");
assertEquals("\"foo\"", result);
}
@Test
public void simpleIntegerPayload() throws Exception {
ObjectToJsonTransformer transformer = new ObjectToJsonTransformer();
String result = transformer.transformPayload(123);
assertEquals("123", result);
}
@Test
public void objectPayload() throws Exception {
ObjectToJsonTransformer transformer = new ObjectToJsonTransformer();
TestAddress address = new TestAddress(123, "Main Street");
TestPerson person = new TestPerson("John", "Doe", 42);
person.setAddress(address);
String result = transformer.transformPayload(person);
String expected = "{\"address\":{\"number\":123,\"street\":\"Main Street\"},\"firstName\":\"John\",\"lastName\":\"Doe\",\"age\":42}";
assertEquals(expected, result);
}
@Test
public void objectPayloadWithCustomObjectMapper() throws Exception {
ObjectMapper customMapper = new ObjectMapper();
customMapper.configure(Feature.QUOTE_FIELD_NAMES, Boolean.FALSE);
ObjectToJsonTransformer transformer = new ObjectToJsonTransformer(customMapper);
TestPerson person = new TestPerson("John", "Doe", 42);
person.setAddress(new TestAddress(123, "Main Street"));
String result = transformer.transformPayload(person);
String expected = "{address:{number:123,street:\"Main Street\"},firstName:\"John\",lastName:\"Doe\",age:42}";
assertEquals(expected, result);
}
@SuppressWarnings("unused")
private static class TestPerson {
private String firstName;
private String lastName;
private int age;
private TestAddress address;
public TestPerson(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public TestAddress getAddress() {
return address;
}
public void setAddress(TestAddress address) {
this.address = address;
}
}
@SuppressWarnings("unused")
private static class TestAddress {
private int number;
private String street;
public TestAddress(int number, String street) {
this.number = number;
this.street = street;
}
public int getNumber() {
return number;
}
public String getStreet() {
return street;
}
}
}