* Added JSON library to parse LLSD fields in the login reply
* Added fields in Avatar to hold the parsed data from login * CoarseLocationUpdate packets handled internally * Added Network.LoginValues hashtable, removed second parameter from .Login() * Updated examples to reflect LoginValues / Login() change git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@39 52acb1d6-8a22-11de-b505-999d5b087335
This commit is contained in:
629
libsecondlife-cs/JSONlib/JSONArray.cs
Normal file
629
libsecondlife-cs/JSONlib/JSONArray.cs
Normal file
@@ -0,0 +1,629 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
|
||||
namespace Nii.JSON
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// A JSONArray is an ordered sequence of values. Its external form is a string
|
||||
/// wrapped in square brackets with commas between the values. The internal form
|
||||
/// is an object having get() and opt() methods for accessing the values by
|
||||
/// index, and put() methods for adding or replacing values. The values can be
|
||||
/// any of these types: Boolean, JSONArray, JSONObject, Number, String, or the
|
||||
/// JSONObject.NULL object.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The constructor can convert a JSON external form string into an
|
||||
/// internal form Java object. The toString() method creates an external
|
||||
/// form string.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// A get() method returns a value if one can be found, and throws an exception
|
||||
/// if one cannot be found. An opt() method returns a default value instead of
|
||||
/// throwing an exception, and so is useful for obtaining optional values.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The generic get() and opt() methods return an object which you can cast or
|
||||
/// query for type. There are also typed get() and opt() methods that do typing
|
||||
/// checking and type coersion for you.
|
||||
///</para>
|
||||
/// <para>
|
||||
/// The texts produced by the toString() methods are very strict.
|
||||
/// The constructors are more forgiving in the texts they will accept.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <list type="bullet">
|
||||
/// <item><description>An extra comma may appear just before the closing bracket.</description></item>
|
||||
/// <item><description>Strings may be quoted with single quotes.</description></item>
|
||||
/// <item><description>Strings do not need to be quoted at all if they do not contain leading
|
||||
/// or trailing spaces, and if they do not contain any of these characters:
|
||||
/// { } [ ] / \ : , </description></item>
|
||||
/// <item><description>Numbers may have the 0- (octal) or 0x- (hex) prefix.</description></item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Public Domain 2002 JSON.org
|
||||
/// @author JSON.org
|
||||
/// @version 0.1
|
||||
///</para>
|
||||
/// Ported to C# by Are Bjolseth, teleplan.no
|
||||
/// TODO:
|
||||
/// 1. Implement Custom exceptions
|
||||
/// 2. Add indexer JSONObject[i] = object, and object = JSONObject[i];
|
||||
/// 3. Add indexer JSONObject["key"] = object, and object = JSONObject["key"]
|
||||
/// 4. Add unit testing
|
||||
/// 5. Add log4net
|
||||
/// 6. Make get/put methods private, to force use of indexer instead?
|
||||
/// </summary>
|
||||
public class JSONArray
|
||||
{
|
||||
/// <summary>The ArrayList where the JSONArray's properties are kept.</summary>
|
||||
private ArrayList myArrayList;
|
||||
|
||||
#region JSONArray Constructors
|
||||
/// <summary>
|
||||
/// Construct an empty JSONArray
|
||||
/// </summary>
|
||||
public JSONArray()
|
||||
{
|
||||
myArrayList = new ArrayList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a JSONArray from a JSONTokener.
|
||||
/// </summary>
|
||||
/// <param name="x">A JSONTokener</param>
|
||||
public JSONArray(JSONTokener x) : this()
|
||||
{
|
||||
if (x.nextClean() != '[')
|
||||
{
|
||||
throw new Exception("A JSONArray must start with '['");
|
||||
}
|
||||
if (x.nextClean() == ']')
|
||||
{
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
while (true)
|
||||
{
|
||||
myArrayList.Add(x.nextObject());
|
||||
switch (x.nextClean())
|
||||
{
|
||||
case ',':
|
||||
if (x.nextClean() == ']')
|
||||
{
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
break;
|
||||
case ']':
|
||||
return;
|
||||
default:
|
||||
throw new Exception("Expected a ',' or ']'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a JSONArray from a source string.
|
||||
/// </summary>
|
||||
/// <param name="s">A string that begins with '[' and ends with ']'.</param>
|
||||
public JSONArray(string s) : this(new JSONTokener(s))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a JSONArray from a Collection.
|
||||
/// </summary>
|
||||
/// <param name="collection">A Collection.</param>
|
||||
public JSONArray(ICollection collection)
|
||||
{
|
||||
myArrayList = new ArrayList(collection);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region C# extensions. Indexer and property
|
||||
/// <summary>
|
||||
/// Alternate to Java get/put method, by using indexer
|
||||
/// </summary>
|
||||
public object this[int i]
|
||||
{
|
||||
get
|
||||
{
|
||||
return opt(i);
|
||||
}
|
||||
set
|
||||
{
|
||||
//myArrayList[i] = value;
|
||||
put(i, value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Alternativ to Java, getArrayList, by using propery
|
||||
/// </summary>
|
||||
public IList List
|
||||
{
|
||||
get
|
||||
{
|
||||
return myArrayList;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Getters for a value associated with an index.
|
||||
/// <summary>
|
||||
/// Get the object value associated with an index.
|
||||
/// Use indexer instead!!! Added to be true to the original Java implementation
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript. The index must be between 0 and length()-1</param>
|
||||
/// <returns>An object value.</returns>
|
||||
public object getValue(int i)
|
||||
{
|
||||
object obj = opt(i);
|
||||
if (obj == null)
|
||||
{
|
||||
string msg = string.Format("JSONArray[{0}] not found", i);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
return obj;
|
||||
//return myArrayList[i];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the ArrayList which is holding the elements of the JSONArray.
|
||||
/// Use the indexer instead!! Added to be true to the orignal Java src
|
||||
/// </summary>
|
||||
/// <returns>The ArrayList</returns>
|
||||
public IList getArrayList()
|
||||
{
|
||||
return myArrayList;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the boolean value associated with an index.
|
||||
/// The string values "true" and "false" are converted to boolean.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>The truth</returns>
|
||||
public bool getBoolean(int i)
|
||||
{
|
||||
object obj = getValue(i);
|
||||
if (obj is bool)
|
||||
{
|
||||
return (bool)obj;
|
||||
}
|
||||
string msg = string.Format("JSONArray[{0}]={1} not a Boolean", i, obj);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the double value associated with an index.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>A double value</returns>
|
||||
public double getDouble(int i)
|
||||
{
|
||||
object obj = getValue(i);
|
||||
if (obj is double)
|
||||
return (double)obj;
|
||||
if (obj is float)
|
||||
return (double)obj;
|
||||
if (obj is string)
|
||||
{
|
||||
return Convert.ToDouble(obj);
|
||||
}
|
||||
string msg = string.Format("JSONArray[{0}]={1} not a double", i, obj);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the int value associated with an index.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>The int value</returns>
|
||||
public int getInt(int i)
|
||||
{
|
||||
object obj = getValue(i);
|
||||
if (obj is int)
|
||||
return (int)obj;
|
||||
if (obj is string)
|
||||
{
|
||||
return Convert.ToInt32(obj);
|
||||
}
|
||||
string msg = string.Format("JSONArray[{0}]={1} not a int", i, obj);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the JSONArray associated with an index.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>A JSONArray value</returns>
|
||||
public JSONArray getJSONArray(int i)
|
||||
{
|
||||
object obj = getValue(i);
|
||||
if (obj is JSONArray)
|
||||
{
|
||||
return (JSONArray)obj;
|
||||
}
|
||||
string msg = string.Format("JSONArray[{0}]={1} not a JSONArray", i, obj);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the JSONObject associated with an index.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>A JSONObject value</returns>
|
||||
public JSONObject getJSONObject(int i)
|
||||
{
|
||||
object obj = getValue(i);
|
||||
if (obj is JSONObject)
|
||||
{
|
||||
return (JSONObject)obj;
|
||||
}
|
||||
string msg = string.Format("JSONArray[{0}]={1} not a JSONObject", i, obj);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the string associated with an index.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>A string value.</returns>
|
||||
public string getString(int i)
|
||||
{
|
||||
object obj = getValue(i);
|
||||
if (obj is string)
|
||||
{
|
||||
return (string)obj;
|
||||
}
|
||||
string msg = string.Format("JSONArray[{0}]={1} not a string", i, obj);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Determine if the value is null.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>true if the value at the index is null, or if there is no value.</returns>
|
||||
public bool isNull(int i)
|
||||
{
|
||||
object obj = opt(i);
|
||||
return (obj == null || obj.Equals(null));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make a string from the contents of this JSONArray. The separator string
|
||||
/// is inserted between each element.
|
||||
/// Warning: This method assumes that the data structure is acyclical.
|
||||
/// </summary>
|
||||
/// <param name="separator">separator A string that will be inserted between the elements.</param>
|
||||
/// <returns>A string.</returns>
|
||||
public string join(string separator)
|
||||
{
|
||||
object obj;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i=0; i<myArrayList.Count; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
sb.Append(separator);
|
||||
}
|
||||
obj = myArrayList[i];
|
||||
|
||||
if (obj == null)
|
||||
{
|
||||
sb.Append("");
|
||||
}
|
||||
else if (obj is string)
|
||||
{
|
||||
sb.Append(JSONUtils.Enquote((string)obj));
|
||||
}
|
||||
else if (obj is int)
|
||||
{
|
||||
sb.Append(((int)obj).ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(obj.ToString());
|
||||
}
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the length of the JSONArray.
|
||||
/// Added to be true to the original Java implementation
|
||||
/// </summary>
|
||||
/// <returns>Number of JSONObjects in array</returns>
|
||||
public int Length()
|
||||
{
|
||||
return myArrayList.Count;
|
||||
}
|
||||
/// <summary>
|
||||
/// Get the length of the JSONArray.
|
||||
/// Using a propery instead of method
|
||||
/// </summary>
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return myArrayList.Count;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region Get the optional value associated with an index.
|
||||
/// <summary>
|
||||
/// Get the optional object value associated with an index.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>object at that index.</returns>
|
||||
public object opt(int i)
|
||||
{
|
||||
if (i < 0 || i >= myArrayList.Count)
|
||||
throw new ArgumentOutOfRangeException("int i", i, "Index out of bounds!");
|
||||
|
||||
return myArrayList[i];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the optional boolean value associated with an index.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>The truth</returns>
|
||||
public bool optBoolean(int i)
|
||||
{
|
||||
return optBoolean(i,false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the optional boolean value associated with an index.
|
||||
/// It returns the defaultValue if there is no value at that index or if it is not
|
||||
/// a Boolean or the String "true" or "false".
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns>The truth.</returns>
|
||||
public bool optBoolean(int i, bool defaultValue)
|
||||
{
|
||||
object obj = opt(i);
|
||||
if (obj != null)
|
||||
{
|
||||
return (bool)obj;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the optional double value associated with an index.
|
||||
/// NaN is returned if the index is not found,
|
||||
/// or if the value is not a number and cannot be converted to a number.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>The double value object</returns>
|
||||
public double optDouble(int i)
|
||||
{
|
||||
return optDouble(i,double.NaN);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the optional double value associated with an index.
|
||||
/// NaN is returned if the index is not found,
|
||||
/// or if the value is not a number and cannot be converted to a number.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns>The double value object</returns>
|
||||
public double optDouble(int i, double defaultValue)
|
||||
{
|
||||
object obj = opt(i);
|
||||
if (obj != null)
|
||||
{
|
||||
if (obj is double)
|
||||
return (double)obj;
|
||||
if (obj is float)
|
||||
return (float)obj;
|
||||
if (obj is string)
|
||||
{
|
||||
return Convert.ToDouble(obj);
|
||||
}
|
||||
string msg = string.Format("JSONArray[{0}]={1} not a double", i, obj);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the optional int value associated with an index.
|
||||
/// Zero is returned if the index is not found,
|
||||
/// or if the value is not a number and cannot be converted to a number.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>The int value object</returns>
|
||||
public int optInt(int i)
|
||||
{
|
||||
return optInt(i,0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the optional int value associated with an index.
|
||||
/// The defaultValue is returned if the index is not found,
|
||||
/// or if the value is not a number and cannot be converted to a number.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <param name="defaultValue">The default value</param>
|
||||
/// <returns>The int value object</returns>
|
||||
public int optInt(int i, int defaultValue)
|
||||
{
|
||||
object obj = opt(i);
|
||||
if (obj != null)
|
||||
{
|
||||
if (obj is int)
|
||||
return (int)obj;
|
||||
if (obj is string)
|
||||
return Convert.ToInt32(obj);
|
||||
string msg = string.Format("JSONArray[{0}]={1} not a int", i, obj);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the optional JSONArray associated with an index.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>A JSONArray value, or null if the index has no value, or if the value is not a JSONArray.</returns>
|
||||
public JSONArray optJSONArray(int i)
|
||||
{
|
||||
object obj = opt(i);
|
||||
if (obj is JSONArray)
|
||||
return (JSONArray)obj;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the optional JSONObject associated with an index.
|
||||
/// Null is returned if the key is not found, or null if the index has
|
||||
/// no value, or if the value is not a JSONObject.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>A JSONObject value</returns>
|
||||
public JSONObject optJSONObject(int i)
|
||||
{
|
||||
object obj = opt(i);
|
||||
if (obj is JSONObject)
|
||||
{
|
||||
return (JSONObject)obj;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the optional string value associated with an index. It returns an
|
||||
/// empty string if there is no value at that index. If the value
|
||||
/// is not a string and is not null, then it is coverted to a string.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <returns>A String value</returns>
|
||||
public string optString(int i)
|
||||
{
|
||||
return optString(i, "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the optional string associated with an index.
|
||||
/// The defaultValue is returned if the key is not found.
|
||||
/// </summary>
|
||||
/// <param name="i">index subscript</param>
|
||||
/// <param name="defaultValue">The default value</param>
|
||||
/// <returns>A string value</returns>
|
||||
public string optString(int i, string defaultValue)
|
||||
{
|
||||
object obj = opt(i);
|
||||
if (obj != null)
|
||||
{
|
||||
return obj.ToString();
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Put methods - use indexer instead
|
||||
/**
|
||||
* OMITTED:
|
||||
* public JSONArray put(bool val)
|
||||
* public JSONArray put(double val)
|
||||
* public JSONArray put(int val)
|
||||
*/
|
||||
/// <summary>
|
||||
/// Append an object value.
|
||||
/// </summary>
|
||||
/// <param name="val">An object value. The value should be a Boolean, Double, Integer, JSONArray, JSObject, or String, or the JSONObject.NULL object</param>
|
||||
/// <returns>this (JSONArray)</returns>
|
||||
public JSONArray put(object val)
|
||||
{
|
||||
myArrayList.Add(val);
|
||||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
* OMITTED:
|
||||
* public JSONArray put(int index, boolean value)
|
||||
* public JSONArray put(int index, double value)
|
||||
* public JSONArray put(int index, int value)
|
||||
*/
|
||||
/// <summary>
|
||||
/// Put or replace a boolean value in the JSONArray.
|
||||
/// </summary>
|
||||
/// <param name="i">
|
||||
/// The subscript. If the index is greater than the length of
|
||||
/// the JSONArray, then null elements will be added as necessary to pad it out.
|
||||
/// </param>
|
||||
/// <param name="val">An object value.</param>
|
||||
/// <returns>this (JSONArray)</returns>
|
||||
public JSONArray put(int i, object val)
|
||||
{
|
||||
if (i < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("i", i, "Negative indexes illegal");
|
||||
}
|
||||
else if (val == null)
|
||||
{
|
||||
throw new ArgumentNullException("val", "Object cannt be null");
|
||||
}
|
||||
else if (i < myArrayList.Count)
|
||||
{
|
||||
myArrayList.Insert(i, val);
|
||||
}
|
||||
// NOTE! Since i is >= Count, fill null vals before index i, then append new object at i
|
||||
else
|
||||
{
|
||||
while (i != myArrayList.Count)
|
||||
{
|
||||
myArrayList.Add(null);
|
||||
}
|
||||
myArrayList.Add(val);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Produce a JSONObject by combining a JSONArray of names with the values
|
||||
/// of this JSONArray.
|
||||
/// </summary>
|
||||
/// <param name="names">
|
||||
/// A JSONArray containing a list of key strings. These will be paired with the values.
|
||||
/// </param>
|
||||
/// <returns>A JSONObject, or null if there are no names or if this JSONArray</returns>
|
||||
public JSONObject toJSONObject(JSONArray names)
|
||||
{
|
||||
if (names == null || names.Length() == 0 || this.Length() == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
JSONObject jo = new JSONObject();
|
||||
for (int i=0; i <names.Length(); i++)
|
||||
{
|
||||
jo.put(names.getString(i),this.opt(i));
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Make an JSON external form string of this JSONArray. For compactness, no
|
||||
/// unnecessary whitespace is added.
|
||||
/// </summary>
|
||||
/// <returns>a printable, displayable, transmittable representation of the array.</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return '['+ join(",") + ']';
|
||||
}
|
||||
}
|
||||
}
|
||||
36
libsecondlife-cs/JSONlib/JSONFacade.cs
Normal file
36
libsecondlife-cs/JSONlib/JSONFacade.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace Nii.JSON
|
||||
{
|
||||
/// <summary>
|
||||
/// Summary description for JsonFacade.
|
||||
/// </summary>
|
||||
public sealed class JsonFacade
|
||||
{
|
||||
/// <summary>
|
||||
/// Parse a Hashtable and return a JSON formatted string
|
||||
/// </summary>
|
||||
/// <param name="idict"></param>
|
||||
/// <returns></returns>
|
||||
public static string toJSON(IDictionary idict)
|
||||
{
|
||||
JSONObject jsob = new JSONObject(idict);
|
||||
return jsob.ToString();
|
||||
}
|
||||
/// <summary>
|
||||
/// Parse JSON formatted string and return a Hashtable
|
||||
/// </summary>
|
||||
/// <param name="sJSON"></param>
|
||||
/// <returns></returns>
|
||||
public static IDictionary fromJSON(string sJSON)
|
||||
{
|
||||
JSONObject jsob = new JSONObject(sJSON);
|
||||
IDictionary idict = jsob.getDictionary();
|
||||
return idict;
|
||||
}
|
||||
}
|
||||
}
|
||||
882
libsecondlife-cs/JSONlib/JSONObject.cs
Normal file
882
libsecondlife-cs/JSONlib/JSONObject.cs
Normal file
@@ -0,0 +1,882 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Specialized;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
|
||||
/*
|
||||
* A JSONObject is an unordered collection of name/value pairs. Its
|
||||
* external form is a string wrapped in curly braces with colons between the
|
||||
* names and values, and commas between the values and names. The internal form
|
||||
* is an object having get() and opt() methods for accessing the values by name,
|
||||
* and put() methods for adding or replacing values by name. The values can be
|
||||
* any of these types: Boolean, JSONArray, JSONObject, Number, String, or the
|
||||
* JSONObject.NULL object.
|
||||
* <p>
|
||||
* The constructor can convert an external form string into an internal form
|
||||
* Java object. The toString() method creates an external form string.
|
||||
* <p>
|
||||
* A get() method returns a value if one can be found, and throws an exception
|
||||
* if one cannot be found. An opt() method returns a default value instead of
|
||||
* throwing an exception, and so is useful for obtaining optional values.
|
||||
* <p>
|
||||
* The generic get() and opt() methods return an object, which you can cast or
|
||||
* query for type. There are also typed get() and opt() methods that do typing
|
||||
* checking and type coersion for you.
|
||||
* <p>
|
||||
* The texts produced by the toString() methods are very strict.
|
||||
* The constructors are more forgiving in the texts they will accept.
|
||||
* <ul>
|
||||
* <li>An extra comma may appear just before the closing brace.</li>
|
||||
* <li>Strings may be quoted with single quotes.</li>
|
||||
* <li>Strings do not need to be quoted at all if they do not contain leading
|
||||
* or trailing spaces, and if they do not contain any of these characters:
|
||||
* { } [ ] / \ : , </li>
|
||||
* <li>Numbers may have the 0- (octal) or 0x- (hex) prefix.</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* Public Domain 2002 JSON.org
|
||||
* @author JSON.org
|
||||
* @version 0.1
|
||||
* <p>
|
||||
* Ported to C# by Are Bjolseth, teleplan.no
|
||||
* TODO:
|
||||
* 1. Implement Custom exceptions
|
||||
* 2. Add indexer JSONObject[i] = object, and object = JSONObject[i];
|
||||
* 3. Add indexer JSONObject["key"] = object, and object = JSONObject["key"]
|
||||
* 4. Add unit testing
|
||||
* 5. Add log4net
|
||||
*/
|
||||
namespace Nii.JSON
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// A JSONArray is an ordered sequence of values. Its external form is a string
|
||||
/// wrapped in square brackets with commas between the values. The internal form
|
||||
/// is an object having get() and opt() methods for accessing the values by
|
||||
/// index, and put() methods for adding or replacing values. The values can be
|
||||
/// any of these types: Boolean, JSONArray, JSONObject, Number, String, or the
|
||||
/// JSONObject.NULL object.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The constructor can convert a JSON external form string into an
|
||||
/// internal form Java object. The toString() method creates an external
|
||||
/// form string.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// A get() method returns a value if one can be found, and throws an exception
|
||||
/// if one cannot be found. An opt() method returns a default value instead of
|
||||
/// throwing an exception, and so is useful for obtaining optional values.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The generic get() and opt() methods return an object which you can cast or
|
||||
/// query for type. There are also typed get() and opt() methods that do typing
|
||||
/// checking and type coersion for you.
|
||||
///</para>
|
||||
/// <para>
|
||||
/// The texts produced by the toString() methods are very strict.
|
||||
/// The constructors are more forgiving in the texts they will accept.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <list type="bullet">
|
||||
/// <item><description>An extra comma may appear just before the closing bracket.</description></item>
|
||||
/// <item><description>Strings may be quoted with single quotes.</description></item>
|
||||
/// <item><description>Strings do not need to be quoted at all if they do not contain leading
|
||||
/// or trailing spaces, and if they do not contain any of these characters:
|
||||
/// { } [ ] / \ : , </description></item>
|
||||
/// <item><description>Numbers may have the 0- (octal) or 0x- (hex) prefix.</description></item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Public Domain 2002 JSON.org
|
||||
/// @author JSON.org
|
||||
/// @version 0.1
|
||||
///</para>
|
||||
/// Ported to C# by Are Bjolseth, teleplan.no
|
||||
/// TODO:
|
||||
/// 1. Implement Custom exceptions
|
||||
/// 2. Add indexer JSONObject[i] = object, and object = JSONObject[i];
|
||||
/// 3. Add indexer JSONObject["key"] = object, and object = JSONObject["key"]
|
||||
/// 4. Add unit testing
|
||||
/// 5. Add log4net
|
||||
/// 6. Make get/put methods private, to force use of indexer instead?
|
||||
/// </summary>
|
||||
public class JSONObject
|
||||
{
|
||||
#region Local struct JSONNull
|
||||
/// <summary>
|
||||
/// Make a Null object
|
||||
/// JSONObject.NULL is equivalent to the value that JavaScript calls null,
|
||||
/// whilst C#'s null is equivalent to the value that JavaScript calls undefined.
|
||||
/// </summary>
|
||||
public struct JSONNull
|
||||
{
|
||||
/*
|
||||
public object clone()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
public bool equals(object obj)
|
||||
{
|
||||
return (obj == null) || (obj == this);
|
||||
}
|
||||
*/
|
||||
/// <summary>
|
||||
/// Overriden to return "null"
|
||||
/// </summary>
|
||||
/// <returns>null</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
//return base.ToString ();
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
///<summary>The hash map where the JSONObject's properties are kept.</summary>
|
||||
private Hashtable myHashMap;
|
||||
|
||||
///<summary>A shadow list of keys to enable access by sequence of insertion</summary>
|
||||
private ArrayList myKeyIndexList;
|
||||
|
||||
/// <summary>
|
||||
/// It is sometimes more convenient and less ambiguous to have a NULL
|
||||
/// object than to use C#'s null value.
|
||||
/// JSONObject.NULL.toString() returns "null".
|
||||
/// </summary>
|
||||
public static readonly JSONNull NULL = new JSONNull();
|
||||
|
||||
#region Constructors for JSONObject
|
||||
/// <summary>
|
||||
/// Construct an empty JSONObject.
|
||||
/// </summary>
|
||||
public JSONObject()
|
||||
{
|
||||
myHashMap = new Hashtable();
|
||||
myKeyIndexList = new ArrayList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Construct a JSONObject from a JSONTokener.
|
||||
/// </summary>
|
||||
/// <param name="x">A JSONTokener object containing the source string.</param>
|
||||
public JSONObject(JSONTokener x) : this()
|
||||
{
|
||||
char c;
|
||||
string key;
|
||||
if (x.next() == '%')
|
||||
{
|
||||
x.unescape();
|
||||
}
|
||||
x.back();
|
||||
if (x.nextClean() != '{')
|
||||
{
|
||||
throw new Exception("A JSONObject must begin with '{'");
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
c = x.nextClean();
|
||||
switch (c)
|
||||
{
|
||||
case (char)0:
|
||||
throw new Exception("A JSONObject must end with '}'");
|
||||
case '}':
|
||||
return;
|
||||
default:
|
||||
x.back();
|
||||
key = x.nextObject().ToString();
|
||||
break;
|
||||
}
|
||||
if (x.nextClean() != ':')
|
||||
{
|
||||
throw new Exception("Expected a ':' after a key");
|
||||
}
|
||||
object obj = x.nextObject();
|
||||
myHashMap.Add(key, obj);
|
||||
myKeyIndexList.Add(key);
|
||||
switch (x.nextClean())
|
||||
{
|
||||
case ',':
|
||||
if (x.nextClean() == '}')
|
||||
{
|
||||
return;
|
||||
}
|
||||
x.back();
|
||||
break;
|
||||
case '}':
|
||||
return;
|
||||
default:
|
||||
throw new Exception("Expected a ',' or '}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Construct a JSONObject from a string.
|
||||
/// </summary>
|
||||
/// <param name="sJSON">A string beginning with '{' and ending with '}'.</param>
|
||||
public JSONObject(string sJSON) : this(new JSONTokener(sJSON))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// public JSONObject(Hashtable map)
|
||||
// By changing to arg to interface, all classes that implements IDictionary can be used
|
||||
// public interface IDictionary : ICollection, IEnumerable
|
||||
// Classes that implements IDictionary
|
||||
// 1. BaseChannelObjectWithProperties - Provides a base implementation of a channel object that wants to provide a dictionary interface to its properties.
|
||||
// 2. DictionaryBase - Provides the abstract (MustInherit in Visual Basic) base class for a strongly typed collection of key-and-value pairs.
|
||||
// 3. Hashtable - Represents a collection of key-and-value pairs that are organized based on the hash code of the key.
|
||||
// 4. HybridDictionary - Implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large.
|
||||
// 5. ListDictionary - Implements IDictionary using a singly linked list. Recommended for collections that typically contain 10 items or less.
|
||||
// 6. PropertyCollection - Contains the properties of a DirectoryEntry.
|
||||
// 7. PropertyDescriptorCollection - Represents a collection of PropertyDescriptor objects.
|
||||
// 8. SortedList - Represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index.
|
||||
// 9. StateBag - Manages the view state of ASP.NET server controls, including pages. This class cannot be inherited.
|
||||
// See ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsidictionaryclasstopic.htm
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Construct a JSONObject from a IDictionary
|
||||
/// </summary>
|
||||
/// <param name="map"></param>
|
||||
public JSONObject(IDictionary map)
|
||||
{
|
||||
myHashMap = new Hashtable(map);
|
||||
myKeyIndexList = new ArrayList(map);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Accumulate values under a key. It is similar to the put method except
|
||||
/// that if there is already an object stored under the key then a
|
||||
/// JSONArray is stored under the key to hold all of the accumulated values.
|
||||
/// If there is already a JSONArray, then the new value is appended to it.
|
||||
/// In contrast, the put method replaces the previous value.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <param name="val">An object to be accumulated under the key.</param>
|
||||
/// <returns>this</returns>
|
||||
public JSONObject accumulate(string key, object val)
|
||||
{
|
||||
JSONArray a;
|
||||
object obj = opt(key);
|
||||
if (obj == null)
|
||||
{
|
||||
put(key, val);
|
||||
}
|
||||
else if (obj is JSONArray)
|
||||
{
|
||||
a = (JSONArray)obj;
|
||||
a.put(val);
|
||||
}
|
||||
else
|
||||
{
|
||||
a = new JSONArray();
|
||||
a.put(obj);
|
||||
a.put(val);
|
||||
put(key,a);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
#region C# specific extensions
|
||||
/// <summary>
|
||||
/// Return the key for the associated index
|
||||
/// </summary>
|
||||
public string this[int i]
|
||||
{
|
||||
get
|
||||
{
|
||||
DictionaryEntry de = (DictionaryEntry)myKeyIndexList[i];
|
||||
return de.Key.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get/Add an object with the associated key
|
||||
/// </summary>
|
||||
public object this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
return getValue(key);
|
||||
}
|
||||
set
|
||||
{
|
||||
put(key,value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the number of JSON items in hashtable
|
||||
/// </summary>
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return myHashMap.Count;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// C# convenience method
|
||||
/// </summary>
|
||||
/// <returns>The Hashtable</returns>
|
||||
public IDictionary getDictionary()
|
||||
{
|
||||
return myHashMap;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Gettes for a value associated with a key - use indexer instead
|
||||
/// <summary>
|
||||
/// Alias to Java get method
|
||||
/// Get the value object associated with a key.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <returns>The object associated with the key.</returns>
|
||||
public object getValue(string key)
|
||||
{
|
||||
//return myHashMap[key];
|
||||
object obj = opt(key);
|
||||
if (obj == null)
|
||||
{
|
||||
throw new Exception("No such element");
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the boolean value associated with a key.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <returns>The truth.</returns>
|
||||
public bool getBool(string key)
|
||||
{
|
||||
object o = getValue(key);
|
||||
if (o is bool)
|
||||
{
|
||||
bool b = (bool)o;
|
||||
return b;
|
||||
}
|
||||
string msg = string.Format("JSONObject[{0}] is not a Boolean",JSONUtils.Enquote(key));
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the double value associated with a key.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <returns>The double value</returns>
|
||||
public double getDouble(string key)
|
||||
{
|
||||
object o = getValue(key);
|
||||
if (o is double)
|
||||
return (double)o;
|
||||
if (o is float)
|
||||
return (double)o;
|
||||
|
||||
if (o is string)
|
||||
{
|
||||
return Convert.ToDouble(o);
|
||||
}
|
||||
string msg = string.Format("JSONObject[{0}] is not a double",JSONUtils.Enquote(key));
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the int value associated with a key.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string</param>
|
||||
/// <returns> The integer value.</returns>
|
||||
public int getInt(string key)
|
||||
{
|
||||
object o = getValue(key);
|
||||
if (o is int)
|
||||
{
|
||||
return (int)o;
|
||||
}
|
||||
|
||||
if (o is string)
|
||||
{
|
||||
return Convert.ToInt32(o);
|
||||
}
|
||||
string msg = string.Format("JSONObject[{0}] is not a int",JSONUtils.Enquote(key));
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the JSONArray value associated with a key.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string</param>
|
||||
/// <returns>A JSONArray which is the value</returns>
|
||||
public JSONArray getJSONArray(string key)
|
||||
{
|
||||
object o = getValue(key);
|
||||
if (o is JSONArray)
|
||||
{
|
||||
return (JSONArray)o;
|
||||
}
|
||||
string msg = string.Format("JSONObject[{0}]={1} is not a JSONArray",JSONUtils.Enquote(key),o);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the JSONObject value associated with a key.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <returns>A JSONObject which is the value.</returns>
|
||||
public JSONObject getJSONObject(string key)
|
||||
{
|
||||
object o = getValue(key);
|
||||
if (o is JSONObject)
|
||||
{
|
||||
return (JSONObject)o;
|
||||
}
|
||||
string msg = string.Format("JSONObject[{0}]={1} is not a JSONArray",JSONUtils.Enquote(key),o);
|
||||
throw new Exception(msg);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the string associated with a key.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <returns>A string which is the value.</returns>
|
||||
public string getString(string key)
|
||||
{
|
||||
return getValue(key).ToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Determine if the JSONObject contains a specific key.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <returns>true if the key exists in the JSONObject.</returns>
|
||||
public bool has(string key)
|
||||
{
|
||||
return myHashMap.ContainsKey(key);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get an enumeration of the keys of the JSONObject.
|
||||
/// Added to be true to orginal Java implementation
|
||||
/// Indexers are easier to use
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerator keys()
|
||||
{
|
||||
return myHashMap.Keys.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine if the value associated with the key is null or if there is no value.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string</param>
|
||||
/// <returns>true if there is no value associated with the key or if the valus is the JSONObject.NULL object</returns>
|
||||
public bool isNull(string key)
|
||||
{
|
||||
return JSONObject.NULL.Equals(opt(key));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the number of keys stored in the JSONObject.
|
||||
/// </summary>
|
||||
/// <returns>The number of keys in the JSONObject.</returns>
|
||||
public int Length()
|
||||
{
|
||||
return myHashMap.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Produce a JSONArray containing the names of the elements of this JSONObject
|
||||
/// </summary>
|
||||
/// <returns>A JSONArray containing the key strings, or null if the JSONObject</returns>
|
||||
public JSONArray names()
|
||||
{
|
||||
JSONArray ja = new JSONArray();
|
||||
|
||||
//NOTE!! I choose to use keys stored in the ArrayList, to maintain sequence order
|
||||
foreach (string key in myKeyIndexList)
|
||||
{
|
||||
ja.put(key);
|
||||
}
|
||||
if (ja.Length() == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return ja;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Produce a string from a number.
|
||||
/// </summary>
|
||||
/// <param name="number">Number value type object</param>
|
||||
/// <returns>String representation of the number</returns>
|
||||
public string numberToString(object number)
|
||||
{
|
||||
if (number is float && ((float)number) == float.NaN)
|
||||
{
|
||||
string msg = string.Format("");
|
||||
throw new ArgumentException("object must be a valid number", "number");
|
||||
}
|
||||
if (number is double && ((double)number) == double.NaN)
|
||||
{
|
||||
string msg = string.Format("");
|
||||
throw new ArgumentException("object must be a valid number", "number");
|
||||
}
|
||||
|
||||
// Shave off trailing zeros and decimal point, if possible
|
||||
|
||||
string s = ((double) number).ToString(NumberFormatInfo.InvariantInfo).ToLower();
|
||||
|
||||
if (s.IndexOf('e') < 0 && s.IndexOf('.') > 0)
|
||||
{
|
||||
while(s.EndsWith("0"))
|
||||
{
|
||||
s.Substring(0,s.Length-1);
|
||||
}
|
||||
if (s.EndsWith("."))
|
||||
{
|
||||
s.Substring(0,s.Length-1);
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
#region Get an optional value associated with a key.
|
||||
/// <summary>
|
||||
/// Get an optional value associated with a key.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string</param>
|
||||
/// <returns>An object which is the value, or null if there is no value.</returns>
|
||||
public object opt(string key)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException("key", "Null key");
|
||||
}
|
||||
return myHashMap[key];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an optional value associated with a key.
|
||||
/// It returns false if there is no such key, or if the value is not
|
||||
/// Boolean.TRUE or the String "true".
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <returns>bool value object</returns>
|
||||
public bool optBoolean(string key)
|
||||
{
|
||||
return optBoolean(key, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an optional value associated with a key.
|
||||
/// It returns false if there is no such key, or if the value is not
|
||||
/// Boolean.TRUE or the String "true".
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <param name="defaultValue">The preferred return value if conversion fails</param>
|
||||
/// <returns>bool value object</returns>
|
||||
public bool optBoolean(string key, bool defaultValue)
|
||||
{
|
||||
object obj = opt(key);
|
||||
if (obj != null)
|
||||
{
|
||||
if (obj is bool)
|
||||
return (bool)obj;
|
||||
if (obj is string)
|
||||
{
|
||||
return Convert.ToBoolean(obj);
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an optional double associated with a key,
|
||||
/// or NaN if there is no such key or if its value is not a number.
|
||||
/// If the value is a string, an attempt will be made to evaluate it as
|
||||
/// a number.
|
||||
/// </summary>
|
||||
/// <param name="key">A string which is the key.</param>
|
||||
/// <returns>A double value object</returns>
|
||||
public double optDouble(string key)
|
||||
{
|
||||
return optDouble(key, double.NaN);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an optional double associated with a key,
|
||||
/// or NaN if there is no such key or if its value is not a number.
|
||||
/// If the value is a string, an attempt will be made to evaluate it as
|
||||
/// a number.
|
||||
/// </summary>
|
||||
/// <param name="key">A string which is the key.</param>
|
||||
/// <param name="defaultValue">The default</param>
|
||||
/// <returns>A double value object</returns>
|
||||
public double optDouble(string key, double defaultValue)
|
||||
{
|
||||
object obj = opt(key);
|
||||
if (obj != null)
|
||||
{
|
||||
if (obj is double)
|
||||
return (double)obj;
|
||||
if (obj is float)
|
||||
return (double)obj;
|
||||
if (obj is string)
|
||||
{
|
||||
return Convert.ToDouble(obj);
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an optional double associated with a key, or the
|
||||
/// defaultValue if there is no such key or if its value is not a number.
|
||||
/// If the value is a string, an attempt will be made to evaluate it as
|
||||
/// number.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <returns>An int object value</returns>
|
||||
public int optInt(string key)
|
||||
{
|
||||
return optInt(key, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an optional double associated with a key, or the
|
||||
/// defaultValue if there is no such key or if its value is not a number.
|
||||
/// If the value is a string, an attempt will be made to evaluate it as
|
||||
/// number.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <param name="defaultValue">The default value</param>
|
||||
/// <returns>An int object value</returns>
|
||||
public int optInt(string key, int defaultValue)
|
||||
{
|
||||
object obj = opt(key);
|
||||
if (obj != null)
|
||||
{
|
||||
if (obj is int)
|
||||
return (int)obj;
|
||||
if (obj is string)
|
||||
return Convert.ToInt32(obj);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an optional JSONArray associated with a key.
|
||||
/// It returns null if there is no such key, or if its value is not a JSONArray
|
||||
/// </summary>
|
||||
/// <param name="key">A key string</param>
|
||||
/// <returns>A JSONArray which is the value</returns>
|
||||
public JSONArray optJSONArray(string key)
|
||||
{
|
||||
object obj = opt(key);
|
||||
if (obj is JSONArray)
|
||||
{
|
||||
return (JSONArray)obj;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an optional JSONObject associated with a key.
|
||||
/// It returns null if there is no such key, or if its value is not a JSONObject.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <returns>A JSONObject which is the value</returns>
|
||||
public JSONObject optJSONObject(string key)
|
||||
{
|
||||
object obj = opt(key);
|
||||
if (obj is JSONObject)
|
||||
{
|
||||
return (JSONObject)obj;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an optional string associated with a key.
|
||||
/// It returns an empty string if there is no such key. If the value is not
|
||||
/// a string and is not null, then it is coverted to a string.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <returns>A string which is the value.</returns>
|
||||
public string optString(string key)
|
||||
{
|
||||
object obj = opt(key);
|
||||
|
||||
return optString(key, "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an optional string associated with a key.
|
||||
/// It returns the defaultValue if there is no such key.
|
||||
/// </summary>
|
||||
/// <param name="key">A key string.</param>
|
||||
/// <param name="defaultValue">The default</param>
|
||||
/// <returns>A string which is the value.</returns>
|
||||
public string optString(string key, string defaultValue)
|
||||
{
|
||||
object obj = opt(key);
|
||||
if (obj != null)
|
||||
{
|
||||
return obj.ToString();
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Put methods for adding key/value pairs
|
||||
// OMITTED - all put methods can be replaced by a indexer in C#
|
||||
// - ===================================================
|
||||
// public JSONObject put(String key, boolean value)
|
||||
// public JSONObject put(String key, double value)
|
||||
// public JSONObject put(String key, int value)
|
||||
|
||||
/// <summary>
|
||||
/// Put a key/value pair in the JSONObject. If the value is null,
|
||||
/// then the key will be removed from the JSONObject if it is present.
|
||||
/// </summary>
|
||||
/// <param name="key"> A key string.</param>
|
||||
/// <param name="val">
|
||||
/// An object which is the value. It should be of one of these
|
||||
/// types: Boolean, Double, Integer, JSONArray, JSONObject, String, or the
|
||||
/// JSONObject.NULL object.
|
||||
/// </param>
|
||||
/// <returns>JSONObject</returns>
|
||||
public JSONObject put(string key, object val)
|
||||
{
|
||||
if (key == null)
|
||||
{
|
||||
throw new ArgumentNullException("key", "key cannot be null");
|
||||
}
|
||||
if (val != null)
|
||||
{
|
||||
if (!myHashMap.ContainsKey(key))
|
||||
{
|
||||
string test = key;
|
||||
myHashMap.Add(key,val);
|
||||
myKeyIndexList.Add(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
myHashMap[key]=val;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
remove(key);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a key value pair
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="val"></param>
|
||||
/// <returns></returns>
|
||||
public JSONObject putOpt(string key, object val)
|
||||
{
|
||||
if (val != null)
|
||||
{
|
||||
put(key,val);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Remove a object assosiateted with the given key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public object remove(string key)
|
||||
{
|
||||
if (myHashMap.ContainsKey(key))
|
||||
{
|
||||
// TODO - does it really work ???
|
||||
object obj = myHashMap[key];
|
||||
myHashMap.Remove(key);
|
||||
myKeyIndexList.Remove(key);
|
||||
return obj;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Append an array of JSONObjects to current object
|
||||
/// </summary>
|
||||
/// <param name="names"></param>
|
||||
/// <returns></returns>
|
||||
public JSONArray toJSONArray(JSONArray names)
|
||||
{
|
||||
if (names == null | names.Length() == 0)
|
||||
return null;
|
||||
|
||||
JSONArray ja = new JSONArray();
|
||||
for (int i=0; i<names.Length(); i++)
|
||||
{
|
||||
ja.put(this.opt(names.getString(i)));
|
||||
}
|
||||
return ja;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overridden to return a JSON formattet object as a string
|
||||
/// </summary>
|
||||
/// <returns>JSON object as formatted string</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
object obj = null;
|
||||
//string s;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.Append('{');
|
||||
foreach (string key in myHashMap.Keys) //NOTE! Could also use myKeyIndexList !!!
|
||||
{
|
||||
if (obj != null)
|
||||
sb.Append(',');
|
||||
obj = myHashMap[key];
|
||||
if (obj != null)
|
||||
{
|
||||
sb.Append(JSONUtils.Enquote(key));
|
||||
sb.Append(':');
|
||||
|
||||
if (obj is string)
|
||||
{
|
||||
sb.Append(JSONUtils.Enquote((string)obj));
|
||||
}
|
||||
else if (obj is float || obj is double)
|
||||
{
|
||||
sb.Append(numberToString(obj));
|
||||
}
|
||||
else if (obj is bool)
|
||||
{
|
||||
sb.Append(obj.ToString().ToLower());
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(obj.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
sb.Append('}');
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
463
libsecondlife-cs/JSONlib/JSONTokener.cs
Normal file
463
libsecondlife-cs/JSONlib/JSONTokener.cs
Normal file
@@ -0,0 +1,463 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Nii.JSON
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// A JSONTokener takes a source string and extracts characters and tokens from
|
||||
/// it. It is used by the JSONObject and JSONArray constructors to parse
|
||||
/// JSON source strings.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Public Domain 2002 JSON.org
|
||||
/// @author JSON.org
|
||||
/// @version 0.1
|
||||
/// </para>
|
||||
/// <para>Ported to C# by Are Bjolseth, teleplan.no</para>
|
||||
/// <para>
|
||||
/// <list type="bullet">
|
||||
/// <item><description>Implement Custom exceptions</description></item>
|
||||
/// <item><description>Add unit testing</description></item>
|
||||
/// <item><description>Add log4net</description></item>
|
||||
/// </list>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public class JSONTokener
|
||||
{
|
||||
/// <summary>The index of the next character.</summary>
|
||||
private int myIndex;
|
||||
/// <summary>The source string being tokenized.</summary>
|
||||
private string mySource;
|
||||
|
||||
/// <summary>
|
||||
/// Construct a JSONTokener from a string.
|
||||
/// </summary>
|
||||
/// <param name="s">A source string.</param>
|
||||
public JSONTokener(string s)
|
||||
{
|
||||
myIndex = 0;
|
||||
mySource = s;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Back up one character. This provides a sort of lookahead capability,
|
||||
/// so that you can test for a digit or letter before attempting to parse
|
||||
/// the next number or identifier.
|
||||
/// </summary>
|
||||
public void back()
|
||||
{
|
||||
if (myIndex > 0)
|
||||
myIndex -= 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the hex value of a character (base16).
|
||||
/// </summary>
|
||||
/// <param name="c">
|
||||
/// A character between '0' and '9' or between 'A' and 'F' or
|
||||
/// between 'a' and 'f'.
|
||||
/// </param>
|
||||
/// <returns>An int between 0 and 15, or -1 if c was not a hex digit.</returns>
|
||||
public static int dehexchar(char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
return c - '0';
|
||||
}
|
||||
if (c >= 'A' && c <= 'F')
|
||||
{
|
||||
return c + 10 - 'A';
|
||||
}
|
||||
if (c >= 'a' && c <= 'f')
|
||||
{
|
||||
return c + 10 - 'a';
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine if the source string still contains characters that next() can consume.
|
||||
/// </summary>
|
||||
/// <returns>true if not yet at the end of the source.</returns>
|
||||
public bool more()
|
||||
{
|
||||
return myIndex < mySource.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the next character in the source string.
|
||||
/// </summary>
|
||||
/// <returns>The next character, or 0 if past the end of the source string.</returns>
|
||||
public char next()
|
||||
{
|
||||
char c = more() ? mySource[myIndex] : (char)0;
|
||||
myIndex +=1;
|
||||
return c;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Consume the next character, and check that it matches a specified character
|
||||
/// </summary>
|
||||
/// <param name="c">The character to match.</param>
|
||||
/// <returns>The character.</returns>
|
||||
public char next(char c)
|
||||
{
|
||||
char n = next();
|
||||
if (n != c)
|
||||
{
|
||||
string msg = "Expected '" + c + "' and instead saw '" + n + "'.";
|
||||
throw (new Exception(msg));
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the next n characters.
|
||||
/// </summary>
|
||||
/// <param name="n">The number of characters to take.</param>
|
||||
/// <returns>A string of n characters.</returns>
|
||||
public string next(int n)
|
||||
{
|
||||
int i = myIndex;
|
||||
int j = i + n;
|
||||
if (j >= mySource.Length)
|
||||
{
|
||||
string msg = "Substring bounds error";
|
||||
throw (new Exception(msg));
|
||||
}
|
||||
myIndex += n;
|
||||
return mySource.Substring(i,j);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the next char in the string, skipping whitespace
|
||||
/// and comments (slashslash and slashstar).
|
||||
/// </summary>
|
||||
/// <returns>A character, or 0 if there are no more characters.</returns>
|
||||
public char nextClean()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
char c = next();
|
||||
if (c == '/')
|
||||
{
|
||||
switch (next())
|
||||
{
|
||||
case '/':
|
||||
do
|
||||
{
|
||||
c = next();
|
||||
} while (c != '\n' && c != '\r' && c != 0);
|
||||
break;
|
||||
case '*':
|
||||
while (true)
|
||||
{
|
||||
c = next();
|
||||
if (c == 0)
|
||||
{
|
||||
throw (new Exception("Unclosed comment."));
|
||||
}
|
||||
if (c == '*')
|
||||
{
|
||||
if (next() == '/')
|
||||
{
|
||||
break;
|
||||
}
|
||||
back();
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
back();
|
||||
return '/';
|
||||
}
|
||||
}
|
||||
else if (c == 0 || c > ' ')
|
||||
{
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the characters up to the next close quote character.
|
||||
/// Backslash processing is done. The formal JSON format does not
|
||||
/// allow strings in single quotes, but an implementation is allowed to
|
||||
/// accept them.
|
||||
/// </summary>
|
||||
/// <param name="quote">The quoting character, either " or '</param>
|
||||
/// <returns>A String.</returns>
|
||||
public string nextString(char quote)
|
||||
{
|
||||
char c;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (true)
|
||||
{
|
||||
c = next();
|
||||
if ((c == 0x00) || (c == 0x0A) || (c == 0x0D))
|
||||
{
|
||||
throw (new Exception("Unterminated string"));
|
||||
}
|
||||
// CTRL chars
|
||||
if (c == '\\')
|
||||
{
|
||||
c = next();
|
||||
switch (c)
|
||||
{
|
||||
case 'b': //Backspace
|
||||
sb.Append('\b');
|
||||
break;
|
||||
case 't': //Horizontal tab
|
||||
sb.Append('\t');
|
||||
break;
|
||||
case 'n': //newline
|
||||
sb.Append('\n');
|
||||
break;
|
||||
case 'f': //Form feed
|
||||
sb.Append('\f');
|
||||
break;
|
||||
case 'r': // Carriage return
|
||||
sb.Append('\r');
|
||||
break;
|
||||
case 'u':
|
||||
//sb.append((char)Integer.parseInt(next(4), 16)); // 16 == radix, ie. hex
|
||||
int iascii = int.Parse(next(4),System.Globalization.NumberStyles.HexNumber);
|
||||
sb.Append((char)iascii);
|
||||
break;
|
||||
default:
|
||||
sb.Append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (c == quote)
|
||||
{
|
||||
return sb.ToString();
|
||||
}
|
||||
sb.Append(c);
|
||||
}
|
||||
}//END-while
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the text up but not including the specified character or the
|
||||
/// end of line, whichever comes first.
|
||||
/// </summary>
|
||||
/// <param name="d">A delimiter character.</param>
|
||||
/// <returns>A string.</returns>
|
||||
public string nextTo(char d)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (true)
|
||||
{
|
||||
char c = next();
|
||||
if (c == d || c == (char)0 || c == '\n' || c == '\r')
|
||||
{
|
||||
if (c != (char)0)
|
||||
{
|
||||
back();
|
||||
}
|
||||
return sb.ToString().Trim();
|
||||
}
|
||||
sb.Append(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the text up but not including one of the specified delimeter
|
||||
/// characters or the end of line, which ever comes first.
|
||||
/// </summary>
|
||||
/// <param name="delimiters">A set of delimiter characters.</param>
|
||||
/// <returns>A string, trimmed.</returns>
|
||||
public string nextTo(string delimiters)
|
||||
{
|
||||
char c;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (true)
|
||||
{
|
||||
c = next();
|
||||
if ((delimiters.IndexOf(c) >= 0) || (c == (char)0 ) || (c == '\n') || (c == '\r'))
|
||||
{
|
||||
if (c != (char)0)
|
||||
{
|
||||
back();
|
||||
}
|
||||
return sb.ToString().Trim();
|
||||
}
|
||||
sb.Append(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the next value as object. The value can be a Boolean, Double, Integer,
|
||||
/// JSONArray, JSONObject, or String, or the JSONObject.NULL object.
|
||||
/// </summary>
|
||||
/// <returns>An object.</returns>
|
||||
public object nextObject()
|
||||
{
|
||||
char c = nextClean();
|
||||
string s;
|
||||
|
||||
if (c == '"' || c == '\'')
|
||||
{
|
||||
return nextString(c);
|
||||
}
|
||||
// Object
|
||||
if (c == '{')
|
||||
{
|
||||
back();
|
||||
return new JSONObject(this);
|
||||
}
|
||||
|
||||
// JSON Array
|
||||
if (c == '[')
|
||||
{
|
||||
back();
|
||||
return new JSONArray(this);
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
char b = c;
|
||||
while (c >= ' ' && c != ':' && c != ',' && c != ']' && c != '}' && c != '/')
|
||||
{
|
||||
sb.Append(c);
|
||||
c = next();
|
||||
}
|
||||
back();
|
||||
|
||||
s = sb.ToString().Trim();
|
||||
if (s == "true")
|
||||
return bool.Parse("true");
|
||||
if (s == "false")
|
||||
return bool.Parse("false");
|
||||
if (s == "null")
|
||||
return JSONObject.NULL;
|
||||
|
||||
if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+')
|
||||
{
|
||||
try
|
||||
{
|
||||
return Convert.ToInt32(s);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
string msg = e.Message;
|
||||
}
|
||||
try
|
||||
{
|
||||
return Convert.ToDouble(s, NumberFormatInfo.InvariantInfo);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
string msg = e.Message;
|
||||
}
|
||||
}
|
||||
if (s == "")
|
||||
{
|
||||
throw (new Exception("Missing value"));
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skip characters until the next character is the requested character.
|
||||
/// If the requested character is not found, no characters are skipped.
|
||||
/// </summary>
|
||||
/// <param name="to">A character to skip to.</param>
|
||||
/// <returns>
|
||||
/// The requested character, or zero if the requested character is not found.
|
||||
/// </returns>
|
||||
public char skipTo(char to)
|
||||
{
|
||||
char c;
|
||||
int i = myIndex;
|
||||
do
|
||||
{
|
||||
c = next();
|
||||
if (c == (char)0)
|
||||
{
|
||||
myIndex = i;
|
||||
return c;
|
||||
}
|
||||
}while (c != to);
|
||||
|
||||
back();
|
||||
return c;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skip characters until past the requested string.
|
||||
/// If it is not found, we are left at the end of the source.
|
||||
/// </summary>
|
||||
/// <param name="to">A string to skip past.</param>
|
||||
public void skipPast(string to)
|
||||
{
|
||||
myIndex = mySource.IndexOf(to, myIndex);
|
||||
if (myIndex < 0)
|
||||
{
|
||||
myIndex = mySource.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
myIndex += to.Length;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO implement exception SyntaxError
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Make a printable string of this JSONTokener.
|
||||
/// </summary>
|
||||
/// <returns>" at character [myIndex] of [mySource]"</returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return " at charachter " + myIndex + " of " + mySource;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unescape the source text. Convert %hh sequences to single characters,
|
||||
/// and convert plus to space. There are Web transport systems that insist on
|
||||
/// doing unnecessary URL encoding. This provides a way to undo it.
|
||||
/// </summary>
|
||||
public void unescape()
|
||||
{
|
||||
mySource = unescape(mySource);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert %hh sequences to single characters, and convert plus to space.
|
||||
/// </summary>
|
||||
/// <param name="s">A string that may contain plus and %hh sequences.</param>
|
||||
/// <returns>The unescaped string.</returns>
|
||||
public static string unescape(string s)
|
||||
{
|
||||
int len = s.Length;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i=0; i < len; i++)
|
||||
{
|
||||
char c = s[i];
|
||||
if (c == '+')
|
||||
{
|
||||
c = ' ';
|
||||
}
|
||||
else if (c == '%' && (i + 2 < len))
|
||||
{
|
||||
int d = dehexchar(s[i+1]);
|
||||
int e = dehexchar(s[i+2]);
|
||||
if (d >= 0 && e >= 0)
|
||||
{
|
||||
c = (char)(d*16 + e);
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
sb.Append(c);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
69
libsecondlife-cs/JSONlib/JSONUtils.cs
Normal file
69
libsecondlife-cs/JSONlib/JSONUtils.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace Nii.JSON
|
||||
{
|
||||
/// <summary>
|
||||
/// Public Domain 2002 JSON.org
|
||||
/// @author JSON.org
|
||||
/// @version 0.1
|
||||
/// Ported to C# by Are Bjolseth, teleplan.no
|
||||
/// </summary>
|
||||
public sealed class JSONUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Produce a string in double quotes with backslash sequences in all the right places.
|
||||
/// </summary>
|
||||
/// <param name="s">A String</param>
|
||||
/// <returns>A String correctly formatted for insertion in a JSON message.</returns>
|
||||
public static string Enquote(string s)
|
||||
{
|
||||
if (s == null || s.Length == 0)
|
||||
{
|
||||
return "\"\"";
|
||||
}
|
||||
char c;
|
||||
int i;
|
||||
int len = s.Length;
|
||||
StringBuilder sb = new StringBuilder(len + 4);
|
||||
string t;
|
||||
|
||||
sb.Append('"');
|
||||
for (i = 0; i < len; i += 1)
|
||||
{
|
||||
c = s[i];
|
||||
if ((c == '\\') || (c == '"') || (c == '>'))
|
||||
{
|
||||
sb.Append('\\');
|
||||
sb.Append(c);
|
||||
}
|
||||
else if (c == '\b')
|
||||
sb.Append("\\b");
|
||||
else if (c == '\t')
|
||||
sb.Append("\\t");
|
||||
else if (c == '\n')
|
||||
sb.Append("\\n");
|
||||
else if (c == '\f')
|
||||
sb.Append("\\f");
|
||||
else if (c == '\r')
|
||||
sb.Append("\\r");
|
||||
else
|
||||
{
|
||||
if (c < ' ')
|
||||
{
|
||||
//t = "000" + Integer.toHexString(c);
|
||||
string tmp = new string(c,1);
|
||||
t = "000" + int.Parse(tmp,System.Globalization.NumberStyles.HexNumber);
|
||||
sb.Append("\\u" + t.Substring(t.Length - 4));
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
sb.Append('"');
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
115
libsecondlife-cs/JSONlib/Nii.JSON.csproj
Normal file
115
libsecondlife-cs/JSONlib/Nii.JSON.csproj
Normal file
@@ -0,0 +1,115 @@
|
||||
<VisualStudioProject>
|
||||
<CSHARP
|
||||
ProjectType = "Local"
|
||||
ProductVersion = "7.10.3077"
|
||||
SchemaVersion = "2.0"
|
||||
ProjectGuid = "{C40EB7FD-F957-4659-A184-A1C28908D748}"
|
||||
>
|
||||
<Build>
|
||||
<Settings
|
||||
ApplicationIcon = ""
|
||||
AssemblyKeyContainerName = ""
|
||||
AssemblyName = "JSON"
|
||||
AssemblyOriginatorKeyFile = ""
|
||||
DefaultClientScript = "JScript"
|
||||
DefaultHTMLPageLayout = "Grid"
|
||||
DefaultTargetSchema = "IE50"
|
||||
DelaySign = "false"
|
||||
OutputType = "Library"
|
||||
PreBuildEvent = ""
|
||||
PostBuildEvent = ""
|
||||
RootNamespace = "Nii.JSON"
|
||||
RunPostBuildEvent = "OnBuildSuccess"
|
||||
StartupObject = ""
|
||||
>
|
||||
<Config
|
||||
Name = "Debug"
|
||||
AllowUnsafeBlocks = "false"
|
||||
BaseAddress = "285212672"
|
||||
CheckForOverflowUnderflow = "false"
|
||||
ConfigurationOverrideFile = ""
|
||||
DefineConstants = "DEBUG;TRACE"
|
||||
DocumentationFile = "json.doc.xml"
|
||||
DebugSymbols = "true"
|
||||
FileAlignment = "4096"
|
||||
IncrementalBuild = "false"
|
||||
NoStdLib = "false"
|
||||
NoWarn = ""
|
||||
Optimize = "false"
|
||||
OutputPath = "..\bin\Debug\"
|
||||
RegisterForComInterop = "false"
|
||||
RemoveIntegerChecks = "false"
|
||||
TreatWarningsAsErrors = "false"
|
||||
WarningLevel = "4"
|
||||
/>
|
||||
<Config
|
||||
Name = "Release"
|
||||
AllowUnsafeBlocks = "false"
|
||||
BaseAddress = "285212672"
|
||||
CheckForOverflowUnderflow = "false"
|
||||
ConfigurationOverrideFile = ""
|
||||
DefineConstants = "TRACE"
|
||||
DocumentationFile = ""
|
||||
DebugSymbols = "false"
|
||||
FileAlignment = "4096"
|
||||
IncrementalBuild = "false"
|
||||
NoStdLib = "false"
|
||||
NoWarn = ""
|
||||
Optimize = "true"
|
||||
OutputPath = "bin\Release\"
|
||||
RegisterForComInterop = "false"
|
||||
RemoveIntegerChecks = "false"
|
||||
TreatWarningsAsErrors = "false"
|
||||
WarningLevel = "4"
|
||||
/>
|
||||
</Settings>
|
||||
<References>
|
||||
<Reference
|
||||
Name = "System"
|
||||
AssemblyName = "System"
|
||||
HintPath = "..\..\..\WINNT\Microsoft.NET\Framework\v1.1.4322\System.dll"
|
||||
/>
|
||||
<Reference
|
||||
Name = "System.Data"
|
||||
AssemblyName = "System.Data"
|
||||
HintPath = "..\..\..\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Data.dll"
|
||||
/>
|
||||
<Reference
|
||||
Name = "System.XML"
|
||||
AssemblyName = "System.Xml"
|
||||
HintPath = "..\..\..\WINNT\Microsoft.NET\Framework\v1.1.4322\System.XML.dll"
|
||||
/>
|
||||
</References>
|
||||
</Build>
|
||||
<Files>
|
||||
<Include>
|
||||
<File
|
||||
RelPath = "JSONArray.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "JSONFacade.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "JSONObject.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "JSONTokener.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
<File
|
||||
RelPath = "JSONUtils.cs"
|
||||
SubType = "Code"
|
||||
BuildAction = "Compile"
|
||||
/>
|
||||
</Include>
|
||||
</Files>
|
||||
</CSHARP>
|
||||
</VisualStudioProject>
|
||||
|
||||
878
libsecondlife-cs/JSONlib/json.doc.xml
Normal file
878
libsecondlife-cs/JSONlib/json.doc.xml
Normal file
@@ -0,0 +1,878 @@
|
||||
<?xml version="1.0"?>
|
||||
<doc>
|
||||
<assembly>
|
||||
<name>JSON</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="T:Nii.JSON.JSONArray">
|
||||
<summary>
|
||||
<para>
|
||||
A JSONArray is an ordered sequence of values. Its external form is a string
|
||||
wrapped in square brackets with commas between the values. The internal form
|
||||
is an object having get() and opt() methods for accessing the values by
|
||||
index, and put() methods for adding or replacing values. The values can be
|
||||
any of these types: Boolean, JSONArray, JSONObject, Number, String, or the
|
||||
JSONObject.NULL object.
|
||||
</para>
|
||||
<para>
|
||||
The constructor can convert a JSON external form string into an
|
||||
internal form Java object. The toString() method creates an external
|
||||
form string.
|
||||
</para>
|
||||
<para>
|
||||
A get() method returns a value if one can be found, and throws an exception
|
||||
if one cannot be found. An opt() method returns a default value instead of
|
||||
throwing an exception, and so is useful for obtaining optional values.
|
||||
</para>
|
||||
<para>
|
||||
The generic get() and opt() methods return an object which you can cast or
|
||||
query for type. There are also typed get() and opt() methods that do typing
|
||||
checking and type coersion for you.
|
||||
</para>
|
||||
<para>
|
||||
The texts produced by the toString() methods are very strict.
|
||||
The constructors are more forgiving in the texts they will accept.
|
||||
</para>
|
||||
<para>
|
||||
<list type="bullet">
|
||||
<item><description>An extra comma may appear just before the closing bracket.</description></item>
|
||||
<item><description>Strings may be quoted with single quotes.</description></item>
|
||||
<item><description>Strings do not need to be quoted at all if they do not contain leading
|
||||
or trailing spaces, and if they do not contain any of these characters:
|
||||
{ } [ ] / \ : , </description></item>
|
||||
<item><description>Numbers may have the 0- (octal) or 0x- (hex) prefix.</description></item>
|
||||
</list>
|
||||
</para>
|
||||
<para>
|
||||
Public Domain 2002 JSON.org
|
||||
@author JSON.org
|
||||
@version 0.1
|
||||
</para>
|
||||
Ported to C# by Are Bjolseth, teleplan.no
|
||||
TODO:
|
||||
1. Implement Custom exceptions
|
||||
2. Add indexer JSONObject[i] = object, and object = JSONObject[i];
|
||||
3. Add indexer JSONObject["key"] = object, and object = JSONObject["key"]
|
||||
4. Add unit testing
|
||||
5. Add log4net
|
||||
6. Make get/put methods private, to force use of indexer instead?
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:Nii.JSON.JSONArray.myArrayList">
|
||||
<summary>The ArrayList where the JSONArray's properties are kept.</summary>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.#ctor">
|
||||
<summary>
|
||||
Construct an empty JSONArray
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.#ctor(Nii.JSON.JSONTokener)">
|
||||
<summary>
|
||||
Construct a JSONArray from a JSONTokener.
|
||||
</summary>
|
||||
<param name="x">A JSONTokener</param>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.#ctor(System.String)">
|
||||
<summary>
|
||||
Construct a JSONArray from a source string.
|
||||
</summary>
|
||||
<param name="s">A string that begins with '[' and ends with ']'.</param>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.#ctor(System.Collections.ICollection)">
|
||||
<summary>
|
||||
Construct a JSONArray from a Collection.
|
||||
</summary>
|
||||
<param name="collection">A Collection.</param>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.getValue(System.Int32)">
|
||||
<summary>
|
||||
Get the object value associated with an index.
|
||||
Use indexer instead!!! Added to be true to the original Java implementation
|
||||
</summary>
|
||||
<param name="i">index subscript. The index must be between 0 and length()-1</param>
|
||||
<returns>An object value.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.getArrayList">
|
||||
<summary>
|
||||
Get the ArrayList which is holding the elements of the JSONArray.
|
||||
Use the indexer instead!! Added to be true to the orignal Java src
|
||||
</summary>
|
||||
<returns>The ArrayList</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.getBoolean(System.Int32)">
|
||||
<summary>
|
||||
Get the boolean value associated with an index.
|
||||
The string values "true" and "false" are converted to boolean.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>The truth</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.getDouble(System.Int32)">
|
||||
<summary>
|
||||
Get the double value associated with an index.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>A double value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.getInt(System.Int32)">
|
||||
<summary>
|
||||
Get the int value associated with an index.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>The int value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.getJSONArray(System.Int32)">
|
||||
<summary>
|
||||
Get the JSONArray associated with an index.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>A JSONArray value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.getJSONObject(System.Int32)">
|
||||
<summary>
|
||||
Get the JSONObject associated with an index.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>A JSONObject value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.getString(System.Int32)">
|
||||
<summary>
|
||||
Get the string associated with an index.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>A string value.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.isNull(System.Int32)">
|
||||
<summary>
|
||||
Determine if the value is null.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>true if the value at the index is null, or if there is no value.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.join(System.String)">
|
||||
<summary>
|
||||
Make a string from the contents of this JSONArray. The separator string
|
||||
is inserted between each element.
|
||||
Warning: This method assumes that the data structure is acyclical.
|
||||
</summary>
|
||||
<param name="separator">separator A string that will be inserted between the elements.</param>
|
||||
<returns>A string.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.Length">
|
||||
<summary>
|
||||
Get the length of the JSONArray.
|
||||
Added to be true to the original Java implementation
|
||||
</summary>
|
||||
<returns>Number of JSONObjects in array</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.opt(System.Int32)">
|
||||
<summary>
|
||||
Get the optional object value associated with an index.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>object at that index.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.optBoolean(System.Int32)">
|
||||
<summary>
|
||||
Get the optional boolean value associated with an index.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>The truth</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.optBoolean(System.Int32,System.Boolean)">
|
||||
<summary>
|
||||
Get the optional boolean value associated with an index.
|
||||
It returns the defaultValue if there is no value at that index or if it is not
|
||||
a Boolean or the String "true" or "false".
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<param name="defaultValue"></param>
|
||||
<returns>The truth.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.optDouble(System.Int32)">
|
||||
<summary>
|
||||
Get the optional double value associated with an index.
|
||||
NaN is returned if the index is not found,
|
||||
or if the value is not a number and cannot be converted to a number.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>The double value object</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.optDouble(System.Int32,System.Double)">
|
||||
<summary>
|
||||
Get the optional double value associated with an index.
|
||||
NaN is returned if the index is not found,
|
||||
or if the value is not a number and cannot be converted to a number.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<param name="defaultValue"></param>
|
||||
<returns>The double value object</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.optInt(System.Int32)">
|
||||
<summary>
|
||||
Get the optional int value associated with an index.
|
||||
Zero is returned if the index is not found,
|
||||
or if the value is not a number and cannot be converted to a number.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>The int value object</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.optInt(System.Int32,System.Int32)">
|
||||
<summary>
|
||||
Get the optional int value associated with an index.
|
||||
The defaultValue is returned if the index is not found,
|
||||
or if the value is not a number and cannot be converted to a number.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<param name="defaultValue">The default value</param>
|
||||
<returns>The int value object</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.optJSONArray(System.Int32)">
|
||||
<summary>
|
||||
Get the optional JSONArray associated with an index.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>A JSONArray value, or null if the index has no value, or if the value is not a JSONArray.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.optJSONObject(System.Int32)">
|
||||
<summary>
|
||||
Get the optional JSONObject associated with an index.
|
||||
Null is returned if the key is not found, or null if the index has
|
||||
no value, or if the value is not a JSONObject.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>A JSONObject value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.optString(System.Int32)">
|
||||
<summary>
|
||||
Get the optional string value associated with an index. It returns an
|
||||
empty string if there is no value at that index. If the value
|
||||
is not a string and is not null, then it is coverted to a string.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<returns>A String value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.optString(System.Int32,System.String)">
|
||||
<summary>
|
||||
Get the optional string associated with an index.
|
||||
The defaultValue is returned if the key is not found.
|
||||
</summary>
|
||||
<param name="i">index subscript</param>
|
||||
<param name="defaultValue">The default value</param>
|
||||
<returns>A string value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.put(System.Object)">
|
||||
OMITTED:
|
||||
public JSONArray put(bool val)
|
||||
public JSONArray put(double val)
|
||||
public JSONArray put(int val)
|
||||
<summary>
|
||||
Append an object value.
|
||||
</summary>
|
||||
<param name="val">An object value. The value should be a Boolean, Double, Integer, JSONArray, JSObject, or String, or the JSONObject.NULL object</param>
|
||||
<returns>this (JSONArray)</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.put(System.Int32,System.Object)">
|
||||
<summary>
|
||||
Put or replace a boolean value in the JSONArray.
|
||||
</summary>
|
||||
<param name="i">
|
||||
The subscript. If the index is greater than the length of
|
||||
the JSONArray, then null elements will be added as necessary to pad it out.
|
||||
</param>
|
||||
<param name="val">An object value.</param>
|
||||
<returns>this (JSONArray)</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.toJSONObject(Nii.JSON.JSONArray)">
|
||||
<summary>
|
||||
Produce a JSONObject by combining a JSONArray of names with the values
|
||||
of this JSONArray.
|
||||
</summary>
|
||||
<param name="names">
|
||||
A JSONArray containing a list of key strings. These will be paired with the values.
|
||||
</param>
|
||||
<returns>A JSONObject, or null if there are no names or if this JSONArray</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONArray.ToString">
|
||||
<summary>
|
||||
Make an JSON external form string of this JSONArray. For compactness, no
|
||||
unnecessary whitespace is added.
|
||||
</summary>
|
||||
<returns>a printable, displayable, transmittable representation of the array.</returns>
|
||||
</member>
|
||||
<member name="P:Nii.JSON.JSONArray.Item(System.Int32)">
|
||||
<summary>
|
||||
Alternate to Java get/put method, by using indexer
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Nii.JSON.JSONArray.List">
|
||||
<summary>
|
||||
Alternativ to Java, getArrayList, by using propery
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Nii.JSON.JSONArray.Count">
|
||||
<summary>
|
||||
Get the length of the JSONArray.
|
||||
Using a propery instead of method
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Nii.JSON.JsonFacade">
|
||||
<summary>
|
||||
Summary description for JsonFacade.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JsonFacade.toJSON(System.Collections.IDictionary)">
|
||||
<summary>
|
||||
Parse a Hashtable and return a JSON formatted string
|
||||
</summary>
|
||||
<param name="idict"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JsonFacade.fromJSON(System.String)">
|
||||
<summary>
|
||||
Parse JSON formatted string and return a Hashtable
|
||||
</summary>
|
||||
<param name="sJSON"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:Nii.JSON.JSONObject">
|
||||
<summary>
|
||||
<para>
|
||||
A JSONArray is an ordered sequence of values. Its external form is a string
|
||||
wrapped in square brackets with commas between the values. The internal form
|
||||
is an object having get() and opt() methods for accessing the values by
|
||||
index, and put() methods for adding or replacing values. The values can be
|
||||
any of these types: Boolean, JSONArray, JSONObject, Number, String, or the
|
||||
JSONObject.NULL object.
|
||||
</para>
|
||||
<para>
|
||||
The constructor can convert a JSON external form string into an
|
||||
internal form Java object. The toString() method creates an external
|
||||
form string.
|
||||
</para>
|
||||
<para>
|
||||
A get() method returns a value if one can be found, and throws an exception
|
||||
if one cannot be found. An opt() method returns a default value instead of
|
||||
throwing an exception, and so is useful for obtaining optional values.
|
||||
</para>
|
||||
<para>
|
||||
The generic get() and opt() methods return an object which you can cast or
|
||||
query for type. There are also typed get() and opt() methods that do typing
|
||||
checking and type coersion for you.
|
||||
</para>
|
||||
<para>
|
||||
The texts produced by the toString() methods are very strict.
|
||||
The constructors are more forgiving in the texts they will accept.
|
||||
</para>
|
||||
<para>
|
||||
<list type="bullet">
|
||||
<item><description>An extra comma may appear just before the closing bracket.</description></item>
|
||||
<item><description>Strings may be quoted with single quotes.</description></item>
|
||||
<item><description>Strings do not need to be quoted at all if they do not contain leading
|
||||
or trailing spaces, and if they do not contain any of these characters:
|
||||
{ } [ ] / \ : , </description></item>
|
||||
<item><description>Numbers may have the 0- (octal) or 0x- (hex) prefix.</description></item>
|
||||
</list>
|
||||
</para>
|
||||
<para>
|
||||
Public Domain 2002 JSON.org
|
||||
@author JSON.org
|
||||
@version 0.1
|
||||
</para>
|
||||
Ported to C# by Are Bjolseth, teleplan.no
|
||||
TODO:
|
||||
1. Implement Custom exceptions
|
||||
2. Add indexer JSONObject[i] = object, and object = JSONObject[i];
|
||||
3. Add indexer JSONObject["key"] = object, and object = JSONObject["key"]
|
||||
4. Add unit testing
|
||||
5. Add log4net
|
||||
6. Make get/put methods private, to force use of indexer instead?
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:Nii.JSON.JSONObject.myHashMap">
|
||||
<summary>The hash map where the JSONObject's properties are kept.</summary>
|
||||
</member>
|
||||
<member name="F:Nii.JSON.JSONObject.myKeyIndexList">
|
||||
<summary>A shadow list of keys to enable access by sequence of insertion</summary>
|
||||
</member>
|
||||
<member name="F:Nii.JSON.JSONObject.NULL">
|
||||
<summary>
|
||||
It is sometimes more convenient and less ambiguous to have a NULL
|
||||
object than to use C#'s null value.
|
||||
JSONObject.NULL.toString() returns "null".
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.#ctor">
|
||||
<summary>
|
||||
Construct an empty JSONObject.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.#ctor(Nii.JSON.JSONTokener)">
|
||||
<summary>
|
||||
Construct a JSONObject from a JSONTokener.
|
||||
</summary>
|
||||
<param name="x">A JSONTokener object containing the source string.</param>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.#ctor(System.String)">
|
||||
<summary>
|
||||
Construct a JSONObject from a string.
|
||||
</summary>
|
||||
<param name="sJSON">A string beginning with '{' and ending with '}'.</param>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.#ctor(System.Collections.IDictionary)">
|
||||
<summary>
|
||||
Construct a JSONObject from a IDictionary
|
||||
</summary>
|
||||
<param name="map"></param>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.accumulate(System.String,System.Object)">
|
||||
<summary>
|
||||
Accumulate values under a key. It is similar to the put method except
|
||||
that if there is already an object stored under the key then a
|
||||
JSONArray is stored under the key to hold all of the accumulated values.
|
||||
If there is already a JSONArray, then the new value is appended to it.
|
||||
In contrast, the put method replaces the previous value.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<param name="val">An object to be accumulated under the key.</param>
|
||||
<returns>this</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.getDictionary">
|
||||
<summary>
|
||||
C# convenience method
|
||||
</summary>
|
||||
<returns>The Hashtable</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.getValue(System.String)">
|
||||
<summary>
|
||||
Alias to Java get method
|
||||
Get the value object associated with a key.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<returns>The object associated with the key.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.getBool(System.String)">
|
||||
<summary>
|
||||
Get the boolean value associated with a key.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<returns>The truth.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.getDouble(System.String)">
|
||||
<summary>
|
||||
Get the double value associated with a key.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<returns>The double value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.getInt(System.String)">
|
||||
<summary>
|
||||
Get the int value associated with a key.
|
||||
</summary>
|
||||
<param name="key">A key string</param>
|
||||
<returns> The integer value.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.getJSONArray(System.String)">
|
||||
<summary>
|
||||
Get the JSONArray value associated with a key.
|
||||
</summary>
|
||||
<param name="key">A key string</param>
|
||||
<returns>A JSONArray which is the value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.getJSONObject(System.String)">
|
||||
<summary>
|
||||
Get the JSONObject value associated with a key.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<returns>A JSONObject which is the value.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.getString(System.String)">
|
||||
<summary>
|
||||
Get the string associated with a key.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<returns>A string which is the value.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.has(System.String)">
|
||||
<summary>
|
||||
Determine if the JSONObject contains a specific key.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<returns>true if the key exists in the JSONObject.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.keys">
|
||||
<summary>
|
||||
Get an enumeration of the keys of the JSONObject.
|
||||
Added to be true to orginal Java implementation
|
||||
Indexers are easier to use
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.isNull(System.String)">
|
||||
<summary>
|
||||
Determine if the value associated with the key is null or if there is no value.
|
||||
</summary>
|
||||
<param name="key">A key string</param>
|
||||
<returns>true if there is no value associated with the key or if the valus is the JSONObject.NULL object</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.Length">
|
||||
<summary>
|
||||
Get the number of keys stored in the JSONObject.
|
||||
</summary>
|
||||
<returns>The number of keys in the JSONObject.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.names">
|
||||
<summary>
|
||||
Produce a JSONArray containing the names of the elements of this JSONObject
|
||||
</summary>
|
||||
<returns>A JSONArray containing the key strings, or null if the JSONObject</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.numberToString(System.Object)">
|
||||
<summary>
|
||||
Produce a string from a number.
|
||||
</summary>
|
||||
<param name="number">Number value type object</param>
|
||||
<returns>String representation of the number</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.opt(System.String)">
|
||||
<summary>
|
||||
Get an optional value associated with a key.
|
||||
</summary>
|
||||
<param name="key">A key string</param>
|
||||
<returns>An object which is the value, or null if there is no value.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.optBoolean(System.String)">
|
||||
<summary>
|
||||
Get an optional value associated with a key.
|
||||
It returns false if there is no such key, or if the value is not
|
||||
Boolean.TRUE or the String "true".
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<returns>bool value object</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.optBoolean(System.String,System.Boolean)">
|
||||
<summary>
|
||||
Get an optional value associated with a key.
|
||||
It returns false if there is no such key, or if the value is not
|
||||
Boolean.TRUE or the String "true".
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<param name="defaultValue">The preferred return value if conversion fails</param>
|
||||
<returns>bool value object</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.optDouble(System.String)">
|
||||
<summary>
|
||||
Get an optional double associated with a key,
|
||||
or NaN if there is no such key or if its value is not a number.
|
||||
If the value is a string, an attempt will be made to evaluate it as
|
||||
a number.
|
||||
</summary>
|
||||
<param name="key">A string which is the key.</param>
|
||||
<returns>A double value object</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.optDouble(System.String,System.Double)">
|
||||
<summary>
|
||||
Get an optional double associated with a key,
|
||||
or NaN if there is no such key or if its value is not a number.
|
||||
If the value is a string, an attempt will be made to evaluate it as
|
||||
a number.
|
||||
</summary>
|
||||
<param name="key">A string which is the key.</param>
|
||||
<param name="defaultValue">The default</param>
|
||||
<returns>A double value object</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.optInt(System.String)">
|
||||
<summary>
|
||||
Get an optional double associated with a key, or the
|
||||
defaultValue if there is no such key or if its value is not a number.
|
||||
If the value is a string, an attempt will be made to evaluate it as
|
||||
number.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<returns>An int object value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.optInt(System.String,System.Int32)">
|
||||
<summary>
|
||||
Get an optional double associated with a key, or the
|
||||
defaultValue if there is no such key or if its value is not a number.
|
||||
If the value is a string, an attempt will be made to evaluate it as
|
||||
number.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<param name="defaultValue">The default value</param>
|
||||
<returns>An int object value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.optJSONArray(System.String)">
|
||||
<summary>
|
||||
Get an optional JSONArray associated with a key.
|
||||
It returns null if there is no such key, or if its value is not a JSONArray
|
||||
</summary>
|
||||
<param name="key">A key string</param>
|
||||
<returns>A JSONArray which is the value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.optJSONObject(System.String)">
|
||||
<summary>
|
||||
Get an optional JSONObject associated with a key.
|
||||
It returns null if there is no such key, or if its value is not a JSONObject.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<returns>A JSONObject which is the value</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.optString(System.String)">
|
||||
<summary>
|
||||
Get an optional string associated with a key.
|
||||
It returns an empty string if there is no such key. If the value is not
|
||||
a string and is not null, then it is coverted to a string.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<returns>A string which is the value.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.optString(System.String,System.String)">
|
||||
<summary>
|
||||
Get an optional string associated with a key.
|
||||
It returns the defaultValue if there is no such key.
|
||||
</summary>
|
||||
<param name="key">A key string.</param>
|
||||
<param name="defaultValue">The default</param>
|
||||
<returns>A string which is the value.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.put(System.String,System.Object)">
|
||||
<summary>
|
||||
Put a key/value pair in the JSONObject. If the value is null,
|
||||
then the key will be removed from the JSONObject if it is present.
|
||||
</summary>
|
||||
<param name="key"> A key string.</param>
|
||||
<param name="val">
|
||||
An object which is the value. It should be of one of these
|
||||
types: Boolean, Double, Integer, JSONArray, JSONObject, String, or the
|
||||
JSONObject.NULL object.
|
||||
</param>
|
||||
<returns>JSONObject</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.putOpt(System.String,System.Object)">
|
||||
<summary>
|
||||
Add a key value pair
|
||||
</summary>
|
||||
<param name="key"></param>
|
||||
<param name="val"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.remove(System.String)">
|
||||
<summary>
|
||||
Remove a object assosiateted with the given key
|
||||
</summary>
|
||||
<param name="key"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.toJSONArray(Nii.JSON.JSONArray)">
|
||||
<summary>
|
||||
Append an array of JSONObjects to current object
|
||||
</summary>
|
||||
<param name="names"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.ToString">
|
||||
<summary>
|
||||
Overridden to return a JSON formattet object as a string
|
||||
</summary>
|
||||
<returns>JSON object as formatted string</returns>
|
||||
</member>
|
||||
<member name="P:Nii.JSON.JSONObject.Item(System.Int32)">
|
||||
<summary>
|
||||
Return the key for the associated index
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Nii.JSON.JSONObject.Item(System.String)">
|
||||
<summary>
|
||||
Get/Add an object with the associated key
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Nii.JSON.JSONObject.Count">
|
||||
<summary>
|
||||
Return the number of JSON items in hashtable
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Nii.JSON.JSONObject.JSONNull">
|
||||
<summary>
|
||||
Make a Null object
|
||||
JSONObject.NULL is equivalent to the value that JavaScript calls null,
|
||||
whilst C#'s null is equivalent to the value that JavaScript calls undefined.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONObject.JSONNull.ToString">
|
||||
<summary>
|
||||
Overriden to return "null"
|
||||
</summary>
|
||||
<returns>null</returns>
|
||||
</member>
|
||||
<member name="T:Nii.JSON.JSONTokener">
|
||||
<summary>
|
||||
<para>
|
||||
A JSONTokener takes a source string and extracts characters and tokens from
|
||||
it. It is used by the JSONObject and JSONArray constructors to parse
|
||||
JSON source strings.
|
||||
</para>
|
||||
<para>
|
||||
Public Domain 2002 JSON.org
|
||||
@author JSON.org
|
||||
@version 0.1
|
||||
</para>
|
||||
<para>Ported to C# by Are Bjolseth, teleplan.no</para>
|
||||
<para>
|
||||
<list type="bullet">
|
||||
<item><description>Implement Custom exceptions</description></item>
|
||||
<item><description>Add unit testing</description></item>
|
||||
<item><description>Add log4net</description></item>
|
||||
</list>
|
||||
</para>
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:Nii.JSON.JSONTokener.myIndex">
|
||||
<summary>The index of the next character.</summary>
|
||||
</member>
|
||||
<member name="F:Nii.JSON.JSONTokener.mySource">
|
||||
<summary>The source string being tokenized.</summary>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.#ctor(System.String)">
|
||||
<summary>
|
||||
Construct a JSONTokener from a string.
|
||||
</summary>
|
||||
<param name="s">A source string.</param>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.back">
|
||||
<summary>
|
||||
Back up one character. This provides a sort of lookahead capability,
|
||||
so that you can test for a digit or letter before attempting to parse
|
||||
the next number or identifier.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.dehexchar(System.Char)">
|
||||
<summary>
|
||||
Get the hex value of a character (base16).
|
||||
</summary>
|
||||
<param name="c">
|
||||
A character between '0' and '9' or between 'A' and 'F' or
|
||||
between 'a' and 'f'.
|
||||
</param>
|
||||
<returns>An int between 0 and 15, or -1 if c was not a hex digit.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.more">
|
||||
<summary>
|
||||
Determine if the source string still contains characters that next() can consume.
|
||||
</summary>
|
||||
<returns>true if not yet at the end of the source.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.next">
|
||||
<summary>
|
||||
Get the next character in the source string.
|
||||
</summary>
|
||||
<returns>The next character, or 0 if past the end of the source string.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.next(System.Char)">
|
||||
<summary>
|
||||
Consume the next character, and check that it matches a specified character
|
||||
</summary>
|
||||
<param name="c">The character to match.</param>
|
||||
<returns>The character.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.next(System.Int32)">
|
||||
<summary>
|
||||
Get the next n characters.
|
||||
</summary>
|
||||
<param name="n">The number of characters to take.</param>
|
||||
<returns>A string of n characters.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.nextClean">
|
||||
<summary>
|
||||
Get the next char in the string, skipping whitespace
|
||||
and comments (slashslash and slashstar).
|
||||
</summary>
|
||||
<returns>A character, or 0 if there are no more characters.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.nextString(System.Char)">
|
||||
<summary>
|
||||
Return the characters up to the next close quote character.
|
||||
Backslash processing is done. The formal JSON format does not
|
||||
allow strings in single quotes, but an implementation is allowed to
|
||||
accept them.
|
||||
</summary>
|
||||
<param name="quote">The quoting character, either " or '</param>
|
||||
<returns>A String.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.nextTo(System.Char)">
|
||||
<summary>
|
||||
Get the text up but not including the specified character or the
|
||||
end of line, whichever comes first.
|
||||
</summary>
|
||||
<param name="d">A delimiter character.</param>
|
||||
<returns>A string.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.nextTo(System.String)">
|
||||
<summary>
|
||||
Get the text up but not including one of the specified delimeter
|
||||
characters or the end of line, which ever comes first.
|
||||
</summary>
|
||||
<param name="delimiters">A set of delimiter characters.</param>
|
||||
<returns>A string, trimmed.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.nextObject">
|
||||
<summary>
|
||||
Get the next value as object. The value can be a Boolean, Double, Integer,
|
||||
JSONArray, JSONObject, or String, or the JSONObject.NULL object.
|
||||
</summary>
|
||||
<returns>An object.</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.skipTo(System.Char)">
|
||||
<summary>
|
||||
Skip characters until the next character is the requested character.
|
||||
If the requested character is not found, no characters are skipped.
|
||||
</summary>
|
||||
<param name="to">A character to skip to.</param>
|
||||
<returns>
|
||||
The requested character, or zero if the requested character is not found.
|
||||
</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.skipPast(System.String)">
|
||||
<summary>
|
||||
Skip characters until past the requested string.
|
||||
If it is not found, we are left at the end of the source.
|
||||
</summary>
|
||||
<param name="to">A string to skip past.</param>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.ToString">
|
||||
<summary>
|
||||
Make a printable string of this JSONTokener.
|
||||
</summary>
|
||||
<returns>" at character [myIndex] of [mySource]"</returns>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.unescape">
|
||||
<summary>
|
||||
Unescape the source text. Convert %hh sequences to single characters,
|
||||
and convert plus to space. There are Web transport systems that insist on
|
||||
doing unnecessary URL encoding. This provides a way to undo it.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONTokener.unescape(System.String)">
|
||||
<summary>
|
||||
Convert %hh sequences to single characters, and convert plus to space.
|
||||
</summary>
|
||||
<param name="s">A string that may contain plus and %hh sequences.</param>
|
||||
<returns>The unescaped string.</returns>
|
||||
</member>
|
||||
<member name="T:Nii.JSON.JSONUtils">
|
||||
<summary>
|
||||
Public Domain 2002 JSON.org
|
||||
@author JSON.org
|
||||
@version 0.1
|
||||
Ported to C# by Are Bjolseth, teleplan.no
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Nii.JSON.JSONUtils.Enquote(System.String)">
|
||||
<summary>
|
||||
Produce a string in double quotes with backslash sequences in all the right places.
|
||||
</summary>
|
||||
<param name="s">A String</param>
|
||||
<returns>A String correctly formatted for insertion in a JSON message.</returns>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
||||
Reference in New Issue
Block a user