eXist-db / exist

eXist Native XML Database and Application Platform
https://exist-db.org
GNU Lesser General Public License v2.1
416 stars 179 forks source link

[BUG] JSON serialization of sequences #4984

Open line-o opened 12 months ago

line-o commented 12 months ago

Describe the bug

This query

serialize((1 to 5), map{"method":"json"})

evaluates to [1,2,3,4,5].

The XQuery specification of the JSON serialization method says that an error should be raised instead: A sequence of length greater than one in the data model instance will result in a serialization error [err:SERE0023].

Expected behavior

JSON serialization to be spec compliant.

To Reproduce

xquery version "3.1";

module namespace t="http://exist-db.org/xquery/test";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare
    %test:assertError("SERE0023")
function t:test() {
    serialize((1 to 5), map{"method":"json"})
};

Context (please always complete the following information)

Build: eXist-6.2.0 (c8fa4958b6d4a50bd0cba7f3e76a150226414187) Java: 11.0.18 (Debian) OS: Linux 5.15.49-linuxkit-pr (aarch64)

Additional context

line-o commented 12 months ago

It seems that there is discussion in the XQuery 4 working group to allow sequences to be serialised to JSON arrays.

This only requires one change: serializing a sequence of length >1 should output the sequence as if it were an array, rather than raising an error.

from https://github.com/qt4cg/qtspecs/issues/576

We could keep our implementation as-is, knowing it will be part of the upcoming spec. Which means this is the current, shared understanding of the correct approach.

joewiz commented 11 months ago

As discussed in the Community Call, perhaps this issue should be resolved as a “won’t fix” - since, as Michael Kay proposes, XQuery 4 is going to behave as eXist already does.

adamretter commented 11 months ago

As discussed in the Community Call, perhaps this issue should be resolved as a “won’t fix”

@joewiz I feel that that is a bit of a dangerous proposition. At the moment:

  1. XQuery 4 has not been finalised or finished. There is no set timeline for that to happen.
  2. eXist-db does not intentionally support any draft aspects of XQuery 4.

As eXist-db is aiming for XQuery 3.1 compliance, I think we should fix this for eXist-db 7.0.0.

line-o commented 1 month ago

This issue came up today in the working group meeting. In the related issue qt4cg/qtspecs#641 the discussion went further. Allowing sequences to be serialised to JSON has implications on the streamability of of the output as we have to look ahead, and buffer, the data before we can output the first character as it might be [ or not depending on the length of the sequence.