On 10.11.2021 19:34, Jonathan Robie wrote:
> I want to serialize JSON. Consider the following function:
>
> declare function local:index()
> {
> array{ $levinsohn/levinsohn/feature/header ! map {./name :
> ./description ! string() } }
> };
>
> When I run that, I get output that looks like this:
>
> [map {
> "Ambiguous": "Marked but ambiguous constituent order."
> }, map {
> "annotations": "Inline annotations."
> }
> !!! SNIP !!!]
>
> That won't work as output in a REST API, so I want to serialize that
> without the map keyword. I tried to use fn:serialize():
>
> declare function local:index()
> {
> array{ $levinsohn/levinsohn/feature/header ! map {./name :
> ./description ! string() } } ! serialize(.)
> };
>
> That gave me this error:
>
> items of type map(*) cannot be serialized
>
>
> So ... what's the right way to do this?
I would use
serialize(local:index(), map { 'method' : 'json' })
to serialize the whole array as JSON