Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

JsonScalaCodec JSON output is not what I expected #227

Open
zzztimbo opened this issue Aug 31, 2015 · 1 comment
Open

JsonScalaCodec JSON output is not what I expected #227

zzztimbo opened this issue Aug 31, 2015 · 1 comment

Comments

@zzztimbo
Copy link

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 zzztimbo changed the title JsonScalaCodec is not what I expected JsonScalaCodec json output is not what I expected Aug 31, 2015
@zzztimbo zzztimbo changed the title JsonScalaCodec json output is not what I expected JsonScalaCodec JSON output is not what I expected Aug 31, 2015
@benpence
Copy link
Contributor

benpence commented Sep 1, 2015

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants