Javascript JSON

From Yate Documentation
(Difference between revisions)
Jump to: navigation, search
 
Line 15: Line 15:
 
Stringify a Javascript primitive or object.<br/>
 
Stringify a Javascript primitive or object.<br/>
 
Objects already added (repeated in input object tree) will be replaced by an object with a '''$ref''' property having a string value as specified in RFC 6901 URI fragment.<br>
 
Objects already added (repeated in input object tree) will be replaced by an object with a '''$ref''' property having a string value as specified in RFC 6901 URI fragment.<br>
Example:
 
  "a": {"$ref":"#/a/b/c"}
 
 
 
Parameters:<br/>
 
Parameters:<br/>
 
'''obj''' Object of primitive type<br/>
 
'''obj''' Object of primitive type<br/>
Line 24: Line 21:
 
Return:<br/>
 
Return:<br/>
 
String JSON data, ''undefined'' is an error occured.
 
String JSON data, ''undefined'' is an error occured.
 +
 +
References:
 +
  var a = {id:"a"};
 +
  var b = {id:"b"};
 +
  var c = {id:"c", b:b, root:a};
 +
  a.b = b;
 +
  a.c = c;
 +
JSON.stringify(a,undefined,2):
 +
{                                                                                                                                                                                                                 
 +
  "id": "a",                                                                                                                                                                                                     
 +
  "b": {                                                                                                                                                                                                         
 +
    "id": "b"                                                                                                                                                                                                     
 +
  },
 +
  "c": {
 +
    "id": "c",
 +
    "b": {
 +
      "$ref": "#/b"
 +
    },
 +
    "root": {
 +
      "$ref": "#"
 +
    }
 +
  }
 +
}
  
  
Line 73: Line 93:
 
Return:<br>
 
Return:<br>
 
True on success, false otherwise
 
True on success, false otherwise
 +
 +
Input:
 +
{
 +
  "id": "a",
 +
  "array": [
 +
    {
 +
      "id": "b"
 +
    },
 +
    {
 +
      "id": "c",
 +
      "b": {
 +
        "$ref": "#/array/0"
 +
      },
 +
      "root": {
 +
        "$ref": "#"
 +
      }
 +
    }
 +
  ],
 +
  "b": {
 +
    "$ref": "#/array/0"
 +
  },
 +
  "c": {
 +
    "$ref": "#/array/1"
 +
  }
 +
}
 +
Engine.print_var_r() after replace
 +
's' = [object Object]
 +
  'id' = 'a'
 +
  'array' = [object Array]
 +
    '0' = [object Object]
 +
      'id' = 'b'
 +
    '1' = [object Object]
 +
      'id' = 'c'
 +
      'b' = [object Object] (already seen) #/array/0
 +
      'root' = [object Object] (already seen) #
 +
  'b' = [object Object] (already seen) #/array/0
 +
  'c' = [object Object] (already seen) #/array/1
 +
 +
Input:
 +
{
 +
  "id": "a",
 +
  "b": {
 +
    "id": "b"
 +
  },
 +
  "c": {
 +
    "id": "c",
 +
    "b": {
 +
      "$ref": "#/b"
 +
    },
 +
    "root": {
 +
      "$ref": "#"
 +
    }
 +
  },
 +
  "array": [
 +
    {
 +
      "$ref": "#/b"
 +
    },
 +
    {
 +
      "$ref": "#/c"
 +
    }
 +
  ]
 +
}
 +
Engine.print_var_r() after replace
 +
's' = [object Object]
 +
  'id' = 'a'
 +
  'b' = [object Object]
 +
    'id' = 'b'
 +
  'c' = [object Object]
 +
    'id' = 'c'
 +
    'b' = [object Object] (already seen) #/b
 +
    'root' = [object Object] (already seen) #
 +
  'array' = [object Array]
 +
    '0' = [object Object] (already seen) #/b
 +
    '1' = [object Object] (already seen) #/c

Latest revision as of 15:34, 27 October 2021

JSON parsing global object.

[edit] Methods

  • parse(str)

Parse JSON string.

