using System; using System.Globalization; namespace Boomlagoon.JSON { public class JSONValue { public JSONValue(JSONValueType type) { this.Type = type; } public JSONValue(string str) { this.Type = JSONValueType.String; this.Str = str; } public JSONValue(double number) { this.Type = JSONValueType.Number; this.Number = number; } public JSONValue(JSONObject obj) { if (obj == null) { this.Type = JSONValueType.Null; } else { this.Type = JSONValueType.Object; this.Obj = obj; } } public JSONValue(JSONArray array) { this.Type = JSONValueType.Array; this.Array = array; } public JSONValue(bool boolean) { this.Type = JSONValueType.Boolean; this.Boolean = boolean; } public JSONValue(JSONValue value) { this.Type = value.Type; switch (this.Type) { case JSONValueType.String: this.Str = value.Str; break; case JSONValueType.Number: this.Number = value.Number; break; case JSONValueType.Object: if (value.Obj != null) { this.Obj = new JSONObject(value.Obj); } break; case JSONValueType.Array: this.Array = new JSONArray(value.Array); break; case JSONValueType.Boolean: this.Boolean = value.Boolean; break; } } public JSONValueType Type { get; private set; } public string Str { get; set; } public double Number { get; set; } public JSONObject Obj { get; set; } public JSONArray Array { get; set; } public bool Boolean { get; set; } public JSONValue Parent { get; set; } public static implicit operator JSONValue(string str) { return new JSONValue(str); } public static implicit operator JSONValue(double number) { return new JSONValue(number); } public static implicit operator JSONValue(JSONObject obj) { return new JSONValue(obj); } public static implicit operator JSONValue(JSONArray array) { return new JSONValue(array); } public static implicit operator JSONValue(bool boolean) { return new JSONValue(boolean); } public override string ToString() { switch (this.Type) { case JSONValueType.String: return "\"" + this.Str + "\""; case JSONValueType.Number: return this.Number.ToString(CultureInfo.InvariantCulture); case JSONValueType.Object: return this.Obj.ToString(); case JSONValueType.Array: return this.Array.ToString(); case JSONValueType.Boolean: return (!this.Boolean) ? "false" : "true"; case JSONValueType.Null: return "null"; default: return "null"; } } } }