Changes

Jump to navigation Jump to search
10 bytes added ,  01:38, 15 August 2011
no edit summary
As there are notable differences between JSON and XML, no mapping exists that guarantees a lossless, bidirectional conversion between JSON and XML. For this reason, we offer two sets of functions in this module:
* [[JSON Functions#json:parse|json:parse]] and [[JSON Functions#json:serialize|json:serialize]] facilitate a lossless representation of JSON to XML and back, and* [[JSON Functions#json:serialize-ml|json:serialize-ml]] and [[JSON Functions#json:parse-ml|json:parse-ml]] are used to transform XML to JSON and back. ==json:parse=={||-| valign='top' width='90' | '''Signatures'''|<code><b>json:parse</b>($input as xs:string()) as element(json)</code>|-| valign='top' | '''Summary'''|Converts the JSON document specified by <code>$input</code> to XML, and returns the result as <code>element(json)</code> instance. The converted XML document is both well readable and lossless, i.e., the converted document can be serialized back to the original JSON representation. The transformation is based on the following rules:# The resulting document has a <code><json/></code> root node. # Names (keys) of objects are represented as elements:## Empty names are represented by a single underscore (<code>&lt;_&gt;...&lt;/_&gt;</code>).## Underscore characters are rewritten to two underscores (<code>__</code>).## A character that cannot be represented as NCName character is rewritten to an underscore and its four-digit Unicode.# As arrays have no names, {@code <value/>} is used as element name.# JSON values are represented as text nodes.# The types of values are represented in attributes:## The value types ''number'', ''boolean'', ''null'', ''object'' and ''array'' are represented by a <code>type</code> attribute.## The ''string'' type is omitted, as it is treated as default type.## If a name has the same type throughout the document, the <code>type</code> attribute will be omitted. Instead, the name will be listed in additional, type-specific attributes in the root node. The attributes are named by their type in the plural (''numbers'', ''booleans'', ''nulls'', ''objects'' and ''arrays''), and the attribute value contains all names with that type, separated by whitespaces. |-| valign='top' | '''Errors'''|<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0015]]</b> is raised if the specified input cannot be parsed as JSON document.|} ==json:serialize=={||-| valign='top' width='90' | '''Signatures'''|<code><b>json:serialize</b>($input as node()) as xs:string()</code>|-| valign='top' | '''Summary'''|Serializes the node specified by <code>$input</code> as JSON, and returns the result as <code>xs:string</code> instance. The serialized node must conform to the syntax specified by the [[JSON Functions#json:parse|json:parse()]] function.<br />XML documents in the JSON format can also be serialized if the [[Serialization|Serialization Option]] <code>"method"</code> is set to <code>"json"</code>.|-| valign='top' | '''Errors'''|<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0016]]</b> is raised if the specified node cannot be serialized as JSON document.|}
==json:serialize-ml=={||-| valign='top' width='90' | '''Signatures'''|<code><b>json:serialize</b>($input as node()) as xs:string()</code>|-| valign='top' | '''Summary'''|Serializes the node specified by <code>$input</code> as described in the [http://jsonml.org JsonML] specification, and returns the result as <code>xs:string</code> instance. JsonML can be used to transform any XML document to JSON and back.Note, however, that namespaces, comments and processing instructions will be discarded in the transformation process. More details are found in the official [http://jsonml.org/XML JsonML documentation].|-| valign='top' | '''Errors'''|<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0016]]</b> is raised if the specified node cannot be serialized as JSON document.|} ==json:parse-ml=={||-| valign='top' width='90' | '''Signatures'''|<code><b>json:parse</b>($input as xs:string()) as element()</code>|-| valign='top' | '''Summary'''|Converts the [http://jsonml.org JsonML] document specified by <code>$input</code> to XML, and returns the result as <code>element()</code> instance. The JSON input must conform to the JsonML specification to be successfully converted.|-| valign='top' | '''Errors'''|<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0015]]</b> is raised if the specified input cannot be parsed as JsonML instance.|} =Examples=Examples==
'''Example 1: A basic example to convert an empty JSON document'''
</json>
</pre>
 
* [[JSON Functions#json:parse|json:parse]] and [[JSON Functions#json:serialize|json:serialize]] facilitate a lossless representation of JSON to XML and back, and
* [[JSON Functions#json:serialize-ml|json:serialize-ml]] and [[JSON Functions#json:parse-ml|json:parse-ml]] are used to transform XML to JSON and back.
 
==json:parse==
{|
|-
| valign='top' width='90' | '''Signatures'''
|<code><b>json:parse</b>($input as xs:string()) as element(json)</code>
|-
| valign='top' | '''Summary'''
|Converts the JSON document specified by <code>$input</code> to XML, and returns the result as <code>element(json)</code> instance. The converted XML document is both well readable and lossless, i.e., the converted document can be serialized back to the original JSON representation. The transformation is based on the following rules:
# The resulting document has a <code><json/></code> root node.
# Names (keys) of objects are represented as elements:
## Empty names are represented by a single underscore (<code>&lt;_&gt;...&lt;/_&gt;</code>).
## Underscore characters are rewritten to two underscores (<code>__</code>).
## A character that cannot be represented as NCName character is rewritten to an underscore and its four-digit Unicode.
# As arrays have no names, {@code <value/>} is used as element name.
# JSON values are represented as text nodes.
# The types of values are represented in attributes:
## The value types ''number'', ''boolean'', ''null'', ''object'' and ''array'' are represented by a <code>type</code> attribute.
## The ''string'' type is omitted, as it is treated as default type.
## If a name has the same type throughout the document, the <code>type</code> attribute will be omitted. Instead, the name will be listed in additional, type-specific attributes in the root node. The attributes are named by their type in the plural (''numbers'', ''booleans'', ''nulls'', ''objects'' and ''arrays''), and the attribute value contains all names with that type, separated by whitespaces.
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0015]]</b> is raised if the specified input cannot be parsed as JSON document.
|}
 
==json:serialize==
{|
|-
| valign='top' width='90' | '''Signatures'''
|<code><b>json:serialize</b>($input as node()) as xs:string()</code>
|-
| valign='top' | '''Summary'''
|Serializes the node specified by <code>$input</code> as JSON, and returns the result as <code>xs:string</code> instance. The serialized node must conform to the syntax specified by the [[JSON Functions#json:parse|json:parse()]] function.<br />XML documents in the JSON format can also be serialized if the [[Serialization|Serialization Option]] <code>"method"</code> is set to <code>"json"</code>.
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0016]]</b> is raised if the specified node cannot be serialized as JSON document.
|}
 
==json:serialize-ml==
{|
|-
| valign='top' width='90' | '''Signatures'''
|<code><b>json:serialize</b>($input as node()) as xs:string()</code>
|-
| valign='top' | '''Summary'''
|Serializes the node specified by <code>$input</code> as described in the [http://jsonml.org JsonML] specification, and returns the result as <code>xs:string</code> instance. JsonML can be used to transform any XML document to JSON and back.Note, however, that namespaces, comments and processing instructions will be discarded in the transformation process. More details are found in the official [http://jsonml.org/XML JsonML documentation].
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0016]]</b> is raised if the specified node cannot be serialized as JSON document.
|}
 
==json:parse-ml==
{|
|-
| valign='top' width='90' | '''Signatures'''
|<code><b>json:parse</b>($input as xs:string()) as element()</code>
|-
| valign='top' | '''Summary'''
|Converts the [http://jsonml.org JsonML] document specified by <code>$input</code> to XML, and returns the result as <code>element()</code> instance. The JSON input must conform to the JsonML specification to be successfully converted.
|-
| valign='top' | '''Errors'''
|<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0015]]</b> is raised if the specified input cannot be parsed as JsonML instance.
|}
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu