Difference between revisions of "Libdjson"

From Makers Local 256
Jump to: navigation, search
m (Documentation: do some documentation conversion...I may employ vim for the rest of this)
(Documentation: remove junk html tags and do some reformatting)
Line 86: Line 86:
 
::<b>Returns:</b> The casted reference or <b>null</b> on a failed cast.
 
::<b>Returns:</b> The casted reference or <b>null</b> on a failed cast.
  
</dd>
 
<dt><big>abstract JSONString <u>toJSONString</u>();
 
  
</big></dt>
+
<big>abstract JSONString <u>toJSONString</u>();</big>
<dd>Convenience function for casting to JSONString.
+
Convenience function for casting to JSONString.
<br><br>
+
<b>Returns:</b> The casted reference or <b>null</b> on a failed cast.
<b>Returns:</b><br>
+
The casted reference or <b>null</b> on a failed cast.<br><br>
+
  
</dd>
+
<big>abstract JSONBoolean <u>toJSONBoolean</u>();</big>
<dt><big>abstract JSONBoolean <u>toJSONBoolean</u>();
+
Convenience function for casting to JSONBoolean.
</big></dt>
+
<b>Returns:</b> The casted reference or <b>null</b> on a failed cast.
<dd>Convenience function for casting to JSONBoolean.
+
  
<br><br>
+
<big>abstract JSONNumber <u>toJSONNumber</u>();</big>
<b>Returns:</b><br>
+
Convenience function for casting to JSONNumber.
The casted reference or <b>null</b> on a failed cast.<br><br>
+
<b>Returns:</b> The casted reference or <b>null</b> on a failed cast.
  
</dd>
+
<big>abstract JSONNull <u>toJSONNull</u>();</big>
<dt><big>abstract JSONNumber <u>toJSONNumber</u>();
+
Convenience function for casting to JSONNull.
</big></dt>
+
<b>Returns:</b> The casted reference or <b>null</b> on a failed cast.
<dd>Convenience function for casting to JSONNumber.
+
<br><br>
+
<b>Returns:</b><br>
+
  
The casted reference or <b>null</b> on a failed cast.<br><br>
+
<big>abstract JSONType <u>opIndex</u>(char[] <i>key</i>);</big>
 +
Associative array index function for objects describing associative array-like attributes.
 +
<b>Returns:</b> The chosen index or a <b>null</b> reference if the index does not exist.
  
</dd>
+
<big>abstract int <u>opApply</u>(int delegate(char[], JSONType) <i>dg</i>);</big>
<dt><big>abstract JSONNull <u>toJSONNull</u>();
+
Allow foreach over the object with string key.
</big></dt>
+
<dd>Convenience function for casting to JSONNull.
+
<br><br>
+
<b>Returns:</b><br>
+
The casted reference or <b>null</b> on a failed cast.<br><br>
+
  
</dd>
+
<big>abstract int <u>opApply</u>(int delegate(char[], ref JSONType) <i>dg</i>);</big>
<dt><big>abstract JSONType <u>opIndex</u>(char[] <i>key</i>);
+
Allow foreach over the object with string key and ref value.
</big></dt>
+
<dd>Associative array index function for objects describing associative array-like attributes.
+
<br><br>
+
<b>Returns:</b><br>
+
The chosen index or a <b>null</b> reference if the index does not exist.<br><br>
+
  
</dd>
+
<big>abstract JSONType <u>opIndex</u>(int <i>key</i>);</big>
<dt><big>abstract int <u>opApply</u>(int delegate(char[], JSONType) <i>dg</i>);
+
Array index function for objects describing array-like attributes.
</big></dt>
+
<b>Returns:</b> The chosen index or a <b>null</b> reference if the index does not exist.
<dd>Allow foreach over the object with string key.<br><br>
+
  
</dd>
 
<dt><big>abstract int <u>opApply</u>(int delegate(char[], ref JSONType) <i>dg</i>);
 
  
</big></dt>
+
<big>abstract int <u>opApply</u>(int delegate(int, JSONType) <i>dg</i>);</big>
<dd>Allow foreach over the object with string key and ref value.<br><br>
+
Allow foreach over the object with integer key.
  
