I need to perform RPC communication with .net.
We have defined a class containing the map type:public class TestMapDto { public int TestNumber; public Map<String, String> Pairs; }
I assign a value to this class and use ProtobufIOUtil for serialization:Map<String, String> hashMap = new TreeMap<>();
hashMap.put("test", "handsome");
TestMapDto testMapDto = TestMapDto.builder().TestNumber(111).Pairs(hashMap).build();
The resulting byte array is as follows:
But the result after serializing the native Protobuf file is as follows,8,111,18,18,10,16,10,4,116,101,115,116,18,8,104,97,110,100,115,111,109,101
8,111,18 ,16,10,4,116,101,115,116,18,8,104,97,110,100,115,111,109,101
They have different results after serialization,Causes .net not to deserialize properly
Hope for help,Sincerely
--
You received this message because you are subscribed to the Google Groups "protostuff" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protostuff+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protostuff/44dd478f-9a12-4f21-8a03-196545c6ac3f%40googlegroups.com.
syntax = "proto3";
@Data
public class PbArray implements Serializable { @Tag(1) public HashMap<String, String> dic;}
public static void main(String[] args) throws InvalidProtocolBufferException { // the proto.file generate java class serialization PbArrayModule.PbArray.Builder builder = PbArrayModule.PbArray.newBuilder(); builder.putDic("test", "cc"); PbArrayModule.PbArray pbArray = builder.build(); byte[] bytes = pbArray.toByteArray(); System.out.println("proto.file serialize:----- " + Arrays.toString(bytes));
// using ProtobufIOUtil HashMap<String, String> aMap = new HashMap<>(1); aMap.put("test", "cc"); PbArray array = new PbArray(); array.setDic(aMap); RuntimeSchema<PbArray> schema = RuntimeSchema.createFrom(PbArray.class); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); byte[] bytes1 = ProtobufIOUtil.toByteArray(array, schema, buffer); System.out.println("ProtobufIOUtil serialize: " + Arrays.toString(bytes1));
PbArrayModule.PbArray pbArray1 = PbArrayModule.PbArray.parseFrom(bytes1); System.out.println("using proto.file's way deserialize ProtobufIOUtil bytes1:"); System.out.println(pbArray1.toString()); }
proto.file serialize:----- [10, 10, 10, 4, 116, 101, 115, 116, 18, 2, 99, 99]ProtobufIOUtil serialize: [10, 12, 10, 10, 10, 4, 116, 101, 115, 116, 18, 2, 99, 99]using proto.file's way deserialize ProtobufIOUtil bytes1:dic { key: "\n\004test\022\002cc" value: ""}
To unsubscribe from this group and stop receiving emails from it, send an email to proto...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to protostuff+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protostuff/dd3774ee-48a5-4462-9ea5-e45c39de2dd3%40googlegroups.com.
syntax = "proto3";
message User { string userName = 1; map<string,string> testMap = 2;}
UserOuterClass.User user = UserOuterClass.User.newBuilder()
.setUserName("BB")
.putTestMap("aa","AA")
.build();
byte[] bytes = user.toByteArray();
//output bytes :10,2,66,66,18,8,10,2,97,97,18,2,65,65
@Data
public class ProtostuffUser implements Serializable {
@Tag(1)
private String userName;
@Tag(2)
private Map<String, String> testmap;
}
ProtostuffUser user = new ProtostuffUser();
user.setUserName("BB");
user.setTestmap(new HashMap<>());
user.getTestmap().put("aa","AA");
byte[] output= ProtoStuffUtil.serializer(user);
//output bytes :10,2,66,66,19,11,10,2,97,97,18,2,65,65,12,20
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.7.2</version>
</dependency>
public class ProtoStuffUtil { private static Logger log = LoggerFactory.getLogger(ProtoStuffutil.class); private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();
private static Objenesis objenesis = new ObjenesisStd(true);
private static <T> Schema<T> getSchema(Class<T> cls) { Schema<T> schema = (Schema<T>) cachedSchema.get(cls); if (schema == null) { schema = RuntimeSchema.createFrom(cls); if (schema != null) { cachedSchema.put(cls, schema); } } return schema; }
public static <T> byte[] serializer(T obj) { Class<T> cls = (Class<T>) obj.getClass(); LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); try { Schema<T> schema = getSchema(cls); return ProtostuffIOUtil.toByteArray(obj, schema, buffer); } catch (Exception e) { log.error("protobuf serialize fail"); throw new IllegalStateException(e.getMessage(), e); } finally { buffer.clear(); } }
public static <T> T deserializer(byte[] bytes, Class<T> clazz) { try { T message = (T) objenesis.newInstance(clazz); Schema<T> schema = getSchema(clazz); ProtostuffIOUtil.mergeFrom(bytes, message, schema); return message; } catch (Exception e) { log.error("protobuf deserialize fail"); throw new IllegalStateException(e.getMessage(), e); } }
}
--
You received this message because you are subscribed to the Google Groups "protostuff" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protostuff+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protostuff/3bc08290-0ad0-4200-b1d9-e1af3bfa4865o%40googlegroups.com.