JSON

private class
system.JSON

Description

Provides utilities for parsing data in JavaScript Object Notation (JSON) format.

There are two types of JSON: JSON Objects and JSON Arrays. JSON Objects are analogous to Loom Dictionaries in that they consist of key / value pairs, where all the keys are strings. JSON Arrays are analogous to Vectors in that they contain data ordered by indexes. It is important when dealing with JSON to understand the difference between these two types. If there is a situation where the type of JSON being dealt with is unknown, the getJSONType() function can be used and will return a JSONType enum describing what kind of JSON is being dealt with. The isArray() function can also be used to the same effect, returning true if the JSON is an array and false otherwise. Attempting to use the functions intended for Objects on Arrays or vice versa will result in failure.

Before the JSON data structure can be used, loadString() must be called to populate the structure with data. Dump the JSON data structure by calling serialize().

In order to return strongly typed values from the data structure, every data type has its own getter and setter. The getters and setters come in two flavors, for operating on Objects or Arrays, e.g.:

  • getBoolean(key), setBoolean(key, value)
  • getArrayBoolean(index), setArrayBoolean(index, value)

See also:
http://www.json.org/
#getJSONType()
#isArray()
#loadString()
#serialize()

API overview

Constructor

JSON ()

Attributes

Public attributes

length: Number read-only

Property that retrieves the number of items in a JSON Array, wraps getArrayCount()

See also:
#getArrayCount()

Functions

Public functions

applyField ( o: Object , field: String ): Void

Convenience method that sets the field on the provided object using the JSON value of the same name

fromDictionary ( d: Dictionary.<String, Object> ): JSON static

Creates a JSON object from the given Dictionary mapping keys to values

fromVector ( v: Vector.<Object> ): JSON static

Creates a JSON array from the given Vector of objects

getArray ( key: String ): JSON native

For a JSON Object, retrieves the JSON Array value associated with the provided key

getArrayArray ( index: Number ): JSON native

For a JSON Array, retrieves a JSON Array value at the provided index

getArrayBoolean ( index: Number ): Boolean native

For a JSON Array, retrieves a Boolean value at the provided index

getArrayCount (): Number native

For a JSON Array, retrieves the number of items

getArrayFloat ( index: Number ): Number native

For a JSON Array, retrieves a Number value at the provided index

getArrayInteger ( index: Number ): Number native

For a JSON Array, retrieves a 32-bit Integer value at the provided index

getArrayJSONType ( index: Number ): JSONType native

For a JSON Array, retrieves the type of the value at the provided index

getArrayNumber ( index: Number ): Number native

For a JSON Array, retrieves a Number (64-bit Float or 32-bit Integer) value at the provided index

getArrayObject ( index: Number ): JSON native

For a JSON Array, retrieves a JSON Object value at the provided index

getArrayString ( index: Number ): String native

For a JSON Array, retrieves a String value at the provided index

getArrayValue ( index: Number ): Object

General function that gets the value of the index provided in Json Arrays

getBoolean ( key: String ): Boolean native

For a JSON Object, retrieves the Boolean value associated with the provided key

getDictionary (): Dictionary

Create a Loom friendly dictionary from this JSON object

getError (): String native

Retrieves a string representation of the last error thrown

getFloat ( key: String ): Number native

For a JSON Object, retrieves the Number associated with the provided key

getInteger ( key: String ): Number native

For a JSON Object, retrieves the 32-bit Integer value associated with the provided key

getJSONType (): JSONType native

Retrieves the type of the current JSON Object

getLongLongAsString ( key: String ): String native

For a JSON Object, retrieves a String representation of the 64-bit Integer value associated with the provided key

getNumber ( key: String ): Number native

For a JSON Object, retrieves the number value (64-bit Float or 32-bit Integer) associated with the provided key

getObject ( key: String ): JSON native

For a JSON Object, retrieves the JSON Object value associated with the provided key

getObjectFirstKey (): String native

For a JSON Object, retrieves the name of the first key in the property list

getObjectJSONType ( key: String ): JSONType native

For a JSON Object, retrieves the type of the value associated with the provided key

getObjectNextKey ( key: String ): String native

For a JSON Object, retrieves the name of the key immediately following the provided key in the property list

getString ( key: String ): String native

For a JSON Object, retrieves the String value associated with the provided key

getValue ( key: String ): Object

General function that gets the value of the Key provided

getVector (): Vector

Create a Loom friendly vector from this JSON array

initArray (): Boolean native

Initialize the JSON instance as an empty JSON array, clearing all the previously held values

initObject (): Boolean native

