Parse Json array and Json object in the same field

40 views
Skip to first unread message

Matej Briškár

unread,
Mar 9, 2015, 4:33:21 AM3/9/15
to spray...@googlegroups.com
I am facing a problem when parsing expedia result. The result JSON differs according to the number of rooms available. In case multiple rooms are available, array is returned, otherwise only single object.

I am using case-classes for parsing the JSON, however I don't know what type to use to match both cases with the field (right now I am using something like List[Room], this fails in case one object is returned only).
Is there a way to catch this using case-classes only? If no, what other solution would you suggest?

Thanks,
Matej

Mathias Doenitz

unread,
Mar 9, 2015, 5:10:54 AM3/9/15
to spray...@googlegroups.com
Matej,

if you unmarshal to `Either[List[Room], Room]` things should “just work”.

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> 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/00f78961-72ac-4e54-80fa-e4899fcea641%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Roland Kuhn

unread,
Mar 9, 2015, 6:10:29 AM3/9/15
to spray...@googlegroups.com
That’s quite cool, didn’t know!
> To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/57D22607-3B01-4237-90DF-E04964CB9AFF%40spray.io.
> For more options, visit https://groups.google.com/d/optout.



Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


Mathias Doenitz

unread,
Mar 9, 2015, 6:17:11 AM3/9/15
to spray...@googlegroups.com
Once again: type-classes FTW!

---
mat...@spray.io
http://spray.io
> To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/FA412C8F-C7AD-41D5-84E8-948614537E6C%40rkuhn.info.
Reply all
Reply to author
Forward
0 new messages