Parameters:
str String containing JSON data
Return:
Primitive type or object representing the data, undefined if parsing failed.


  • stringify(obj,unused,spaces)

Stringify a Javascript primitive or object.
Objects already added (repeated in input object tree) will be replaced by an object with a $ref property having a string value as specified in RFC 6901 URI fragment.
Parameters:
obj Object of primitive type
unused Unused optional parameter, for compatibility with the standard
spaces Optional number of spaces to use for indentation
Return:
String JSON data, undefined is an error occured.

References:

  var a = {id:"a"};
  var b = {id:"b"};
  var c = {id:"c", b:b, root:a};
  a.b = b;
  a.c = c;

JSON.stringify(a,undefined,2):

{                                                                                                                                                                                                                  
  "id": "a",                                                                                                                                                                                                       
  "b": {                                                                                                                                                                                                           
    "id": "b"                                                                                                                                                                                                      
  },
  "c": {
    "id": "c",
    "b": {
      "$ref": "#/b"
    },
    "root": {
      "$ref": "#"
    }
  }
}


  • loadFile(name)

Parse JSON file (non-standard method).

Parameters:
name Name of the file containing JSON data
Return:
Primitive type or object representing the data, undefined if parsing failed.


  • saveFile(name,obj,spaces)

Save JSON data in a file (non-standard method).

Parameters:
name Name of the JSON file to write
unused Unused optional parameter, for compatibility with the standard
spaces Optional number of spaces to use for indentation
Return:
TRUE if operation succeeded, FALSE if an error occured.


  • replaceParams(obj, params [,sqlEscape [,extraEsc]])

Replace ${paramname} instances in 'buf' from 'params' field values.
Replacements are done in string property values.
Parameters:
buf: Array: replace in its elements. Object: replace in its properties. Any other type is ignored
params: Object whose fields are used to replace. Ignored (no replace is made) if not an object
sqlEscape: Optional. True to apply SQL escaping to parameter values
extraEsc: Character to escape other than the SQL default ones


  • findPath(obj, path)

Find object property value or array at index value by a JSON path.
Parameters:
obj: Object to search in
path: Path to use. May be a JPath object or a string
Return:
Found value, undefined if not found, obj is not an object or path is invalid


  • replaceReferences(obj)

Replace path references in given object properties or array values.
A path reference is an object with a $ref property with value containing a path URI as specified in RFC 6901.
Parameters:
obj: Object to replace in
Return:
True on success, false otherwise

Input:

{
  "id": "a",
  "array": [
    {
      "id": "b"
    },
    {
      "id": "c",
      "b": {
        "$ref": "#/array/0"
      },
      "root": {
        "$ref": "#"
      }
    }
  ],
  "b": {
    "$ref": "#/array/0"
  },
  "c": {
    "$ref": "#/array/1"
  }
}

Engine.print_var_r() after replace

's' = [object Object]
  'id' = 'a'
  'array' = [object Array]
    '0' = [object Object]
      'id' = 'b'
    '1' = [object Object]
      'id' = 'c'
      'b' = [object Object] (already seen) #/array/0
      'root' = [object Object] (already seen) #
  'b' = [object Object] (already seen) #/array/0
  'c' = [object Object] (already seen) #/array/1

Input:

{
  "id": "a",
  "b": {
    "id": "b"
  },
  "c": {
    "id": "c",
    "b": {
      "$ref": "#/b"
    },
    "root": {
      "$ref": "#"
    }
  },
  "array": [
    {
      "$ref": "#/b"
    },
    {
      "$ref": "#/c"
    }
  ]
}

Engine.print_var_r() after replace

's' = [object Object]
  'id' = 'a'
  'b' = [object Object]
    'id' = 'b'
  'c' = [object Object]
    'id' = 'c'
    'b' = [object Object] (already seen) #/b
    'root' = [object Object] (already seen) #
  'array' = [object Array]
    '0' = [object Object] (already seen) #/b
    '1' = [object Object] (already seen) #/c
Personal tools
Namespaces

Variants
Actions
Preface
Configuration
Administrators
Developers