Re: [MarkLogic Dev General] MarkLogic 8.0-5 Indenting JSON output

2016-12-02 Thread Florent Georges
Hi Gary,

If MarkDown is handled on the client, e.g. using something like
marked.js, an alternative is to handle the formatting of the JSON
snippets on the client as well (directly in JS in the browser).

Regards,

-- 
Florent Georges
http://fgeorges.org/
http://h2o.consulting/ - New website!


On 1 December 2016 at 21:25, Gary Vidal wrote:
> Thanks Justin,
>
> I figured that one out about JSON.stringify.  Ideally, it would be best to
> eval to a function which can be reused.  Of course safety and performance
> would be issue.  Not sure if V8 connection is kept open during xquery
> evaluation. Anyways its for a small documentation app, that just pretty
> prints json examples for view on MarkDown page.  I can deal with indented
> json in a large file of examples. :-)
>
> ___
> General mailing list
> General@developer.marklogic.com
> Manage your subscription at:
> http://developer.marklogic.com/mailman/listinfo/general
>
___
General mailing list
General@developer.marklogic.com
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general


Re: [MarkLogic Dev General] MarkLogic 8.0-5 Indenting JSON output

2016-12-01 Thread Gary Vidal
Thanks Justin,

I figured that one out about JSON.stringify.  Ideally, it would be best to
eval to a function which can be reused.  Of course safety and performance
would be issue.  Not sure if V8 connection is kept open during xquery
evaluation. Anyways its for a small documentation app, that just pretty
prints json examples for view on MarkDown page.  I can deal with indented
json in a large file of examples. :-)
___
General mailing list
General@developer.marklogic.com
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general


Re: [MarkLogic Dev General] MarkLogic 8.0-5 Indenting JSON output

2016-12-01 Thread Justin Makeig
Gary, I hope all is well with you.

Currently xdmp:output doesn't affect JSON nodes. It should. I'll add a Request 
For Enhancement (RFE) on my end to track this.

Here's a potential workaround:


(: See security guidance below :)
declare function json:stringify($json as node(), $spaces as xs:int) as 
xs:string? {
  xdmp:javascript-eval("JSON.stringify($json.toObject(), null, $spaces)", 
('$json', $json, '$spaces', $spaces))
};

declare function json:stringify($json as node()) as xs:string? {
  json:stringify($json, 2)
};

let $node := xdmp:unquote('["B", "B"]')
let $node :=  (: returns empty-sequence() :)
let $node := xdmp:unquote('{"a": "A", "b": ["B", "B"]}')
return json:stringify($node)

=> 

{
  "a": "A",
  "b": [
"B",
"B"
  ]
}

It uses xdmp:javascript-eval, which is potentially unsafe. You'd want to amp 
the json:stringify in a real production implementation so the eval can only be 
called in the context of json:stringify. See 
. (Note that 
JSON.stringify is *not* unsafe because it doesn't execute arbitrary 
JavaScript.) I haven't throughly tested the edge cases, but you get the idea.

Justin

--
Justin Makeig
Director, Product Management
MarkLogic
jmak...@marklogic.com

> On Dec 1, 2016, at 9:55 AM, Gary Vidal  wrote:
> 
> All,
> 
> Is there an equivalent to format json output like
>  declare option xdmp:output "indent=yes";
> ___
> General mailing list
> General@developer.marklogic.com
> Manage your subscription at: 
> http://developer.marklogic.com/mailman/listinfo/general



___
General mailing list
General@developer.marklogic.com
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general


[MarkLogic Dev General] MarkLogic 8.0-5 Indenting JSON output

2016-12-01 Thread Gary Vidal
All,

Is there an equivalent to format json output like
 declare option xdmp:output "indent=yes";
___
General mailing list
General@developer.marklogic.com
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general