JSON Class
Properties Methods Events Config Settings Errors
The JSON class can be used to parse and write JSON documents.
Class Name
IPWorks_JSON
Procedural Interface
ipworks_json_open(); ipworks_json_close($res); ipworks_json_register_callback($res, $id, $function); ipworks_json_get_last_error($res); ipworks_json_get_last_error_code($res); ipworks_json_set($res, $id, $index, $value); ipworks_json_get($res, $id, $index); ipworks_json_do_config($res, $configurationstring); ipworks_json_do_endarray($res); ipworks_json_do_endobject($res); ipworks_json_do_flush($res); ipworks_json_do_hasxpath($res, $xpath); ipworks_json_do_insertproperty($res, $name, $value, $valuetype, $position); ipworks_json_do_insertvalue($res, $value, $valuetype, $position); ipworks_json_do_parse($res); ipworks_json_do_putname($res, $name); ipworks_json_do_putproperty($res, $name, $value, $valuetype); ipworks_json_do_putraw($res, $text); ipworks_json_do_putvalue($res, $value, $valuetype); ipworks_json_do_remove($res); ipworks_json_do_reset($res); ipworks_json_do_save($res); ipworks_json_do_setname($res, $name); ipworks_json_do_setvalue($res, $value, $valuetype); ipworks_json_do_startarray($res); ipworks_json_do_startobject($res); ipworks_json_do_tryxpath($res, $xpath);
Remarks
The JSON class offers a fast and simple way to parse and write information in JSON documents.
Parsing JSON
The JSON class parses JSON documents and verifies that they are well-formed. The results are provided through a set of events.
In addition, the document structure may be queried through an XPath mechanism that supports a subset of the XPath and JSONPath specification.
The parser is optimized for read applications, with a very fast engine that builds internal DOM structures with close to zero heap allocations. Additionally, BuildDOM can be set to False, which reduces the overhead of creating the DOM and offers a fast forward-only parsing implementation that fires events to provide the parsed data.
When parsing a document, events will fire to provide information about the parsed data. After Parse returns the document, it may be navigated by setting XPath if BuildDOM is True (default). If BuildDOM is False, parsed data are accessible only through the events.
The following events will fire during parsing:
If BuildDOM is True (default), XPath may be set after this method returns. XPath may be set to navigate to specific elements within the JSON document. This will be the path to a specified value within the document. Because arrays in JSON only contain values, and no associated object name, an empty name will be used for these values. To reach an array element at position 1, the path must be set to "[1]". In addition, a root element named "json" will be added to each JSON document in the parser.
BuildDOM must be set to True before parsing the document for the XPath functionality to be available.
The XPath property accepts both XPath and JSONPath formats. Please review the following notes on both formats.
XPath
The path is a series of one or more element accessors separated by '/'. The path can be absolute (starting with '/') or relative to the current XPath location.
The following are possible values for an element accessor:
'name' | A particular element name. |
[i] | The i-th subelement of the current element. |
.. | the parent of the current element. |
BuildDOM must be set to True before parsing the document for the XPath functionality to be available.
Simple JSON Document
{ "firstlevel": { "one": "value", "two": ["first", "second"], "three": "value three" } }Example 1. Setting XPath:
Document root | JsonControl.XPath = "/" |
Specific Element | JsonControl.XPath = "/json/firstlevel/one/" |
i-th Child | JsonControl.XPath = "/json/firstlevel/two/[i]/" |
JSONPath
This property implements a subset of the JSONPath notation. This may be set to point to a specific element in the JSON document.The JSONPath is a series of one or more accessors in either dot-notation
$.store.book[0].titleor in bracket-notation, as follows:
$['store']['book'][0]['title']
After setting XPath, the following properties are populated:
- XChildren
- XElement
- XElementType
- XSubTree
- XText
Given the following JSON document:
{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, }The following code shows several examples.
Get the first book's author:
json.XPath = "$.store.book[0].author";
Console.WriteLine(json.XText);
//Output
//"Nigel Rees"
Select the first book and inspect the children:
json.XPath = "$.store.book[0]";
Console.WriteLine("Child Count: " + json.XChildren.Count);
Console.WriteLine(json.XChildren[1].Name + ": " + json.XChildren[1].XText);
//Output
//Child Count: 4
//author: "Nigel Rees"
Get the price of the second book:
json.XPath = "$['store']['book'][1]['price']";
Console.WriteLine(json.XText);
//Output
//12.99
Get the second to last book's author:
json.XPath = "$['store']['book'][last() - 1]['author']";
Console.WriteLine(json.XText);
Console.WriteLine(json.XPath); //Note that "last() - 1" is resolved to "3".
//Output
//"Herman Melville"
//$['store']['book'][3]['author']
Display the full subtree at the current path:
json.XPath = "$.store.book[0]";
Console.WriteLine(json.XSubTree);
//Output
// {
// "category": "reference",
// "author": "Nigel Rees",
// "title": "Sayings of the Century",
// "price": 8.95
// }
Input Properties
The class will determine the source of the input based on which properties are set.
The order in which the input properties are checked is as follows:
When a valid source is found, the search stops.If parsing multiple documents, call Reset between documents to reset the parser.
Writing JSON
The JSON class also can be used to create a JSON document.
The document is written to the selected output property. In addition, as the document is written, the JSON event will fire. The Text event parameter contains the part of the document currently being written.
Output Properties
The class will determine the destination of the output based on which properties are set.
The order in which the output properties are checked is as follows:
- OutputFile
- OutputData: The output data are written to this property if no other destination is specified.
Example. Writing a JSON Document:
Writing a simple JSON document describing a pet:
Json json = new Json();
json.OutputFile = "C:\\temp\\fido.json";
json.StartObject();
json.PutProperty("name", "fido", 2);
json.PutName("previousOwners");
json.StartArray();
json.PutValue("Steve Widgetson", 2);
json.PutValue("Wanda Widgetson", 2);
json.PutValue("Randy Cooper", 2);
json.PutValue("Linda Glover", 2);
json.EndArray();
json.PutProperty("weightUnit", "lbs", 2);
json.PutProperty("weight", "62", 3);
json.EndObject();
json.Flush();
This example results in the following JSON:
{ "name": "fido", "previousOwners": [ "Steve Widgetson", "Wanda Widgetson", "Randy Cooper", "Linda Glover" ], "weightUnit": "lbs", "weight": 62 }
When writing multiple documents, call Reset between documents to reset the writer.
Modifying JSON
The JSON class also allows for modifying existing JSON documents. After loading a JSON document with Parse the document may be edited. The class supports inserting new values, renaming or overwriting existing values, and removing values. After editing is complete, call Save to output the updated JSON document.
The following methods are applicable when modifying a JSON document:
When Save is called, the modified JSON is written to the specified output location.
Output Properties
The class will determine the destination of the output based on which properties are set.
The order in which the output properties are checked is as follows:
- OutputFile
- OutputData: The output data are written to this property if no other destination is specified.
Example 1. Inserting New Values:
To insert new values in a JSON document, first load the existing document with Parse. Next set XPath to the sibling or parent of the data to be inserted. Call InsertProperty or InsertValue and pass the ValueType and Position parameters to indicate the type of data being inserted and the position.
The ValueType parameter of these methods specifies the type of the value. Possible values are as follows:
- 0 (Object)
- 1 (Array)
- 2 (String)
- 3 (Number)
- 4 (Bool)
- 5 (Null)
- 6 (Raw)
The Position parameter of these methods specifies the position of Value. Possible values are as follows:
- 0 (Before the current element)
- 1 (After the current element)
- 2 (The first child of the current element)
- 3 (The last child of the current element)
For example:
Given the following JSON:
{ "store": { "books": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", } ] } }
Insert a new property "price" for each book:
json.XPath = "/json/store/books/[1]";
json.InsertProperty("price", "8.95", 3, 3); //3 - Number, 3 - Last Child
json.XPath = "/json/store/books/[2]";
json.InsertProperty("price", "12.99", 3, 3); //3 - Number, 3 - Last Child
json.Save();
Produces the JSON:
{ "store": { "books": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 } ] } }
To add a new book to the array:
json.XPath = "/json/store/books";
json.InsertValue("", 0, 3); //0 - Object, 3 - Last Child
json.XPath = "/json/store/books/[3]";
json.InsertProperty("category", "fiction", 2, 3); //2 - String, 3 - Last Child
json.InsertProperty("author", "Herman Melville", 2, 3); //2 - String, 3 - Last Child
json.InsertProperty("title", "Moby Dick", 2, 3); //2 - String, 3 - Last Child
json.InsertProperty("price", "8.99", 3, 3); //3 - Number, 3 - Last Child
json.Save();
Produces the JSON:
{ "store": { "books": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "price": 8.99 } ] } }
To add a new array property to each book:
json.XPath = "/json/store/books/[1]";
json.InsertProperty("tags", "", 1, 2); //1 - Array, 2 - First Child
json.XPath = "/json/store/books/[1]/tags";
json.InsertValue("quotes", 2, 3); //2 - String, 3 - Last Child
json.InsertValue("british", 2, 3); //2 - String, 3 - Last Child
json.XPath = "/json/store/books/[2]";
json.InsertProperty("tags", "", 1, 2); //1 - Array, 2 - First Child
json.XPath = "/json/store/books/[2]/tags";
json.InsertValue("trilogy", 2, 3); //2 - String, 3 - Last Child
json.InsertValue("war", 2, 3); //2 - String, 3 - Last Child
json.XPath = "/json/store/books/[3]";
json.InsertProperty("tags", "", 1, 2); //1 - Array, 2 - First Child
json.XPath = "/json/store/books/[3]/tags";
json.InsertValue("classic", 2, 3); //2 - String, 3 - Last Child
json.InsertValue("whales", 2, 3); //2 - String, 3 - Last Child
json.Save();
Produces the JSON:
{ "store": { "books": [ { "tags": ["quotes", "british"], "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "tags": ["trilogy", "war"], "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "tags": ["classic", "whales"], "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "price": 8.99 } ] } }
Example 2. Removing Values:
To remove existing values, set XPath and call the Remove method. Continuing with example 1, to remove
the first book:
json.XPath = "/json/store/books/[1]";
json.Remove();
json.Save();
Produces the JSON:
{ "store": { "books": [ { "tags": ["trilogy", "war"], "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "tags": ["classic", "whales"], "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "price": 8.99 } ] } }
To remove the "category" properties from each book:
json.XPath = "/json/store/books/[1]/category";
json.Remove();
json.XPath = "/json/store/books/[2]/category";
json.Remove();
json.Save();
Produces the JSON:
{ "store": { "books": [ { "tags": ["trilogy", "war"], "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "tags": ["classic", "whales"], "author": "Herman Melville", "title": "Moby Dick", "price": 8.99 } ] } }
Example 3. Updating Existing Names and Values:
The SetName and SetValue methods may be used to modify existing names and values. Continuing with the preceding JSON in example 2, to rename "tags" to "meta" and update values within the array and prices:
//Rename "tags" to "meta" for 1st book
json.XPath = "/json/store/books/[1]/tags";
json.SetName("meta");
//Update Price
json.XPath = "/json/store/books/[1]/price";
json.SetValue("13.99", 3); //3 - Number
//Rename "tags" to "meta" for 2nd book
json.XPath = "/json/store/books/[2]/tags";
json.SetName("meta");
//Update tag "whales" to "revenge"
json.XPath = "/json/store/books/[2]/meta/[2]";
json.SetValue("revenge", 2); //2 - String
//Update Price
json.XPath = "/json/store/books/[2]/price";
json.SetValue("9.99", 3); //3 - Number
json.Save();
Produces the JSON:
{ "store": { "books": [ { "meta": ["trilogy", "war"], "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 13.99 }, { "meta": ["classic", "revenge"], "author": "Herman Melville", "title": "Moby Dick", "price": 9.99 } ] } }
Property List
The following is the full list of the properties of the class with short descriptions. Click on the links for further details.
BuildDOM | When True, an internal object model of the JSON document is created. |
InputData | This property includes the JSON data to parse. |
InputFile | This property specifies the file to process. |
OutputData | This property includes the output JSON after processing. |
OutputFile | This is the path to a local file where the output will be written. |
Overwrite | This property indicates whether or not the class should overwrite files. |
Validate | This property controls whether documents are validated during parsing. |
XChildCount | The number of records in the XChild arrays. |
XChildElementType | The ElementType property indicates the data type of the element. |
XChildName | The Name property provides the name of the element. |
XChildXText | This property contains the text of the element. |
XElement | This property includes the name of the current element. |
XElementType | This property indicates the data type of the current element. |
XErrorPath | This property includes an XPath to check the server response for errors. |
XParent | The parent of the current element. |
XPath | This property provides a way to point to a specific element in the response. |
XSubTree | This property includes a snapshot of the current element in the document. |
XText | This property includes the text of the current element. |
Method List
The following is the full list of the methods of the class with short descriptions. Click on the links for further details.
Config | Sets or retrieves a configuration setting. |
EndArray | This method writes the closing bracket of a JSON array. |
EndObject | This method writes the closing brace of a JSON object. |
Flush | This method flushes the parser's or writer's buffers. |
HasXPath | This method determines whether a specific element exists in the document. |
InsertProperty | This method inserts the specified name and value at the selected position. |
InsertValue | This method inserts the specified value at the selected position. |
Parse | This method parses the specified JSON data. |
PutName | This method writes the name of a property. |
PutProperty | This method writes a property and value. |
PutRaw | This method writes a raw JSON fragment. |
PutValue | This method writes a value of a property. |
Remove | This method removes the element or value set in XPath. |
Reset | This method resets the class. |
Save | This method saves the modified JSON document. |
SetName | This method sets a new name for the element specified by XPath. |
SetValue | This method sets a new value for the element specified by XPath. |
StartArray | This method writes the opening bracket of a JSON array. |
StartObject | This event writes the opening brace of a JSON object. |
TryXPath | This method navigates to the specified XPath if it exists. |
Event List
The following is the full list of the events fired by the class with short descriptions. Click on the links for further details.
Characters | This event is fired for plaintext segments of the input stream. |
EndDocument | This event fires when the end of a JSON document is encountered. |
EndElement | This event is fired when an end-element tag is encountered. |
Error | Fired when information is available about errors during data delivery. |
IgnorableWhitespace | This event is fired when a section of ignorable whitespace is encountered. |
JSON | This event fires with the JSON data being written. |
StartDocument | This event fires when the start of a new JSON document is encountered. |
StartElement | This event is fired when a new element is encountered in the document. |
Config Settings
The following is a list of config settings for the class with short descriptions. Click on the links for further details.
CacheContent | If true, the original JSON is stored internally in a buffer. |
CloseInputStreamAfterProcess | Determines whether or not the input stream is closed after processing. |
CloseOutputStreamAfterProcess | Determines whether or not the output stream is closed after processing. |
ElementXPath | The XPath value for the current element in the document. |
EscapeForwardSlashes | Whether to escape forward slashes when writing a JSON object. |
InputFormat | Specifies the input format used in JSON streaming. |
PrettyPrint | Determines whether output is on one line or "pretty printed". |
RecordEndDelimiter | The character sequence after the end of a JSON document. |
RecordStartDelimiter | The character sequence before the start of a JSON document. |
StringProcessingOptions | Defines options to use when processing string values. |
XPathNotation | Specifies the expected format when setting XPath. |
BuildInfo | Information about the product's build. |
CodePage | The system code page used for Unicode to Multibyte translations. |
LicenseInfo | Information about the current license. |
MaskSensitiveData | Whether sensitive data is masked in log messages. |
ProcessIdleEvents | Whether the class uses its internal event loop to process events when the main thread is idle. |
SelectWaitMillis | The length of time in milliseconds the class will wait when DoEvents is called if there are no events to process. |
UseInternalSecurityAPI | Whether or not to use the system security libraries or an internal implementation. |
BuildDOM Property (IPWorks_JSON Class)
When True, an internal object model of the JSON document is created.
Object Oriented Interface
public function getBuildDOM(); public function setBuildDOM($value);
Procedural Interface
ipworks_json_get($res, 1 ); ipworks_json_set($res, 1, $value );
Default Value
true
Remarks
Set this property to True when you need to browse the current document through XPath.
Data Type
Boolean
InputData Property (IPWorks_JSON Class)
This property includes the JSON data to parse.
Object Oriented Interface
public function getInputData(); public function setInputData($value);
Procedural Interface
ipworks_json_get($res, 2 ); ipworks_json_set($res, 2, $value );
Default Value
''
Remarks
This property specifies the JSON to be processed. Set this property before calling Parse.
This may be set to a complete JSON document, or partial data. When setting partial data, call Parse after each chunk of data is set. For instance:
//Parse the following in chunks: { "data": 1}
json.InputData = "{ \"data\""
json.Parse();
json.InputData = ": 1}"
json.Parse();
Input Properties
The class will determine the source of the input based on which properties are set.
The order in which the input properties are checked is as follows:
- InputFile
- InputData
Data Type
String
InputFile Property (IPWorks_JSON Class)
This property specifies the file to process.
Object Oriented Interface
public function getInputFile(); public function setInputFile($value);
Procedural Interface
ipworks_json_get($res, 3 ); ipworks_json_set($res, 3, $value );
Default Value
''
Remarks
This property specifies the file to be processed. Set this property to the full or relative path to the file that will be processed.
After setting this property, call Parse to parse the document.
Input Properties
The class will determine the source of the input based on which properties are set.
The order in which the input properties are checked is as follows:
- InputFile
- InputData
Data Type
String
OutputData Property (IPWorks_JSON Class)
This property includes the output JSON after processing.
Object Oriented Interface
public function getOutputData(); public function setOutputData($value);
Procedural Interface
ipworks_json_get($res, 4 ); ipworks_json_set($res, 4, $value );
Default Value
''
Remarks
This property contains the resultant JSON after processing.
Output Properties
The class will determine the destination of the output based on which properties are set.
The order in which the output properties are checked is as follows:
- OutputFile
- OutputData: The output data are written to this property if no other destination is specified.
Data Type
String
OutputFile Property (IPWorks_JSON Class)
This is the path to a local file where the output will be written.
Object Oriented Interface
public function getOutputFile(); public function setOutputFile($value);
Procedural Interface
ipworks_json_get($res, 5 ); ipworks_json_set($res, 5, $value );
Default Value
''
Remarks
This property specifies the file to which the output will be written. This may be set to an absolute or relative path.
Output Properties
The class will determine the destination of the output based on which properties are set.
The order in which the output properties are checked is as follows:
- OutputFile
- OutputData: The output data are written to this property if no other destination is specified.
Data Type
String
Overwrite Property (IPWorks_JSON Class)
This property indicates whether or not the class should overwrite files.
Object Oriented Interface
public function getOverwrite(); public function setOverwrite($value);
Procedural Interface
ipworks_json_get($res, 6 ); ipworks_json_set($res, 6, $value );
Default Value
false
Remarks
This property indicates whether or not the class will overwrite OutputFile. If Overwrite is False, an error will be thrown whenever OutputFile exists before an operation. The default value is False.
Data Type
Boolean
Validate Property (IPWorks_JSON Class)
This property controls whether documents are validated during parsing.
Object Oriented Interface
public function getValidate(); public function setValidate($value);
Procedural Interface
ipworks_json_get($res, 7 ); ipworks_json_set($res, 7, $value );
Default Value
true
Remarks
When true (default), the document will be validated during parsing. To disable validation set Validate to false. Disabling validation may be useful in cases in which data can still be parsed even if the document is not well formed.
Data Type
Boolean
XChildCount Property (IPWorks_JSON Class)
The number of records in the XChild arrays.
Object Oriented Interface
public function getXChildCount(); public function setXChildCount($value);
Procedural Interface
ipworks_json_get($res, 8 ); ipworks_json_set($res, 8, $value );
Default Value
0
Remarks
This property controls the size of the following arrays:
The array indices start at 0 and end at XChildCount - 1.This property is not available at design time.
Data Type
Integer
XChildElementType Property (IPWorks_JSON Class)
The ElementType property indicates the data type of the element.
Object Oriented Interface
public function getXChildElementType($xchildindex);
Procedural Interface
ipworks_json_get($res, 9 , $xchildindex);
Default Value
0
Remarks
The XChildElementType property indicates the data type of the element.
Possible values are as follows:
- 0 (Object)
- 1 (Array)
- 2 (String)
- 3 (Number)
- 4 (Bool)
- 5 (Null)
- 6 (Raw)
The $xchildindex parameter specifies the index of the item in the array. The size of the array is controlled by the XChildCount property.
This property is read-only and not available at design time.
Data Type
Integer
XChildName Property (IPWorks_JSON Class)
The Name property provides the name of the element.
Object Oriented Interface
public function getXChildName($xchildindex);
Procedural Interface
ipworks_json_get($res, 10 , $xchildindex);
Default Value
''
Remarks
The XChildName property provides the name of the element. For elements within an array, the XChildName property will be empty.
The $xchildindex parameter specifies the index of the item in the array. The size of the array is controlled by the XChildCount property.
This property is read-only and not available at design time.
Data Type
String
XChildXText Property (IPWorks_JSON Class)
This property contains the text of the element.
Object Oriented Interface
public function getXChildXText($xchildindex);
Procedural Interface
ipworks_json_get($res, 11 , $xchildindex);
Default Value
''
Remarks
This property contains the text of the element.
The $xchildindex parameter specifies the index of the item in the array. The size of the array is controlled by the XChildCount property.
This property is read-only and not available at design time.
Data Type
String
XElement Property (IPWorks_JSON Class)
This property includes the name of the current element.
Object Oriented Interface
public function getXElement(); public function setXElement($value);
Procedural Interface
ipworks_json_get($res, 12 ); ipworks_json_set($res, 12, $value );
Default Value
''
Remarks
This property contains the name of the current element. The current element is specified through the XPath property.
Data Type
String
XElementType Property (IPWorks_JSON Class)
This property indicates the data type of the current element.
Object Oriented Interface
public function getXElementType();
Procedural Interface
ipworks_json_get($res, 13 );
Default Value
0
Remarks
This property specifies the data type of the current element. After setting XPath, this property is populated. Possible values are as follows:
- 0 (Object)
- 1 (Array)
- 2 (String)
- 3 (Number)
- 4 (Bool)
- 5 (Null)
- 6 (Raw)
Note: This property is not applicable when parsing a document and BuildDOM is False.
This property is read-only.
Data Type
Integer
XErrorPath Property (IPWorks_JSON Class)
This property includes an XPath to check the server response for errors.
Object Oriented Interface
public function getXErrorPath(); public function setXErrorPath($value);
Procedural Interface
ipworks_json_get($res, 14 ); ipworks_json_set($res, 14, $value );
Default Value
''
Remarks
This property contains an XPath to check the server response for errors. If the XPath exists, an exception will be thrown containing the value of the element at the path.
Data Type
String
XParent Property (IPWorks_JSON Class)
The parent of the current element.
Object Oriented Interface
public function getXParent();
Procedural Interface
ipworks_json_get($res, 15 );
Default Value
''
Remarks
This property contains the parent of the current element. The current element is specified via the XPath property.
This property is read-only.
Data Type
String
XPath Property (IPWorks_JSON Class)
This property provides a way to point to a specific element in the response.
Object Oriented Interface
public function getXPath(); public function setXPath($value);
Procedural Interface
ipworks_json_get($res, 16 ); ipworks_json_set($res, 16, $value );
Default Value
''
Remarks
XPath may be set to navigate to specific elements within the JSON document. This will be the path to a specified value within the document. Because arrays in JSON only contain values, and no associated object name, an empty name will be used for these values. To reach an array element at position 1, the path must be set to "[1]". In addition, a root element named "json" will be added to each JSON document in the parser.
BuildDOM must be set to True before parsing the document for the XPath functionality to be available.
The XPath property accepts both XPath and JSONPath formats. Please review the following notes on both formats.
XPath
The path is a series of one or more element accessors separated by '/'. The path can be absolute (starting with '/') or relative to the current XPath location.
The following are possible values for an element accessor:
'name' | A particular element name. |
[i] | The i-th subelement of the current element. |
.. | the parent of the current element. |
BuildDOM must be set to True before parsing the document for the XPath functionality to be available.
Simple JSON Document
{ "firstlevel": { "one": "value", "two": ["first", "second"], "three": "value three" } }Example 1. Setting XPath:
Document root | JsonControl.XPath = "/" |
Specific Element | JsonControl.XPath = "/json/firstlevel/one/" |
i-th Child | JsonControl.XPath = "/json/firstlevel/two/[i]/" |
JSONPath
This property implements a subset of the JSONPath notation. This may be set to point to a specific element in the JSON document.The JSONPath is a series of one or more accessors in either dot-notation
$.store.book[0].titleor in bracket-notation, as follows:
$['store']['book'][0]['title']
After setting XPath, the following properties are populated:
- XChildren
- XElement
- XElementType
- XSubTree
- XText
Given the following JSON document:
{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, }The following code shows several examples.
Get the first book's author:
json.XPath = "$.store.book[0].author";
Console.WriteLine(json.XText);
//Output
//"Nigel Rees"
Select the first book and inspect the children:
json.XPath = "$.store.book[0]";
Console.WriteLine("Child Count: " + json.XChildren.Count);
Console.WriteLine(json.XChildren[1].Name + ": " + json.XChildren[1].XText);
//Output
//Child Count: 4
//author: "Nigel Rees"
Get the price of the second book:
json.XPath = "$['store']['book'][1]['price']";
Console.WriteLine(json.XText);
//Output
//12.99
Get the second to last book's author:
json.XPath = "$['store']['book'][last() - 1]['author']";
Console.WriteLine(json.XText);
Console.WriteLine(json.XPath); //Note that "last() - 1" is resolved to "3".
//Output
//"Herman Melville"
//$['store']['book'][3]['author']
Display the full subtree at the current path:
json.XPath = "$.store.book[0]";
Console.WriteLine(json.XSubTree);
//Output
// {
// "category": "reference",
// "author": "Nigel Rees",
// "title": "Sayings of the Century",
// "price": 8.95
// }
Data Type
String
XSubTree Property (IPWorks_JSON Class)
This property includes a snapshot of the current element in the document.
Object Oriented Interface
public function getXSubTree();
Procedural Interface
ipworks_json_get($res, 17 );
Default Value
''
Remarks
The current element is specified through this property. For this property to work, you must have the CacheContent set to True.
This property is read-only.
Data Type
String
XText Property (IPWorks_JSON Class)
This property includes the text of the current element.
Object Oriented Interface
public function getXText(); public function setXText($value);
Procedural Interface
ipworks_json_get($res, 18 ); ipworks_json_set($res, 18, $value );
Default Value
''
Remarks
This property contains the text of the current element. The current element is specified through the XPath property.
Data Type
String
Config Method (IPWorks_JSON Class)
Sets or retrieves a configuration setting.
Object Oriented Interface
public function doConfig($configurationstring);
Procedural Interface
ipworks_json_do_config($res, $configurationstring);
Remarks
Config is a generic method available in every class. It is used to set and retrieve configuration settings for the class.
These settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the class, access to these internal properties is provided through the Config method.
To set a configuration setting named PROPERTY, you must call Config("PROPERTY=VALUE"), where VALUE is the value of the setting expressed as a string. For boolean values, use the strings "True", "False", "0", "1", "Yes", or "No" (case does not matter).
To read (query) the value of a configuration setting, you must call Config("PROPERTY"). The value will be returned as a string.
EndArray Method (IPWorks_JSON Class)
This method writes the closing bracket of a JSON array.
Object Oriented Interface
public function doEndArray();
Procedural Interface
ipworks_json_do_endarray($res);
Remarks
This method writes the closing bracket of a JSON array to the output. An array must already have been opened by calling StartArray.
EndObject Method (IPWorks_JSON Class)
This method writes the closing brace of a JSON object.
Object Oriented Interface
public function doEndObject();
Procedural Interface
ipworks_json_do_endobject($res);
Remarks
This method writes the closing brace of a JSON object. An object must have been started previously by calling StartObject.
Flush Method (IPWorks_JSON Class)
This method flushes the parser's or writer's buffers.
Object Oriented Interface
public function doFlush();
Procedural Interface
ipworks_json_do_flush($res);
Remarks
When Flush is called, the component flushes all of its buffers, firing events as necessary.
When parsing, the end state of the JSON is checked. If Validate is also True, the parser verifies that all open elements were closed, returning an error if not.
When writing, the resultant JSON is available in one of the output properties.
Output Properties
The class will determine the destination of the output based on which properties are set.
The order in which the output properties are checked is as follows:
- OutputFile
- OutputData: The output data are written to this property if no other destination is specified.
HasXPath Method (IPWorks_JSON Class)
This method determines whether a specific element exists in the document.
Object Oriented Interface
public function doHasXPath($xpath);
Procedural Interface
ipworks_json_do_hasxpath($res, $xpath);
Remarks
This method determines whether a particular XPath exists within the document. This may be used to check if a path exists before setting it through XPath.
This method returns True if the xpath exists, and False if not.
See XPath for details on the XPath syntax.
InsertProperty Method (IPWorks_JSON Class)
This method inserts the specified name and value at the selected position.
Object Oriented Interface
public function doInsertProperty($name, $value, $valuetype, $position);
Procedural Interface
ipworks_json_do_insertproperty($res, $name, $value, $valuetype, $position);
Remarks
This method inserts a property and its corresponding value relative to the element specified by XPath. Before calling this method, a valid JSON document must first be loaded by calling Parse.
The Name parameter specifies the name of the property.
The Value parameter specifies the value of the property.
The ValueType parameter specifies the type of the value. Possible values are as follows:
- 0 (Object)
- 1 (Array)
- 2 (String)
- 3 (Number)
- 4 (Bool)
- 5 (Null)
- 6 (Raw)
The Position parameter specifies the position of Value relative to the element specified by XPath. Possible values are as follows:
- 0 (Before the current element)
- 1 (After the current element)
- 2 (The first child of the current element)
- 3 (The last child of the current element)
See Save for details.
InsertValue Method (IPWorks_JSON Class)
This method inserts the specified value at the selected position.
Object Oriented Interface
public function doInsertValue($value, $valuetype, $position);
Procedural Interface
ipworks_json_do_insertvalue($res, $value, $valuetype, $position);
Remarks
This method inserts a value relative to the element specified by XPath. Before calling this method, a valid JSON document must first be loaded by calling Parse.
The Value parameter specifies the value of the property.
The ValueType parameter specifies the type of the value. Possible values are as follows:
- 0 (Object)
- 1 (Array)
- 2 (String)
- 3 (Number)
- 4 (Bool)
- 5 (Null)
- 6 (Raw)
The Position parameter specifies the position of Value relative to the element specified by XPath. Possible values are as follows:
- 0 (Before the current element)
- 1 (After the current element)
- 2 (The first child of the current element)
- 3 (The last child of the current element)
See Save for details.
Parse Method (IPWorks_JSON Class)
This method parses the specified JSON data.
Object Oriented Interface
public function doParse();
Procedural Interface
ipworks_json_do_parse($res);
Remarks
This method parses the specified JSON data.
When parsing a document, events will fire to provide information about the parsed data. After Parse returns the document, it may be navigated by setting XPath if BuildDOM is True (default). If BuildDOM is False, parsed data are accessible only through the events.
The following events will fire during parsing:
If BuildDOM is True (default), XPath may be set after this method returns. XPath may be set to navigate to specific elements within the JSON document. This will be the path to a specified value within the document. Because arrays in JSON only contain values, and no associated object name, an empty name will be used for these values. To reach an array element at position 1, the path must be set to "[1]". In addition, a root element named "json" will be added to each JSON document in the parser.
BuildDOM must be set to True before parsing the document for the XPath functionality to be available.
The XPath property accepts both XPath and JSONPath formats. Please review the following notes on both formats.
XPath
The path is a series of one or more element accessors separated by '/'. The path can be absolute (starting with '/') or relative to the current XPath location.
The following are possible values for an element accessor:
'name' | A particular element name. |
[i] | The i-th subelement of the current element. |
.. | the parent of the current element. |
BuildDOM must be set to True before parsing the document for the XPath functionality to be available.
Simple JSON Document
{ "firstlevel": { "one": "value", "two": ["first", "second"], "three": "value three" } }Example 1. Setting XPath:
Document root | JsonControl.XPath = "/" |
Specific Element | JsonControl.XPath = "/json/firstlevel/one/" |
i-th Child | JsonControl.XPath = "/json/firstlevel/two/[i]/" |
JSONPath
This property implements a subset of the JSONPath notation. This may be set to point to a specific element in the JSON document.The JSONPath is a series of one or more accessors in either dot-notation
$.store.book[0].titleor in bracket-notation, as follows:
$['store']['book'][0]['title']
After setting XPath, the following properties are populated:
- XChildren
- XElement
- XElementType
- XSubTree
- XText
Given the following JSON document:
{ "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, }The following code shows several examples.
Get the first book's author:
json.XPath = "$.store.book[0].author";
Console.WriteLine(json.XText);
//Output
//"Nigel Rees"
Select the first book and inspect the children:
json.XPath = "$.store.book[0]";
Console.WriteLine("Child Count: " + json.XChildren.Count);
Console.WriteLine(json.XChildren[1].Name + ": " + json.XChildren[1].XText);
//Output
//Child Count: 4
//author: "Nigel Rees"
Get the price of the second book:
json.XPath = "$['store']['book'][1]['price']";
Console.WriteLine(json.XText);
//Output
//12.99
Get the second to last book's author:
json.XPath = "$['store']['book'][last() - 1]['author']";
Console.WriteLine(json.XText);
Console.WriteLine(json.XPath); //Note that "last() - 1" is resolved to "3".
//Output
//"Herman Melville"
//$['store']['book'][3]['author']
Display the full subtree at the current path:
json.XPath = "$.store.book[0]";
Console.WriteLine(json.XSubTree);
//Output
// {
// "category": "reference",
// "author": "Nigel Rees",
// "title": "Sayings of the Century",
// "price": 8.95
// }
Input Properties
The class will determine the source of the input based on which properties are set.
The order in which the input properties are checked is as follows:
When a valid source is found, the search stops.If parsing multiple documents, call Reset between documents to reset the parser.
PutName Method (IPWorks_JSON Class)
This method writes the name of a property.
Object Oriented Interface
public function doPutName($name);
Procedural Interface
ipworks_json_do_putname($res, $name);
Remarks
This method writes the name of a property. The Name parameter specifies the value to write.
PutProperty Method (IPWorks_JSON Class)
This method writes a property and value.
Object Oriented Interface
public function doPutProperty($name, $value, $valuetype);
Procedural Interface
ipworks_json_do_putproperty($res, $name, $value, $valuetype);
Remarks
This method writes a property and its corresponding value to the output.
The Name parameter specifies the name of the property.
The Value parameter specifies the value of the property.
The ValueType parameter specifies the type of the value. Possible values are as follows:
- 0 (Object)
- 1 (Array)
- 2 (String)
- 3 (Number)
- 4 (Bool)
- 5 (Null)
- 6 (Raw)
PutRaw Method (IPWorks_JSON Class)
This method writes a raw JSON fragment.
Object Oriented Interface
public function doPutRaw($text);
Procedural Interface
ipworks_json_do_putraw($res, $text);
Remarks
This method writes raw data to the output. This may be used to write any data of any format directly to the output.
PutValue Method (IPWorks_JSON Class)
This method writes a value of a property.
Object Oriented Interface
public function doPutValue($value, $valuetype);
Procedural Interface
ipworks_json_do_putvalue($res, $value, $valuetype);
Remarks
This method writes the value of a property to the output. The Value parameter specifies the value. The ValueType parameter specifies the type of data. Possible values are as follows:
- 0 (Object)
- 1 (Array)
- 2 (String)
- 3 (Number)
- 4 (Bool)
- 5 (Null)
- 6 (Raw)
Remove Method (IPWorks_JSON Class)
This method removes the element or value set in XPath.
Object Oriented Interface
public function doRemove();
Procedural Interface
ipworks_json_do_remove($res);
Remarks
This method removes the current object at the specified XPath. This is used when editing previously loaded JSON documents.
See Save for details.
Reset Method (IPWorks_JSON Class)
This method resets the class.
Object Oriented Interface
public function doReset();
Procedural Interface
ipworks_json_do_reset($res);
Remarks
This method resets the JSON parser.
Save Method (IPWorks_JSON Class)
This method saves the modified JSON document.
Object Oriented Interface
public function doSave();
Procedural Interface
ipworks_json_do_save($res);
Remarks
This method saves the modified JSON data. This is used after editing a previously loaded JSON document.
After loading a JSON document with Parse the document may be edited. The class supports inserting new values, renaming or overwriting existing values, and removing values. After editing is complete, call Save to output the updated JSON document.
The following methods are applicable when modifying a JSON document:
When Save is called, the modified JSON is written to the specified output location.
Output Properties
The class will determine the destination of the output based on which properties are set.
The order in which the output properties are checked is as follows:
- OutputFile
- OutputData: The output data are written to this property if no other destination is specified.
Example 1. Inserting New Values:
To insert new values in a JSON document, first load the existing document with Parse. Next set XPath to the sibling or parent of the data to be inserted. Call InsertProperty or InsertValue and pass the ValueType and Position parameters to indicate the type of data being inserted and the position.
The ValueType parameter of these methods specifies the type of the value. Possible values are as follows:
- 0 (Object)
- 1 (Array)
- 2 (String)
- 3 (Number)
- 4 (Bool)
- 5 (Null)
- 6 (Raw)
The Position parameter of these methods specifies the position of Value. Possible values are as follows:
- 0 (Before the current element)
- 1 (After the current element)
- 2 (The first child of the current element)
- 3 (The last child of the current element)
For example:
Given the following JSON:
{ "store": { "books": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", } ] } }
Insert a new property "price" for each book:
json.XPath = "/json/store/books/[1]";
json.InsertProperty("price", "8.95", 3, 3); //3 - Number, 3 - Last Child
json.XPath = "/json/store/books/[2]";
json.InsertProperty("price", "12.99", 3, 3); //3 - Number, 3 - Last Child
json.Save();
Produces the JSON:
{ "store": { "books": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 } ] } }
To add a new book to the array:
json.XPath = "/json/store/books";
json.InsertValue("", 0, 3); //0 - Object, 3 - Last Child
json.XPath = "/json/store/books/[3]";
json.InsertProperty("category", "fiction", 2, 3); //2 - String, 3 - Last Child
json.InsertProperty("author", "Herman Melville", 2, 3); //2 - String, 3 - Last Child
json.InsertProperty("title", "Moby Dick", 2, 3); //2 - String, 3 - Last Child
json.InsertProperty("price", "8.99", 3, 3); //3 - Number, 3 - Last Child
json.Save();
Produces the JSON:
{ "store": { "books": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "price": 8.99 } ] } }
To add a new array property to each book:
json.XPath = "/json/store/books/[1]";
json.InsertProperty("tags", "", 1, 2); //1 - Array, 2 - First Child
json.XPath = "/json/store/books/[1]/tags";
json.InsertValue("quotes", 2, 3); //2 - String, 3 - Last Child
json.InsertValue("british", 2, 3); //2 - String, 3 - Last Child
json.XPath = "/json/store/books/[2]";
json.InsertProperty("tags", "", 1, 2); //1 - Array, 2 - First Child
json.XPath = "/json/store/books/[2]/tags";
json.InsertValue("trilogy", 2, 3); //2 - String, 3 - Last Child
json.InsertValue("war", 2, 3); //2 - String, 3 - Last Child
json.XPath = "/json/store/books/[3]";
json.InsertProperty("tags", "", 1, 2); //1 - Array, 2 - First Child
json.XPath = "/json/store/books/[3]/tags";
json.InsertValue("classic", 2, 3); //2 - String, 3 - Last Child
json.InsertValue("whales", 2, 3); //2 - String, 3 - Last Child
json.Save();
Produces the JSON:
{ "store": { "books": [ { "tags": ["quotes", "british"], "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "tags": ["trilogy", "war"], "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "tags": ["classic", "whales"], "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "price": 8.99 } ] } }
Example 2. Removing Values:
To remove existing values, set XPath and call the Remove method. Continuing with example 1, to remove
the first book:
json.XPath = "/json/store/books/[1]";
json.Remove();
json.Save();
Produces the JSON:
{ "store": { "books": [ { "tags": ["trilogy", "war"], "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "tags": ["classic", "whales"], "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "price": 8.99 } ] } }
To remove the "category" properties from each book:
json.XPath = "/json/store/books/[1]/category";
json.Remove();
json.XPath = "/json/store/books/[2]/category";
json.Remove();
json.Save();
Produces the JSON:
{ "store": { "books": [ { "tags": ["trilogy", "war"], "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "tags": ["classic", "whales"], "author": "Herman Melville", "title": "Moby Dick", "price": 8.99 } ] } }
Example 3. Updating Existing Names and Values:
The SetName and SetValue methods may be used to modify existing names and values. Continuing with the preceding JSON in example 2, to rename "tags" to "meta" and update values within the array and prices:
//Rename "tags" to "meta" for 1st book
json.XPath = "/json/store/books/[1]/tags";
json.SetName("meta");
//Update Price
json.XPath = "/json/store/books/[1]/price";
json.SetValue("13.99", 3); //3 - Number
//Rename "tags" to "meta" for 2nd book
json.XPath = "/json/store/books/[2]/tags";
json.SetName("meta");
//Update tag "whales" to "revenge"
json.XPath = "/json/store/books/[2]/meta/[2]";
json.SetValue("revenge", 2); //2 - String
//Update Price
json.XPath = "/json/store/books/[2]/price";
json.SetValue("9.99", 3); //3 - Number
json.Save();
Produces the JSON:
{ "store": { "books": [ { "meta": ["trilogy", "war"], "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 13.99 }, { "meta": ["classic", "revenge"], "author": "Herman Melville", "title": "Moby Dick", "price": 9.99 } ] } }
SetName Method (IPWorks_JSON Class)
This method sets a new name for the element specified by XPath.
Object Oriented Interface
public function doSetName($name);
Procedural Interface
ipworks_json_do_setname($res, $name);
Remarks
This method sets a new name for the element specified in XPath. This is used to modify an existing JSON document.
The Name parameter specifies the new name of the element.
See Save for details.
SetValue Method (IPWorks_JSON Class)
This method sets a new value for the element specified by XPath.
Object Oriented Interface
public function doSetValue($value, $valuetype);
Procedural Interface
ipworks_json_do_setvalue($res, $value, $valuetype);
Remarks
This method sets a new value for the element specified in XPath. This is used to modify an existing JSON document.
Value specifies the new value.
ValueType specifies the type of the value. Possible values are as follows:
- 0 (Object)
- 1 (Array)
- 2 (String)
- 3 (Number)
- 4 (Bool)
- 5 (Null)
- 6 (Raw)
See Save for details.
StartArray Method (IPWorks_JSON Class)
This method writes the opening bracket of a JSON array.
Object Oriented Interface
public function doStartArray();
Procedural Interface
ipworks_json_do_startarray($res);
Remarks
This method writes the opening bracket of a JSON array to the output. To close the array, call EndArray.
StartObject Method (IPWorks_JSON Class)
This event writes the opening brace of a JSON object.
Object Oriented Interface
public function doStartObject();
Procedural Interface
ipworks_json_do_startobject($res);
Remarks
This method writes the opening brace of a JSON object to the output. To close the object, call EndObject.
TryXPath Method (IPWorks_JSON Class)
This method navigates to the specified XPath if it exists.
Object Oriented Interface
public function doTryXPath($xpath);
Procedural Interface
ipworks_json_do_tryxpath($res, $xpath);
Remarks
This method will attempt to navigate to the specified XPath parameter if it exists within the document.
If the XPath exists, the XPath property will be updated and this method returns True.
If the XPath does not exist, the XPath property is not updated and this method returns False.
Characters Event (IPWorks_JSON Class)
This event is fired for plaintext segments of the input stream.
Object Oriented Interface
public function fireCharacters($param);
Procedural Interface
ipworks_json_register_callback($res, 1, array($this, 'fireCharacters'));
Parameter List
'text'
Remarks
The Characters event provides the plaintext content of the JSON document (i.e., the text inside the elements). The text is provided through the Text parameter.
The text includes white space as well as end-of-line characters, except for ignorable whitespace, which is fired through the IgnorableWhitespace event.
EndDocument Event (IPWorks_JSON Class)
This event fires when the end of a JSON document is encountered.
Object Oriented Interface
public function fireEndDocument($param);
Procedural Interface
ipworks_json_register_callback($res, 2, array($this, 'fireEndDocument'));
Parameter List
Remarks
This event fires when parsing of a JSON document ends. This event may fire multiple times if InputFormat is set to a value that accepts multiple JSON documents.
EndElement Event (IPWorks_JSON Class)
This event is fired when an end-element tag is encountered.
Object Oriented Interface
public function fireEndElement($param);
Procedural Interface
ipworks_json_register_callback($res, 3, array($this, 'fireEndElement'));
Parameter List
'element'
Remarks
The EndElement event is fired when the end of an element is found in the document.
The element name is provided by the Element parameter.
Error Event (IPWorks_JSON Class)
Fired when information is available about errors during data delivery.
Object Oriented Interface
public function fireError($param);
Procedural Interface
ipworks_json_register_callback($res, 4, array($this, 'fireError'));
Parameter List
'errorcode'
'description'
Remarks
The Error event is fired in case of exceptional conditions during message processing. Normally the class fails with an error.
The ErrorCode parameter contains an error code, and the Description parameter contains a textual description of the error. For a list of valid error codes and their descriptions, please refer to the Error Codes section.
IgnorableWhitespace Event (IPWorks_JSON Class)
This event is fired when a section of ignorable whitespace is encountered.
Object Oriented Interface
public function fireIgnorableWhitespace($param);
Procedural Interface
ipworks_json_register_callback($res, 5, array($this, 'fireIgnorableWhitespace'));
Parameter List
'text'
Remarks
The ignorable whitespace section is provided by the Text parameter.
JSON Event (IPWorks_JSON Class)
This event fires with the JSON data being written.
Object Oriented Interface
public function fireJSON($param);
Procedural Interface
ipworks_json_register_callback($res, 6, array($this, 'fireJSON'));
Parameter List
'text'
Remarks
This event fires when output data are written.
Text contains the JSON data currently being written.
StartDocument Event (IPWorks_JSON Class)
This event fires when the start of a new JSON document is encountered.
Object Oriented Interface
public function fireStartDocument($param);
Procedural Interface
ipworks_json_register_callback($res, 7, array($this, 'fireStartDocument'));
Parameter List
Remarks
This event fires when parsing of a JSON document begins. This event may fire multiple times if InputFormat is set to a value that accepts multiple JSON documents.
StartElement Event (IPWorks_JSON Class)
This event is fired when a new element is encountered in the document.
Object Oriented Interface
public function fireStartElement($param);
Procedural Interface
ipworks_json_register_callback($res, 8, array($this, 'fireStartElement'));
Parameter List
'element'
Remarks
The StartElement event is fired when a new element is found in the document.
The element name is provided through the Element parameter.
Config Settings (JSON Class)
The class accepts one or more of the following configuration settings. Configuration settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the class, access to these internal properties is provided through the Config method.JSON Config Settings
string elementXPath = json.Config("ElementXPath");
Note: The BuildDOM property must be set to False.
Value | Description |
0 (None - default) | Only a single JSON document is expected. Use this when a single JSON document is being parsed (most cases). |
1 (Line Delimited) | Multiple documents are separated by carriage return (CR), line feed (LF), or CRLF character sequences. |
2 (Record Separated) | A defined start and end delimiter separate documents. See RecordStartDelimiter and RecordEndDelimiter. |
3 (Concatenated) | New documents begin immediately after the previous documents end; no characters or delimiters separate the documents. |
json.Config("PrettyPrint=true"); // false
json.StartObject();
json.PutName("data");
json.StartObject();
json.PutProperty("id", "3", 3);
json.PutProperty("first_name", "Emma", 2);
json.PutProperty("last_name", "Wong", 2);
json.PutProperty("avatar", "https://s3.amazonaws.com/uifaces/faces/twitter/olegpogodaev/128.jpg", 2);
json.EndObject();
json.EndObject();
json.Flush();
Console.WriteLine(json.OutputData);
With PrettyPrint set to False (the default), the output would look like this:
{"data":{"id":3,"first_name":"Emma","last_name":"Wong","avatar":"https:\/\/s3.amazonaws.com\/uifaces\/faces\/twitter\/olegpogodaev\/128.jpg"}}With PrettyPrint set to True, the output instead would look like this:
{ "data": { "id": 3, "first_name": "Emma", "last_name": "Wong", "avatar": "https:\/\/s3.amazonaws.com\/uifaces\/faces\/twitter\/olegpogodaev\/128.jpg" } }The default value is False.
0 (none - default) | No additional processing is performed. |
1 (unquote) | Strings are unquoted. |
2 (unescape) | Any escaped sequences are unescaped. |
3 (unquote and unescape) | Values are both unquoted and unescaped. |
"example" : "value\ntest"The following table shows the resulting value for the XText of the element:
StringProcessingOption | Output |
0 (none) | "value\ntest" |
1 (unquote) | value\ntest |
2 (unescape) | "value test" |
3 (unquote and unescape) | value test |
- 0 (Auto - default)
- 1 (XPath)
- 2 (JSONPath)
Base Config Settings
The following is a list of valid code page identifiers:
Identifier | Name |
037 | IBM EBCDIC - U.S./Canada |
437 | OEM - United States |
500 | IBM EBCDIC - International |
708 | Arabic - ASMO 708 |
709 | Arabic - ASMO 449+, BCON V4 |
710 | Arabic - Transparent Arabic |
720 | Arabic - Transparent ASMO |
737 | OEM - Greek (formerly 437G) |
775 | OEM - Baltic |
850 | OEM - Multilingual Latin I |
852 | OEM - Latin II |
855 | OEM - Cyrillic (primarily Russian) |
857 | OEM - Turkish |
858 | OEM - Multilingual Latin I + Euro symbol |
860 | OEM - Portuguese |
861 | OEM - Icelandic |
862 | OEM - Hebrew |
863 | OEM - Canadian-French |
864 | OEM - Arabic |
865 | OEM - Nordic |
866 | OEM - Russian |
869 | OEM - Modern Greek |
870 | IBM EBCDIC - Multilingual/ROECE (Latin-2) |
874 | ANSI/OEM - Thai (same as 28605, ISO 8859-15) |
875 | IBM EBCDIC - Modern Greek |
932 | ANSI/OEM - Japanese, Shift-JIS |
936 | ANSI/OEM - Simplified Chinese (PRC, Singapore) |
949 | ANSI/OEM - Korean (Unified Hangul Code) |
950 | ANSI/OEM - Traditional Chinese (Taiwan; Hong Kong SAR, PRC) |
1026 | IBM EBCDIC - Turkish (Latin-5) |
1047 | IBM EBCDIC - Latin 1/Open System |
1140 | IBM EBCDIC - U.S./Canada (037 + Euro symbol) |
1141 | IBM EBCDIC - Germany (20273 + Euro symbol) |
1142 | IBM EBCDIC - Denmark/Norway (20277 + Euro symbol) |
1143 | IBM EBCDIC - Finland/Sweden (20278 + Euro symbol) |
1144 | IBM EBCDIC - Italy (20280 + Euro symbol) |
1145 | IBM EBCDIC - Latin America/Spain (20284 + Euro symbol) |
1146 | IBM EBCDIC - United Kingdom (20285 + Euro symbol) |
1147 | IBM EBCDIC - France (20297 + Euro symbol) |
1148 | IBM EBCDIC - International (500 + Euro symbol) |
1149 | IBM EBCDIC - Icelandic (20871 + Euro symbol) |
1200 | Unicode UCS-2 Little-Endian (BMP of ISO 10646) |
1201 | Unicode UCS-2 Big-Endian |
1250 | ANSI - Central European |
1251 | ANSI - Cyrillic |
1252 | ANSI - Latin I |
1253 | ANSI - Greek |
1254 | ANSI - Turkish |
1255 | ANSI - Hebrew |
1256 | ANSI - Arabic |
1257 | ANSI - Baltic |
1258 | ANSI/OEM - Vietnamese |
1361 | Korean (Johab) |
10000 | MAC - Roman |
10001 | MAC - Japanese |
10002 | MAC - Traditional Chinese (Big5) |
10003 | MAC - Korean |
10004 | MAC - Arabic |
10005 | MAC - Hebrew |
10006 | MAC - Greek I |
10007 | MAC - Cyrillic |
10008 | MAC - Simplified Chinese (GB 2312) |
10010 | MAC - Romania |
10017 | MAC - Ukraine |
10021 | MAC - Thai |
10029 | MAC - Latin II |
10079 | MAC - Icelandic |
10081 | MAC - Turkish |
10082 | MAC - Croatia |
12000 | Unicode UCS-4 Little-Endian |
12001 | Unicode UCS-4 Big-Endian |
20000 | CNS - Taiwan |
20001 | TCA - Taiwan |
20002 | Eten - Taiwan |
20003 | IBM5550 - Taiwan |
20004 | TeleText - Taiwan |
20005 | Wang - Taiwan |
20105 | IA5 IRV International Alphabet No. 5 (7-bit) |
20106 | IA5 German (7-bit) |
20107 | IA5 Swedish (7-bit) |
20108 | IA5 Norwegian (7-bit) |
20127 | US-ASCII (7-bit) |
20261 | T.61 |
20269 | ISO 6937 Non-Spacing Accent |
20273 | IBM EBCDIC - Germany |
20277 | IBM EBCDIC - Denmark/Norway |
20278 | IBM EBCDIC - Finland/Sweden |
20280 | IBM EBCDIC - Italy |
20284 | IBM EBCDIC - Latin America/Spain |
20285 | IBM EBCDIC - United Kingdom |
20290 | IBM EBCDIC - Japanese Katakana Extended |
20297 | IBM EBCDIC - France |
20420 | IBM EBCDIC - Arabic |
20423 | IBM EBCDIC - Greek |
20424 | IBM EBCDIC - Hebrew |
20833 | IBM EBCDIC - Korean Extended |
20838 | IBM EBCDIC - Thai |
20866 | Russian - KOI8-R |
20871 | IBM EBCDIC - Icelandic |
20880 | IBM EBCDIC - Cyrillic (Russian) |
20905 | IBM EBCDIC - Turkish |
20924 | IBM EBCDIC - Latin-1/Open System (1047 + Euro symbol) |
20932 | JIS X 0208-1990 & 0121-1990 |
20936 | Simplified Chinese (GB2312) |
21025 | IBM EBCDIC - Cyrillic (Serbian, Bulgarian) |
21027 | Extended Alpha Lowercase |
21866 | Ukrainian (KOI8-U) |
28591 | ISO 8859-1 Latin I |
28592 | ISO 8859-2 Central Europe |
28593 | ISO 8859-3 Latin 3 |
28594 | ISO 8859-4 Baltic |
28595 | ISO 8859-5 Cyrillic |
28596 | ISO 8859-6 Arabic |
28597 | ISO 8859-7 Greek |
28598 | ISO 8859-8 Hebrew |
28599 | ISO 8859-9 Latin 5 |
28605 | ISO 8859-15 Latin 9 |
29001 | Europa 3 |
38598 | ISO 8859-8 Hebrew |
50220 | ISO 2022 Japanese with no halfwidth Katakana |
50221 | ISO 2022 Japanese with halfwidth Katakana |
50222 | ISO 2022 Japanese JIS X 0201-1989 |
50225 | ISO 2022 Korean |
50227 | ISO 2022 Simplified Chinese |
50229 | ISO 2022 Traditional Chinese |
50930 | Japanese (Katakana) Extended |
50931 | US/Canada and Japanese |
50933 | Korean Extended and Korean |
50935 | Simplified Chinese Extended and Simplified Chinese |
50936 | Simplified Chinese |
50937 | US/Canada and Traditional Chinese |
50939 | Japanese (Latin) Extended and Japanese |
51932 | EUC - Japanese |
51936 | EUC - Simplified Chinese |
51949 | EUC - Korean |
51950 | EUC - Traditional Chinese |
52936 | HZ-GB2312 Simplified Chinese |
54936 | Windows XP: GB18030 Simplified Chinese (4 Byte) |
57002 | ISCII Devanagari |
57003 | ISCII Bengali |
57004 | ISCII Tamil |
57005 | ISCII Telugu |
57006 | ISCII Assamese |
57007 | ISCII Oriya |
57008 | ISCII Kannada |
57009 | ISCII Malayalam |
57010 | ISCII Gujarati |
57011 | ISCII Punjabi |
65000 | Unicode UTF-7 |
65001 | Unicode UTF-8 |
Identifier | Name |
1 | ASCII |
2 | NEXTSTEP |
3 | JapaneseEUC |
4 | UTF8 |
5 | ISOLatin1 |
6 | Symbol |
7 | NonLossyASCII |
8 | ShiftJIS |
9 | ISOLatin2 |
10 | Unicode |
11 | WindowsCP1251 |
12 | WindowsCP1252 |
13 | WindowsCP1253 |
14 | WindowsCP1254 |
15 | WindowsCP1250 |
21 | ISO2022JP |
30 | MacOSRoman |
10 | UTF16String |
0x90000100 | UTF16BigEndian |
0x94000100 | UTF16LittleEndian |
0x8c000100 | UTF32String |
0x98000100 | UTF32BigEndian |
0x9c000100 | UTF32LittleEndian |
65536 | Proprietary |
- Product: The product the license is for.
- Product Key: The key the license was generated from.
- License Source: Where the license was found (e.g., RuntimeLicense, License File).
- License Type: The type of license installed (e.g., Royalty Free, Single Server).
- Last Valid Build: The last valid build number for which the license will work.
This setting only works on these classes: AS3Receiver, AS3Sender, Atom, Client(3DS), FTP, FTPServer, IMAP, OFTPClient, SSHClient, SCP, Server(3DS), Sexec, SFTP, SFTPServer, SSHServer, TCPClient, TCPServer.
Setting this configuration setting to true tells the class to use the internal implementation instead of using the system security libraries.
On Windows, this setting is set to false by default. On Linux/macOS, this setting is set to true by default.
To use the system security libraries for Linux, OpenSSL support must be enabled. For more information on how to enable OpenSSL, please refer to the OpenSSL Notes section.
Trappable Errors (JSON Class)
JSON Errors
10231 | Unbalanced element tag. |
10232 | Invalid JSON markup. |
10233 | Invalid XPath. |
10234 | DOM tree unavailable (set BuildDOM to True and reparse). |
XML Errors
101 | Invalid attribute index. |
102 | No attributes available. |
103 | Invalid namespace index. |
104 | No namespaces available. |
105 | Invalid element index. |
106 | No elements available. |
107 | Attribute does not exist. |
201 | Unbalanced element tag. |
202 | Unknown element prefix (cannot find namespace). |
203 | Unknown attribute prefix (cannot find namespace). |
204 | Invalid XML markup. |
205 | Invalid end state for parser. |
206 | Document contains unbalanced elements. |
207 | Invalid XPath. |
208 | No such child. |
209 | Top element does not match start of path. |
210 | DOM tree unavailable (set BuildDOM to True and reparse). |
302 | Cannot open file. |
401 | Invalid XML would be generated. |
402 | An invalid XML name has been specified. |