There are many existing protobuf codecs, but not suit for us, so we create new one:
one factory instance globally is enough
ProtoMapperFactory factory = new ProtoMapperFactory();
create mapper instance for each proto definition
ProtoMapper protoMapper = factory.createFromName("protobuf.proto");
ProtoMapper protoMapper = factory.createFromContent(protoContent);
serializer is reusable and thread safe
Assuming you have a proto definition
message User { string name = 1; }
and a POJO class
public class User { private String name; // getter and setter }
RootSerializer serializer = protoMapper.findRootSerializer("User"); User user = new User(); user.setName("userName"); byte[] pojoBytes= serializer.serialize(user); Map<String, Object> map = new HashMap<>(); map.put("name", "userName"); byte[] mapBytes = serializer.serialize(map); // pojoBytes equals mapBytes
deserializer is reusable and thread safe
RootDeserializer pojoDeserializer = protoMapper.createRootDeserializer(User.class, "User"); RootDeserializer mapDeserializer = protoMapper.createRootDeserializer(Map.class, "User"); User user = pojoDeserializer.deserialize(bytes); Map<String, Object> map = mapDeserializer.deserialize(bytes);
1.protobuf in our real scenes business model never bind to transport, and can switch between different transports dynamically that means if we choose standard protobuf, must build protobuf models from business models each time so should be much slower than the test results 2.protoStuff some scenes, there is no field but have getter or setter, so we can not use unsafe to access field so we disable protoStuff unsafe feature 3.jackson not support map, so skip map in map/mixed test 4.serialize result size ScbStrong/ScbWeak/Protobuf have the same and smaller size, because skip all default/null value Empty: Protostuff ScbStrong ScbWeak Protobuf Jackson ser time(ms) :250 250 235 156 437 ser len :36 0 0 0 56 deser time(ms):125 15 0 257 483 deser-ser len :36 0 0 0 56 Scalars: Protostuff ScbStrong ScbWeak Protobuf Jackson ser time(ms) :235 264 218 156 413 ser len :53 21 21 21 73 deser time(ms):156 63 94 225 469 deser-ser len :53 21 21 21 73 SimpleList: Protostuff ScbStrong ScbWeak Protobuf Jackson ser time(ms) :266 250 220 172 440 ser len :68 32 32 32 88 deser time(ms):234 94 109 265 499 deser-ser len :68 32 32 32 88 PojoList: Protostuff ScbStrong ScbWeak Protobuf Jackson ser time(ms) :297 343 235 187 543 ser len :56 20 20 20 76 deser time(ms):211 126 168 298 610 deser-ser len :56 20 20 20 76 Map: Protostuff ScbStrong ScbWeak Protobuf Jackson ser time(ms) :404 512 424 533 403 ser len :92 54 54 54 56 deser time(ms):500 343 406 750 359 deser-ser len :92 54 54 54 56 Mixed: Protostuff ScbStrong ScbWeak Protobuf Jackson ser time(ms) :579 704 547 579 625 ser len :161 127 127 127 125 deser time(ms):736 623 766 1015 798 deser-ser len :161 127 127 127 125