如何编写符合MarkLogic 7和8中JSON API差异的代码?

时间:2015-02-06 19:29:55

标签: json xquery marklogic

MarkLogic 8以多种方式改进了JSON支持,但是一些MarkLogic 7 JSON功能现在具有不同的签名或执行不同的操作。如何编写适用于这两个版本的XQuery代码?

1 个答案:

答案 0 :(得分:4)

到目前为止,我遇到的主要变化是xdmp:from-jsonxdmp:to-jsonjson:transfrom-to-json。如果您从MarkLogic 7获得了大量与JSON相关的代码,则这些更改可能会破坏现有代码。

不是直接调用xdmpjson函数,而是导入此库模块并调用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项目。