如何编写代码来处理MarkLogic 7和8中JSON API的差异?
MarkLogic 8在许多方面改进了JSON支持,但MarkLogic 7的一些JSON函数现在具有不同的签名或执行不同的操作。如何编写能够同时使用这两个版本的XQuery代码?到目前为止,我遇到的主要更改是将xdmp:from json、如何编写代码来处理MarkLogic 7和8中JSON API的差异?,json,xquery,marklogic,Json,Xquery,Marklogic,MarkLogic 8在许多方面改进了JSON支持,但MarkLogic 7的一些JSON函数现在具有不同的签名或执行不同的操作。如何编写能够同时使用这两个版本的XQuery代码?到目前为止,我遇到的主要更改是将xdmp:from json、xdmp:to json和json:transfrom to json。如果您有大量来自MarkLogic 7的JSON相关代码,这些更改可能会破坏现有代码 不要直接调用那些xdmp和json函数,而是导入此库模块并从json等调用c:from json。这
xdmp:to json
和json:transfrom to json
。如果您有大量来自MarkLogic 7的JSON相关代码,这些更改可能会破坏现有代码
不要直接调用那些xdmp
和json
函数,而是导入此库模块并从json等调用c:from json。这允许为MarkLogic 7编写的代码在这两个版本中都能工作
module namespace c="http://blakeley.com/marklogic/json/compatibility";
import module namespace json="http://marklogic.com/xdmp/json"
at "/MarkLogic/json/json.xqy";
(: No prefix for the fn:* functions :)
declare default function namespace "http://www.w3.org/2005/xpath-functions";
declare variable $VERSION := xs:integer(
substring-before(xdmp:version(), ".")) ;
declare function c:from-json(
$arg as xs:string)
as item()*
{
xdmp:from-json(
if ($VERSION ge 8) then xdmp:unquote($arg, (), "format-json")
else $arg)
};
declare function c:to-json(
$item as item()*)
as xs:string*
{
if ($VERSION ge 8) then xdmp:quote($item)
else xdmp:to-json($item)
};
declare function c:transform-to-json(
$node as node(),
$config as map:map?)
as xs:string
{
json:transform-to-json($node, $config) ! (
if ($VERSION ge 8) then xdmp:quote(.)
else .)
};
declare function c:transform-to-json(
$node as node())
as xs:string
{
c:transform-to-json($node, ())
};
当我遇到其他更改或听说它们时,我将扩展此功能。如果太长,我将把它移到gist或github项目。ML8发行说明列出了所有不兼容: