Protobuf ignoring bool and ints values due to default

Manas Saxena picture Manas Saxena · Jul 11, 2018 · Viewed 7.4k times · Source

I am converting a json file to string and then the string is converted to proto3 file.

Here is the json file:

{    "a": false,
     "b": 0
}

Here is how I convert my json file to string:

String json =Files.lines(Paths.get(filePath)).collect(Collectors.joining());

Here is how I convert my string to proto3 file:

    JsonFormat.parser().ignoringUnknownFields().merge(json,messageBuilder);
   MyProto proto = messageBuilder.build();

I have boolean and int fields in my json , the values of some of them are required to be default values (false for boolean and 0 for int).

When I deserialise the above file to proto3 java file then both the above fields are ignored , and my json becomes empty even though I have explicitly set the values(as you can see it in the above json file).

I know that proto3 ignores the default values while deserializing/serializing , however is there any way not to ignore fields which are explicitly set even though they are the default values?

Answer

Manas Saxena picture Manas Saxena · Jul 15, 2018

Used proto2 instead of proto3 . Proto2 has this behaviour - if a field is explicitly set , even though it is default value of the field , it is serialized and deserialized .