ActivityUtil { public static byte[] serializeActivityUnit(ActivityUnit act) throws Exception { byte[] ret=null; java.io.ByteArrayOutputStream baos=new java.io.ByteArrayOutputStream(); MAPPER.writeValue(baos, act); ret = baos.toByteArray(); baos.close(); return (ret); } private  ActivityUtil(); static byte[] serializeActivityUnit(ActivityUnit act); static byte[] serializeQuerySpec(QuerySpec qs); static String objectToJSONString(Object obj); static byte[] serializeActivityUnitList(java.util.List<ActivityUnit> activities); static byte[] serializeActivityTypeList(java.util.List<ActivityType> activities); static ActivityUnit deserializeActivityUnit(byte[] act); static QuerySpec deserializeQuerySpec(byte[] qs); static java.util.List<ActivityUnit> deserializeActivityUnitList(byte[] act); static java.util.List<ActivityType> deserializeActivityTypeList(byte[] act);  }
@Test public void testJSONBinarySerialization() { ActivityUnit act1=createTestActivityUnit("TestId"); try { java.io.ByteArrayOutputStream os=new java.io.ByteArrayOutputStream(); java.io.ObjectOutputStream oos=new java.io.ObjectOutputStream(os); oos.writeObject(act1); oos.close(); os.close(); java.io.ByteArrayInputStream is=new java.io.ByteArrayInputStream(os.toByteArray()); java.io.ObjectInputStream ois=new java.io.ObjectInputStream(is); ActivityUnit act2=(ActivityUnit)ois.readObject(); ois.close(); is.close(); String s1=new String(ActivityUtil.serializeActivityUnit(act1)); String s2=new String(ActivityUtil.serializeActivityUnit(act2)); JsonNode node1=ActivityUtil.MAPPER.readTree(s1); JsonNode node2=ActivityUtil.MAPPER.readTree(s2); if (!node1.equals(node2)) { fail("Representations are different: s1="+s1+" s2="+s2); } } catch(Exception e) { e.printStackTrace(); fail("Failed to serialize: "+e); } }
@Test public void testJSONCompareActivityUnit() { ActivityUnit act=createTestActivityUnit("TestId"); try { byte[] b=ActivityUtil.serializeActivityUnit(act); if (b == null) { fail("null returned"); } java.io.InputStream is=ActivityUtilTest.class.getResourceAsStream("/json/activity.json"); byte[] inb2=new byte[is.available()]; is.read(inb2); is.close(); JsonNode node1=ActivityUtil.MAPPER.readTree(b); JsonNode node2=ActivityUtil.MAPPER.readTree(inb2); if (!node1.equals(node2)) { String s1=new String(b); String s2=new String(inb2); fail("JSON is different: created="+s1+" stored="+s2); } } catch(Exception e) { e.printStackTrace(); fail("Failed to serialize: "+e); } }