[2.3.x scala] play-json Seq[Either[A,B]] deserialization

521 views
Skip to first unread message

Nikita Melkozerov

unread,
Dec 23, 2014, 12:02:53 AM12/23/14
to play-fr...@googlegroups.com

Dear Play Framework users,

I'm trying to convert json to Seq[Either[Profile, Page]]

Here are Profile and Page case classes:

case class Page(id: Int, name: String, screen_name: String, is_closed: Int, photo_50: String, photo_100: String, photo_200: String)

case class Profile(id: Int, first_name: String, last_name: String)

Sample json:

{  
  "response":{  
    "count":3,
    "items":[  
      {  
        "id":2158488,
        "name":"LIVE",
        "screen_name":"live",
        "is_closed":0,
        "type":"page",
        "photo_50":"https://pp.vk.me/c623416/v623416604/10e29/AUwb4GWpbD4.jpg",
        "photo_100":"https://pp.vk.me/c623416/v623416604/10e28/DNSjabzHo70.jpg",
        "photo_200":"https://pp.vk.me/c623416/v623416604/10e26/UAWL2PWZ_wM.jpg"
      },
      {  
        "id":11982368,
        "name":"Forbes",
        "screen_name":"forbes",
        "is_closed":0,
        "type":"page",
        "photo_50":"https://pp.vk.me/c621326/v621326364/46b3/PE8CH3wuGSU.jpg",
        "photo_100":"https://pp.vk.me/c621326/v621326364/46b2/_K_Pn8ayf08.jpg",
        "photo_200":"https://pp.vk.me/c621326/v621326364/46b1/U58-5jSTiHI.jpg"
      },
      {  
        "type":"profile",
        "id":123456,
        "first_name":"John",
        "last_name":"Doe"
      }
    ]
  }
}

I wonder how to write an implicit Reads for Either type.

Can someone please give me a hint on implementing this?


Julien Richard-Foy

unread,
Dec 23, 2014, 3:01:37 AM12/23/14
to play-fr...@googlegroups.com
Hi,

Maybe this project can give you a hint: http://github.com/julienrf/play-json-variants

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
  Z E N G U L A R I T Y
––––––––––––––––––––––
Julien RICHARD-FOY - Programmer

56 rue Saint Lazare 75009 Paris
625 Market Street - San Francisco

Kenji Yoshida

unread,
Dec 23, 2014, 6:46:15 PM12/23/14
to play-fr...@googlegroups.com

Nikita Melkozerov

unread,
Dec 23, 2014, 11:58:31 PM12/23/14
to play-fr...@googlegroups.com
Hi Julien,

Thank you for your reply, I got some inspiration while I was looking at sources of this project.

Nikita Melkozerov

unread,
Dec 24, 2014, 12:11:10 AM12/24/14
to play-fr...@googlegroups.com
Hi Kenji, you've literally done my job.

I implemented it in another way by using case class wrapper,

https://github.com/meln1k/vk-scala-api/blob/master/src/main/scala/io/github/meln1k/vkApi/models/user/ItemsWrapper.scala

however your implementation is much more clean and concise.



On Wednesday, December 24, 2014 4:46:15 AM UTC+5, Kenji Yoshida wrote:

Thomas Grainger

unread,
Mar 3, 2017, 7:42:56 AM3/3/17
to Play Framework

Thomas Grainger

unread,
Mar 3, 2017, 7:42:56 AM3/3/17
to Play Framework
Reply all
Reply to author
Forward
0 new messages