<commit_before>xquery version "3.0";

 (:
  : Copyright 2006-2009 The FLWOR Foundation.
  :
  : Licensed under the Apache License, Version 2.0 (the "License");
  : you may not use this file except in compliance with the License.
  : You may obtain a copy of the License at
  :
  : http://www.apache.org/licenses/LICENSE-2.0
  :
  : Unless required by applicable law or agreed to in writing, software
  : distributed under the License is distributed on an "AS IS" BASIS,
  : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  : See the License for the specific language governing permissions and
  : limitations under the License.
  :)

 (:~
  : <p>In order to enable JSON processing with XQuery, Zorba implements a set
  : of functions that open XQuery developers the door to process JSON
  : data. Specifically, this module provides two types of functions. Functions
  : to:
  : <ul>
  :   <li>parse JSON and convert it to XDM and</li>
  :   <li>serialize XDM in order to output JSON.</li>
  : </ul>
  : </p>
  :
  : <p>Both types of functions are available to parse and serialize two
  : types of XDM-JSON mappings:<ul><li>the first mapping called in this document
  : <strong>simple XDM-JSON</strong> has been
  : <a href="http://john.snelson.org.uk/parsing-json-into-xquery">
  : proposed by <NAME></a></li><li>the second mapping is called
  : <a href="http://jsonml.org/">JsonML</a></li></ul>In the following, we
  : briefly describe both mappings.</p>
  :
  : <h2>Simple XDM-JSON Mapping</h2>
  : <ul><li>In order to process JSON with XQuery, Zorba implements a mapping between
  : JSON and XML that was initially proposed by <NAME> in his article
  : <a href="http://john.snelson.org.uk/parsing-json-into-xquery"
  : target="_blank">Parsing JSON into XQuery</a></li></ul>
  :
  : <h2>JsonML Mapping</h2>
  : <ul>
  : <li><a href="http://jsonml.org/" target="_blank">JSonML</a> (JSON Markup Language)
  : is an application of the JSON format.</li>
  : <li>The purpose of JSonML is to provide a compact format for transporting
  : XML-based markup as JSon. In contrast to the <strong>simple XDM-JSON</strong> mapping described above
  : <strong>JsonML</strong> allows a lossless conversion back and forth.</li></ul>
  :
  : @see <a href="http://john.snelson.org.uk/parsing-json-into-xquery">Mapping proposed by <NAME></a>
  : @see <a href="http://jsonml.org" target="_blank">JSonML</a>
  : @project data processing/data converters
  :
  :)

module namespace json = "http://www.zorba-xquery.com/modules/converters/json";

import module namespace schema = "http://www.zorba-xquery.com/modules/schema";

import schema namespace json-options = "http://www.zorba-xquery.com/modules/converters/json-options";

declare namespace err = "http://www.w3.org/2005/xqt-errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Parses a JSON string and returns an XDM instance according to either one of
 : the mappings described above.
 :
 : @param $json a sequence of valid JSON strings
 : @param $options a set of name and value pairs that provide options
 :        to configure the JSON mapping process that have to be validated against the
 :        "http://www.zorba-xquery.com/modules/converters/json-options" schema.
 : @return  a sequence of nodes according to either one of the mappings described above.
 : @error err:XQDY0027 if $options can not be validated against the json-options schema
 : @error json:ParseError if the JSON string passed as parameter is not
 :    valid JSON.
 :)
declare function json:parse(
  $json as xs:string?,
  $options as element(json-options:options)
) as element(*, xs:untyped)*
{
  let $validated-options := if ( schema:is-validated( $options ) ) then
                                $options
                              else
                                validate { $options }
  return json:parse-internal( $json, $validated-options )
};

(:~
 : This function parses a JSON string and returns an XDM instance according
 : to simple XDM-JSON mapping described above.
 :
 : @param $json a sequence of valid JSON strings.
 : @return a sequence of nodes according to Simple XDM-JSON mapping described above.
 : @error json:ParseError if the JSON string passed as parameter is not
 :    valid JSON.
 : @example test_json/Queries/converters/jansson/parse_json_11.xq
 :)
