# HG changeset patch # User Yaroslav Zhuravlev # Date 1528212199 -10800 # Node ID 95b406f1f347257c108b3c7ab4ed75dcef67ec90 # Parent cd4889fdcfa485a570bb590df3dcbc1b45939b69 Added njs JSON API. diff -r cd4889fdcfa4 -r 95b406f1f347 xml/en/docs/njs/njs_api.xml --- a/xml/en/docs/njs/njs_api.xml Tue Jun 05 18:22:00 2018 +0300 +++ b/xml/en/docs/njs/njs_api.xml Tue Jun 05 18:23:19 2018 +0300 @@ -9,7 +9,7 @@
+ rev="2">
@@ -21,6 +21,83 @@
+
+ + +
+ + +The JSON object (ES 5.1) provides functions +to convert njs values to and from JSON format. + + +JSON.parse(string[, +reviver]) + +Converts a string that represents JSON data +into an njs object ({...}) or +array ([...]). +The optional reviver parameter is a function (key, value) +that will be called for each (key,value) pair and can transform the value. + + +JSON.stringify(value[, +replacer] [, space]) + +Converts an njs object back to JSON. +The obligatory value parameter is generally a JSON +object or array that will be converted. +If the value has a toJSON() method, +it defines how the object will be serialized. +The optional replacer parameter is +a function or array +that transforms results. +The optional space parameter is +a string or number. +If it is a number, +it indicates the number of white spaces placed before a result +(no more than 10). +If it is a string, +it is used as a white space (or first 10 characters of it). +If omitted or is null, no white space is used. + + + + + + +>> var json = JSON.parse('{"a":1, "b":true}') +>> json.a +1 + +>> JSON.stringify(json) +{"a":1,"b":true} + +>> JSON.stringify(json, undefined, 1) +{ +"a": 1, +"b": true +} + +>> JSON.stringify({ x: [10, undefined, function(){}] }) +{"x":[10,null,null]} + +>> JSON.stringify({"a":1, "toJSON": function() {return "xxx"}}) +"xxx" + +# Example with function replacer + +>> function replacer(key, value) {return (typeof value === 'string') ? undefined : value} +>>JSON.stringify({a:1, b:"b", c:true}, replacer) +{"a":1,"c":true} + + + +
+ +
+ +