/// <summary> /// Public constructor that accepts any object /// </summary> /// <param name="objValue">object to be reflected/evaluated for JSON conversion</param> public JSONReflector(object objValue) { Dictionary<JSONStringValue, JSONValue> jsonNameValuePairs = new Dictionary<JSONStringValue, JSONValue>(); Type type = objValue.GetType(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo pi in properties) { if (pi.GetIndexParameters().Length == 0) { JSONStringValue jsonParameterName = new JSONStringValue(pi.Name); JSONValue jsonParameterValue = this.GetJSONValue(pi.GetValue(objValue, null)); if (jsonParameterValue != null) { jsonNameValuePairs.Add(jsonParameterName, jsonParameterValue); } } } FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (FieldInfo fi in fields) { JSONStringValue jsonParameterName = new JSONStringValue(fi.Name); JSONValue jsonParameterValue = this.GetJSONValue(fi.GetValue(objValue)); if (jsonParameterValue != null) { jsonNameValuePairs.Add(jsonParameterName, jsonParameterValue); } } this._jsonObjectCollection = new JSONObjectCollection(jsonNameValuePairs); }
private JSONValue GetJSONValue(object objValue) { if (objValue == null) { return null; } Type thisType = objValue.GetType(); JSONValue jsonValue = null; if (thisType == typeof(System.Int32)) { jsonValue = new JSONNumberValue(Convert.ToInt32(objValue)); } else if (thisType == typeof(System.Single)) { jsonValue = new JSONNumberValue(Convert.ToSingle(objValue)); } else if (thisType == typeof(System.Double)) { jsonValue = new JSONNumberValue(Convert.ToDouble(objValue)); } else if (thisType == typeof(System.Decimal)) { jsonValue = new JSONNumberValue(Convert.ToDecimal(objValue)); } else if (thisType == typeof(System.Byte)) { jsonValue = new JSONNumberValue(Convert.ToByte(objValue)); } else if (thisType == typeof(System.String)) { jsonValue = new JSONStringValue(Convert.ToString(objValue)); } else if (thisType == typeof(System.Boolean)) { jsonValue = new JSONBoolValue(Convert.ToBoolean(objValue)); } else if (thisType.BaseType == typeof(System.Enum)) { jsonValue = new JSONStringValue(Enum.GetName(thisType, objValue)); } else if (thisType.IsArray) { List<JSONValue> jsonValues = new List<JSONValue>(); Array arrValue = (Array)objValue; for (int x = 0; x < arrValue.Length; x++) { JSONValue jsValue = this.GetJSONValue(arrValue.GetValue(x)); jsonValues.Add(jsValue); } jsonValue = new JSONArrayCollection(jsonValues); } else if ( typeof(IDictionary<string, string>).IsAssignableFrom(thisType)) { Dictionary<JSONStringValue, JSONValue> jsonNameValuePairs = new Dictionary<JSONStringValue, JSONValue>(); foreach (KeyValuePair<string, string> pair in (IDictionary<string, string>)objValue) { JSONStringValue jsonParameterName = new JSONStringValue(pair.Key); JSONStringValue jsonParameterValue = new JSONStringValue(pair.Value); if (jsonParameterValue != null) { jsonNameValuePairs.Add(jsonParameterName, jsonParameterValue); } } jsonValue = new JSONObjectCollection(jsonNameValuePairs); } else if (thisType.IsSubclassOf(typeof(System.Object))) { jsonValue = new JSONReflector(objValue); } return jsonValue; }
/// <summary> /// Public constructor that accepts any object /// </summary> /// <param name="objValue">object to be reflected/evaluated for JSON conversion</param> public JSONReflector(object objValue) { Dictionary<JSONStringValue, JSONValue> jsonNameValuePairs = new Dictionary<JSONStringValue, JSONValue>(); Type type = objValue.GetType(); PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo pi in properties) { JSONStringValue jsonParameterName = new JSONStringValue(pi.Name); object val = pi.GetValue(objValue, null); JSONValue jsonParameterValue = this.GetJSONValue(val); if (jsonParameterValue != null) { jsonNameValuePairs.Add(jsonParameterName, jsonParameterValue); } } this._jsonObjectCollection = new JSONObjectCollection(jsonNameValuePairs); }
private JSONValue GetJSONValue(object objValue) { Type thisType = objValue.GetType(); JSONValue jsonValue = null; if (thisType == typeof(System.Int32)) { jsonValue = new JSONNumberValue(Convert.ToInt32(objValue)); } else if (thisType == typeof(System.Single)) { jsonValue = new JSONNumberValue(Convert.ToSingle(objValue)); } else if (thisType == typeof(System.Double)) { jsonValue = new JSONNumberValue(Convert.ToDouble(objValue)); } else if (thisType == typeof(System.Decimal)) { jsonValue = new JSONNumberValue(Convert.ToDecimal(objValue)); } else if (thisType == typeof(System.Byte)) { jsonValue = new JSONNumberValue(Convert.ToByte(objValue)); } else if (thisType == typeof(System.String)) { jsonValue = new JSONStringValue(Convert.ToString(objValue)); } else if (thisType == typeof(System.Boolean)) { jsonValue = new JSONBoolValue(Convert.ToBoolean(objValue)); } else if (thisType.BaseType == typeof(System.Enum)) { jsonValue = new JSONStringValue(Enum.GetName(thisType, objValue)); } else if (thisType.IsArray) { List<JSONValue> jsonValues = new List<JSONValue>(); Array arrValue = (Array)objValue; for (int x = 0; x < arrValue.Length; x++) { JSONValue jsValue = this.GetJSONValue(arrValue.GetValue(x)); jsonValues.Add(jsValue); } jsonValue = new JSONArrayCollection(jsonValues); } return jsonValue; }
/// <summary> /// Required override of the ToString() method. /// </summary> /// <returns>contained string in JSON-compliant form</returns> public override string ToString() { return(JSONStringValue.ToJSONString(this._value)); }
/// <summary> /// An example implementation of JSONSharp. The location of a purveyor of fine aleas is used /// to show how different data types are handled when converted to JSON format at render time. /// </summary> /// <param name="args">command-line arguments; none used in this example</param> public static void Main(string[] args) { // Create a Dictionary of name/value pairs for the entire JSON object Dictionary<JSONStringValue, JSONValue> jsonNameValuePairs = new Dictionary<JSONStringValue, JSONValue>(); // Create and add name/value pairs for business, address, city, state, // zipcode, latitude, longitude, covercharge and url JSONStringValue jsv_name_Business = new JSONStringValue("business"); JSONStringValue jsv_value_Business = new JSONStringValue("Tractor Tavern"); jsonNameValuePairs.Add(jsv_name_Business, jsv_value_Business); JSONStringValue jsv_name_Address = new JSONStringValue("address"); JSONStringValue jsv_value_Address = new JSONStringValue("5213 Ballard Ave NW"); jsonNameValuePairs.Add(jsv_name_Address, jsv_value_Address); JSONStringValue jsv_name_City = new JSONStringValue("city"); JSONStringValue jsv_value_City = new JSONStringValue("Seattle"); jsonNameValuePairs.Add(jsv_name_City, jsv_value_City); JSONStringValue jsv_name_State = new JSONStringValue("state"); JSONStringValue jsv_value_State = new JSONStringValue("WA"); jsonNameValuePairs.Add(jsv_name_State, jsv_value_State); JSONStringValue jsv_name_Zipcode = new JSONStringValue("zipcode"); JSONNumberValue jsv_value_Zipcode = new JSONNumberValue(98107); jsonNameValuePairs.Add(jsv_name_Zipcode, jsv_value_Zipcode); JSONStringValue jsv_name_Latitude = new JSONStringValue("latitude"); JSONNumberValue jsv_value_Latitude = new JSONNumberValue(47.665663); jsonNameValuePairs.Add(jsv_name_Latitude, jsv_value_Latitude); JSONStringValue jsv_name_Longitude = new JSONStringValue("longitude"); JSONNumberValue jsv_value_Longitude = new JSONNumberValue(-122.382343); jsonNameValuePairs.Add(jsv_name_Longitude, jsv_value_Longitude); JSONStringValue jsv_name_CoverCharge = new JSONStringValue("covercharge"); JSONBoolValue jsv_value_CoverCharge = new JSONBoolValue(true); jsonNameValuePairs.Add(jsv_name_CoverCharge, jsv_value_CoverCharge); JSONStringValue jsv_name_Url = new JSONStringValue("url"); JSONStringValue jsv_value_Url = new JSONStringValue("http://tractortavern.citysearch.com/"); jsonNameValuePairs.Add(jsv_name_Url, jsv_value_Url); // Add an array of payment methods JSONStringValue jsv_name_PaymentMethods = new JSONStringValue("paymentmethods"); List<JSONValue> listPaymentMethods = new List<JSONValue>(); listPaymentMethods.Add(new JSONStringValue("Cash")); listPaymentMethods.Add(new JSONStringValue("Visa")); listPaymentMethods.Add(new JSONStringValue("Mastercard")); listPaymentMethods.Add(new JSONStringValue("American Express")); JSONArrayCollection jsv_value_PaymentMethods = new JSONArrayCollection(listPaymentMethods); jsonNameValuePairs.Add(jsv_name_PaymentMethods, jsv_value_PaymentMethods); // Construct our object, passing the Dictionary of name/value pairs. We could have // created our JSONObjectCollection first, then called the Add() method to populate // the object's internal Dictionary. JSONObjectCollection jsonObjectCollection = new JSONObjectCollection(jsonNameValuePairs); // The ToString() is the compact representation of the object's JSON output Console.WriteLine("JSONObjectCollection.ToString()"); Console.WriteLine("==============================="); Console.WriteLine(jsonObjectCollection.ToString()); Console.WriteLine("==============================="); Console.WriteLine(); // PrettyPrint() is great for readability Console.WriteLine("JSONObjectCollection.PrettyPrint()"); Console.WriteLine("==============================="); Console.WriteLine(jsonObjectCollection.PrettyPrint()); Console.WriteLine("==============================="); }
/// <summary> /// Adds a JSONStringValue as the "name" and a JSONValue as the "value" to the /// internal Dictionary. Values are checked to ensure no duplication occurs /// in the internal Dictionary. /// </summary> /// <param name="name">JSONStringValue "name" to add to the internal dictionary</param> /// <param name="value">JSONValue "value" to add to the internal dictionary</param> public void Add(JSONStringValue name, JSONValue value) { if (!this._namevaluepairs.ContainsKey(name)) this._namevaluepairs.Add(name, value); }