Skip to content

Instantly share code, notes, and snippets.

@khoubyari
Created June 4, 2015 00:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khoubyari/35bc17342f2018206475 to your computer and use it in GitHub Desktop.
Save khoubyari/35bc17342f2018206475 to your computer and use it in GitHub Desktop.
Jackson JSON (De)Serialization Test
public abstract class AbstractJackson2MarshallingTest {
protected ObjectMapper mapper;
@Before
public void setUp() {
mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
protected String write(Object object) throws Exception {
Writer writer = new StringWriter();
mapper.writeValue(writer, object);
return writer.toString();
}
protected <T> T read(String source, Class<T> targetType) throws Exception {
return mapper.readValue(source, targetType);
}
}
//============
import org.skyscreamer.jsonassert.JSONAssert;
public class SomeObjectSerializationTest extends AbstractJackson2MarshallingTest {
@Test
public void testSomeObjectSerialization() throws Exception {
SomeObject o1 = new SomeObject();
//... create and fill the object
String o1Json = write(o1);
SomeObject o2 = read(o1Json, SomeObject.class);
String o2Json = write(o2);
JSONAssert.assertEquals(o1Json, o2Json, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment