I'm trying to convert a gpb to JSON, but I can't get a satisfactory result.
I am quite new to GPB and ScalaPB, and therefore not sure if this is a bug or not
How to reproduce the problem:
Proto file:
syntax = "proto3";
package test;
message Dictionary {
repeated Pair pairs = 1;
}
message Pair {
string key = 1;
oneof value_by_type {
string string_value = 2;
uint32 uint32_value = 3;
}
}
val dict = Dictionary(Seq(Pair("myKey", Uint32Value(0))))
println(dict)
val dictBytes = dict.toByteArray
val dictP = Dictionary.parseFrom(dictBytes)
println(dictP)
val jObj = new scalapb.json4s.Printer(
includingDefaultValueFields = true,
preservingProtoFieldNames = true
).print(dictP)
println(jObj)
output:
Dictionary(List(Pair(keykey,Uint32Value(0))))
Dictionary(Vector(Pair(keykey,Uint32Value(0))))
{"pairs":[{"key":"keykey","string_value":"","uint32_value":0}]}
If I set includingDefaultValueFields to false, I get:
{"pairs":[{"key":"keykey"}]}
I understand that 0 is the default value and that's why the field is removed, but is there a way to say that we want to keep all the fields that were actually used ?
The printing of dictP seems quite good:
Dictionary(Vector(Pair(keykey,Uint32Value(0)))), and that's what I would like to get as a JSON... :
{"pairs":[{"key":"keykey","uint32_value":0}]}
Is there a way to do that ?