use of org.apache.servicecomb.foundation.protobuf.RootSerializer in project incubator-servicecomb-java-chassis by apache.
the class TestModelWrap method pojoModel.
@SuppressWarnings("unchecked")
@Test
public void pojoModel() throws IOException {
RootSerializer pojoSerializer = modelProtoMapper.createRootSerializer("PojoModel", PojoModel.class);
RootDeserializer<Map<String, Object>> pojoMapDeserializer = modelProtoMapper.createRootDeserializer("PojoModel", Map.class);
RootDeserializer<PojoModel> pojoModelDeserializer = modelProtoMapper.createRootDeserializer("PojoModel", PojoModel.class);
RootSerializer protoSerializer = modelProtoMapper.createRootSerializer("ProtoModel", ProtoModel.class);
RootDeserializer<Map<String, Object>> protoMapDeserializer = modelProtoMapper.createRootDeserializer("ProtoModel", Map.class);
RootDeserializer<ProtoModel> protoModelDeserializer = modelProtoMapper.createRootDeserializer("ProtoModel", ProtoModel.class);
PojoModel pojoModel = new PojoModel();
pojoModel.init();
String jsonPojoModel = Json.encode(pojoModel);
Map<String, Object> mapFromPojoModel = (Map<String, Object>) Json.decodeValue(jsonPojoModel, Map.class);
ProtoModel protoModel = new ProtoModel();
protoModel.init();
String jsonProtoModel = Json.encode(protoModel);
Map<String, Object> mapFromProtoModel = (Map<String, Object>) Json.decodeValue(jsonProtoModel, Map.class);
// serialize
byte[] bytes = protoSerializer.serialize(protoModel);
Assert.assertArrayEquals(bytes, protoSerializer.serialize(mapFromProtoModel));
Assert.assertArrayEquals(bytes, pojoSerializer.serialize(pojoModel));
Assert.assertArrayEquals(bytes, pojoSerializer.serialize(mapFromPojoModel));
// deserialize pojoModel
PojoModel newPojoModel = pojoModelDeserializer.deserialize(bytes);
Assert.assertEquals(jsonPojoModel, Json.encode(newPojoModel));
Map<String, Object> mapFromNewPojoModel = pojoMapDeserializer.deserialize(bytes);
Assert.assertEquals(jsonPojoModel, Json.encode(mapFromNewPojoModel));
// deserialize protoModel
ProtoModel newProtoModel = protoModelDeserializer.deserialize(bytes);
Assert.assertEquals(jsonProtoModel, Json.encode(newProtoModel));
Map<String, Object> mapFromNewProtoModel = protoMapDeserializer.deserialize(bytes);
Assert.assertEquals(jsonProtoModel, Json.encode(mapFromNewProtoModel));
}
Aggregations