/// <summary> /// Parse json to object /// </summary> /// <typeparam name="T">Type</typeparam> /// <param name="adapter">Json Data Adapter</param> /// <param name="initType">Init FIELD or PROPERTY or All</param> /// <returns>object</returns> public static T FormJsonByLsonAttribute <T>(LsonAdapter adapter, INITTYPE initType = INITTYPE.ALL) { T instance = System.Activator.CreateInstance <T>(); FormJsonByLsonAttribute(instance, adapter, initType); return(instance); }
/// <summary> /// Parse json to object /// </summary> /// <param name="instance">object instance</param> /// <param name="adapter">Json Data Adapter</param> /// <param name="initType">Init FIELD or PROPERTY or All</param> /// <returns>object</returns> public static object FormJsonByLsonAttribute(object instance, LsonAdapter adapter, INITTYPE initType = INITTYPE.ALL) { if (instance == null) { Debug.Log("Lson:Instance cant be null"); return(null); } Type type = instance.GetType(); FieldInfo[] fieldInfos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); switch (initType) { case INITTYPE.ALL: SetValues(adapter, instance, fieldInfos, true); SetValues(adapter, instance, propertyInfos, true); break; case INITTYPE.FIELD: SetValues(adapter, instance, fieldInfos, true); break; case INITTYPE.PROPERTY: SetValues(adapter, instance, propertyInfos, true); break; } if (typeof(ILsonBean).IsAssignableFrom(type)) { type.GetMethod("AfterInit").Invoke(instance, null); } return(instance); }
/// <summary> /// Set object value /// </summary> /// <param name="instance">object instance</param> /// <param name="adapter">Json Data Adapter</param> /// <param name="propertyInfos">propertyInfos of object</param> /// <param name="isOnlyLsonAttribute">Is only check LsonAttribute</param> private static void SetValues(LsonAdapter adapter, object instance, PropertyInfo[] propertyInfos, bool isOnlyLsonAttribute = false) { for (int i = 0, j = propertyInfos.Length; i < j; i++) { PropertyInfo propertyInfo = propertyInfos[i]; //false不查找继承链 object[] attributes = propertyInfo.GetCustomAttributes(false); LsonAttribute lsonAttribute = GetLsonAttribute(attributes); String jsonKey = propertyInfo.Name; if (lsonAttribute != null) { jsonKey = lsonAttribute.JsonKey; } else if (isOnlyLsonAttribute) { continue; } if (!adapter.Contains(jsonKey)) { continue; } object value = adapter.GetValue(jsonKey, propertyInfo.PropertyType); if (adapter.GetValueJsonType(jsonKey) == JsonType.Object) { object _instance = propertyInfo.GetValue(instance, null); if (isOnlyLsonAttribute) { value = FormJsonByLsonAttribute(_instance, (LsonAdapter)value); } else { value = FromJson(_instance, (LsonAdapter)value); } instanceLson.SetValue(instance, propertyInfo, value); } instanceLson.SetValue(instance, propertyInfo, value); } }