Created
February 5, 2014 07:12
-
-
Save huntc/8818682 to your computer and use it in GitHub Desktop.
Illustrates how a single Itinerary or an array of Itinerary objects can be rendered as a List[Itinerary]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import play.api._ | |
import play.api.mvc._ | |
import play.api.libs.json._ | |
case class Itinerary(name: String) | |
object Itinerary { | |
implicit val format = Json.format[Itinerary] | |
} | |
object Application extends Controller { | |
private val itineraryReads = Json.reads[Itinerary] | |
private val itinerariesReads: Reads[(List[Itinerary])] = __.read[List[Itinerary]] | |
def receiveJson = Action(parse.json) { | |
request => | |
val itineraryRead: JsResult[List[Itinerary]] = itinerariesReads.reads(request.body) | |
.map(x => JsSuccess(x)) | |
.recover { | |
case _: JsError => itineraryReads.reads(request.body).map(x => List(x)) | |
} | |
.flatMap(x => x) | |
Logger.info(s"Received $itineraryRead, ${request.body}") | |
Ok("cool") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment