protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

JsonParser is unable to parse JSON messages serialized by Newtonsoft.Json or System.Text.Json due to mismatched property casing.

Open simon1749 opened this issue 2 weeks ago • 1 comments

What version of protobuf and what language are you using? Version: main/v3.6.0/v3.5.0 etc. (NOTE: please try updating to the latest version of protoc/runtime possible beforehand to attempt to resolve your problem)

Version: 3.32.1 from NuGet

Language: C++/Java/Python/C#/Ruby/PHP/Objective-C/Javascript

Language: C#

What operating system (Linux, Windows, ...) and version? OS: Windows 11

What runtime / compiler are you using (e.g., python version or gcc version) Runtime: .Net 9

What did you do? Steps to reproduce the behavior:

  1. Create a protobuf message.
  2. Instantiate an object of the generated class and assign values to its properties.
  3. Use either Newtonsoft.Json or System.Text.Json to serialize the object to a JSON string.
  4. Use the Protobuf JsonParser to parse the JSON string from step 3.

What did you expect to see The JsonParser should be able to parse the json string for it's a standard way of json deserialization.

What did you see instead? Unknow filed exception was thrown.

While examining this related issue (https://github.com/protocolbuffers/protobuf/issues/11987), I observed that CreateJsonFieldMap populates the map with both field.Name and field.JsonName. It seems reasonable that field.PropertyName should also be added after field.Name.

simon1749 avatar Dec 08 '25 20:12 simon1749

Use either Newtonsoft.Json or System.Text.Json to serialize the object to a JSON string.

Basically, don't do that, I'm afraid. Protobuf has specific rules about how the JSON format is handled - you need to use the parse/format capabilities of Google.Protobuf. I realize that can be counterintuitive, but I don't think there's any real alternative. You might be able to create Newtonsoft.Json or System.Text.Json adapters that would then delegate to the protobuf code, if this needs to be serialized/deserialized within a bigger document.

jskeet avatar Dec 09 '25 15:12 jskeet