public static dynamic Parse(string jsonString, string applicationType) { try { var result = new JsonToDynamic(JsonConvert.DeserializeObject<JObject>(jsonString)); result.Add("ApplicationType", applicationType); return result; } catch { try { var jarray = JsonConvert.DeserializeObject<JArray>(jsonString); var result = new List<dynamic>(); foreach (var item in jarray) { result.Add(new JsonToDynamic(item)); } return result; } catch { bool valueAsBool = false; int valueAsInt = 0; double valueAsDouble = 0; DateTime valueAsDateTime = DateTime.MinValue; if (bool.TryParse(jsonString, out valueAsBool)) return valueAsBool; if (int.TryParse(jsonString, out valueAsInt)) return valueAsInt; if (double.TryParse(jsonString, out valueAsDouble)) return valueAsDouble; if (DateTime.TryParse(jsonString, out valueAsDateTime)) return valueAsDateTime; return jsonString; } } }
public override bool TryGetMember(GetMemberBinder binder, out object result) { var dictionary = underlyingObject as Dictionary<string, object>; if (CanConvertToJson(dictionary[binder.Name])) result = new JsonToDynamic(dictionary[binder.Name]); else if (dictionary[binder.Name] is JArray) result = ToArray(dictionary[binder.Name] as JArray); else if (dictionary[binder.Name] is string) result = dictionary[binder.Name]; else result = ((JValue)dictionary[binder.Name]).Value; return true; }
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { var o = (underlyingObject as List<object>).ElementAt((int)indexes[0]); if (CanConvertToJson(o)) result = new JsonToDynamic(o); else result = o; return true; }