Changes

Jump to navigation Jump to search
220 bytes removed ,  16:07, 26 May 2012
no edit summary
=Conventions=
All functions in this module are assigned to the <code>{{Code|http://basex.org/modules/json</code> }} namespace, which is statically bound to the <code>{{Code|json</code> }} prefix.<br/>All errors are assigned to the <code>{{Code|http://basex.org/errors</code> }} namespace, which is statically bound to the <code>{{Code|bxerr</code> }} prefix.
=JSON Functions=
[[#json:parse|json:parse]] and [[#json:serialize|json:serialize]] facilitate a lossless conversion from JSON to XML and back. The transformation is based on the following rules:
# The resulting document has a <code>{{Code|<json/></code> }} root node.
# Names (keys) of objects are represented as elements:
## Empty names are represented by a single underscore (<code>{{Code|&lt;_&gt;...&lt;/_&gt;</code>}}).## Underscore characters are rewritten to two underscores (<code>{{Code|__</code>}}).
## A character that cannot be represented as NCName character is rewritten to an underscore and its four-digit Unicode.
# As the members of arrays have no names, <code>{{Code|&lt;value/&gt;</code> }} 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>{{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>{{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.
==json:parse==
|-
| width='90' | '''Signatures'''
|<code><b>{{Func|json:parse</b>(|$input as xs:string()) as |element(json)</code>}}
|-
| '''Summary'''
|Converts the JSON document specified by <code>{{Code|$input</code> }} to XML, and returns the result as <code>{{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.
|-
| '''Errors'''
|<b>[[{{Error|BXJS0001|#Errors|BXJS0001]]</b> is raised if }} the specified input cannot be parsed as JSON document.
|}
|-
| width='90' | '''Signatures'''
|<code><b>{{Func|json:serialize</b>(|$input as node()) as |xs:string()</code>}}
|-
| '''Summary'''
|Serializes the node specified by <code>{{Code|$input</code> }} as JSON, and returns the result as <code>{{Code|xs:string</code> }} instance. The serialized node must conform to the syntax specified by the [[#json:parse|json:parse()]] function.<br />XML documents can also be serialized as JSON if the [[Serialization|Serialization Option]] <code>{{Code|"method"</code> }} is set to <code>{{Code|"json"</code>}}.
|-
| '''Errors'''
|<b>[[{{Error|BXJS0002|#Errors|BXJS0002]]</b> is raised if }} the specified node cannot be serialized as JSON document.
|}
<street>21 2nd Street</street>
<city>New York</city>
<code>{{Code|10021</code>}}
</address>
<phone>
|-
| width='90' | '''Signatures'''
|<code><b>{{Func|json:serialize-ml</b>(|$input as node()) as |xs:string()</code>}}
|-
| '''Summary'''
|Serializes the node specified by <code>{{Code|$input</code> }} and returns the result as <code>{{Code|xs:string</code> }} instance.<br />XML documents can also be output in the JsonML format by setting the [[Serialization|Serialization Option]] <code>{{Code|"method"</code> }} to <code>{{Code|"jsonml"</code>}}.
|-
| '''Errors'''
|<b>[[{{Error|BXJS0002|#Errors|BXJS0002]]</b> is raised if }} the specified value cannot be serialized.
|}
|-
| width='90' | '''Signatures'''
|<code><b>{{Func|json:parse-ml</b>(|$input as xs:string()) as |element()</code>}}
|-
| '''Summary'''
|Converts the [http://jsonml.org JsonML] document specified by <code>{{Code|$input</code> }} to XML, and returns the result as <code>{{Code|element()</code> }} instance. The JSON input must conform to the JsonML specification to be successfully converted.
|-
| '''Errors'''
|<b>[[{{Error|BXJS0001|#Errors|BXJS0001]]</b> is raised if }} the specified input cannot be parsed as JsonML instance.
|}
<street>21 2nd Street</street>
<city>New York</city>
<code>{{Code|10021</code>}}
</address>
<phone type='home'>212 555-1234</phone>
! width="95%"|Description
|-
|<code>{{Code|BXJS0001</code>}}
|The specified input cannot be parsed as JSON document.
|-
|<code>{{Code|BXJS0002</code>}}
|The specified node cannot be serialized as JSON document.
|}
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu