Rendering Case Class JSON with Square Braces

31 views
Skip to first unread message

Elisabeth Anderson

unread,
Mar 9, 2015, 1:22:22 PM3/9/15
to spray...@googlegroups.com
I have a case class which has three parameters, and renders as:

{
    "name": "foo",
    "items": [
        "bar"
    ],
}

For this I have a jsonFormatter in my own JsonProtocol class. 

Although how could I render the JSON as:

[
    {
        "name": "foo",
        "items": [
            "bar"
        ]
    }
]

...?

The way I was approaching it was to create the JSON object using:

List(TheCaseClass(name, items)).toJson.asJsObject

...and having a formatter such as:

implicit val formatter = jsonFormat1(List[TheCaseClass])

...although then I'm getting an error that my JsonProtocol class couldn't be initialized.

The only other way I can think of doing it is to have something like a 'TheCaseClassItems' class which could be a collection of the 'TheCaseClass' objects and using a formatter such as:

implicit def theCaseClassItems Items[T :JsonFormat] = jsonFormat1(TheCaseClassItems.apply[T])

...although this would yield the JSON:

{
    "items": [
        {
            "name": "foo",
            "items": [
                "bar"
            ]
        }
    ]
}

...which isn't what I'm after. Could you advise the best approach? Thanks!

Mathias Doenitz

unread,
Mar 9, 2015, 4:46:00 PM3/9/15
to spray...@googlegroups.com
Elisabeth,

spray-json already pre-defines the type-classes for sequences / lists.
This means that, if you provide the logic for rendering a `T` then rendering for `List[T]` instances will automatically be available as well.

So, in your case you already have a `jsonFormat` for `TheCaseClass` and should be all set.
No need to add any other `JsonFormat`.

Just call `List(TheCaseClass(name, items)).toJson`.

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray.io User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> Visit this group at http://groups.google.com/group/spray-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/7dedc10c-220d-48c1-8162-45b02d981680%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Elisabeth Anderson

unread,
Mar 10, 2015, 5:07:12 AM3/10/15
to spray...@googlegroups.com
Thanks Mathias! All I can think is that I'd had a long day and my brain wasn't working so well...all sorted this morning! :)


On Monday, March 9, 2015 at 8:46:00 PM UTC, Mathias wrote:
Elisabeth,

spray-json already pre-defines the type-classes for sequences / lists.
This means that, if you provide the logic for rendering a `T` then rendering for `List[T]` instances will automatically be available as well.

So, in your case you already have a `jsonFormat` for `TheCaseClass` and should be all set.
No need to add any other `JsonFormat`.

Just call `List(TheCaseClass(name, items)).toJson`.

Cheers,
Mathias

---
mat...@spray.io
http://spray.io

Reply all
Reply to author
Forward
0 new messages