Initialize the JSON instance as an empty JSON object, clearing all the previously held values

isArray (): Boolean native

Indicates whether the JSON Object is considered an Array

isObject (): Boolean native

Indicates whether the JSON Object is considered an Object

loadString ( json: String ): Boolean native

Loads a JSON-formatted string into memory

parse ( json: String ): JSON static

Convenience function will parse a JSON string and return a JSON object

serialize (): String native

Serializes the in-memory data structure to a JSON formatted String

setArray ( key: String , value: JSON ): Void native

For a JSON Object, associates a JSON Array value with the provided key

setArrayArray ( index: Number , value: JSON ): Void native

For a JSON Array, associates a JSON Array value with the provided index

setArrayBoolean ( index: Number , value: Boolean ): Void native

For a JSON Array, associates a Boolean value with the provided index

setArrayDictionary ( index: Number , d: Dictionary.<String, Object> ): Void

Sets the value of the array index to the JSON object created from the provided Dictionary using fromDictionary

setArrayFloat ( index: Number , value: Number ): Void native

For a JSON Array, associates a 64-bit Float value with the provided index

setArrayInteger ( index: Number , value: Number ): Void native

For a JSON Array, associates a 32-bit Integer value with the provided index

setArrayNumber ( index: Number , value: Number ): Void native

For a JSON Array, associates a Number (64-bit Float) value with the provided index

setArrayObject ( index: Number , value: JSON ): Void native

For a JSON Array, associates a JSON Object value with the provided index

setArrayString ( index: Number , value: String ): Void native

For a JSON Array, associates a String value with the provided index

setArrayValue ( index: Number , o: Object ): Void

General function that sets the value of the array index based on the type of object

setArrayVector ( index: Number , v: Vector.<Object> ): Void

Sets the value of the array index to the JSON array created from the provided Vector using fromVector

setBoolean ( key: String , value: Boolean ): Void native

For a JSON Object, associates a Boolean value with the provided key

setDictionary ( key: String , d: Dictionary.<String, Object> ): Void

Sets the value of the key to the JSON object created from the provided Dictionary using fromDictionary

setFloat ( key: String , value: Number ): Void native

For a JSON Object, associates a 64-bit Float value with the provided key

setInteger ( key: String , value: Number ): Void native

For a JSON Object, associates an Integer value with the provided key

setNumber ( key: String , value: Number ): Void native

For a JSON Object, associates a 64-bit Float value with the provided key

setObject ( key: String , value: JSON ): Void native

For a JSON Object, associates a JSON Object value with the provided key

setString ( key: String , value: String ): Void native

For a JSON Object, associates a String value with the provided key

setValue ( key: String , o: Object ): Void

General function that sets the value of the key based on the type of object

setVector ( key: String , v: Vector.<Object> ): Void

Sets the value of key to the JSON array created from the provided Vector using fromVector

stringify ( o: Object , visited: Vector.<Object> = null ): String static

Traverses through the object's fields and builds a JSON string from the hierarchy

Constructor

JSON ()

Attributes

length: Number

read-only

Property that retrieves the number of items in a JSON Array, wraps getArrayCount()

See also:
#getArrayCount()

Functions

applyField ( o: Object , field: String ): Void

Convenience method that sets the field on the provided object using the JSON value of the same name.

Parameters

o: Object The Object to set the field on.
field: String The key name of the JSON object to get the value of and the field name on the Object to set the value on.


fromDictionary ( d: Dictionary.<String, Object> ): JSON

static

Creates a JSON object from the given Dictionary mapping keys to values. The setValue function is the primary force behind the conversion, see setValue for limitations and additional information.

See also:
#setValue()

Parameters

d: Dictionary.<String, Object> The Dictionary mapping Strings to Objects to source from.

Returns

JSON The JSON object having equivalent keys and values.

fromVector ( v: Vector.<Object> ): JSON

static

Creates a JSON array from the given Vector of objects. The setArrayValue function is the primary force behind the conversion, see setArrayValue for limitations and additional information.

See also:
#setArrayValue()

Parameters

v: Vector.<Object> The Vector containing Objects to source from.

Returns

JSON The JSON array containing equivalent values to the Vector.

getArray ( key: String ): JSON

native

For a JSON Object, retrieves the JSON Array value associated with the provided key.

The object returned is a JSON array (ordered set of values).

See also:
#getArrayJSONType()
#getArrayCount()
#getArrayBoolean()
#getArrayInteger()
#getArrayString()
#getArrayArray()
#getArrayObject()

Parameters

key: String Identifies the array to be retrieved


