use of org.apache.servicecomb.foundation.protobuf.RootSerializer in project java-chassis by ServiceComb.
the class AnyEntrySchema method createEntryWriter.
private SchemaWriter<Object> createEntryWriter(String actualTypeName, Object _value) {
Message message = protoMapper.getProto().getMessage(actualTypeName);
if (message == null) {
// not standard, protobuf can not support or not define this type , just extend
return this::jsonExtend;
}
// standard pack
RootSerializer valueSerializer = protoMapper.createRootSerializer(message, _value.getClass());
String valueCanonicalName = message.getCanonicalName();
return (output, value) -> {
standardPack(output, value, valueCanonicalName, valueSerializer);
};
}
use of org.apache.servicecomb.foundation.protobuf.RootSerializer in project incubator-servicecomb-java-chassis by apache.
the class AnyEntrySchema method createEntryWriter.
private SchemaWriter<Object> createEntryWriter(String actualTypeName, Object _value) {
Message message = protoMapper.getProto().getMessage(actualTypeName);
if (message == null) {
// not standard, protobuf can not support or not define this type , just extend
return this::jsonExtend;
}
// standard pack
RootSerializer valueSerializer = protoMapper.createRootSerializer(message, _value.getClass());
String valueCanonicalName = message.getCanonicalName();
return (output, value) -> {
standardPack(output, value, valueCanonicalName, valueSerializer);
};
}
use of org.apache.servicecomb.foundation.protobuf.RootSerializer in project incubator-servicecomb-java-chassis by apache.
the class SerializerSchemaManager method createRootSerializer.
public RootSerializer createRootSerializer(Message message, Type type) {
if (ProtoUtils.isAnyMessage(message)) {
SchemaEx<Object> messageSchema = new AnyEntrySchema(protoMapper, type);
return new RootSerializer(messageSchema);
}
JavaType javaType = TypeFactory.defaultInstance().constructType(type);
SchemaEx<Object> messageSchema = getOrCreateMessageSchema(message, javaType);
return new RootSerializer(messageSchema);
}
use of org.apache.servicecomb.foundation.protobuf.RootSerializer in project java-chassis by ServiceComb.
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));
}
use of org.apache.servicecomb.foundation.protobuf.RootSerializer in project java-chassis by ServiceComb.
the class SerializerSchemaManager method createRootSerializer.
public RootSerializer createRootSerializer(Message message, Type type) {
if (ProtoUtils.isAnyMessage(message)) {
SchemaEx<Object> messageSchema = new AnyEntrySchema(protoMapper, type);
return new RootSerializer(messageSchema);
}
JavaType javaType = TypeFactory.defaultInstance().constructType(type);
SchemaEx<Object> messageSchema = getOrCreateMessageSchema(message, javaType);
return new RootSerializer(messageSchema);
}
Aggregations