Skip to content

Commit

Permalink
Add support for JSON format
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilly98 committed Jan 22, 2014
1 parent 54a43c4 commit af33caf
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/output/html.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
<a href="{xray:url($module-pattern, $test-pattern, 'xunit')}">xUnit</a>
<xsl:text>&#160;|&#160;</xsl:text>
<a href="{xray:url($module-pattern, $test-pattern, 'text')}">text</a>
<xsl:text>&#160;|&#160;</xsl:text>
<a href="{xray:url($module-pattern, $test-pattern, 'json')}">json</a>
<br/>
<a href="http://github.com/robwhitby/xray">xray</a> version <xsl:value-of select="/@xray-version"/>
</p>
Expand Down
27 changes: 27 additions & 0 deletions src/output/json.xqy
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
xquery version "1.0-ml";

module namespace xray-json = "http://github.com/robwhitby/xray/json";
import module namespace json = "http://marklogic.com/xdmp/json" at "/MarkLogic/json/json.xqy";

declare namespace xray = "http://github.com/robwhitby/xray";
declare namespace test = "http://github.com/robwhitby/xray/test";

declare function to-json(
$node as node()
) as xs:string
{
let $_ := xdmp:log($node)
let $custom :=
let $config := json:config("custom")
return (
map:put($config, "array-element-names", (
"module",
"test",
"assert",
fn:QName("http://marklogic.com/xdmp/error", "frame"))),
map:put($config, "element-namespace", "http://github.com/robwhitby/xray"),
$config)
return
json:transform-to-json($node, $custom)
};

17 changes: 17 additions & 0 deletions src/output/json.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xray="http://github.com/robwhitby/xray"
xmlns:error="http://marklogic.com/xdmp/error"
xmlns:xdmp="http://marklogic.com/xdmp"
xmlns:xray-json="http://github.com/robwhitby/xray/json"
xmlns:json="http://json.org/"
exclude-result-prefixes="xdmp"
extension-element-prefixes="xdmp"
version="2.0">

<xdmp:import-module href="json.xqy" namespace="http://github.com/robwhitby/xray/json"/>

<xsl:template match="xray:tests">
<xsl:value-of select="xray-json:to-json(.)"/>
</xsl:template>

</xsl:stylesheet>
1 change: 1 addition & 0 deletions src/xray.xqy
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ declare private function transform(
) as document-node()
{
if ($format eq "text") then xdmp:set-response-content-type("text/plain") else (),
if ($format eq "json") then xdmp:set-response-content-type("application/json") else (),
if ($format ne "xml")
then
let $params := map:map()
Expand Down

0 comments on commit af33caf

Please sign in to comment.