</dd>
+
<big>abstract int <u>opApply</u>(int delegate(int, ref JSONType) <i>dg</i>);</big>
<dt><big>abstract JSONType <u>opIndex</u>(int <i>key</i>);
+
Allow foreach over the object with integer key and ref value.
</big></dt>
+
<dd>Array index function for objects describing array-like attributes.
+
<br><br>
+
<b>Returns:</b><br>
+
The chosen index or a <b>null</b> reference if the index does not exist.<br><br>
+
  
</dd>
+
<big>abstract int <u>opApply</u>(int delegate(JSONType) <i>dg</i>);</big>
<dt><big>abstract int <u>opApply</u>(int delegate(int, JSONType) <i>dg</i>);
+
Convenience function for iteration that apply to both AA and array type operations
</big></dt>
+
<dd>Allow foreach over the object with integer key.<br><br>
+
  
</dd>
+
<big>abstract int <u>opApply</u>(int delegate(ref JSONType) <i>dg</i>);</big>
<dt><big>abstract int <u>opApply</u>(int delegate(int, ref JSONType) <i>dg</i>);
+
Convenience function for iteration that apply to both AA and array type operations with ref value
  
</big></dt>
 
<dd>Allow foreach over the object with integer key and ref value.<br><br>
 
  
</dd>
+
<big>class <u>JSONObject</u>: libdjson.json.JSONType;</big>
<dt><big>abstract int <u>opApply</u>(int delegate(JSONType) <i>dg</i>);
+
<u>JSONObject</u> represents a single JSON object node and has methods for
</big></dt>
+
<dd>Convenience function for iteration that apply to both AA and array type operations<br><br>
+
 
+
</dd>
+
<dt><big>abstract int <u>opApply</u>(int delegate(ref JSONType) <i>dg</i>);
+
 
+
</big></dt>
+
<dd>Convenience function for iteration that apply to both AA and array type operations with ref value<br><br>
+
 
+
</dd>
+
</dl>
+
</dd>
+
<dt><big>class <u>JSONObject</u>: libdjson.json.JSONType;
+
</big></dt>
+
<dd><u>JSONObject</u> represents a single JSON object node and has methods for
+
 
  adding children.  All methods that make changes modify this
 
  adding children.  All methods that make changes modify this
 
  <u>JSONObject</u> rather than making a copy, unless otherwise noted.  Many methods
 
  <u>JSONObject</u> rather than making a copy, unless otherwise noted.  Many methods
  return a self reference to allow cascaded calls.<br><br>
+
  return a self reference to allow cascaded calls.
 
+
<big>this();</big>
<dl><dt><big>this();
+
Nothing to see here except for the boring constructor, move along.
</big></dt>
+
<dd>Nothing to see here except for the boring constructor, move along.<br><br>
+
 
+
</dd>
+
<dt><big>void <u>opIndexAssign</u>(JSONType <i>type</i>, char[] <i>key</i>);
+
</big></dt>
+
<dd>Operator overload for setting keys in the AA.<br><br>
+
 
+
</dd>
+
<dt><big>JSONType <u>opIndex</u>(char[] <i>key</i>);
+
</big></dt>
+
<dd>Operator overload for accessing values already in the AA.
+
<br><br>
+
<b>Returns:</b><br>
+
The child node if it exists, otherwise <b>null</b>.<br><br>
+
 
+
</dd>
+
<dt><big>int <u>length</u>();
+
</big></dt>
+
<dd>Allow the user to get the number of elements in this object
+
<br><br>
+
<b>Returns:</b><br>
+
The number of child nodes contained within this JSONObject<br><br>
+
 
+
</dd>
+
<dt><big>int <u>opApply</u>(int delegate(JSONType) <i>dg</i>);
+
 
+
</big></dt>
+
<dd>Operator overload for foreach iteration through the object with values only<br><br>
+
 
+
</dd>
+
<dt><big>int <u>opApply</u>(int delegate(ref JSONType) <i>dg</i>);
+
</big></dt>
+
<dd>Operator overload for foreach iteration through the object with values only and allow modification of the reference<br><br>
+
 
