bijection icon indicating copy to clipboard operation
bijection copied to clipboard

JsonScalaCodec JSON output is not what I expected

Open zzztimbo opened this issue 10 years ago • 1 comments

Example thrift struct

struct Prediction {
  10: i64 userId,
  20: list<double> rawPrediction,
  30: list<double> probability,
  40: double prediction
}

Set up code

val pred = Prediction(123L, List(0.232,0.1234), List(0.123, 0.6789), 1.0)
val jsonCodec = JsonScalaCodec(Prediction)

Scrooge to string

scala> jsonCodec(pred)
res0: String = eyIxMCI6eyJpNjQiOjEyM30sIjIwIjp7ImxzdCI6WyJkYmwiLDIsMC4yMzIsMC4xMjM0XX0sIjMwIjp7ImxzdCI6WyJkYmwiLDIsMC4xMjMsMC42Nzg5XX0sIjQwIjp7ImRibCI6MS4wfX0=

The result String base64 decoded yields

{"10":{"i64":123},"20":{"lst":["dbl",2,0.232,0.1234]},"30":{"lst":["dbl",2,0.123,0.6789]},"40":{"dbl":1.0}}

This output is far from being human readable. I was expecting something along the lines of

{"userId":123,"rawPrediction":[0.232,0.1234],"probability":[0.123,0.6789],"prediction":1}

zzztimbo avatar Aug 31 '15 21:08 zzztimbo

This is by design, I believe. It uses the same protocol that thrift/scrooge uses to serialize thrift structs as json:

https://github.com/apache/thrift/blob/master/lib/java/src/org/apache/thrift/protocol/TJSONProtocol.java

benpence avatar Sep 01 '15 17:09 benpence