protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Why is map null value rejected on json parsing?

Open occho opened this issue 8 years ago • 2 comments

What language does this apply to?

About json parser. I'm looking at Java code.

Question

JsonFormat.Parser throws InvalidProtocolBufferException when a map value is null.

What is this for?

Line throwing an exception: https://github.com/protocolbuffers/protobuf/blob/v3.6.1/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java#L1542

Test: https://github.com/protocolbuffers/protobuf/blob/v3.6.1/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java#L649

This behavior seems to be inconsistent with the ways of handling null for other types. For repeated fields, the parser treats it as if the field is not present. For not repeated fields, it does the same thing.

Describe the solution you'd like

I'd like to make JsonFormat.Parser ignore null values even when they appear in map value.

occho avatar Sep 06 '18 12:09 occho

Neither repeated field nor map field allows null values: https://github.com/protocolbuffers/protobuf/blob/master/java/util/src/test/java/com/google/protobuf/util/JsonFormatTest.java#L435-L442

What's accepted:

{ "singularField": null, "repeataedField": null, "mapField": null}

What's not accepted:

{"repeatedField": [null], "mapField": {"key": null}}

It's done this way because a null value in repeated/map field can be interpreted in different ways. For example, {"repeatedField": [null]} can be interpreted as a repeated field with 0 entries, or a repeated field with 1 entry having a default value. Same applies to map fields. Because of this possibility of misinterpretation and that proto-to-json will never produce such output, we decided to reject such inputs.

xfxyjwf avatar Sep 07 '18 18:09 xfxyjwf

Thank you for your explanation. :) My question was answered.

occho avatar Sep 08 '18 09:09 occho