getArrayArray ( index: Number ): JSON

native

For a JSON Array, retrieves a JSON Array value at the provided index.

Parameters

index: Number Identifies the item in the Array to be retrieved as a JSON Array


getArrayBoolean ( index: Number ): Boolean

native

For a JSON Array, retrieves a Boolean value at the provided index.

Parameters

index: Number Identifies the item in the Array to be retrieved as a Boolean


getArrayCount (): Number

native

For a JSON Array, retrieves the number of items.


Returns

Number The number of items, -1 if object is not a JSON Array.

getArrayFloat ( index: Number ): Number

native

For a JSON Array, retrieves a Number value at the provided index. Alias of getNumber so it converts integers to floats instead of returning 0.

Parameters

index: Number Identifies the item in the Array to be retrieved as a Float


getArrayInteger ( index: Number ): Number

native

For a JSON Array, retrieves a 32-bit Integer value at the provided index.

Parameters

index: Number Identifies the item in the Array to be retrieved as an Integer


getArrayJSONType ( index: Number ): JSONType

native

For a JSON Array, retrieves the type of the value at the provided index.

Parameters

index: Number Identifies the item in the Array to be queried


getArrayNumber ( index: Number ): Number

native

For a JSON Array, retrieves a Number (64-bit Float or 32-bit Integer) value at the provided index.

Parameters

index: Number Identifies the item in the Array to be retrieved as a Number


getArrayObject ( index: Number ): JSON

native

For a JSON Array, retrieves a JSON Object value at the provided index.

Parameters

index: Number Identifies the item in the Array to be retrieved as a JSON Object


getArrayString ( index: Number ): String

native

For a JSON Array, retrieves a String value at the provided index.

Parameters

index: Number Identifies the item in the Array to be retrieved as a String


getArrayValue ( index: Number ): Object

General function that gets the value of the index provided in Json Arrays

Parameters

index: Number The index to be queried

Returns

Object May return null, a Boolean, a String, a Number, a JSON Object, or a JSON Array

getBoolean ( key: String ): Boolean

native

For a JSON Object, retrieves the Boolean value associated with the provided key.

See also:
#setBoolean()

Parameters

key: String Identifies the boolean to be retrieved


getDictionary (): Dictionary

Create a Loom friendly dictionary from this JSON object. Note that if there are any JSON Objects or JSON Arrays nested within this object then thay will not be recursively converted.

can contain Numbers, Strings, Booleans, JSON Arrays, and JSON Objects. Will return NULL if this JSON object is empty or is a JSON Array.


Returns

Dictionary A Loom friendly dictionary that contains all the data of this JSON object. This dicionary

getError (): String

native

Retrieves a string representation of the last error thrown.



getFloat ( key: String ): Number

native

For a JSON Object, retrieves the Number associated with the provided key. Alias of getNumber so it converts integers to floats instead of returning 0.

See also:
#setFloat()

Parameters

key: String Identifies the number to be retrieved


getFullTypeName (): String
Inherited from Object

native

Gets the fully qualified type name of the Object. The fully qualified type name includes the package of the type.


Returns

String fully qualified type name of the Object.

getInteger ( key: String ): Number

native

For a JSON Object, retrieves the 32-bit Integer value associated with the provided key.

Note: LoomScript does not support 64-bit integers natively, but longer integers can be retrieved as Strings via getLongLongAsString().

See also:
#getLongLongAsString()

Parameters

key: String Identifies the number to be retrieved


getJSONType (): JSONType

native

Retrieves the type of the current JSON Object.

See also:
system.JSONType


Returns

JSONType An enumerated type value

getLongLongAsString ( key: String ): String

native

For a JSON Object, retrieves a String representation of the 64-bit Integer value associated with the provided key.

LoomScript does not support 64-bit integers natively.

Parameters

key: String Identifies the number in the Object to be retrieved (as a String)


getNumber ( key: String ): Number

native

For a JSON Object, retrieves the number value (64-bit Float or 32-bit Integer) associated with the provided key.

See also:
#setFloat()

Parameters

key: String Identifies the number to be retrieved


getObject ( key: String ): JSON

native

For a JSON Object, retrieves the JSON Object value associated with the provided key.

The object returned is a JSON Object (map of key-value pairs).

See also:
#getObjectJSONType()
#getObjectFirstKey()
#getObjectNextKey()
#getBoolean()
#getInteger()
#getLongLongAsString()
#getFloat()
#getString()
#getArray()
#getObject()

Parameters

key: String Identifies the object to be retrieved


getObjectFirstKey (): String

native

