Stripped XmlRpcCS and JSONlib down to just the methods we use.

git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@100 52acb1d6-8a22-11de-b505-999d5b087335
This commit is contained in:
axial
2006-07-28 10:52:01 +00:00
parent 536ec5fbb7
commit e3ea1f1561
21 changed files with 5 additions and 3234 deletions

View File

@@ -103,7 +103,6 @@ namespace Nii.JSON
/// </summary>
public class JSONObject
{
#region Local struct JSONNull
/// <summary>
/// Make a Null object
/// JSONObject.NULL is equivalent to the value that JavaScript calls null,
@@ -133,7 +132,6 @@ namespace Nii.JSON
return "null";
}
}
#endregion
///<summary>The hash map where the JSONObject's properties are kept.</summary>
private Hashtable myHashMap;
@@ -148,7 +146,7 @@ namespace Nii.JSON
/// </summary>
public static readonly JSONNull NULL = new JSONNull();
#region Constructors for JSONObject
/// <summary>
/// Construct an empty JSONObject.
/// </summary>
@@ -223,106 +221,6 @@ namespace Nii.JSON
}
// 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>
@@ -331,416 +229,7 @@ namespace Nii.JSON
{
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)
@@ -768,7 +257,6 @@ namespace Nii.JSON
{
if (!myHashMap.ContainsKey(key))
{
string test = key;
myHashMap.Add(key,val);
myKeyIndexList.Add(key);
}
@@ -784,22 +272,6 @@ namespace Nii.JSON
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>
@@ -817,66 +289,5 @@ namespace Nii.JSON
}
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();
}
}
}