// 로케일 파싱 IEnumerator _LoadLocalData() { WWW www = new WWW(NetworkMgr.Instance.URL("Jsons/localdata.json")); GameHelper.DevDebugLog(www.url); while (!www.isDone) { ProgressBar.value = www.progress; yield return(null); } yield return(www); if (www.isDone) { if (www.error == null) { DataMgr.Instance.DicLocal.Clear(); TinyJSON.Variant variant = TinyJSON.JSON.Load(www.text); List <LocalData> list = new List <LocalData>(); TinyJSON.JSON.MakeInto <List <LocalData> >(variant, out list); for (int i = 0; i < list.Count; ++i) { DataMgr.Instance.DicLocal.Add(list[i].id, list[i]); } } else { GameHelper.DevDebugLog(www.error, LOGSTATE.ERROR); } } }
// 단어 데이터 파싱 IEnumerator _LoadWordData() { WWW www = new WWW(NetworkMgr.Instance.URL("Jsons/worddata.json")); GameHelper.DevDebugLog(www.url); while (!www.isDone) { ProgressBar.value = www.progress; yield return(null); } yield return(www); if (www.isDone) { if (www.error == null) { DataMgr.Instance.ListWordData.Clear(); TinyJSON.Variant variant = TinyJSON.JSON.Load(www.text); TinyJSON.JSON.MakeInto <List <WordData> >(variant, out DataMgr.Instance.ListWordData); } else { GameHelper.DevDebugLog(www.error, LOGSTATE.ERROR); } } }
/// <summary> /// Deserializes a JSON string into an object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="json"></param> /// <returns>The deserialized JSON object</returns> public static T LoadFromString<T>(string json) { T returnValue; if (json != "") { TinyJSON.Variant variant = TinyJSON.Decoder.Decode(json); variant.Make<T>(out returnValue); return returnValue; } return default(T); }
static int PrepareAssets(ILuaState lua) { var abLoader = AssetsMgr.A.Loader; abLoader.ClearPreload(); TinyJSON.Variant jObj = lua.ToJsonObj(1); var assets = jObj as TinyJSON.ProxyArray; if (assets != null) { for (int i = 0; i < assets.Count; ++i) { string abName = assets[i]["name"]; bool allowUnload = assets[i]["unload"]; abLoader.CachedPreload(abName, allowUnload); } } return(0); }
/// <summary> /// Loads a JSON file from disk and deserializes its contents into an object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <returns>The deserialized JSON object</returns> public static T Load<T>(string path) { T returnValue; if (path != "") { string readFile; using (System.IO.StreamReader file = new System.IO.StreamReader(path)) { readFile = file.ReadToEnd(); } TinyJSON.Variant variant = TinyJSON.Decoder.Decode(readFile); variant.Make<T>(out returnValue); return returnValue; } return default(T); }
public static void SetDict(this ILuaState self, string key, TinyJSON.Variant json) { self.PushString(key); self.PushVariant(json); self.RawSet(-3); }
public static void SetArray(this ILuaState self, int n, TinyJSON.Variant json) { self.PushInteger(n); self.PushVariant(json); self.RawSet(-3); }
private static T DecodeType <T>(Variant data) { if (data == null) { return(default(T)); } var type = typeof(T); if (type.IsEnum) { return((T)Enum.Parse(type, data.ToString())); } if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal)) { return((T)Convert.ChangeType(data, type)); } if (type.IsArray) { if (type.GetArrayRank() == 1) { var makeFunc = decodeArrayMethod.MakeGenericMethod(new Type[] { type.GetElementType() }); return((T)makeFunc.Invoke(null, new object[] { data })); } else { var arrayData = data as ProxyArray; var arrayRank = type.GetArrayRank(); var rankLengths = new int[arrayRank]; if (arrayData.CanBeMultiRankArray(rankLengths)) { var array = Array.CreateInstance(type.GetElementType(), rankLengths); var makeFunc = decodeMultiRankArrayMethod.MakeGenericMethod(new Type[] { type.GetElementType() }); try { makeFunc.Invoke(null, new object[] { arrayData, array, 1, rankLengths }); } catch (Exception e) { throw new DecodeException("Error decoding multidimensional array. Did you try to decode into an array of incompatible rank or element type?", e); } return((T)Convert.ChangeType(array, typeof(T))); } throw new DecodeException("Error decoding multidimensional array; JSON data doesn't seem fit this structure."); #pragma warning disable 0162 return(default(T)); #pragma warning restore 0162 } } if (typeof(IList).IsAssignableFrom(type)) { var makeFunc = decodeListMethod.MakeGenericMethod(type.GetGenericArguments()); return((T)makeFunc.Invoke(null, new object[] { data })); } if (typeof(IDictionary).IsAssignableFrom(type)) { var makeFunc = decodeDictionaryMethod.MakeGenericMethod(type.GetGenericArguments()); return((T)makeFunc.Invoke(null, new object[] { data })); } // At this point we should be dealing with a class or struct. T instance; var proxyObject = data as ProxyObject; if (proxyObject == null) { throw new InvalidCastException("ProxyObject expected when decoding into '" + type.FullName + "'."); } // If there's a type hint, use it to create the instance. var typeHint = proxyObject.TypeHint; if (typeHint != null && typeHint != type.FullName) { var makeType = FindType(typeHint); if (makeType == null) { throw new TypeLoadException("Could not load type '" + typeHint + "'."); } else { if (type.IsAssignableFrom(makeType)) { instance = (T)Activator.CreateInstance(makeType); type = makeType; } else { throw new InvalidCastException("Cannot assign type '" + typeHint + "' to type '" + type.FullName + "'."); } } } else { // We don't have a type hint, so just instantiate the type we have. instance = Activator.CreateInstance <T>(); } // Now decode fields and properties. foreach (var pair in data as ProxyObject) { var field = type.GetField(pair.Key, instanceBindingFlags); if (field != null) { var shouldDecode = field.IsPublic; foreach (var attribute in field.GetCustomAttributes(true)) { if (excludeAttrType.IsAssignableFrom(attribute.GetType())) { shouldDecode = false; } if (includeAttrType.IsAssignableFrom(attribute.GetType())) { shouldDecode = true; } } if (shouldDecode) { var makeFunc = decodeTypeMethod.MakeGenericMethod(new Type[] { field.FieldType }); if (type.IsValueType) { // Type is a struct. var instanceRef = (object)instance; field.SetValue(instanceRef, makeFunc.Invoke(null, new object[] { pair.Value })); instance = (T)instanceRef; } else { // Type is a class. field.SetValue(instance, makeFunc.Invoke(null, new object[] { pair.Value })); } } } var property = type.GetProperty(pair.Key, instanceBindingFlags); if (property != null) { if (property.CanWrite && property.GetCustomAttributes(false).AnyOfType(includeAttrType)) { var makeFunc = decodeTypeMethod.MakeGenericMethod(new Type[] { property.PropertyType }); if (type.IsValueType) { // Type is a struct. var instanceRef = (object)instance; property.SetValue(instanceRef, makeFunc.Invoke(null, new object[] { pair.Value }), null); instance = (T)instanceRef; } else { // Type is a class. property.SetValue(instance, makeFunc.Invoke(null, new object[] { pair.Value }), null); } } } } // Invoke methods tagged with [AfterDecode] attribute. foreach (var method in type.GetMethods(instanceBindingFlags)) { if (method.GetCustomAttributes(false).AnyOfType(typeof(AfterDecode))) { if (method.GetParameters().Length == 0) { method.Invoke(instance, null); } else { method.Invoke(instance, new object[] { data }); } } } return(instance); }
public static void MakeInto <T>(Variant data, out T item) { item = DecodeType <T>(data); }
private static T DecodeType <T>(Variant data) { var type = typeof(T); if (type.IsEnum) { return((T)Enum.Parse(type, data.ToString())); } if (type.IsPrimitive || type == typeof(string)) { return((T)Convert.ChangeType(data, type)); } if (type.IsArray) { var makeFunc = decodeArrayMethod.MakeGenericMethod(new Type[] { type.GetElementType() }); return((T)makeFunc.Invoke(null, new object[] { data })); } if (typeof(IList).IsAssignableFrom(type)) { var makeFunc = decodeListMethod.MakeGenericMethod(type.GetGenericArguments()); return((T)makeFunc.Invoke(null, new object[] { data })); } if (typeof(IDictionary).IsAssignableFrom(type)) { var makeFunc = decodeDictionaryMethod.MakeGenericMethod(type.GetGenericArguments()); return((T)makeFunc.Invoke(null, new object[] { data })); } // This is a class or struct, so instantiate it with the default constructor. var instance = Activator.CreateInstance <T>(); // Now decode each field, except for those tagged with [Skip] attribute. if (data is ProxyObject) { foreach (var pair in data as ProxyObject) { var field = type.GetField(pair.Key, instanceBindingFlags); if (field != null) { if (!Attribute.GetCustomAttributes(field).Any(attr => attr is Skip)) { var makeFunc = decodeTypeMethod.MakeGenericMethod(new Type[] { field.FieldType }); if (type.IsValueType) { // Type is a struct var instanceRef = (object)instance; field.SetValue(instanceRef, makeFunc.Invoke(null, new object[] { pair.Value })); instance = (T)instanceRef; } else { // Type is a class if (pair.Value != null) { field.SetValue(instance, makeFunc.Invoke(null, new object[] { pair.Value })); } } } } } } // Invoke methods tagged with [Load] attribute. foreach (var method in type.GetMethods(instanceBindingFlags)) { if (method.GetCustomAttributes(false).Any(attr => attr is Load)) { if (method.GetParameters().Length == 0) { method.Invoke(instance, null); } else { method.Invoke(instance, new object[] { data }); } } } return(instance); }
static T DecodeType <T>(Variant data) { if (data == null) { return(default(T)); } var type = typeof(T); if (type.IsEnum) { return((T)Enum.Parse(type, data.ToString(CultureInfo.InvariantCulture))); } if (type.IsPrimitive || type == typeof(string) || type == typeof(char) || type == typeof(decimal) || type == typeof(DateTime) #if UNITY_ENGINE || type == typeof(Color) || type == typeof(Color32) #endif ) { return((T)Convert.ChangeType(data, type)); } if (type.IsArray) { if (type.GetArrayRank() == 1) { var makeFunc = decodeArrayMethod.MakeGenericMethod(type.GetElementType()); return((T)makeFunc.Invoke(null, new object[] { data })); } var arrayData = data as ProxyArray; if (arrayData == null) { throw new DecodeException("Variant is expected to be a ProxyArray here, but it is not."); } var arrayRank = type.GetArrayRank(); var rankLengths = new int[arrayRank]; if (arrayData.CanBeMultiRankArray(rankLengths)) { var elementType = type.GetElementType(); if (elementType == null) { throw new DecodeException("Array element type is expected to be not null, but it is."); } var array = Array.CreateInstance(elementType, rankLengths); var makeFunc = decodeMultiRankArrayMethod.MakeGenericMethod(elementType); try { makeFunc.Invoke(null, new object[] { arrayData, array, 1, rankLengths }); } catch (Exception e) { throw new DecodeException("Error decoding multidimensional array. Did you try to decode into an array of incompatible rank or element type?", e); } return((T)Convert.ChangeType(array, typeof(T))); } throw new DecodeException("Error decoding multidimensional array; JSON data doesn't seem fit this structure."); } if (typeof(IList).IsAssignableFrom(type)) { List <Type> typeArguments = new List <Type> { type }; typeArguments.AddRange(type.GetGenericArguments()); var makeFunc = decodeListMethod.MakeGenericMethod(typeArguments.ToArray()); return((T)makeFunc.Invoke(null, new object[] { data })); } if (typeof(IDictionary).IsAssignableFrom(type)) { List <Type> typeArguments = new List <Type> { type }; typeArguments.AddRange(type.GetGenericArguments()); var makeFunc = decodeDictionaryMethod.MakeGenericMethod(typeArguments.ToArray()); return((T)makeFunc.Invoke(null, new object[] { data })); } // At this point we should be dealing with a class or struct. T instance; var proxyObject = data as ProxyObject; if (proxyObject == null) { throw new InvalidCastException("ProxyObject expected when decoding into '" + type.FullName + "'."); } // If there's a type hint, use it to create the instance. var typeHint = proxyObject.TypeHint; if (typeHint != null && typeHint != type.FullName) { var makeType = FindType(typeHint); if (makeType == null) { throw new TypeLoadException("Could not load type '" + typeHint + "'."); } if (type.IsAssignableFrom(makeType)) { instance = (T)Activator.CreateInstance(makeType); type = makeType; } else { throw new InvalidCastException("Cannot assign type '" + typeHint + "' to type '" + type.FullName + "'."); } } else { // We don't have a type hint, so just instantiate the type we have. instance = Activator.CreateInstance <T>(); } // Now decode fields and properties. foreach (var pair in (ProxyObject)data) { var field = type.GetField(pair.Key, instanceBindingFlags); // If the field doesn't exist or is excluded, search for an [EncodeName] or any [DecodeAlias] if (field == null || Attribute.IsDefined(field, typeof(Exclude))) { foreach (var fieldInfo in type.GetFields(instanceBindingFlags)) { foreach (var attribute in fieldInfo.GetCustomAttributes(true)) { if (encodeNameAttrType.IsInstanceOfType(attribute)) { if (((EncodeName)attribute).Name == pair.Key) { field = fieldInfo; break; } } if (decodeAliasAttrType.IsInstanceOfType(attribute)) { if (((DecodeAlias)attribute).Contains(pair.Key)) { field = fieldInfo; break; } } } } } if (field != null) { var shouldDecode = field.IsPublic; foreach (var attribute in field.GetCustomAttributes(true)) { if (excludeAttrType.IsInstanceOfType(attribute)) { shouldDecode = false; } if (includeAttrType.IsInstanceOfType(attribute)) { shouldDecode = true; } } if (shouldDecode) { var makeFunc = decodeTypeMethod.MakeGenericMethod(field.FieldType); if (type.IsValueType) { // Type is a struct. var instanceRef = (object)instance; field.SetValue(instanceRef, makeFunc.Invoke(null, new object[] { pair.Value })); instance = (T)instanceRef; } else { // Type is a class. field.SetValue(instance, makeFunc.Invoke(null, new object[] { pair.Value })); } } } var property = type.GetProperty(pair.Key, instanceBindingFlags); // If the property doesn't exist or is excluded, search for an [EncodeName] or any [DecodeAlias] if (property == null || Attribute.IsDefined(property, typeof(Exclude))) { foreach (var propertyInfo in type.GetProperties(instanceBindingFlags)) { foreach (var attribute in propertyInfo.GetCustomAttributes(false)) { if (encodeNameAttrType.IsInstanceOfType(attribute)) { if (((EncodeName)attribute).Name == pair.Key) { property = propertyInfo; break; } } if (decodeAliasAttrType.IsInstanceOfType(attribute)) { if (((DecodeAlias)attribute).Contains(pair.Key)) { property = propertyInfo; break; } } } } } if (property != null) { if (property.CanWrite && property.GetCustomAttributes(false).AnyOfType(includeAttrType)) { var makeFunc = decodeTypeMethod.MakeGenericMethod(new Type[] { property.PropertyType }); if (type.IsValueType) { // Type is a struct. var instanceRef = (object)instance; property.SetValue(instanceRef, makeFunc.Invoke(null, new object[] { pair.Value }), null); instance = (T)instanceRef; } else { // Type is a class. property.SetValue(instance, makeFunc.Invoke(null, new object[] { pair.Value }), null); } } } } // Invoke methods tagged with [AfterDecode] attribute. foreach (var method in type.GetMethods(instanceBindingFlags)) { if (Attribute.IsDefined(method, typeof(AfterDecode))) { method.Invoke(instance, method.GetParameters().Length == 0 ? null : new object[] { data }); } } return(instance); }
private static T DecodeType <T>(Variant data) { if (data == null) { return(default(T)); } Type type = typeof(T); if (type.IsEnum) { return((T)Enum.Parse(type, data.ToString())); } if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal)) { return((T)Convert.ChangeType(data, type)); } if (type.IsArray) { if (type.GetArrayRank() == 1) { var makeFunc = decodeArrayMethod.MakeGenericMethod(new Type[] { type.GetElementType() }); return((T)makeFunc.Invoke(null, new object[] { data })); } else { var arrayData = data as ProxyArray; var arrayRank = type.GetArrayRank(); var rankLengths = new int[arrayRank]; if (arrayData.CanBeMultiRankArray(rankLengths)) { var array = Array.CreateInstance(type.GetElementType(), rankLengths); var makeFunc = decodeMultiRankArrayMethod.MakeGenericMethod(new Type[] { type.GetElementType() }); try { makeFunc.Invoke(null, new object[] { arrayData, array, 1, rankLengths }); } catch (Exception e) { throw new DecodeException("Error decoding multidimensional array. Did you try to decode into an array of incompatible rank or element type?", e); } return((T)Convert.ChangeType(array, typeof(T))); } throw new DecodeException("Error decoding multidimensional array; JSON data doesn't seem fit this structure."); #pragma warning disable 0162 return(default(T)); #pragma warning restore 0162 } } if (typeof(IList).IsAssignableFrom(type)) { var makeFunc = decodeListMethod.MakeGenericMethod(type.GetGenericArguments()); return((T)makeFunc.Invoke(null, new object[] { data })); } if (typeof(IDictionary).IsAssignableFrom(type)) { var makeFunc = decodeDictionaryMethod.MakeGenericMethod(type.GetGenericArguments()); return((T)makeFunc.Invoke(null, new object[] { data })); } // At this point we should be dealing with a class or struct. T instance; var proxyObject = data as ProxyObject; if (proxyObject == null) { throw new InvalidCastException("ProxyObject expected when decoding into '" + type.FullName + "'."); } // If there's a type hint, use it to create the instance. var typeHint = proxyObject.TypeHint; if (typeHint != null && typeHint != type.FullName) { var makeType = FindType(typeHint); if (makeType == null) { throw new TypeLoadException("Could not load type '" + typeHint + "'."); } else { if (type.IsAssignableFrom(makeType)) { instance = (T)Activator.CreateInstance(makeType, nonPublic: true); type = makeType; } else { throw new InvalidCastException("Cannot assign type '" + typeHint + "' to type '" + type.FullName + "'."); } } } else { // We don't have a type hint, so just instantiate the type we have. instance = (T)Activator.CreateInstance(typeof(T), nonPublic: true); } MemberInfo[] members = type.GetMembers(JSON.INSTANCE_BINDING_FLAGS); for (int i = 0; i < members.Length; i++) { FieldInfo fieldInfo = null; PropertyInfo propertyInfo = null; MemberTypes memberType = members[i].MemberType; if (memberType != MemberTypes.Property && memberType != MemberTypes.Field) { //Not a field or property we skip it. continue; } string memberName = members[i].Name; int shouldEncode = -1; Attribute[] attributes = Attribute.GetCustomAttributes(members[i], inherit: true); for (int x = 0; x < attributes.Length; x++) { if (attributes[x] is ExcludeAttribute) { shouldEncode = 0; break; } if (attributes[x] is IncludeAttribute) { shouldEncode = 1; continue; } if (attributes[x] is AliasAttribute) { memberName = ((AliasAttribute)attributes[x]).alias; continue; } } //We only want to encode the member if we have the key. if (proxyObject.ContainsKey(memberName)) { MethodInfo MakeMethod = null; if (memberType == MemberTypes.Field) { fieldInfo = members[i] as FieldInfo; if (shouldEncode == -1) { //This only happens if no attribute was found that modifies if it should be encoded or not. if (fieldInfo.IsPublic) { shouldEncode = 1; } } if (shouldEncode == 1) { //We are going to encode this field MakeMethod = decodeTypeMethod.MakeGenericMethod(new Type[] { fieldInfo.FieldType }); if (type.IsValueType) { object instanceRef = (object)instance; fieldInfo.SetValue(instanceRef, MakeMethod.Invoke(obj: null, parameters: new object[] { proxyObject[memberName] })); instance = (T)instanceRef; } else { fieldInfo.SetValue(instance, MakeMethod.Invoke(null, new object[] { proxyObject[memberName] })); } } } else if (memberType == MemberTypes.Property) { propertyInfo = members[i] as PropertyInfo; if (shouldEncode == 1 && propertyInfo.CanWrite) { //We are going to encode this property MakeMethod = decodeTypeMethod.MakeGenericMethod(new Type[] { propertyInfo.PropertyType }); if (type.IsValueType) { object instanceRef = (object)instance; propertyInfo.SetValue(instanceRef, MakeMethod.Invoke(obj: null, parameters: new object[] { proxyObject[memberName] }), null); instance = (T)instanceRef; } else { propertyInfo.SetValue(instance, MakeMethod.Invoke(obj: null, parameters: new object[] { proxyObject[memberName] }), null); } } } } } // Invoke methods tagged with [AfterDecode] attribute. foreach (var method in type.GetMethods(instanceBindingFlags)) { if (method.GetCustomAttributes(false).AnyOfType(typeof(AfterDecodeAttribute))) { if (method.GetParameters().Length == 0) { method.Invoke(instance, null); } else { method.Invoke(instance, new object[] { data }); } } } return(instance); }