Questions tagged [playframework-json]

The Play Framework for Scala contains its own JSON manipulation library, which provides utilities for parsing and manipulating JSON, as well as serialization from and deserialization to Scala objects.

The Scala Play Framework has a built-in JSON handling library under the package play.api.libs.json. It consists of:

  • data types for representing a JSON document as a a tree structure (JsValue and subtypes)
  • a simple DSL for describing paths within a JSON document (JsPath)
  • a validation monad (JsResult and subtypes)
  • a DSL for describing serialization and deserialization
  • combinators for describing JSON manipulation
  • macros that provide syntactic sugar for the common case of serialization of case classes.

A related project call JsZipper augments this library with additional functionality for manipulating JSON in functional ways.

48 questions
16
votes
2 answers

serializing objects to json with play.api.libs.json

I'm trying to serialize some relatively simple models into json. For example, I'd like to get the json representation of: case class User(val id: Long, val firstName: String, val lastName: String, val email: Option[String]) { def this() =…
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
12
votes
1 answer

Custom JSON validation constraints in Play Framework 2.3 (Scala)

I managed to implement form validation with custom constraints, but now I want to do the same thing with JSON data. How can I apply custom validation rules to a JSON parser? Example: The client's POST request contains a user name (username) and not…
Nick
  • 2,576
  • 1
  • 23
  • 44
11
votes
2 answers

Constant value in Scala Play JSON Reads

I'd like to use a constant value when constructing an object via a JSON read. For example the class would be: case class UserInfo( userId: Long = -1, firstName: Option[String] = None, lastName: Option[String] = None ) And the read would…
ccudmore
  • 193
  • 1
  • 7
8
votes
1 answer

get all keys of play.api.libs.json.JsValue

I have to store play.api.libs.json.JsValue keys to a List. How i do this? var str = ??? //json String val json: JsValue = Json.parse(str) val data=json.\("data") println(data) …
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
8
votes
1 answer

JsResult - Monad or Applicative?

My understanding of one of the distinctions between Monad and Applicative is that flatMap is available on Monad, but not Applicative. If that's true, I'm confused by these Scala Play JSON docs: So what’s interesting there is that JsResult[A] is a…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
8
votes
2 answers

Scala Play Json Reads

I have a sample code as below. import play.api.libs.json._ import play.api.libs.functional.syntax._ import play.api.data.validation.ValidationError import play.api.libs.json.Reads._ case class…
user809564
  • 259
  • 1
  • 3
  • 10
7
votes
2 answers

Scala playframework implicit reader writer for Timestamp

I'm using play.api.libs.json._ library. I have this kind of Scala class. I need to read / write this class in Json format. As there is no implicit reader/ writer for Timestamp. I have to provide my own. I tried couple ways unfortunately none of…
masiboo
  • 4,537
  • 9
  • 75
  • 136
7
votes
2 answers

Play Json Reads and String

I have the following JSON reader in Play 2.3: import play.api.libs.json._ import play.api.libs.json.Reads._ val airportSearchReads: Reads[String] = (JsPath \ "search").read[String](minLength(3)) and the compiler gives me the error diverging…
elmalto
  • 1,008
  • 1
  • 14
  • 23
7
votes
1 answer

Parsing JSON Date Time in Scala/Play

I have the following Read defined: import org.joda.time.DateTime; implicit val userInfoRead: Reads[UserInfo] = ( (JsPath \ "userName").readNullable[String] and ] (JsPath \ "startDate").readNullable[DateTime] (UserInfo.apply _) With the…
ccudmore
  • 193
  • 1
  • 7
7
votes
1 answer

Play2 does not find my implicit Reads or Format for JSON

This is my Search Object: package models.helper import play.api.libs.json.Format import play.api.libs.json.JsValue import play.api.libs.json.JsObject import play.api.libs.json.JsString case class Search (name: String, `type`:String){ implicit…
Somatik
  • 4,723
  • 3
  • 37
  • 49
6
votes
1 answer

Parsing a JSArray object into a list of objects in Scala Play

I have the following JSON: [{"id_str":"67979542","name":"account"}, {"id_str":"12345678","name":"account2"}, {"id_str":"3423423423","name":"account3"}] which has been parsed into a play.api.libs.json.JsArray object with 3 elements. I want to parse…
jcm
  • 5,499
  • 11
  • 49
  • 78
5
votes
1 answer

How to write a Json Reads Combinator for a Map[Int, Long]

I am trying to write a json reads combinator for type Map[Int, Long] I have this so far: implicit val mapWrites = Json.writes[Map[Int, Long]] implicit val mapReads: Reads[Map[Int, Long]] = ( // ??? ) // ? I'm not sure how this will work, I tried…
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
5
votes
2 answers

json writer for nested classes

I'm using Play! Scala 2.2 and I have a problem to render a class in Json : I have two classes with one depending of the other, as following : case class Artist(id: String, cover: String, website: List[String], link: String, Tracks: List[Track] =…
Simon
  • 6,025
  • 7
  • 46
  • 98
4
votes
2 answers

how to add custom ValidationError in Json Reads in PlayFramework

I am using play Reads validation helpers i want to show some custom message in case of json exception eg:length is minimum then specified or the given email is not valid , i knnow play displays the error message like this error.minLength but i want…
swaheed
  • 3,671
  • 10
  • 42
  • 103
4
votes
1 answer

Play JSON: turn a Seq[Reads[JsObject]] into one Reads[JsObject]

I dynamically generate a bunch of Reads[JsObject] which I then have in a Seq[Reads[JsObject]]. In order to actually apply all these single Reads[JsObject], I've got to merge them with and into one single Reads[JsObject]. Is this possible? I've got…
Nick
  • 2,576
  • 1
  • 23
  • 44
1
2 3 4