string. Converting Parser object into JSON-string

^json:string[object;options]

Method serializes system of user object into JSON-string.

Options
-hash with parsing 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)
false
If this option is set to true the resulting JSON-string will be formatted using tab characters.
$.date[sql-string|gmt-string|unix-timestamp]
sql-string
Defines format of objects of class date (see the methods of class date with the same names).
$.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]
not defined

Defines format of bodies of objects of class file.
If not specified (default) the files' bodies will be omited.
$.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.


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]
"
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–2011 Art. Lebedev Studio | http://www.artlebedev.com Last updated: 02.03.2011