using System; using System.Collections.Generic; using System.Reflection; using System.Text; using JSONSharp.Collections; using JSONSharp.Values; namespace JSONSharp { /// /// JSONReflector provides a convenient way to convert value and reference type objects /// to JSON format through reflection. /// /// This implementation build JSON around reflected public properties of type int, float, /// double, decimal, byte, string, bool, enum or array. (Generics and other types may be /// supported at a later time.) /// public class JSONReflector : JSONValue { private JSONObjectCollection _jsonObjectCollection; private JSONValue GetJSONValue(object objValue) { JSONValue jsonValue = null; if (objValue != null) { Type thisType = objValue.GetType(); 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 jsonValues = new List(); 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 { jsonValue = new JSONStringValue(objValue.ToString()); } } return jsonValue; } /// /// Public constructor that accepts any object /// /// object to be reflected/evaluated for JSON conversion public JSONReflector(object objValue) { Dictionary jsonNameValuePairs = new Dictionary(); 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); } /// /// Required override of the ToString() method. /// /// returns the internal JSONObjectCollection ToString() method public override string ToString() { return this._jsonObjectCollection.ToString(); } /// /// Required override of the PrettyPrint() method. /// /// returns the internal JSONObjectCollection PrettyPrint() method public override string PrettyPrint() { return this._jsonObjectCollection.PrettyPrint(); } public static string ObjectToJSON(object obj) { JSONReflector r = new JSONReflector(obj); return r.ToString(); } } }