suve

awful: function: json-encode

Available since rev.38 (v.0.5.2).

Declaration

ascii :json-encode ( mixed $VALUE , bool $BARE )
ascii :json-encode ( mixed $VALUE )

Summary

Returns a JSON representation of given data structure. Dictionaries will be encoded as objects, and arrays as arrays.

Since JSON standard mandates that the top-level element in JSON data must be either an array or object, if $VALUE is neither of those, the resulting string will have it wrapped in array brackets ([ ]). The optional $BARE parameter can be used to override this behaviour and allow returning bare values.

Example

# :json-encode example
:set &d :dict
:set &d[s'awful'] =AWFUL-VERSION
:set &d[s'array'] :arr f3 f2.5 f0.00000017

# {"array":[3,2.5,1.7E-7],"awful":"0.5.2"}
:writeln :json-encode $d

# ["No bare values allowed!"]
:writeln :json-encode s'No bare values allowed!'

# "But I want bare values!"
:writeln :json-encode s'But I want bare values!' lTRUE

wikipage modified on 2014/0601/2317