Serializing extension fields using scalapb.json4s

15 views
Skip to first unread message

Lior Gildar

unread,
Oct 2, 2019, 9:37:23 AM10/2/19
to ScalaPB
Hi. I'm trying to serialize a message into JSON including its extension fields. Can this be achieved using scalapb.json4s? 

The messages are defined as:

// This is defined in 1 proto file
message User {
  required string name = 1;

  extensions 100 to 999;
}

// This is defined in a another proto file
message UserExtension {
  extend User {
    optional string uuid = 100;
  }
}

And the code is:

private val user = User(name = "lior").withExtension(UserExtension.uuid)(Some(UUID.randomUUID().toString))
println(new Printer().print(user))

I would expect this to print both the name and uuid fields, but the output is {"name":"lior"}. 

Also, a followup question - assuming printing the extensions field is possible, is it possible to change the keys in the JSON to reflect that extensions fields are not part of the original class? For example, by adding a prefix, or putting them all inside a sub JSON?

Thanks.


Nadav Samet

unread,
Oct 2, 2019, 10:45:24 AM10/2/19
to Lior Gildar, ScalaPB
Hi Lior, great question! The implementation we have follows the JSON format, with the Java JsonFormat as the reference implementation. From the latter:

That is, when converting proto2 messages to JSON format, extensions and unknown fields will be treated as if they do not exist. This applies to proto2 messages embedded in proto3 messages as well.

Maybe it's something you can do outside the library? I.e. convert your message to json, convert the extension fields to json, then merge the jsons using the name transformations you need (adding a prefix)?


--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalapb/a8bba721-9d4b-4a25-8320-17349cdbb441%40googlegroups.com.


--
-Nadav

Lior Gildar

unread,
Oct 2, 2019, 11:07:07 AM10/2/19
to ScalaPB
Thanks Nadav. Seeing the google reference relates to proto2, can this be done by using proto3 syntax instead? I'm not 100% familiar with proto3 and the internal differences inside scalapb, but it seems that if I treat all of my extensions as one class and use the Any class, I should be able to serialize them into a single JSON key. Is that the case with your implementation, or am I missing something? 
To unsubscribe from this group and stop receiving emails from it, send an email to sca...@googlegroups.com.


--
-Nadav

Nadav Samet

unread,
Oct 2, 2019, 11:16:21 AM10/2/19
to Lior Gildar, ScalaPB
Adding a field of type Any to User would allow you to set an arbitrary proto message under that field. That's an option for you, or you might add a "UserExtension" message to the User, or the other way around.

To unsubscribe from this group and stop receiving emails from it, send an email to scalapb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalapb/a4261b7a-79e4-4faf-945e-808a2d9f9fd2%40googlegroups.com.


--
-Nadav

Nadav Samet

unread,
Oct 2, 2019, 1:12:42 PM10/2/19
to Lior Gildar, ScalaPB
Lior, another option is to register a custom message formatter for the User message, that can read/write json in the format you expect.
--
-Nadav
Reply all
Reply to author
Forward
0 new messages