Numeric Booleans in ScalaPB and Json4s

22 views
Skip to first unread message

Raman Sarvaria

unread,
Apr 29, 2022, 9:43:49 AM4/29/22
to ScalaPB
Hi,

Context: I'm looking to map a .proto file to a Scala class in order to deserialize JSON and then apply the object to a Spark DataFrame. I forked the repo and added some logic to handle numeric booleans but I'm hoping to find a way to inject a custom parser for specifically Boolean handling so that no scalapb-json4s changes are required. An update to parsePrimitive would mean I need to inject into it an "allowNumericBooleans = false" option to not break existing functionality for users which I doubt is too clean.

I was looking through the scalapb-json4s code and noticed that you are explicitly testing for JInt(0) and JInt(1) being rejected when parsing primitive booleans.

I was wondering if there was a way to inject a custom parser specifically for the conversion of TYPE_BOOL to PBoolean here such that I would be able to support optional numeric booleans 0/1. I'm hoping to avoid creating a top level custom message parser as the provided custom logic works perfectly (minus numeric bool) and would save headache on maintenance.

From what I can tell the only way to do so would be to fork and add numeric 0/1 boolean handling behind an optional (off by default) flag in the switch block above. This is due to Parser.fromJsonToPMessage only looking for custom root parsers (here) and causing default parsing logic to follow: (parseSingleValue) -> (JsonFormat.parsePrimitive) -> (bool handling)

I was wondering:
  • Am I missing something here?
  • Are you in fact able to inject into the JSON parsing logic a custom parser for leaf-level Boolean fields?

Thanks,

Raman

Reply all
Reply to author
Forward
0 new messages