Created
June 4, 2015 00:21
-
-
Save khoubyari/35bc17342f2018206475 to your computer and use it in GitHub Desktop.
Jackson JSON (De)Serialization Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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