+
</dd>
+
<dt><big>int <u>opApply</u>(int delegate(char[], JSONType) <i>dg</i>);
+
 
+
</big></dt>
+
<dd>Operator overload for foreach iteration through the object with key and value<br><br>
+
 
+
</dd>
+
<dt><big>int <u>opApply</u>(int delegate(char[], ref JSONType) <i>dg</i>);
+
</big></dt>
+
<dd>Operator overload for foreach iteration through the object with key and value and allow modification of the reference<br><br>
+
 
+
</dd>
+
<dt><big>char[] <u>toString</u>();
+
 
+
</big></dt>
+
<dd>A method to convert this JSONObject to a user readable format.
+
<br><br>
+
<b>Returns:</b><br>
+
A JSON string representing this object and it's contents.<br><br>
+
 
+
</dd>
+
<dt><big>void <u>parse</u>(ref char[] <i>source</i>);
+
</big></dt>
+
<dd>This function parses a JSONObject out of a string<br><br>
+
 
+
</dd>
+
</dl>
+
</dd>
+
<dt><big>class <u>JSONArray</u>: libdjson.json.JSONType;
+
</big></dt>
+
<dd><u>JSONArray</u> represents a single JSON array, capable of being heterogenous<br><br>
+
 
+
<dl><dt><big>this();
+
</big></dt>
+
<dd>Nothing to see here, move along.<br><br>
+
 
+
</dd>
+
<dt><big>void <u>opCatAssign</u>(JSONType <i>child</i>);
+
</big></dt>
+
<dd>Operator overload to allow addition of children<br><br>
+
 
+
</dd>
+
<dt><big>JSONType <u>opIndex</u>(int <i>key</i>);
+
 
+
</big></dt>
+
<dd>Operator overload to allow access of children
+
<br><br>
+
<b>Returns:</b><br>
+
The child node if it exists, otherwise <b>null</b>.<br><br>
+
 
+
</dd>
+
<dt><big>int <u>length</u>();
+
</big></dt>
+
<dd>Allow the user to get the number of elements in this object
+
<br><br>
+
  
<b>Returns:</b><br>
+
<big>void <u>opIndexAssign</u>(JSONType <i>type</i>, char[] <i>key</i>);</big>
The number of child nodes contained within this JSONObject<br><br>
+
Operator overload for setting keys in the AA.
  
</dd>
+
<big>JSONType <u>opIndex</u>(char[] <i>key</i>);</big>
<dt><big>int <u>opApply</u>(int delegate(JSONType) <i>dg</i>);
+
Operator overload for accessing values already in the AA.
</big></dt>
+
<b>Returns:</b> The child node if it exists, otherwise <b>null</b>.
<dd>Operator overload for foreach iteration through the array with values only<br><br>
+
  
</dd>
+
<big>int <u>length</u>();</big>
<dt><big>int <u>opApply</u>(int delegate(ref JSONType) <i>dg</i>);
+
Allow the user to get the number of elements in this object
 +
<b>Returns:</b> The number of child nodes contained within this JSONObject
  
</big></dt>
+
<big>int <u>opApply</u>(int delegate(JSONType) <i>dg</i>);</big>
<dd>Operator overload for foreach iteration through the array with values only and allow modification of the reference<br><br>
+
Operator overload for foreach iteration through the object with values only
  
</dd>
+
<big>int <u>opApply</u>(int delegate(ref JSONType) <i>dg</i>);</big>
<dt><big>int <u>opApply</u>(int delegate(int, JSONType) <i>dg</i>);
+
Operator overload for foreach iteration through the object with values only and allow modification of the reference
</big></dt>
+
<dd>Operator overload for foreach iteration through the array with key and value<br><br>
+
  
</dd>
+
<big>int <u>opApply</u>(int delegate(char[], JSONType) <i>dg</i>);</big>
<dt><big>int <u>opApply</u>(int delegate(int, ref JSONType) <i>dg</i>);
+
Operator overload for foreach iteration through the object with key and value
  
</big></dt>
+
<big>int <u>opApply</u>(int delegate(char[], ref JSONType) <i>dg</i>);</big>
<dd>Operator overload for foreach iteration through the array with key and value and allow modification of the reference<br><br>
+
Operator overload for foreach iteration through the object with key and value and allow modification of the reference
  