For a JSON Object, retrieves the name of the first key in the property list.



getObjectJSONType ( key: String ): JSONType

native

For a JSON Object, retrieves the type of the value associated with the provided key.

Parameters

key: String Identifies the item in the Object to be queried


getObjectNextKey ( key: String ): String

native

For a JSON Object, retrieves the name of the key immediately following the provided key in the property list.

Parameters

key: String

Returns

String The next key, or null if no further keys exist.

getString ( key: String ): String

native

For a JSON Object, retrieves the String value associated with the provided key.

See also:
#setString()

Parameters

key: String Identifies the string to be retrieved


getType (): Type
Inherited from Object

native

Gets the Type that describes the Object.


Returns

Type The Type that describes the object.

getTypeName (): String
Inherited from Object

native

Gets the type name of the Object.


Returns

String type name of the Object.

getValue ( key: String ): Object

General function that gets the value of the Key provided

Parameters

key: String The key to be queried

Returns

Object May return null, a Boolean, a String, a Number, a JSON Object, or a JSON Array

getVector (): Vector

Create a Loom friendly vector from this JSON array. Note that if there are any JSON Objects or JSON Arrays nested within this array then thay will not be recursively converted.

can contain Numbers, Strings, Booleans, JSON Arrays, and JSON Objects. Will return NULL if this JSON arrat is empty or is a JSON object.


Returns

Vector A Loom friendly vector that contains all the data of this JSON array. This vector

initArray (): Boolean

native

Initialize the JSON instance as an empty JSON array, clearing all the previously held values.


Returns

Boolean Returns true if creation was successful.

initObject (): Boolean

native

Initialize the JSON instance as an empty JSON object, clearing all the previously held values.


Returns

Boolean Returns true if creation was successful.

isArray (): Boolean

native

Indicates whether the JSON Object is considered an Array.


Returns

Boolean true if the JSON object represents an Array, false otherwise

isObject (): Boolean

native

Indicates whether the JSON Object is considered an Object.


Returns

Boolean true if the JSON object represents an object, false otherwise

loadString ( json: String ): Boolean

native

Loads a JSON-formatted string into memory. Required before getters can be called.

If parsing fails, the getError() method will return the error message.

The serialize() method will convert the in-memory data back to a JSON string.

See also:
#getError()
#serialize()

Parameters

json: String A JSON formatted String

Returns

Boolean true if the JSON string was parsed successfully, false if there was an error.

parse ( json: String ): JSON

static

Convenience function will parse a JSON string and return a JSON object. This is functionally the same as creating initializing a new JSON Loom object and loading a string into it, but this function will assert "JSON failed to load" if the call to loadString fails.

Parameters

json: String The JSON string to be parsed.

Returns

JSON The JSON object parsed from the string.

serialize (): String

native

Serializes the in-memory data structure to a JSON formatted String.

See also:
#loadString()


Returns

String A JSON formatted String that can be parsed with loadString()

setArray ( key: String , value: JSON ): Void

native

For a JSON Object, associates a JSON Array value with the provided key.

This value can be later retrieved with getArray(key).

See also:
#getArray()

Parameters

key: String Identifier for the array
value: JSON JSON Array to be associated with the key


setArrayArray ( index: Number , value: JSON ): Void

native

For a JSON Array, associates a JSON Array value with the provided index.

This value can be later retrieved with getArrayArray(index).

See also:
#getArrayArray()

Parameters

index: Number Array position to receive the JSON Array
value: JSON JSON Array to be set at the index


setArrayBoolean ( index: Number , value: Boolean ): Void

native

For a JSON Array, associates a Boolean value with the provided index.

This value can be later retrieved with getArrayBoolean(index).

See also:
#getArrayBoolean()

Parameters

index: Number Array position to receive the Boolean
value: Boolean Boolean to be set at the index


setArrayDictionary ( index: Number , d: Dictionary.<String, Object> ): Void

Sets the value of the array index to the JSON object created from the provided Dictionary using fromDictionary.

Parameters

index: Number
d: Dictionary.<String, Object> The Dictionary from which to construct the JSON object.


setArrayFloat ( index: Number , value: Number ): Void

native

For a JSON Array, associates a 64-bit Float value with the provided index.

This value can be later retrieved with getArrayFloat(index).

See also:
#getArrayFloat()

Parameters

index: Number Array position to receive the Float
value: Number Float to be set at the index


setArrayInteger ( index: Number , value: Number ): Void

native

For a JSON Array, associates a 32-bit Integer value with the provided index.

This value can be later retrieved with getArrayInteger(index).

