Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize functions: consistency #760

Open
ChristianGruen opened this issue Oct 18, 2023 · 3 comments
Open

Serialize functions: consistency #760

ChristianGruen opened this issue Oct 18, 2023 · 3 comments
Labels
Enhancement A change or improvement to an existing feature Serialization An issue related to the Serialization spec XQFO An issue related to Functions and Operators

Comments

@ChristianGruen
Copy link
Contributor

ChristianGruen commented Oct 18, 2023

We should be more ambitious about ensuring consistency when serializing data back to its original representation, and how to achieve it. This is the status quo:

Format Serialize Function Parse Function
XML fn:serialize($input) fn:parse-xml
JSON fn:serialize($input, map { 'method': 'json' }) fn:parse-json
JSON fn:xml-to-json($input) fn:json-to-xml
XHTML fn:serialize($input, map { 'method': 'xhtml' }) fn:parse-html
HTML fn:serialize($input, map { 'method': 'html' }) fn:parse-html
CSV still missing fn:parse-csv and variants

In BaseX, the XML created by fn:json-to-xml can be serialized back to JSON as follows (related: #759):

serialize(
  <map xmlns="http://www.w3.org/2005/xpath-functions">
    <number key="A">1</number>
  </map>,
  { 'method': 'json', 'json': { 'format': 'basic' }
})

For CSV, we have introduced a CSV serialization method that supports all CSV flavors we support (see CSV Functions for more details):

serialize(
  <map xmlns="http://www.w3.org/2005/xpath-functions">
    <number key="A">1</number>
  </map>,
  { 'method': 'csv', 'csv': { 'format': 'direct' }
})

Related (parse functions): #748

@ChristianGruen ChristianGruen added XQFO An issue related to Functions and Operators Enhancement A change or improvement to an existing feature Serialization An issue related to the Serialization spec labels Oct 18, 2023
@ndw
Copy link
Contributor

ndw commented Jun 4, 2024

What is the actual proposal?

@ChristianGruen
Copy link
Contributor Author

Greetings to Prague… The proposal could be twofold:

  1. introduce a csv serialization method;
  2. add a serialization parameter for the existing json method to serialize the result of fn:json-to-xml back to JSON.

@ChristianGruen
Copy link
Contributor Author

An alternative for 2. would be to extend the existing JSON serialization to also support input that was created by fn:json-to-xml.

This would introduce a minor backward-incompatibility (which I believe will not be relevant in practice):

serialize(
  <fn:map><fn:string key='x'>X</fn:string></fn:map>,
  { 'method': 'json'}
)

…would return { "x": "X" } instead of "<fn:map><fn:string key=\"x\">X</fn:string></fn:map>".

If an input node cannot be associated to JSON, if would continue to be serialized as string (serialize(<xml/>, { 'method': 'json'}) would return "<xml/>").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement A change or improvement to an existing feature Serialization An issue related to the Serialization spec XQFO An issue related to Functions and Operators
Projects
None yet
Development

No branches or pull requests

2 participants