</dd>
+
<big>char[] <u>toString</u>();</big>
<dt><big>char[] <u>toString</u>();
+
A method to convert this JSONObject to a user readable format.
</big></dt>
+
<b>Returns:</b> A JSON string representing this object and it's contents.
<dd>A method to convert this JSONArray to a user readable format.
+
<br><br>
+
<b>Returns:</b><br>
+
A JSON string representing this object and it's contents.<br><br>
+
  
</dd>
+
<big>void <u>parse</u>(ref char[] <i>source</i>);</big>
 +
This function parses a JSONObject out of a string
  
<dt><big>void <u>parse</u>(ref char[] <i>source</i>);
 
</big></dt>
 
<dd>This function parses a JSONArray out of a string<br><br>
 
  
</dd>
+
<big>class <u>JSONArray</u>: libdjson.json.JSONType;</big>
</dl>
+
<u>JSONArray</u> represents a single JSON array, capable of being heterogenous
</dd>
+
<big>this();</big>
<dt><big>class <u>JSONString</u>: libdjson.json.JSONType;
+
Nothing to see here, move along.
</big></dt>
+
<dd><u>JSONString</u> represents a JSON string.  Internal representation is escaped for faster parsing and JSON generation.<br><br>
+
  
<dl><dt><big>this();
+
<big>void <u>opCatAssign</u>(JSONType <i>child</i>);</big>
</big></dt>
+
Operator overload to allow addition of children
<dd>The boring default constructor.<br><br>
+
  
</dd>
+
<big>JSONType <u>opIndex</u>(int <i>key</i>);</big>
<dt><big>this(string <i>data</i>);
+
Operator overload to allow access of children
</big></dt>
+
<b>Returns:</b> The child node if it exists, otherwise <b>null</b>.
<dd>The ever so slightly more interesting initializing constructor.<br><br>
+
  
</dd>
+
<big>int <u>length</u>();</big>
<dt><big>void <u>set</u>(char[] <i>data</i>);
+
Allow the user to get the number of elements in this object
  
</big></dt>
+
<b>Returns:</b>
<dd>Allow the <i>data</i> to be <u>set</u> so the object can be reused.<br><br>
+
The number of child nodes contained within this JSONObject
  
</dd>
+
<big>int <u>opApply</u>(int delegate(JSONType) <i>dg</i>);</big>
<dt><big>char[] <u>get</u>();
+
Operator overload for foreach iteration through the array with values only
</big></dt>
+
<dd>Allow the data to be retreived.<br><br>
+
  
</dd>
+
<big>int <u>opApply</u>(int delegate(ref JSONType) <i>dg</i>);</big>
<dt><big>char[] <u>toString</u>();
+
Operator overload for foreach iteration through the array with values only and allow modification of the reference
</big></dt>
+
<dd>A method to convert this JSONString to a user readable format.
+
<br><br>
+
<b>Returns:</b><br>
+
A JSON string representing this object and it's contents.<br><br>
+
  
</dd>
+
<big>int <u>opApply</u>(int delegate(int, JSONType) <i>dg</i>);</big>
<dt><big>void <u>parse</u>(ref char[] <i>source</i>);
+
Operator overload for foreach iteration through the array with key and value
  
</big></dt>
+
<big>int <u>opApply</u>(int delegate(int, ref JSONType) <i>dg</i>);</big>
<dd>This function parses a JSONArray out of a string and eats characters as it goes, hence the ref string parameter.<br><br>
+
Operator overload for foreach iteration through the array with key and value and allow modification of the reference
  
</dd>
+
<big>char[] <u>toString</u>();</big>
</dl>
+
A method to convert this JSONArray to a user readable format.
</dd>
+
<b>Returns:</b> A JSON string representing this object and it's contents.
<dt><big>class <u>JSONBoolean</u>: libdjson.json.JSONType;
+
</big></dt>
+
<dd><u>JSONBoolean</u> represents a JSON boolean value.<br><br>
+
  
<dl><dt><big>this();
+
<big>void <u>parse</u>(ref char[] <i>source</i>);</big>
 +
