Beispiel #1
0
 /// <summary>
 /// Automatically determines appropriate type for JSON object
 /// float, double, int and long get cast into a float container
 /// unknown types will throw an Exception
 /// </summary>
 public Ferr_JSONValue(object aData)
 {
     if (aData == null)
     {
         type = Ferr_JSONType.Object;
         data = null;
     }
     else if (aData is string)
     {
         type = Ferr_JSONType.String;
     }
     else if (aData is object[])
     {
         type = Ferr_JSONType.Array;
     }
     else if (aData is bool)
     {
         type = Ferr_JSONType.Bool;
     }
     else if (aData is float || aData is double || aData is int || aData is long)
     {
         type  = Ferr_JSONType.Number;
         aData = (Convert.ToSingle(aData));
     }
     else if (aData is Ferr_JSONObject)
     {
         type  = Ferr_JSONType.Object;
         aData = (object)(new Ferr_JSONObject[] { (Ferr_JSONObject)aData });
     }
     else
     {
         throw new Exception("Can't convert type '" + aData.GetType().Name + "' to a JSON value!");
     }
     data = aData;
 }
Beispiel #2
0
 public Ferr_JSONValue  Set(int aIndex, Ferr_JSONValue aVal)
 {
     if (type == Ferr_JSONType.Array)
     {
         object[] list = (object[])data;
         if (list.Length <= aIndex)
         {
             Array.Resize <object>(ref list, aIndex + 1);
             data = list;
         }
         list[aIndex] = aVal;
         return(aVal);
     }
     else
     {
         type = Ferr_JSONType.Array;
         object[] list = new object[aIndex + 1];
         list[aIndex] = aVal;
         data         = list;
         return(aVal);
     }
 }
Beispiel #3
0
	/// <summary>
	/// Automatically determines appropriate type for JSON object
	/// float, double, int and long get cast into a float container
	/// unknown types will throw an Exception
	/// </summary>
	public Ferr_JSONValue(object aData) {
		if (aData == null) {
			type = Ferr_JSONType.Object;
			data = null;
		} else if (aData is string) {
			type = Ferr_JSONType.String;
		} else if (aData is object[]) {
			type = Ferr_JSONType.Array;
	    } else if (aData is bool) {
			type = Ferr_JSONType.Bool;
	    } else if (aData is float || aData is double || aData is int || aData is long) {
			type  = Ferr_JSONType.Number;
			aData = (Convert.ToSingle(aData));
	    } else if (aData is Ferr_JSONObject) {
			type  = Ferr_JSONType.Object;
			aData = (object)(new Ferr_JSONObject[] { (Ferr_JSONObject)aData });
		} else {
			throw new Exception("Can't convert type '" + aData.GetType().Name + "' to a JSON value!");
		}
		data = aData;
	}
Beispiel #4
0
	/// <summary>
	/// Creates a JSONValue with type Object, and data of null.
	/// </summary>
	public Ferr_JSONValue() {
		type = Ferr_JSONType.Object;
		data = null;
	}
Beispiel #5
0
	public  Ferr_JSONValue  Set              (int    aIndex, Ferr_JSONValue aVal) {
		if (type == Ferr_JSONType.Array) {
			object[] list = (object[])data;
			if (list.Length <= aIndex) {
				Array.Resize<object>(ref list, aIndex+1);
				data = list;
			}
			list[aIndex] = aVal;
			return aVal;
		} else {
			type = Ferr_JSONType.Array;
			object[] list = new object[aIndex+1];
			list[aIndex] = aVal;
			data = list;
			return aVal;
		}
	}