declare function json:parse(
  $json as xs:string?
) as element(*, xs:untyped)*
{
  json:parse-internal(
    $json,
    validate {
      <options xmlns="http://www.zorba-xquery.com/modules/converters/json-options" >
        <json-format value="Snelson"/>
      </options>
    }
  )
};

(:~
 : The serialize function takes a sequence of nodes as parameter and
 : transforms each element into a valid JSON string according to one of the
 : mappings described above.
 :
 : @param $xml a sequence of nodes.
 : @param $options a set of name and value pairs that provide options
 :        to configure the JSON mapping process that have to be validated against the
 :        "http://www.zorba-xquery.com/modules/converters/json-options" schema.
 : @return a JSON string.
 : @error err:XQDY0027 if $options can not be validated against the json-options schema
 : @error json:InvalidXDM if the input $xml is not a valid XDM
 :  representation of JSON or JSON ML.
 :)
declare function json:serialize(
  $xml as item()*,
  $options as element(json-options:options)
) as xs:string
{
  let $validated-options := if ( schema:is-validated( $options ) ) then
                              $options
                            else
                              validate { $options }
  return json:serialize-internal( $xml, $validated-options )
};

(:~
 : The serialize function takes a sequence of nodes as parameter and
 : transforms each element into a valid JSON string according to the
 : Simple XDM-JSON mapping described above
 :
 : @param $xml a sequence of nodes.
 : @return a JSON string.
 : @error json:InvalidXDM if the input $xml is not a valid XDM
 :  representation of JSON or JSON ML.
 : @example test_json/Queries/converters/jansson/serialize_json_18.xq
 :)