This function parses a JSONArray out of a string
  
</big></dt>
 
<dd>The boring constructor, again.<br><br>
 
  
</dd>
+
<big>class <u>JSONString</u>: libdjson.json.JSONType;</big>
<dt><big>this(bool <i>data</i>);
+
<u>JSONString</u> represents a JSON string.  Internal representation is escaped for faster parsing and JSON generation.
</big></dt>
+
<big>this();</big>
<dd>Only a bit of input for this constructor.<br><br>
+
The boring default constructor.
  
</dd>
+
<big>this(string <i>data</i>);</big>
<dt><big>void <u>set</u>(bool <i>data</i>);
+
The ever so slightly more interesting initializing constructor.
  
</big></dt>
+
<big>void <u>set</u>(char[] <i>data</i>);</big>
<dd>Allow setting of the hidden bit.<br><br>
+
Allow the <i>data</i> to be <u>set</u> so the object can be reused.
  
</dd>
+
<big>char[] <u>get</u>();</big>
<dt><big>bool <u>get</u>();
+
Allow the data to be retreived.
</big></dt>
+
<dd>Allow the bit to be retreived.<br><br>
+
  
</dd>
+
<big>char[] <u>toString</u>();</big>
<dt><big>char[] <u>toString</u>();
+
A method to convert this JSONString to a user readable format.
</big></dt>
+
<b>Returns:</b> A JSON string representing this object and it's contents.
  
<dd>A method to convert this JSONBoolean to a user readable format.
+
<big>void <u>parse</u>(ref char[] <i>source</i>);</big>
<br><br>
+
This function parses a JSONArray out of a string and eats characters as it goes, hence the ref string parameter.
<b>Returns:</b><br>
+
A JSON string representing this object and it's contents.<br><br>
+
  
</dd>
 
<dt><big>void <u>parse</u>(ref char[] <i>source</i>);
 
</big></dt>
 
<dd>This function parses a JSONBoolean out of a string and eats characters as it goes, hence the ref string parameter.<br><br>
 
  
</dd>
+
<big>class <u>JSONBoolean</u>: libdjson.json.JSONType;</big>
</dl>
+
<u>JSONBoolean</u> represents a JSON boolean value.
</dd>
+
<big>this();</big>
<dt><big>class <u>JSONNull</u>: libdjson.json.JSONType;
+
The boring constructor, again.
</big></dt>
+
<dd><u>JSONNull</u> represents a JSON <b>null</b> value.<br><br>
+
  
<dl><dt><big>this();
+
<big>this(bool <i>data</i>);</big>
</big></dt>
+
Only a bit of input for this constructor.
  
<dd>You're forced to use the boring constructor here.<br><br>
+
<big>void <u>set</u>(bool <i>data</i>);</big>
 +
Allow setting of the hidden bit.
  
</dd>
+
<big>bool <u>get</u>();</big>
<dt><big>char[] <u>toString</u>();
+
Allow the bit to be retreived.
</big></dt>
+
<dd>A method to convert this JSONNull to a user readable format.
+
<br><br>
+
<b>Returns:</b><br>
+
"<b>null</b>". Always. Forever.<br><br>
+
  
</dd>
+
<big>char[] <u>toString</u>();</big>
<dt><big>void <u>parse</u>(ref char[] <i>source</i>);
+
A method to convert this JSONBoolean to a user readable format.
</big></dt>
+
<b>Returns:</b> A JSON string representing this object and it's contents.
<dd>This function parses a JSONNull out of a string. Really, it just rips "<b>null</b>" off the beginning of the string and eats whitespace.<br><br>
+
  
</dd>
+
<big>void <u>parse</u>(ref char[] <i>source</i>);</big>
</dl>
+
This function parses a JSONBoolean out of a string and eats characters as it goes, hence the ref string parameter.
</dd>
+
<dt><big>class <u>JSONNumber</u>: libdjson.json.JSONType;
+
  
</big></dt>
 
