counterorbit.org

counterorbit.json

counterorbit.json provides a DOM-like API for JSON data. counterorbit.json is available for .NET.

Downloads are available on the Files page.

This project is distributed under the GNU Lesser General Public License version 3.

.NET API

JsonKind

The JsonKind enumeration defines constants, one for each kind of JSON value.

  • Array
  • Boolean
  • Number
  • Object
  • String

JsonFactory

The JsonFactory creates new IJsonValue objects either by parsing JSON text or by converting .NET types.

public static IJsonValue Parse(string s)
Parse JSON data and return an IJsonValue. This method throws JsonFactoryException if there is an error parsing the JSON data. (Added in 0.1.2; previously this method returned null.)
public static IJsonValue CreateArray()
Return a new, empty array IJsonValue.
public static IJsonValue CreateArray(IList<IJsonValue> list)
Return a new array IJsonValue that contains all of the IJsonValues from the specified list.
public static IJsonValue CreateBoolean(bool value)
Return a new boolean IJsonValue.
public static IJsonValue CreateNumber(decimal value)
Return a new number IJsonValue.
public static IJsonValue CreateNumber(double value)
Return a new number IJsonValue.
public static IJsonValue CreateNumber(float value)
Return a new number IJsonValue.
public static IJsonValue CreateNumber(int value)
Return a new number IJsonValue.
public static IJsonValue CreateNumber(long value)
Return a new number IJsonValue.
public static IJsonValue CreateNumber(string value)
Return a new number IJsonValue. There is currently no verification to ensure that the string value is a valid JSON number. This will be fixed in a future release.
public static IJsonValue CreateObject()
Return a new, empty object IJsonValue.
public static IJsonValue CreateObject(IDictionary<string, IJsonValue> dictionary)
Return a new object IJsonValue that contains all of the keys and values from the specified dictionary.
public static IJsonValue CreateString(string value)
Return a new string IJsonValue.

IJsonValue

The counterorbit.json library implements a universal object pattern. Each value implements the IJsonValue interface. It is never necessary to cast IJsonValue objects to some other class or interface. The entire functionality of the API is available through the IJsonValue interface, although not every operation is supported for every kind of value.

public JsonKind Kind { get; }
Return the JsonKind for the value.
public void Write(TextWriter textWriter)
Convert the IJsonValue to JSON text and write it to the specified TextWriter.
Conversion Members

The conversion methods convert an IJsonValue to a native representation. The conversion methods throw NotSupportedException when a conversion isn't supported for a particular kind of value.

The conversion methods have a generous definition of compatibility. For example, if a value is a JSON string that contains valid numeric data, then the numeric conversions will succeed for that value. The exact compatibility for each conversion method is listed below.

public bool ToBool()
Convert the IJsonValue to a bool. This method works for both actual boolean JSON values and for JSON string values that contain "true" or "false". This method uses bool.Parse to convert the JSON value to a bool. It throws the same exceptions as bool.Parse (TODO: document these).
public decimal ToDecimal()
Convert the IJsonValue to a decimal. This method works for both actual number JSON values and for JSON strings values that contain numeric data. This method uses decimal.Parse to convert the JSON value to a decimal. It throws the same exceptions as decimal.Parse.
public IDictionary<string, IJsonValue> ToDictionary()
Convert the IJsonValue to an IDictionary. This method works for any kind of JSON value. For JSON objects it returns a useful dictionary. For other kinds of values it returns a read-only empty dictionary.
public double ToDouble()
Convert the IJsonValue to a double. This method works for both actual number JSON values and for JSON string values that contain numeric data. This method uses double.Parse to convert the JSON value to a double. It throws the same exceptions as double.Parse.
public IList<IJsonValue> ToList()
Convert the IJsonValue to an IList. This method works for any kind of JSON value. For JSON arrays it returns a useful list. For other kinds of values it returns a read-only empty list.
public float ToFloat()
Convert the IJsonValue to a float. This method works for both actual number JSON values and for JSON string values that contain numeric data. Thsi method uses float.Parse to convert the JSON value to a float. It throws the same exceptions as float.Parse.
public int ToInt()
Convert the IJsonValue to an int. This method works for both actual number JSON values and for JSON string values that contain numeric data. This method uses int.Parse to convert the JSON value to an int. It throws the same exceptions as int.Parse.
public long ToLong()
Convert the IJsonValue to a long. This method works for both actual number JSON values and for JSON string values that contain numeric data. This method uses long.Parse to convert the JSON value to a long. It throws the same exceptions as long.Parse.
ICollection Members

IJsonValue does not implement ICollection (for technical reasons) but it supports all of the usual ICollection methods. Use the ToList method if you want an actual ICollection value.

public int Count { get; }
This property returns the number of items in an array or object JSON value. This property always returns 0 for other kinds of JSON values.
public bool IsReadOnly()
This property returns whether or not the IJsonValue is read-only. It returns false for array and object JSON values and true for all other kinds of values.
public void Add(IJsonValue item)
This method adds the specified item to a JSON array. It only works for array values. It throws NotSupportedException when invoked on other kinds of values.
public void Clear()
This method removes all items from an array or object JSON value. It has no effect for other kinds of JSON values.
public bool Contains(IJsonValue item)
Returns whether or not an array JSON value contains the specified item. It only works for array values. It always returns false for other kinds of JSON values.
public bool Remove(IJsonValue item)
This method removes an item from an array JSON value. It returns true if the item was a member of the array value. It only works for array values. It always returns false for other kinds of JSON values.
IDictionary Members

IJsonValue does not implement IDictionary (for technical reasons) but it supports all of the usual IDictionary methods. Use the ToDictionary method if you want an actual IDictionary value.

public IJsonValue this[string key] { get; set; }
public ICollection<string> Keys { get; }
public ICollection<IJsonValue> Values { get; }Values
public void Add(string key, IJsonValue value)
public bool ContainsKey(string key)
public bool Remove(string key)
public bool TryGetValue(string key, out IJsonValue value)
IList Members

IJsonValue does not implement IList (for technical reasons) but it supports all of the usual IList methods. Use the ToList method if you want an actual IList value.

public IJsonValue this[int index] { get; set; }
public int IndexOf(IJsonValue item)
public void Insert(int index, IJsonValue item)
public void RemoveAt(int index)