// JSON C# Class Generator // http://www.xamasoft.com/json-class-generator using Newtonsoft.Json.Linq; namespace JsonCSharpClassGenerator; internal static class JsonClassHelper { public static T GetJToken(JObject obj, string field) where T : JToken { JToken value; if (obj.TryGetValue(field, out value)) return GetJToken(value); else return null; } private static T GetJToken(JToken token) where T : JToken { if (token == null) return null; if (token.Type == JTokenType.Null) return null; if (token.Type == JTokenType.Undefined) return null; return (T)token; } public static string ReadString(JToken token) { JValue value = GetJToken(token); if (value == null) return null; return (string)value.Value; } public static bool ReadBoolean(JToken token) { JValue value = GetJToken(token); if (value == null) throw new Newtonsoft.Json.JsonSerializationException(); return Convert.ToBoolean(value.Value); } public static bool? ReadNullableBoolean(JToken token) { JValue value = GetJToken(token); if (value == null) return null; return Convert.ToBoolean(value.Value); } public static int ReadInteger(JToken token) { JValue value = GetJToken(token); if (value == null) throw new Newtonsoft.Json.JsonSerializationException(); return Convert.ToInt32((long)value.Value); } public static int? ReadNullableInteger(JToken token) { JValue value = GetJToken(token); if (value == null) return null; return Convert.ToInt32((long)value.Value); } public static long ReadLong(JToken token) { JValue value = GetJToken(token); if (value == null) throw new Newtonsoft.Json.JsonSerializationException(); return Convert.ToInt64(value.Value); } public static long? ReadNullableLong(JToken token) { JValue value = GetJToken(token); if (value == null) return null; return Convert.ToInt64(value.Value); } public static double ReadFloat(JToken token) { JValue value = GetJToken(token); if (value == null) throw new Newtonsoft.Json.JsonSerializationException(); return Convert.ToDouble(value.Value); } public static double? ReadNullableFloat(JToken token) { JValue value = GetJToken(token); if (value == null) return null; return Convert.ToDouble(value.Value); } public static DateTime ReadDate(JToken token) { JValue value = GetJToken(token); if (value == null) throw new Newtonsoft.Json.JsonSerializationException(); return Convert.ToDateTime(value.Value); } public static DateTime? ReadNullableDate(JToken token) { JValue value = GetJToken(token); if (value == null) return null; return Convert.ToDateTime(value.Value); } public static object ReadObject(JToken token) { JToken value = GetJToken(token); if (value == null) return null; if (value.Type == JTokenType.Object) return value; if (value.Type == JTokenType.Array) return ReadArray(value, ReadObject); if (value is JValue jvalue) return jvalue.Value; return value; } public static T ReadStronglyTypedObject(JToken token) where T : class { JObject value = GetJToken(token); if (value == null) return null; return (T)Activator.CreateInstance(typeof(T), new object[] { token }); } public delegate T ValueReader(JToken token); public static T[] ReadArray(JToken token, ValueReader reader) { JArray value = GetJToken(token); if (value == null) return null; T[] array = new T[value.Count]; for (int i = 0; i < array.Length; i++) { array[i] = reader(value[i]); } return array; } public static Dictionary ReadDictionary(JToken token) { JObject value = GetJToken(token); if (value == null) return null; Dictionary dict = new(); return dict; } public static Array ReadArray(JArray jArray, ValueReader reader, Type type) { if (jArray == null) return null; Type elemType = type.GetElementType(); Array array = Array.CreateInstance(elemType, jArray.Count); for (int i = 0; i < array.Length; i++) { if (elemType.IsArray) { array.SetValue(ReadArray(GetJToken(jArray[i]), reader, elemType), i); } else { array.SetValue(reader(jArray[i]), i); } } return array; } }