<dd><u>JSONNumber</u> represents any JSON numeric value.<br><br>
 
  
<dl><dt><big>this();
+
<big>class <u>JSONNull</u>: libdjson.json.JSONType;</big>
</big></dt>
+
<u>JSONNull</u> represents a JSON <b>null</b> value.
<dd>Another boring constructor...<br><br>
+
<big>this();</big>
 +
You're forced to use the boring constructor here.
  
</dd>
+
<big>char[] <u>toString</u>();</big>
<dt><big>this(real <i>data</i>);
+
A method to convert this JSONNull to a user readable format.
</big></dt>
+
<b>Returns:</b> "<b>null</b>". Always. Forever.
<dd>...and its slightly less boring sibling.<br><br>
+
  
</dd>
+
<big>void <u>parse</u>(ref char[] <i>source</i>);</big>
<dt><big>void <u>set</u>(real <i>data</i>);
+
This function parses a JSONNull out of a string.  Really, it just rips "<b>null</b>" off the beginning of the string and eats whitespace.
</big></dt>
+
<dd>Allow setting of the hidden number.<br><br>
+
  
</dd>
 
<dt><big>real <u>get</u>();
 
</big></dt>
 
<dd>Allow the number to be retreived.<br><br>
 
  
</dd>
+
<big>class <u>JSONNumber</u>: libdjson.json.JSONType;</big>
<dt><big>char[] <u>toString</u>();
+
<u>JSONNumber</u> represents any JSON numeric value.
</big></dt>
+
<big>this();</big>
<dd>A method to convert this JSONNumber to a user readable format.
+
Another boring constructor...
<br><br>
+
<b>Returns:</b><br>
+
A JSON string representing this number.<br><br>
+
  
</dd>
+
<big>this(real <i>data</i>);</big>
<dt><big>void <u>parse</u>(ref char[] <i>source</i>);
+
...and its slightly less boring sibling.
  
</big></dt>
+
<big>void <u>set</u>(real <i>data</i>);</big>
<dd>This function parses a JSONNumber out of a string and eats characters as it goes, hence the ref string parameter.<br><br>
+
Allow setting of the hidden number.
  
</dd>
+
<big>real <u>get</u>();</big>
</dl>
+
Allow the number to be retreived.
</dd>
+
<dt><big>char[] <u>JSONEncode</u>(char[] <i>src</i>);
+
</big></dt>
+
<dd>Perform JSON escapes on a string
+
<br><br>
+
<b>Returns:</b><br>
+
  
A JSON encoded string<br><br>
+
<big>char[] <u>toString</u>();</big>
 +
A method to convert this JSONNumber to a user readable format.
 +
<b>Returns:</b> A JSON string representing this number.
  
</dd>
+
<big>void <u>parse</u>(ref char[] <i>source</i>);</big>
<dt><big>char[] <u>JSONDecode</u>(char[] <i>src</i>);
+
This function parses a JSONNumber out of a string and eats characters as it goes, hence the ref string parameter.
</big></dt>
+
<dd>Unescape a JSON string
+
<br><br>
+
<b>Returns:</b><br>
+
A decoded string.<br><br>
+
  
</dd>
 
<dt><big>char[] <u>quickUTF8</u>(dchar <i>dachar</i>);
 
</big></dt>
 
<dd>This probably needs documentation.  It looks like it converts a dchar to the necessary length string of chars.<br><br>
 
  
</dd>
+
<big>char[] <u>JSONEncode</u>(char[] <i>src</i>);</big>
</dl>
+
Perform JSON escapes on a string
 +
<b>Returns:</b> A JSON encoded string
  
 +
<big>char[] <u>JSONDecode</u>(char[] <i>src</i>);</big>
 +
Unescape a JSON string
 +
<b>Returns:</b> A decoded string.
  
 +
<big>char[] <u>quickUTF8</u>(dchar <i>dachar</i>);</big>
 +
This probably needs documentation.  It looks like it converts a dchar to the necessary length string of chars.
  
 
[[Category:Software]]                                                  <!--MAKE AS MANY CATEGORIES AS YOU NEED-->
 
[[Category:Software]]                                                  <!--MAKE AS MANY CATEGORIES AS YOU NEED-->

Revision as of 10:22, 20 May 2010

