spray-json: JsonFormat vs JsonWriter/JsonReader for types which will only be serialized/deserialized

138 views
Skip to first unread message

Stevo Slavić

unread,
Jan 5, 2015, 5:20:28 AM1/5/15
to spray...@googlegroups.com
Hello spray community,

If one has types which will only be serialized or only deserialized to/from JSON in given library/app, why would one define JsonFormat which can write and read, and not just JsonWriter/JsonReader?

Is JsonFormat expected/looked-up by spray-json or is JsonWriter/JsonReader enough? Looking at code in SprayJsonSupport and spray.json package object, it seems only writer/reader should be enough.

It seems easier to define JsonFormat for e.g. a case class with jsonFormatX methods, compared to writing custom extension of JsonWriter/JsonReader (as there seem to be no jsonWriterX/jsonReaderX methods equivalent to jsonFormatX methods), but at least to me it doesn't feel right to provide both when only write or only read will actually be used. What are your thoughts?

Would it make sense to add jsonWriterX/jsonReaderX methods to spray-json?

Kind regards,
Stevo Slavic.

Derek Williams

unread,
Jan 5, 2015, 5:30:37 AM1/5/15
to spray...@googlegroups.com
An issue you might run into with just JsonReader/JsonWriter is that when you try to lookup JsonReader/JsonWriter for Option or a collection, it looks for a JsonFormat for the contained type, which will fail. Not sure if there is something I am missing that will fix that issue.

--
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/CAAUywg8mVec7%2BC8tyVWVH8G%3Dg8tWtsx96Rk2Rp31h7ZqzsszBg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Asko Kauppi

unread,
Jul 6, 2015, 8:42:38 AM7/6/15
to spray...@googlegroups.com
I ran into this today, and am wondering if there's any chance the Writer could work in future versions?

In my case, I need to control the precision of Doubles in a certain data structure, when being output to JSON. I could use the default reader, but now if I need to make a full Format, it brings up the question as to how do I proxy from the Format to the default reader...

Also, since we're working with implicits there's no compiler error when the Writer doesn't kick in. It just doesn't. 

So in short: is there hope this would get "fixed" in later Spray-son versions (I'm on 1.3.2). 

Link to SO entry discussing this:
Reply all
Reply to author
Forward
0 new messages