string. Converting Parser object into JSON-string

^json:string[object;options]

Method serializes a system or user object into JSON-string. By default an object of user-defined class is serialized as hash.

Options
-hash with serialization options.

Default
Value
$.skip-unknown(true|false)
false
By default (false) only the objects of classes void, bool, string, int, double, date, table, hash file are accepted. Other objects will cause an exception.
If this option is set to
true, these objects will be skipped silently.
$.indent(true|false)
$.indent[string]

false
If this option is set to true the resulting JSON-string will be formatted using tab characters.
The option is also can be specified as user-predefined indent's prefix string. [3.4.3]
$.date[sql-string|gmt-string|iso-string|unix-timestamp]
sql-string
Defines format of objects of class date (see the methods of class date with the same names).
$.void[null|string]

null
Defines format of objects of class void. By default it is null, but can be also serialized as an empty string.   [3.4.4]
$.table[object|array|compact]
object
Defines format of objects of class table.
object:
[{"col1":"val11","col2":"val12",...},{"col1":"val21","col2":"val22",...},...]
array:
[["col1","col2",...] || null (for nameless ),["val11","val12",...],...]
compact:
["value11" || ["val11","val12",...],...]

$.file[text|base64|stat]
not defined

Defines format of bodies of objects of class file.
By default (if the option is not specified of or equal "stat") the files' bodies will be omited.
$.xdoc[options for converting into text]
not defined
Options for converting xdoc object into text   [3.4.2]
$.class-name[method name]
not defined
Objects of any class (including mentioned date, table and file) could be serialized with user's method. The method must accept three parameters-object's name, value and options. The options is needed for serializing user's objects recursively.
Parser searches for the specified method in all ancestors classes.   [3.4.2]
$._default[method reference]
not defined
If specified the method will be called for all objects of user-defined classes (other than are handled with option $.class-name[method name]). The method must accept three parameters - key, value and options.   [3.4.2]
$._default[method name]
not defined
If specified and a method with specified name exists in a object of user-defined class, this method will be called for the object serialization (for classes other than are handled with option $.class-name[method name]). The method must accept two parameters - key and options.   [3.4.4]


Example
@main[]
$h[
   $.void[]
   $.bool(true)
   $.double(1/2)
   $.string[
ABC]
   $.hash[
      $.e[ee]
   ]
   $.date[^date::create(2006;08;18;06;09;00)]
   $.table[^table::create{c1   c2   c3^#0Av1   v2   v3^#0Av4   v5   v6}]
   $.file[^file::create[text;
zigi.txt;file-content]]
   $.img[^image::create(100;100;0)]
]
^json:string[$h;
   $.indent(true)
   $.table[array]
   $.file[base64]
   $.image[$image_handler]
]

@image_handler[key;value
;params]
"
custom value of image $key"

will returns:
{
   "void":"",
   "bool":true,
   "double":0.5,
   "string":"ABC",
   "hash":{
      "e":"ee"
   },
   "date":"2006-08-18 06:09:00",
   "table":[
      ["c1","c2","c3"],
      ["v1","v2","v3"],
      ["v4","v5","v6"]
   ],
   "file":{
      "class":"file",
      "name":"zigi.txt",
      "size":12,
      "content-type":"text\/plain",
      "mode":"text",
      "base64":"ZmlsZS1jb250ZW50"
   },
   "img":"custom value of image img"
}



Copyright © 1997–2017 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 23.04.2016