Creator:
Opticron
Status:
Late Implementation
Born On:
14:32, 8 January 2010 (CST)
Last Updated:
10:22, 20 May 2010 (CDT)

Overview

This is a basic JSON parser written in D. It currently supports building JSON structures in code and reading them from a piece of text, as well as outputting those structures to JSON text. It is available from my SVN server.

Examples

Traversing a node tree with known structure is now fairly simple. As seen, foreach is possible over JSON objects and JSON arrays without explicit casting, since it is now part of the interface. If you try to foreach over something that isn't a container, you get an exception.

foreach(obj;jstr.readJSON()["phoneNumbers"]) {
 writefln("Got " ~ obj["type"].toJSONString.get ~ " phone number:" ~ obj["number"].toJSONString.get);
}

ToDo

  • Build some kind of query system so that it is easier to pull data out of the JSON object.
    • XPath inspired?, may always have to return an array of nodes

Documentation

LibDJSON contains functions and classes for reading, parsing, and writing JSON documents.

Authors: William K. Moore, III

License:

Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Standards: Attempts to conform to the subset of Javascript required to implement the JSON Specification.


JSONObject readJSON(char[] src);
Read an entire string into a JSON tree.
Example:
auto root = new JSONObject();
auto arr = new JSONArray();
arr ~= new JSONString("da blue teeths!\"\\");
root["what is that on your ear?"] = arr;
root["my pants"] = new JSONString("are on fire");
root["i am this many"] = new JSONNumber(10.253);
string jstr = root.toString;
writef("Unit Test libDJSON JSON creation...\n");
writef("Generated JSON string: ");writef(jstr);writef("\n");
writef("Regenerated JSON string: ");writef(readJSON(jstr).toString);writef("\n");
Returns: A JSONObject with no name that is the root of the document that was read.
Throws: JSONError on any parsing errors.

class JSONError: object.Exception;
An exception thrown on JSON parsing errors.
abstract interface JSONType;
This is the interface implemented by all classes that represent JSON objects.
abstract void parse(ref char[] source);
The parse method of this interface should ALWAYS be destructive, removing things from the front of source as it parses.
abstract JSONObject toJSONObject();
Convenience function for casting to JSONObject.
Returns: The casted reference or null on a failed cast.
abstract JSONArray toJSONArray();
Convenience function for casting to JSONArray.
Returns: The casted reference or null on a failed cast.


abstract JSONString toJSONString(); Convenience function for casting to JSONString. Returns: The casted reference or null on a failed cast.

abstract JSONBoolean toJSONBoolean(); Convenience function for casting to JSONBoolean. Returns: The casted reference or null on a failed cast.

abstract JSONNumber toJSONNumber(); Convenience function for casting to JSONNumber. Returns: The casted reference or null on a failed cast.

abstract JSONNull toJSONNull(); Convenience function for casting to JSONNull. Returns: The casted reference or null on a failed cast.

abstract JSONType opIndex(char[] key); Associative array index function for objects describing associative array-like attributes. Returns: The chosen index or a null reference if the index does not exist.

abstract int opApply(int delegate(char[], JSONType) dg); Allow foreach over the object with string key.

abstract int opApply(int delegate(char[], ref JSONType) dg); Allow foreach over the object with string key and ref value.

abstract JSONType opIndex(int key); Array index function for objects describing array-like attributes. Returns: The chosen index or a null reference if the index does not exist.


abstract int opApply(int delegate(int, JSONType) dg); Allow foreach over the object with integer key.

abstract int opApply(int delegate(int, ref JSONType) dg); Allow foreach over the object with integer key and ref value.

abstract int opApply(int delegate(JSONType) dg); Convenience function for iteration that apply to both AA and array type operations

abstract int opApply(int delegate(ref JSONType) dg); Convenience function for iteration that apply to both AA and array type operations with ref value


class JSONObject: libdjson.json.JSONType; JSONObject represents a single JSON object node and has methods for

adding children.  All methods that make changes modify this
JSONObject rather than making a copy, unless otherwise noted.  Many methods
return a self reference to allow cascaded calls.

this(); Nothing to see here except for the boring constructor, move along.

void opIndexAssign(JSONType type, char[] key); Operator overload for setting keys in the AA.

