/// <summary> /// Loads JsonObject from JSON string /// </summary> /// <param name="JSON"></param> /// <returns></returns> public static JsonObject fromString(string JSON) { string type; if (!Json.isJson(JSON, out type) || type == "") { return(null); } using (var sr = new StringReader(JSON)) using (var jr = new JsonTextReader(sr) { DateParseHandling = DateParseHandling.None }) { JToken parsed = JToken.ReadFrom(jr); switch (parsed.Type) { case JTokenType.Object: return(JsonObjectFactory.fromJObject((JObject)parsed)); case JTokenType.Array: return(JsonObjectFactory.fromJArray((JArray)parsed)); default: throw new Exception("Currently we don't accept single values, only objects and arrays"); } } }
/// <summary> /// Converts JToken to JsonObject /// </summary> /// <param name="jsonObject"></param> /// <param name="key"></param> /// <param name="item"></param> /// <returns></returns> public static JsonObject fromJToken(JsonObject jsonObject, string key, JToken item) { switch (item.Type) { case JTokenType.Array: jsonObject.set("JsonArray", key, JsonObjectFactory.fromJArray((JArray)item)); break; case JTokenType.Boolean: jsonObject.set(key, (bool)item); break; case JTokenType.Integer: jsonObject.set(key, (int)item); break; case JTokenType.Float: jsonObject.set(key, (decimal)item); break; case JTokenType.String: jsonObject.set(key, (string)item); break; case JTokenType.Object: jsonObject.set("JsonObject", key, JsonObjectFactory.fromJObject((JObject)item)); break; case JTokenType.Null: jsonObject.set("NULL", key, item); break; default: break; } return(jsonObject); }