protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

C# .NET deserialization of message that contains self-defined message type field and empty value field, decodes incorrectly

Open greyDoll opened this issue 2 years ago • 0 comments

What version of protobuf and what language are you using? Version: v3.5.1 Language:C++/C#

What operating system (Linux, Windows, ...) and version? Win10

What runtime / compiler are you using (e.g., python version or gcc version) msvc

What did you do? Hi, jskeet~ The problem performed like this: i use language C++ to serialize a message to a file named 'a.label', however, when i deserialize 'a.label' to a message in language C#, i get a incorrect result, details are as follows: the .proto file contents is:

syntax = "proto3";
import "google/protobuf/any.proto";

message Rect2f {
    float w= 1; 
    float h= 2;
}
message Shape{
    enum ExampleType{
        Type1= 0;
        Type2= 1;
    }

    ExampleType data_type = 1;
    Rect2f rect_size = 2;

    google.protobuf.Any origin = 3;
}

I serialize or deserialize Shape message in C++, the result is:

 {"data_type":"Type1","rect_size":{"w":300,"h":400}, "origin":{}}

but when i serialize Shape message in C++ and deserialize it in C#, i get this:

{ "dataType": "Type1", "rectSize": { "w": 300, "h": 400 }, "origin": { "@type": "", "@value": "" }}

it looks like some key fields are changed: data_type-->dataType, rect_size-->rectSize, empty value field is changed: "origin":{}-->"origin": { "@type": "", "@value": "" }. By the way, theye are many blank spaces appearing in result, i wonder the essential reason.

What did you expect to see

 {"data_type":"Type1","rect_size":{"w":300,"h":400}, "origin":{}}

What did you see instead?

{ "dataType": "Type1", "rectSize": { "w": 300, "h": 400 }, "origin": { "@type": "", "@value": "" }}

Make sure you include information that can help us debug (full error message, exception listing, stack trace, logs).

Anything else we should know about your project / environment

  • use function SerializeToArray(...) to serialize message in C++;
  • write the file as "std::ofstream::binary" in C++;
  • use function Parser.ParseFrom(data_bytes) to deserialize message in C#, the type of parameter data_bytes is byte array.

Looking forward to the reply, thank you.

greyDoll avatar Sep 21 '22 08:09 greyDoll