4
: Copyright 2006-2012 The FLWOR Foundation.
6
: Licensed under the Apache License, Version 2.0 (the "License");
7
: you may not use this file except in compliance with the License.
8
: You may obtain a copy of the License at
10
: http://www.apache.org/licenses/LICENSE-2.0
12
: Unless required by applicable law or agreed to in writing, software
13
: distributed under the License is distributed on an "AS IS" BASIS,
14
: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
: See the License for the specific language governing permissions and
16
: limitations under the License.
21
: This module provides the functions defined by the JSONiq specification,
22
: sections 1.7 (Functions) and 1.10 (Update Primitives). JSONiq extends
23
: the XQuery specification to also deal with JSON data natively. See
25
: http://www.jsoniq.org/
29
: This module depends on having the JSONiq feature enabled in Zorba,
30
: i.e., Zorba must be compiled with ZORBA_WITH_JSON.
32
: @author Markos Zaharioudakis, Matthias Brantner
34
module namespace jn = "http://www.jsoniq.org/functions";
36
import module namespace schema = "http://www.zorba-xquery.com/modules/schema";
38
declare namespace jdm = "http://www.jsoniq.org/";
40
declare namespace err = "http://www.w3.org/2005/xqt-errors";
42
declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
43
declare option ver:module-version "1.0";
47
: This function has the same semantics as fn:parse-xml(), except that
48
: it parses the string as JSON (not XML), and returns an Object or
49
: Array rather than an XML document.
51
: @param $j A string containing a valid JSON text.
52
: @return a JSON Object or Array item.
54
declare function jn:parse-json($j as xs:string) as json-item()? external;
58
: Returns the names used in the Pairs of the object.
59
: The names will be returned in an implementation-defined order
61
: @param $o A JSON Object.
62
: @return The names of pairs in the object.
64
declare function jn:keys($o as object()) as xs:string* external;
68
: Returns the values of the pairs within a given JSON object.
70
: @param $o A JSON Object.
71
: @return the value of each pair within the given object.
73
declare function jn:values($o as object()) as item()* external;
77
: Returns the value of a JSON Pair with a given name within a given JSON object.
78
: If no such pair exists in the object, returns the empty sequence.
80
: @param $o A JSON Object.
81
: @param $name The name of the pair whose value is to be retrieved
82
: @return the value of specified pair within the given object, or the empty sequence.
84
declare function jn:value($o as object(), $name as xs:string) as item()? external;
88
: Creates an object from the specified pairs of another given object.
89
: Specifically, for each name in $names, if the object $o has a pair with
90
: that name, then a copy of that pair is included in the new object.
92
: @param $o A JSON Object.
93
: @param $names The names of the pairs to copy out of $o and insert into the new object
94
: @return The new object.
96
declare function jn:project($o as object(), $names as xs:string*) as object() external;
99
: Returns the size of a JSON Object or JSON Array. The size of an Object
100
: is the number of Pairs contained within it; the size of an Array is
101
: the number of members contained within it.
103
: @param $j A JSON Object or JSON Array.
104
: @return The number of items in $j.
105
: @error jn:JUDY0060 if $j is a JSON Pair.
107
declare function jn:size($j as json-item()) as xs:integer external;
111
: Returns the member of an Array at the specified position (starting from 1).
112
: If the position is out of bounds of the array, returns the empty sequence.
114
: @param $a A JSON Array.
115
: @param $p The position in the array.
116
: @return The member at the specified position, or empty sequence.
118
declare function jn:member($o as array(), $p as xs:integer) as item()? external;
122
: Returns the members of an Array.
124
: @param $a A JSON Array.
125
: @return The members of the specified array.
127
declare function jn:members($o as array()) as item()* external;
131
: Recursively "flatten" a JSON Array, by replacing any arrays with their
132
: members. Equivalent to
134
: define function jn:flatten($arg as array()) {
135
: for $value in jn:values($arg)
137
: if ($value instance of array())
138
: then jn:flatten($value)
142
: @param $a A JSON Array.
143
: @return The flattened version of $a.
145
declare function jn:flatten($a as array()) as item()* external;