See also:
#getArrayInteger()

Parameters

index: Number Array position to receive the Integer
value: Number Integer to be set at the index


setArrayNumber ( index: Number , value: Number ): Void

native

For a JSON Array, associates a Number (64-bit Float) value with the provided index.

This value can be later retrieved with getArrayNumber(index).

See also:
#getArrayNumber()

Parameters

index: Number Array position to receive the Number
value: Number Number to be set at the index


setArrayObject ( index: Number , value: JSON ): Void

native

For a JSON Array, associates a JSON Object value with the provided index.

This value can be later retrieved with getArrayObject(index).

See also:
#getArrayObject()

Parameters

index: Number Array position to receive the JSON Object
value: JSON JSON Object to be set at the index


setArrayString ( index: Number , value: String ): Void

native

For a JSON Array, associates a String value with the provided index.

This value can be later retrieved with getArrayString(index).

See also:
#getArrayString()

Parameters

index: Number Array position to receive the String
value: String String to be set at the index


setArrayValue ( index: Number , o: Object ): Void

General function that sets the value of the array index based on the type of object. Allowed datatypes are system.Boolean, system.Number, system.String, system.Vector, and system.Dictionary.

Parameters

index: Number The array index to set on the JSON array.
o: Object The value to set on the JSON array.


setArrayVector ( index: Number , v: Vector.<Object> ): Void

Sets the value of the array index to the JSON array created from the provided Vector using fromVector.

Parameters

index: Number The array index to set the JSON array on.
v: Vector.<Object> The Vector from which to construct the JSON array.


setBoolean ( key: String , value: Boolean ): Void

native

For a JSON Object, associates a Boolean value with the provided key.

This value can be later retrieved with getBoolean(key).

See also:
#getBoolean()

Parameters

key: String Identifier for the boolean
value: Boolean Boolean to be associated with the key


setDictionary ( key: String , d: Dictionary.<String, Object> ): Void

Sets the value of the key to the JSON object created from the provided Dictionary using fromDictionary.

Parameters

key: String The key to set the JSON object on.
d: Dictionary.<String, Object> The Dictionary from which to construct the JSON object.


setFloat ( key: String , value: Number ): Void

native

For a JSON Object, associates a 64-bit Float value with the provided key.

This value can be later retrieved with getFloat(key).

See also:
#getFloat()

Parameters

key: String Identifier for the float
value: Number Float to be associated with the key


setInteger ( key: String , value: Number ): Void

native

For a JSON Object, associates an Integer value with the provided key.

This value can be later retrieved with getInteger(key).

See also:
#getInteger()

Parameters

key: String Identifier for the integer
value: Number Integer to be associated with the key


setNumber ( key: String , value: Number ): Void

native

For a JSON Object, associates a 64-bit Float value with the provided key.

This value can be later retrieved with getNumber(key).

See also:
#getNumber()

Parameters

key: String Identifier for the Number
value: Number Number to be associated with the key


setObject ( key: String , value: JSON ): Void

native

For a JSON Object, associates a JSON Object value with the provided key.

This value can be later retrieved with getObject(key).

See also:
#getObject()

Parameters

key: String Identifier for the object
value: JSON JSON Object to be associated with the key


setString ( key: String , value: String ): Void

native

For a JSON Object, associates a String value with the provided key.

This value can be later retrieved with getString(key).

See also:
#getString()

Parameters

key: String Identifier for the string
value: String String to be associated with the key


setValue ( key: String , o: Object ): Void

General function that sets the value of the key based on the type of object. Allowed datatypes are system.Boolean, system.Number, system.String, system.Vector, and system.Dictionary.

Parameters

key: String The key name to set the value on.
o: Object The value to set on the JSON object.


setVector ( key: String , v: Vector.<Object> ): Void

Sets the value of key to the JSON array created from the provided Vector using fromVector.

Parameters

key: String The key to set the JSON array on.
v: Vector.<Object> The Vector from which to construct the JSON array.


stringify ( o: Object , visited: Vector.<Object> = null ): String

static

Traverses through the object's fields and builds a JSON string from the hierarchy. If a dictionary is the object, or is included as part of the object, it must use a data type that can be converted into a string as its key identifier.

recursive looping.

Parameters

o: Object The object from which a JSON string will be made.
visited: Vector.<Object> = null This object is not intended to be used. It is used internally to prevent eternal

Returns

String The JSON tree string built from the fields of the object.

toString (): String
Inherited from Object

native

Returns a String that describes the Object. This can be overriden to provide extra details when printing objects using trace().


Returns

String String that described the Object.

: