Skip to content

Instantly share code, notes, and snippets.

@fdescamps
Created June 11, 2013 15:59
Show Gist options
  • Select an option

  • Save fdescamps/5758120 to your computer and use it in GitHub Desktop.

Select an option

Save fdescamps/5758120 to your computer and use it in GitHub Desktop.
Able to read complex json, when you json block contains alternatively an object or an array without predictibility.
//Reads only sector with sequence of industries
implicit val secteurReads: Reads[Secteur] = (
( __ \ "name" ).read[String] ~
( __ \ "industry" ).lazyRead( play.api.libs.json.Reads.seq[Industrie] )
)( Secteur )
//Reads sector with sequence of industries or an only object industry
implicit val secteurReads: Reads[Secteur] = (
( __ \ "name" ).read[String] ~
( __ \ "industry" ).read( Reads.seq[Industrie] or Reads.of[Industrie].map( industrie => Seq(industrie) ) )
)( Secteur )
// able to Read sector with sequence of industries or sector with an only object industry
implicit val secteurReads: Reads[Secteur] = (
( __ \ "name" ).read[String] ~
( __ \ "industry" ).read( Reads.seq[Industrie] or Reads.of[Industrie].map( industrie => Seq(industrie) ) )
)( Secteur )
@fdescamps
Copy link
Author

Able to read :

"sector":[ {
"name":"Basic Materials",
"industry":[
{
"id":"112",
"name":"Agricultural Chemicals"
}, ...
]
...

Or

"industry": {
"id":"112",
"name":"Agricultural Chemicals"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment