rust-protobuf icon indicating copy to clipboard operation
rust-protobuf copied to clipboard

Error parsing 'null' values in protobuf-json-mapping

Open colinbankier opened this issue 3 years ago • 1 comments

Attempting to parse json like:

  let json = r#"{"date":"2019-09-01","val":null}"#;
  let data_point = protobuf_json_mapping::parse_from_str::<DataPoint>(json).expect("could not parse data point.");

Fails with: "ParseError { error: ParseErrorWithoutLoc(ExpectingNumber)"

The protobuf conventions at https://developers.google.com/protocol-buffers/docs/proto3#json say:

If a value is missing in the JSON-encoded data or if its value is null, it will be interpreted as the appropriate default value when parsed into a protocol buffer. null is an accepted value for all field types and treated as the default value of the corresponding field type.

The proto definition for this type has val as optional int32 value = 2 [json_name="val"]; which is correctly generated as pub value: ::std::option::Option<i32>,.

I'd expect this to be parsed into None.

Similarly, if "date" is null, it fails with ParseError { error: ParseErrorWithoutLoc(TokenizerError(ExpectStrLit)), however given the convention above, I'd also expect this to parse successfully as an empty string.

colinbankier avatar Jun 17 '22 07:06 colinbankier

Looks like a bug. Thank you for detailed report. Pr welcome.

stepancheg avatar Jun 17 '22 08:06 stepancheg