Product SiteDocumentation Site

6.6. jn:decode-from-roundtrip

This function decodes non-JSON types previously encoded with jn:encode-for-roundtrip.
jn:decode-from-roundtrip($items as item()*) as item()*
jn:decode-from-roundtrip($items as item()*, $options as object()) as item()*
There is one optional parameter than can be supplied using the $options object.
The above optional parameter is refered to as $prefix for convenience.
Error err:XPTY0004 is raised in case one this parameter is of incorrect type.
Non-JSON values are encoded to a special object containing two required fields "[$prefix]type" (xs:string) and "[$prefix]value" (xs:string), and two optional fields "[$prefix]prefix" (xs:string) and "[$prefix]encoding-level (xs:integer, strictly positive). An object that has the two required fields, and that has no other field that these four fields, and whose values match the types, is called an Encoded Object.
This function maps each item of the input sequence to a new item in the output sequence, preserving order.
For convenience, the map is defined recursively from item() to item().
An array is mapped to an array the members of which are mapped resursively.
Atomic values and XML nodes are mapped to themselves.
An object is mapped depending on whether it is an Encoded Object (according to the above definition).
An Encoded Object whose "[$prefix]type" field is "document-node()", "element()", "text()", "comment()" or "processing-instruction()" is mapped to the XML node optained by parsing the field "[$prefix]value". An error jerr:JNTY0023 is raised if the obtained node does not match the specified type.
An Encoded Object whose "[$prefix]type" field is another string is mapped to an atomic item with the type specified by the value of the field "[$prefix]type" (read as an EQName, with xs: automatically recognized as the XSD namespace).
An object that is not an Encoded Object is mapped to an object with the same keys, and the associated values are mapped recursively.
Example 6.4. Encoding non-native JSON for roundtripping.
Query:
        jn:decode-from-roundtrip(
          {
            "nan" : {
              "Q{http://jsoniq.org/roundtrip}type" : "xs:double",
              "Q{http://jsoniq.org/roundtrip}value" : "NaN"
            },
            "inf" : {
              "Q{http://jsoniq.org/roundtrip}type" : "xs:double",
              "Q{http://jsoniq.org/roundtrip}value" : "INF"
            }
            "date" : {
              "Q{http://jsoniq.org/roundtrip}type" : "xs:date",
              "Q{http://jsoniq.org/roundtrip}value" : "1066-10-14"
             }
            "hat" : {
              "Q{http://jsoniq.org/roundtrip}type" : "Q{http://www.example.com/hat-shop}hatSize",
              "Q{http://jsoniq.org/roundtrip}value" : "M"
            }
          }
        )
Result:
        {
          "nan" : xs:double("NaN"),
          "inf" : xs:double("INF"),
          "date" : xs:date("1066-10-14"),
          "user" : hat:hatSize("M")
        }