Questions tagged [play-json]

The play.api.libs.json package contains data structures for representing JSON data and utilities for converting between these data structures and other data representations.

The play.api.libs.json package contains data structures for representing JSON data and utilities for converting between these data structures and other data representations. It includes the following types:

  • JsString
  • JsNumber
  • JsBoolean
  • JsObject
  • JsArray
  • JsNull
269 questions
46
votes
4 answers

Noise free JSON format for sealed traits with Play 2.2 library

I need to get a simple JSON serialization solution with minimum ceremony. So I was quite happy finding this forthcoming Play 2.2 library. This works perfectly with plain case classes, e.g. import play.api.libs.json._ sealed trait Foo case class…
0__
  • 64,257
  • 16
  • 158
  • 253
10
votes
1 answer

Why does play-json lose precision while reading/parsing?

In the following example (scala 2.11 and play-json 2.13) val j ="""{"t":2.2599999999999997868371792719699442386627197265625}""" println((Json.parse(j) \…
sashas
  • 2,320
  • 3
  • 12
  • 31
9
votes
2 answers

No Json formatter for Option[String]?

I am trying to marshall and un-marshall an Option[String] field to and from JSON. For my use-case, a None value should be marshaled as "null". Here is the code I have: import org.scalatest.{FlatSpec, Matchers} import play.api.libs.json._ import…
Babu Subburathinam
  • 2,374
  • 2
  • 21
  • 20
9
votes
2 answers

Play Framework JSON Format for Case Objects

I have a set of case objects that inherits from a trait as below: sealed trait UserRole case object SuperAdmin extends UserRole case object Admin extends UserRole case object User extends UserRole I want to serialize this as JSON and I just…
joesan
  • 10,737
  • 20
  • 70
  • 176
7
votes
1 answer

Scala + Play Framework + Slick - Json as Model Field

I need to save a Json Field as a column of my Play Framework Model. My table parser in DAO is class Table(tag: Tag) extends Table[Model](tag, "tablename") { implicit val configFormat = Json.format[Config] // Fields ... def…
emmea90
  • 347
  • 4
  • 10
6
votes
3 answers

Json implicit format with recursive class definition

I have a recursive class defined : case class SettingsRepository(id: Option[BSONObjectID], name: Option[String], children: Option[List[SettingsRepository]]) with a JSON implicit format as below…
Xan
  • 65
  • 6
6
votes
3 answers

Play: How to remove the fields without value from JSON and create a new JSON with them

Given the following JSON: { "field1": "value1", "field2": "", "field3": "value3", "field4": "" } How do I get two distinct JSONs, one containing the fields with value and another one containing the fields without value? Here below is how…
j3d
  • 8,708
  • 17
  • 76
  • 150
5
votes
1 answer

Play Json: custom reads one field

Let's say I have to write custom Reads[Person] for Person class: import play.api.libs.functional.syntax._ implicit val personReads: Reads[Person] = ( (__ \ "name").read[String] and // or ~ (__ \ "age").readNullable[Int] ) ((name, age) =>…
Andrii Abramov
  • 7,967
  • 8
  • 55
  • 79
5
votes
1 answer

How can I write and read an empty case class with play-json?

I have an empty case class corresponding to an HTTP GET request: case class GetFoo() extends MyQueryRequest { // ... } and each message has a companion object with describes its implicit JSON writer and reader: object GetFoo extends…
erip
  • 13,935
  • 9
  • 51
  • 102
5
votes
1 answer

Scala PlayJson Cyclic Reference

Context I have a case class which is an item in a hierarchy, which refers to itself like so: case class Node( name: String, children: Option[Seq[Node]] = None ) I would like a PlayJson Format for this. Usually, you can just do: implicit…
Rhys Bradbury
  • 1,566
  • 11
  • 23
5
votes
0 answers

How to fail on json with unused fields in play-json scala?

Using the scala play-json library, how can I reject a JsonObject that contains unused fields? I want to give an error when my api users misspell a field or give an extra field that I would ignore. In the java version, I see mention of…
yonran
  • 15,964
  • 7
  • 60
  • 81
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
  • 4,844
  • 4
  • 32
  • 69
4
votes
2 answers

Processing JSON error responses with Play WSClient

I'm using Play's WSClient to interact with a third-party service request = ws.url(baseUrl) .post(data) .map{ response => response.json.validate[MyResponseClass] The response may be a MyResponseClass or it may be an ErrorResponse like {…
tgk
  • 2,972
  • 1
  • 17
  • 35
4
votes
2 answers

Scala Play Json Format for Map[Locale, String]

I have objects of type Map[java.util.Locale, String] How can I make Json Writes / Reads for this? I have looked at a couple other questions, but couldn't come up with a solution myself. I got (but not tested yet) something for Locale implicit val…
ticofab
  • 7,169
  • 11
  • 44
  • 81
3
votes
2 answers

Conditional filtering of JSON before deserialisation to case class model

How to parse a JSON conditionally before deserialisation to the following case class: case class UserInfo(id: String, startDate: String, endDate: String) I have an implicit reads object UserInfo { implicit val reads: Reads[UserInfo] = ( …
vkt
  • 1,111
  • 1
  • 12
  • 32
1
2 3
17 18