8000 ProtobufIOUtil.toByteArray serializes byte[] data, and uses parseFrom in Protobuf to deserialize Map data error. · Issue #362 · protostuff/protostuff · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
ProtobufIOUtil.toByteArray serializes byte[] data, and uses parseFrom in Protobuf to deserialize Map data error. #362
Open
@zhouzhibing

Description

@zhouzhibing

Java entity class information

MyTest.java、MyTestItem.java

public class MyTest {
    private List<MyTestItem> itemList;
    private Map<Integer , MyTestItem> itemMap;
    // ... getters and setters omission
}
public class MyTestItem {
    private int id;
    private String name;
    // ... getters and setters omission
}

Protobuf protocol file content

message Test {
  repeated TestItem itemList = 1;
  map<int32,TestItem> itemMap = 2 ;
}

message TestItem {
  int32 id = 1;
  string name = 2;
}

Java test program

    @Test
    public void ProtobufTest() throws Exception {
        //Construct raw object data
        MyTest myTest = new MyTest();
        myTest.setItemList(List.of(new MyTestItem(1 , "name1") , new MyTestItem(2 , "name2")));
        myTest.setItemMap(Map.of(1 , new MyTestItem(1 , "name1") , 2, new MyTestItem(2 , "name2")));
        doPrintln("original object" , myTest.getItemList() , myTest.getItemMap());

        //Use ProtobufIOUtil to perform forward and deserialization column testing.
        Schema<MyTest> schema = RuntimeSchema.getSchema(MyTest.class);
        byte[] data = ProtobufIOUtil.toByteArray(myTest, schema, LinkedBuffer.allocate());
        MyTest newMyTest = schema.newMessage();
        ProtobufIOUtil.mergeFrom(data , newMyTest , schema);
        doPrintln("ProtobufIOUtil serialized object" , newMyTest.getItemList() , newMyTest.getItemMap());

        //Use parseFrom and data data in original Google's Protobuf to parse objects.
        Common.Test protobufTest = Common.Test.parseFrom(data);
        doPrintln("parseFrom serialized object" , protobufTest.getItemListList() , protobufTest.getItemMapMap());
    }

    /**
     * Print list, map
     */
    private void doPrintln(String type , List<?> list , Map<Integer , ?> map) {
        System.out.println(type + ", list quantity:" + list.size() + ", map quantity:" + map.size());
        list.forEach((item) -> {
            System.out.println(type + ", list content:" + item);
        });
        map.forEach((id , item) -> {
            System.out.println(type + ", map content:" + item);
        });
        System.out.println();
    }

Java test program results

original object, list quantity:2, map quantity:2
original object, list content:MyTestItem{id=1, name='name1'}
original object, list content:MyTestItem{id=2, name='name2'}
original object, map content:MyTestItem{id=2, name='name2'}
original object, map content:MyTestItem{id=1, name='name1'}

ProtobufIOUtil serialized object, list quantity:2, map quantity:2
ProtobufIOUtil serialized object, list content:MyTestItem{id=1, name='name1'}
ProtobufIOUtil serialized object, list content:MyTestItem{id=2, name='name2'}
ProtobufIOUtil serialized object, map content:MyTestItem{id=1, name='name1'}
ProtobufIOUtil serialized object, map content:MyTestItem{id=2, name='name2'}

parseFrom serialized object, list quantity:2, map quantity:1
parseFrom serialized object, list content:id: 1
name: "name1"

parseFrom serialized object, list content:id: 2
name: "name2"

parseFrom serialized object, map content:

Process ended with exit code 0

In the above example, I defined two entity classes and protocol file contents, used ProtobufIOUtil.toByteArray to serialize the data data, and then used ProtobufIOUtil.mergeFrom to deserialize and receive the data normally.
However, if I use Google Protobuf's Common.Test.parseFrom to parse the data serialized by ProtobufIOUtil.toByteArray, the List data is currently normal, but the Map data has errors.
If I want to parse and deserialize the xxxx.parseFrom for the ProtobufIOUtil.toByteArray to ensure that the Map data is correct, how should I do it?

Operating system information: windows
JDK information: jdk 21
Version information: 'implementation 'io.protostuff:protostuff-core:1.8.0'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0