Beispiel #6
0
	/// <summary>
	/// Recursively converts a JSON string into a JSON data structure. Throws an exception
	/// if given an empty string. Unrecognized data is converted into a string.
	/// </summary>
	public void FromString(string aValue) {
		string text = aValue.Trim();
		float  fVal = 0;
		if (text.Length <= 0) throw new Exception("Bad JSON value: " + aValue);
		
		if (text[0] == '{') {
			type = Ferr_JSONType.Object;
			text = text.Remove(0,1);
			text = text.Remove(text.Length-1,1);
			string         [] items  = Ferr_JSON._Split(text, ',', true);
			Ferr_JSONObject[] result = new Ferr_JSONObject[items.Length];
			for (int i = 0; i < items.Length; i++) {
				result[i] = new Ferr_JSONObject(items[i]);
			}
			data = result;
		} else if (text[0] == '[') {
			type = Ferr_JSONType.Array;
			text = text.Remove(0,1);
			text = text.Remove(text.Length-1,1);
			string        [] items  = Ferr_JSON._Split(text, ',', true);
			Ferr_JSONValue[] result = new Ferr_JSONValue[items.Length];
			for (int i = 0; i < items.Length; i++) {
				result[i] = new Ferr_JSONValue();
				result[i].FromString (items[i]);
			}
			data = result;
		} else if (text[0] == '\"') {
			type = Ferr_JSONType.String;
			text = text.Remove(0,1);
			text = text.Remove(text.Length-1,1);
			data = text;
		} else if (float.TryParse(text, out fVal) ) {
			type = Ferr_JSONType.Number;
			data = fVal;
		} else {
			string lower = text.ToLower();
			if (lower == "false" || lower == "true") {
				type = Ferr_JSONType.Bool;
				data = bool.Parse(text);
			} else if (lower == "null") {
				type = Ferr_JSONType.Object;
				data = null;
			} else {
				type = Ferr_JSONType.String;
				data = text;
			}
		}
	}
Beispiel #7
0
 /// <summary>
 /// Creates a JSONValue with type Object, and data of null.
 /// </summary>
 public Ferr_JSONValue()
 {
     type = Ferr_JSONType.Object;
     data = null;
 }
Beispiel #8
0
    /// <summary>
    /// Recursively converts a JSON string into a JSON data structure. Throws an exception
    /// if given an empty string. Unrecognized data is converted into a string.
    /// </summary>
    public void FromString(string aValue)
    {
        string text = aValue.Trim();
        float  fVal = 0;

        if (text.Length <= 0)
        {
            throw new Exception("Bad JSON value: " + aValue);
        }

        if (text[0] == '{')
        {
            type = Ferr_JSONType.Object;
            text = text.Remove(0, 1);
            text = text.Remove(text.Length - 1, 1);
            string         [] items  = Ferr_JSON._Split(text, ',', true);
            Ferr_JSONObject[] result = new Ferr_JSONObject[items.Length];
            for (int i = 0; i < items.Length; i++)
            {
                result[i] = new Ferr_JSONObject(items[i]);
            }
            data = result;
        }
        else if (text[0] == '[')
        {
            type = Ferr_JSONType.Array;
            text = text.Remove(0, 1);
            text = text.Remove(text.Length - 1, 1);
            string        [] items  = Ferr_JSON._Split(text, ',', true);
            Ferr_JSONValue[] result = new Ferr_JSONValue[items.Length];
            for (int i = 0; i < items.Length; i++)
            {
                result[i] = new Ferr_JSONValue();
                result[i].FromString(items[i]);
            }
            data = result;
        }
        else if (text[0] == '\"')
        {
            type = Ferr_JSONType.String;
            text = text.Remove(0, 1);
            text = text.Remove(text.Length - 1, 1);
            data = text;
        }
        else if (float.TryParse(text, out fVal))
        {
            type = Ferr_JSONType.Number;
            data = fVal;
        }
        else
        {
            string lower = text.ToLower();
            if (lower == "false" || lower == "true")
            {
                type = Ferr_JSONType.Bool;
                data = bool.Parse(text);
            }
            else if (lower == "null")
            {
                type = Ferr_JSONType.Object;
                data = null;
            }
            else
            {
                type = Ferr_JSONType.String;
                data = text;
            }
        }
    }