declare function json:serialize(
  $xml as item()*<commit_msg>Cleaned-up comments.<commit_after>(:
 : Copyright 2006-2009 The FLWOR Foundation.
 :
 : Licensed under the Apache License, Version 2.0 (the "License");
 : you may not use this file except in compliance with the License.
 : You may obtain a copy of the License at
 :
 : http://www.apache.org/licenses/LICENSE-2.0
 :
 : Unless required by applicable law or agreed to in writing, software
 : distributed under the License is distributed on an "AS IS" BASIS,
 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 : See the License for the specific language governing permissions and
 : limitations under the License.
 :)

xquery version "3.0";

 (:~
  : Using this module, you can parse JSON data into XML, manipulate it like any
  : other XML data using XQuery, and serialize the result back as JSON.
  :
  : There are many ways to represent JSON data in XML, some loss-less ("round
  : tripable") and some lossy ("one way").  Loss-less representations preserve
  : the JSON data types <i>boolean</i>, <i>number</i>, and <i>null</i>; lossy
  : representations convert all data to strings.
  :
  : For a loss-less representation, Zorba implements that proposed by
  : <a href="http://john.snelson.org.uk/parsing-json-into-xquery><NAME></a>;
  : for a lossy representation, Zorba implements
  : <a href="http://jsonml.org/">JsonML</a> (the array form).
  :)

module namespace json = "http://www.zorba-xquery.com/modules/converters/json";

import module namespace schema = "http://www.zorba-xquery.com/modules/schema";

import schema namespace json-options =
  "http://www.zorba-xquery.com/modules/converters/json-options";

declare namespace err = "http://www.w3.org/2005/xqt-errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Parses JSON data from a string and returns an XDM instance using one of the
 : representations described above.
 :
 : @param $json The JSON data to parse.
 : @param $options The parsing options.
 : @return said XDM instance.
 : @error err:XQDY0027 if \a $options can not be validated against the \c
 : json-options schema.
 : @error ZJPE0001 if \a $json contains an illegal JSON character.
 : @error ZJPE0002 if \a $json contains an illegal Unicode code-point.
 : @error ZJPE0003 if \a $json contains an illegal JSON character escape.
 : @error ZJPE0004 if \a $json contains an illegal JSON literal.
 : @error ZJPE0005 if \a $json contains an illegal JSON number.
 : @error ZJPE0007 if \a $json contains an unterminated string.
 : @error ZJPE0008 if \a $json contains an illegal QName.
 :)
declare function json:parse(
  $json as xs:string?,
  $options as element(json-options:options)
) as element(*,xs:untyped)*
{
  let $validated-options := if ( schema:is-validated( $options ) ) then
                              $options
                            else
                              validate { $options }
  return json:parse-internal( $json, $validated-options )
};

(:~
 : Parses JSON data from a string and returns an XDM instance using the Snelson
 : representation described above.
 :
 : @param $json The JSON data to parse.
 : @return said XDM instance.
 : @error ZJPE0001 if \a $json contains an illegal JSON character.
 : @error ZJPE0002 if \a $json contains an illegal Unicode code-point.
 : @error ZJPE0003 if \a $json contains an illegal JSON character escape.
 : @error ZJPE0004 if \a $json contains an illegal JSON literal.
 : @error ZJPE0005 if \a $json contains an illegal JSON number.
 : @error ZJPE0007 if \a $json contains an unterminated string.
 : @error ZJPE0008 if \a $json contains an illegal QName.
 :)
declare function json:parse(
  $json as xs:string?
) as element(*,xs:untyped)*
{
  json:parse-internal(
    $json,
    validate {
      <options xmlns="http://www.zorba-xquery.com/modules/converters/json-options" >
        <json-format value="Snelson"/>
      </options>
    }
  )
};

(:~
 : Serializes an XDM into JSON using one of the representations described
 : above.
 :
 : @param $xml The XDM to serialize.
 : @param $options The serializing options.
 : @return a JSON string.
 : @error err:XQDY0027 if $options can not be validated against the \c
 : json-options schema.
 : @error ZJSE0001 if \a $xml is not a document or element node.
 : @error ZJSE0002 if \a $xml contains an element that is missing a required
 : attribute.
 : @error ZJSE0003 if \a $xml contains an attribute having an illegal value.
 : @error ZJSE0004 if \a $xml contains an illegal element.
 : @error ZJSE0005 if \a $xml contains an illegal child element for a JSON type.
 : @error ZJSE0006 if \a $xml contains an illegal child element.
 : @error ZJSE0007 if \a $xml contains an illegal text node.
 : @error ZJSE0008 if \a $xml contains an illegal value for a JSON type.
 :)
declare function json:serialize(
  $xml as item()*,
  $options as element(json-options:options)
) as xs:string
{
  let $validated-options := if ( schema:is-validated( $options ) ) then
                              $options
                            else
                              validate { $options }
  return json:serialize-internal( $xml, $validated-options )
};

(:~
 : Serializes an XDM into JSON using one of the representations described
 : above.
 :
 : @param $xml The XDM to serialize.
 : @return a JSON string.
 : @error ZJSE0001 if \a $xml is not a document or element node.
 : @error ZJSE0002 if \a $xml contains an element that is missing a required
 : attribute.
 : @error ZJSE0003 if \a $xml contains an attribute having an illegal value.
 : @error ZJSE0004 if \a $xml contains an illegal element.
 : @error ZJSE0005 if \a $xml contains an illegal child element for a JSON type.
 : @error ZJSE0006 if \a $xml contains an illegal child element.
 : @error ZJSE0007 if \a $xml contains an illegal text node.
 : @error ZJSE0008 if \a $xml contains an illegal value for a JSON type.
 :)
declare function json:serialize(
  $xml as item()*
{'fraction_non_alphanumeric': 0.1036010171437946, 'fraction_numerical': 0.015257156919038634, 'mean_word_length': 3.2884277172001406, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 16, 'xml': 30}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1787562', 'n_tokens_mistral': 3753, 'n_tokens_neox': 3560, 'n_words': 1405}