@Test public void testSerializeCustom() { SFSSerializer sfsSerializer = new SFSSerializer(); Entity entity = new Entity(); Entity subEntity = new Entity(); Entity subEntityCustom = new Entity(); subEntity.setIntField(10); subEntityCustom.setIntField(99); entity.setWildcardList(Arrays.asList(subEntity)); entity.setWildcardedListCustom(Arrays.asList(subEntityCustom)); sfsSerializer.registerPostProcessor( new SerializePostProcessor() { @Override public <ST, DT extends TransportObject> void process(ST result, DT sourceObject) { if (sourceObject instanceof Entity) ((ISFSObject) result).putBool("postProcessed", true); } }); sfsSerializer.registerPreProcessor( new SerializePreProcessor() { @Override public <DT> void process(DT sourceObject) { if (sourceObject instanceof Entity) ((Entity) sourceObject).setPreProcessed(true); } }); ISFSObject sObj = sfsSerializer.serialize(entity); assertNotNull(sObj.getSFSArray("wildcardList")); assertTrue(sObj.getBool("postProcessed")); assertTrue(sObj.getBool("preProcessed")); Iterator<SFSDataWrapper> iterator = sObj.getSFSArray("wildcardList").iterator(); assertEquals( subEntity.getIntField() * 2, ((SFSObject) iterator.next().getObject()).getInt("intField").intValue()); Iterator<SFSDataWrapper> customIterator = sObj.getSFSArray("wildcardedListCustom").iterator(); assertEquals( subEntityCustom.getIntField(), ((SFSObject) customIterator.next().getObject()).getInt("intField")); }
@Test public void testDeserialize() { SFSSerializer sfsSerializer = new SFSSerializer(); ISFSObject subEntityObj = new SFSObject(); subEntityObj.putLong("longField", 20L); ISFSObject entityObj = new SFSObject(); entityObj.putInt("intField", 10); entityObj.putSFSObject("subEntity", subEntityObj); entityObj.putUtfString("notDeserializable", "value1"); entityObj.putUtfString("notSerializable", "value2"); entityObj.putUtfString("enumField", "white"); entityObj.putUtfString("changedName", "changedValue"); entityObj.putUtfString("fieldWithoutGetter", "value"); entityObj.putInt("fieldCustomSerializable", 10); entityObj.putUtfString("totallyIgnoredField", "value"); entityObj.putUtfStringArray("colors", Arrays.asList("black", "black", "white")); ISFSArray subArray = new SFSArray(); SFSObject subObj1 = new SFSObject(); subObj1.putLong("longField", 30L); subObj1.putUtfStringArray("stringsList", Arrays.asList("testString")); SFSObject subObj2 = new SFSObject(); subArray.addSFSObject(subObj1); subArray.addSFSObject(subObj2); entityObj.putSFSArray("subEntities", subArray); Entity entity = sfsSerializer.deserialize(Entity.class, entityObj); assertEquals(Entity.Color.white, entity.getEnumField()); assertEquals(Entity.Color.white, entity.getColors().get(2)); assertEquals(entityObj.getInt("intField"), entity.getIntField()); assertEquals((Long) 10L, entity.fieldCustomSerializable); assertEquals(entityObj.getUtfString("fieldWithoutGetter"), entity.fieldWithoutGetter); assertEquals(entityObj.getUtfString("notSerializable"), entity.getNotSerializable()); assertEquals(entityObj.getUtfString("changedName"), entity.getNameToBeChanged()); assertNull(entity.getNotDeserializable()); assertNull(entity.totallyIgnoredField); assertEquals(subEntityObj.getLong("longField"), entity.getSubEntity().getLongField()); assertNotNull(entity.getSubEntities()); assertFalse(entity.getSubEntities().isEmpty()); assertEquals(30L, entity.getSubEntities().get(0).getLongField().longValue()); assertEquals("testString", entity.getSubEntities().get(0).getStringsList().get(0)); }
@Test public void testSerialize() { SFSSerializer sfsSerializer = new SFSSerializer(); SubEntity subEntity = new SubEntity(); subEntity.setLongField(10L); subEntity.setStringsList(Arrays.asList("testString")); Entity entity = new Entity(); entity.setIntField(100); entity.setStringField("string"); entity.setSubEntity(subEntity); entity.setSubEntities(Arrays.asList(new SubEntity(100L), new SubEntity(100L))); entity.setNotDeserializable("value1"); entity.fieldWithoutGetter = "value"; entity.setNotSerializable("value2"); entity.setEnumField(Entity.Color.black); entity.setColors(Arrays.asList(Entity.Color.black, Entity.Color.white)); entity.fieldCustomSerializable = 20L; entity.totallyIgnoredField = "value"; ISFSObject sObj = sfsSerializer.serialize(entity); assertEquals(entity.getIntField(), sObj.getInt("intField")); assertEquals(entity.fieldWithoutGetter, sObj.getUtfString("fieldWithoutGetter")); assertEquals(entity.getEnumField().name(), sObj.getUtfString("enumField")); assertEquals(entity.getNameToBeChanged(), sObj.getUtfString("changedName")); assertEquals(entity.getNotDeserializable(), sObj.getUtfString("notDeserializable")); assertEquals(20, sObj.getInt("fieldCustomSerializable").intValue()); Collection<String> colors = sObj.getUtfStringArray("colors"); assertTrue(colors.contains(entity.getColors().get(1).name())); assertNull(sObj.getUtfString("totallyIgnoredField")); assertNull(sObj.getUtfString("notSerializable")); assertNull(sObj.getUtfString("ignoredField")); assertEquals(entity.getStringField(), sObj.getUtfString("stringField")); assertNotNull(sObj.getSFSObject("subEntity")); assertEquals(subEntity.getLongField(), sObj.getSFSObject("subEntity").getLong("longField")); assertNotNull(sObj.getSFSArray("subEntities")); Iterator<SFSDataWrapper> iterator = sObj.getSFSArray("subEntities").iterator(); assertEquals(100L, ((SFSObject) iterator.next().getObject()).getLong("longField").longValue()); assertEquals( "testString", sObj.getSFSObject("subEntity").getUtfStringArray("stringsList").iterator().next()); }