1

I've the following model:

case class Person(name: String, age: Int, job: Option[String])

object PersonJsonFormats {
    implicit val personFormat = Json.format[Person]
}

Converting a Person object into Json (e.g. with Json.toJson(person)) produces the following Json object.

{
    "name": "John",
    "age": 10,
    "job": "gardener"
}

What should I change in order to produce an object like the following instead?

[
    {
        "name": "name",
        "value" : "John"
    },
    {
        "name": "age",
        "value": 10
    },
    {
        "name": "job",
        "value": "gardener"
    }
]

I know I could write custom Reads and Writes but I want to write something I can apply to every case class

1 Answer 1

2

If you don't want to write custom Reads and Writes and want something that you can apply to every case class, you probably should use macros to to this.

That's how the original automatic implicit converters are build.

Places to look:

Sign up to request clarification or add additional context in comments.

1 Comment

I had a quick look at those docs. I think this is a little bit beyond my Scala knowledge :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.