Can't correctly convert GPB to JSON

104 views
Skip to first unread message

Florian Simond

unread,
May 23, 2018, 10:20:30 AM5/23/18
to ScalaPB
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 ?

Nadav Samet

unread,
May 24, 2018, 1:43:31 PM5/24/18
to Florian Simond, ScalaPB
Hi Florian,

I think that the problem you are describing has been fixed in scalapb-json4s 0.7.1. Which version are you using?

-Nadav

--
You received this message because you are subscribed to the Google Groups "ScalaPB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalapb+unsubscribe@googlegroups.com.
To post to this group, send email to sca...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalapb/4094ae8c-4e75-41dd-9b89-7bb8c54783f6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-Nadav

Nadav Samet

unread,
May 24, 2018, 2:06:01 PM5/24/18
to Florian Simond, ScalaPB
FWIW, I added a unit test which seems to confirm that the current version should work as you'd expect; https://github.com/scalapb/scalapb-json4s/commit/7f233c61b1e6f0171febc2886af9cce5c1b184c3
--
-Nadav
Message has been deleted

Florian Simond

unread,
May 25, 2018, 2:27:09 AM5/25/18
to ScalaPB
Hi Nadav,

Thanks so much for your help. Indeed, I was using version 0.7.0, I did not realise that 0.7.1 was out. It now works as expected :)

I have another question, Is there an "easy" way to change the way a generated object is converted to json ? For example, is there a way to force a Pair object to be converted to {key.value: ValueByType.value} instead of {"key": key.value, "ValueByType": ValueByType.value}

{"key":"keykey","uint32_value":0}
should be
{"keykey":0}

I have been able to do that with spray-json, but not with scalapb-json4s at the moment...

Thanks again,
Florian

Nadav Samet

unread,
May 25, 2018, 9:05:08 AM5/25/18
to Florian Simond, ScalaPB
Yes, take a look at FormatRegistry - it lets you customize the way a message get serialized, and you can accomplish the effect that you want by customizing how Pair is serialized.

Hope this helps,
Nadav

On Thu, May 24, 2018 at 11:25 PM, Florian Simond <fsim...@gmail.com> wrote:
Hi Nadav,

Thanks so much for your help. Indeed, I was using version 0.7.0, I did not realise that 0.7.1 was out. It now works as expected :)

I have another question, Is there an "easy" way to change the way a generated object is converted to json ? For example, is there a way to force a Pair object to be converted to {key.value: ValueByType.value} instead of {"key": key.value, "ValueByType": ValueByType.value}
{"key":"keykey","uint32_value":0}
should be
{"keykey":0}

I have been able to do that with spray-json, but not with scalapb-json4s at the moment...

Thanks again,
Florian

--
You received this message because you are subscribed to the Google Groups "ScalaPB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalapb+unsubscribe@googlegroups.com.
To post to this group, send email to sca...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
-Nadav
Reply all
Reply to author
Forward
0 new messages