JSONType opIndex(char[] key); Operator overload for accessing values already in the AA. Returns: The child node if it exists, otherwise null.

int length(); Allow the user to get the number of elements in this object Returns: The number of child nodes contained within this JSONObject

int opApply(int delegate(JSONType) dg); Operator overload for foreach iteration through the object with values only

int opApply(int delegate(ref JSONType) dg); Operator overload for foreach iteration through the object with values only and allow modification of the reference

int opApply(int delegate(char[], JSONType) dg); Operator overload for foreach iteration through the object with key and value

int opApply(int delegate(char[], ref JSONType) dg); Operator overload for foreach iteration through the object with key and value and allow modification of the reference

char[] toString(); A method to convert this JSONObject to a user readable format. Returns: A JSON string representing this object and it's contents.

void parse(ref char[] source); This function parses a JSONObject out of a string


class JSONArray: libdjson.json.JSONType; JSONArray represents a single JSON array, capable of being heterogenous this(); Nothing to see here, move along.

void opCatAssign(JSONType child); Operator overload to allow addition of children

JSONType opIndex(int key); Operator overload to allow access of children Returns: The child node if it exists, otherwise null.

int length(); Allow the user to get the number of elements in this object

Returns: The number of child nodes contained within this JSONObject

int opApply(int delegate(JSONType) dg); Operator overload for foreach iteration through the array with values only

int opApply(int delegate(ref JSONType) dg); Operator overload for foreach iteration through the array with values only and allow modification of the reference

int opApply(int delegate(int, JSONType) dg); Operator overload for foreach iteration through the array with key and value

int opApply(int delegate(int, ref JSONType) dg); Operator overload for foreach iteration through the array with key and value and allow modification of the reference

char[] toString(); A method to convert this JSONArray to a user readable format. Returns: A JSON string representing this object and it's contents.

void parse(ref char[] source); This function parses a JSONArray out of a string


class JSONString: libdjson.json.JSONType; JSONString represents a JSON string. Internal representation is escaped for faster parsing and JSON generation. this(); The boring default constructor.

this(string data); The ever so slightly more interesting initializing constructor.

void set(char[] data); Allow the data to be set so the object can be reused.

char[] get(); Allow the data to be retreived.

char[] toString(); A method to convert this JSONString to a user readable format. Returns: A JSON string representing this object and it's contents.

void parse(ref char[] source); This function parses a JSONArray out of a string and eats characters as it goes, hence the ref string parameter.


class JSONBoolean: libdjson.json.JSONType; JSONBoolean represents a JSON boolean value. this(); The boring constructor, again.

this(bool data); Only a bit of input for this constructor.

void set(bool data); Allow setting of the hidden bit.

bool get(); Allow the bit to be retreived.

char[] toString(); A method to convert this JSONBoolean to a user readable format. Returns: A JSON string representing this object and it's contents.

void parse(ref char[] source); This function parses a JSONBoolean out of a string and eats characters as it goes, hence the ref string parameter.


class JSONNull: libdjson.json.JSONType; JSONNull represents a JSON null value. this(); You're forced to use the boring constructor here.

char[] toString(); A method to convert this JSONNull to a user readable format. Returns: "null". Always. Forever.

void parse(ref char[] source); This function parses a JSONNull out of a string. Really, it just rips "null" off the beginning of the string and eats whitespace.


class JSONNumber: libdjson.json.JSONType; JSONNumber represents any JSON numeric value. this(); Another boring constructor...

this(real data); ...and its slightly less boring sibling.

void set(real data); Allow setting of the hidden number.

real get(); Allow the number to be retreived.

char[] toString(); A method to convert this JSONNumber to a user readable format. Returns: A JSON string representing this number.

void parse(ref char[] source); This function parses a JSONNumber out of a string and eats characters as it goes, hence the ref string parameter.


char[] JSONEncode(char[] src); Perform JSON escapes on a string Returns: A JSON encoded string

char[] JSONDecode(char[] src); Unescape a JSON string Returns: A decoded string.

char[] quickUTF8(dchar dachar); This probably needs documentation. It looks like it converts a dchar to the necessary length string of chars.