1

I'm using lift-json to deserialize simple objects from a POST request. Example:

{"id": "35", "name": "My topic", "slug": "my-slug", "imageUrl": "http://foo.bar/image.png"}

class definition:

class Topic(var id: Option[Long], var name: String, val slug: String, val imageUrl: String) 

Then I use

read[Topic](jsonString)

Is it possible to get json-lift to read the id as a Long automatically?

Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
gregsilin
  • 287
  • 1
  • 7

1 Answers1

2

You can do it by converting the JSON.

val json = parse("""{"id": "35", "name": "My topic", ...}""")
json transform { case JField("id", JString(s)) => JField("id", JInt(s.toInt)) }

And then extract a case class from that transformed JSON.

Joni
  • 2,779
  • 23
  • 17