using System; using System.Reflection; namespace FastloadMedia.NETMF.Http { /// /// This static class contains a method to convert a value to its JSON equivalent. /// Programmed by Huysentruit Wouter /// public static class Json { /// /// Lookup table for hex values. /// private const string HEX_CHARS = "0123456789ABCDEF"; public const string ContentType = "application/json"; /// /// Converts a character to its javascript unicode equivalent. /// /// The character to convert. /// The javascript unicode equivalent. private static string JsUnicode(char c) { string result = "\\u"; ushort value = (ushort)c; for (int i = 0; i < 4; i++, value <<= 4) result += HEX_CHARS[value >> 12]; return result; } /// /// Encodes a javascript string. /// /// The string to encode. /// The encoded string. private static string JsEncode(string s) { string result = ""; foreach (char c in s) { if (c < (char)127) { switch (c) { case '\b': result += "\\b"; break; case '\t': result += "\\t"; break; case '\n': result += "\\n"; break; case '\f': result += "\\f"; break; case '\r': result += "\\r"; break; case '"': result += "\\\""; break; case '/': result += "\\/"; break; case '\\': result += "\\\\"; break; default: if (c < ' ') result += JsUnicode(c); else result += c; break; } } else result += JsUnicode(c); } return result; } /// /// Convert a value to JSON. /// /// The value to convert. Supported types are: Boolean, String, Byte, (U)Int16, (U)Int32, Float, Double, Decimal, JsonObject, JsonArray, Array, Object and null. /// The JSON object as a string or null when the value type is not supported. /// For objects, only public fields are converted. public static string ToJson(object o) { if (o == null) return "null"; Type type = o.GetType(); switch (type.Name) { case "Boolean": return (bool)o ? "true" : "false"; case "String": return "\"" + JsEncode((string)o) + "\""; case "Byte": case "Int16": case "UInt16": case "Int32": case "UInt32": case "Single": case "Double": case "Decimal": case "JsonObject": case "JsonArray": return o.ToString(); } if (type.IsArray) { JsonArray jsonArray = new JsonArray(); foreach (object i in (Array)o) jsonArray.Add(i); return jsonArray.ToString(); } if (type.IsClass) { JsonObject jsonObject = new JsonObject(); foreach (FieldInfo info in type.GetFields()) jsonObject.Add(info.Name, info.GetValue(o)); return jsonObject.ToString(); } return null; } } }