/// <summary> /// 获取字段成员集合 /// </summary> /// <param name="fields"></param> /// <param name="typeAttribute">类型配置</param> /// <returns>字段成员集合</returns> public static LeftArray <FieldIndex> GetFields(FieldIndex[] fields, JsonSerializeAttribute typeAttribute) { LeftArray <FieldIndex> values = new LeftArray <FieldIndex>(fields.Length); foreach (FieldIndex field in fields) { Type type = field.Member.FieldType; if (!type.IsPointer && (!type.IsArray || type.GetArrayRank() == 1) && !field.IsIgnore && !typeof(Delegate).IsAssignableFrom(type) && !field.Member.IsDefined(typeof(IgnoreMemberAttribute), typeAttribute.IsBaseTypeAttribute)) { JsonSerializeMemberAttribute attribute = field.GetAttribute <JsonSerializeMemberAttribute>(typeAttribute.IsBaseTypeAttribute); if (typeAttribute.IsAttribute ? (attribute != null && attribute.IsSetup) : (attribute == null || attribute.IsSetup)) { values.Add(field); } } } return(values); }
/// <summary> /// 获取属性成员集合 /// </summary> /// <param name="properties">属性成员集合</param> /// <param name="typeAttribute">类型配置</param> /// <returns>属性成员集合</returns> public static LeftArray <KeyValue <PropertyIndex, MethodInfo> > GetProperties(PropertyIndex[] properties, JsonSerializeAttribute typeAttribute) { LeftArray <KeyValue <PropertyIndex, MethodInfo> > values = new LeftArray <KeyValue <PropertyIndex, MethodInfo> >(properties.Length); foreach (PropertyIndex property in properties) { if (property.Member.CanRead) { Type type = property.Member.PropertyType; if (!type.IsPointer && (!type.IsArray || type.GetArrayRank() == 1) && !property.IsIgnore && !typeof(Delegate).IsAssignableFrom(type) && !property.Member.IsDefined(typeof(IgnoreMemberAttribute), typeAttribute.IsBaseTypeAttribute)) { JsonSerializeMemberAttribute attribute = property.GetAttribute <JsonSerializeMemberAttribute>(typeAttribute.IsBaseTypeAttribute); if (typeAttribute.IsAttribute ? (attribute != null && attribute.IsSetup) : (attribute == null || attribute.IsSetup)) { MethodInfo method = property.Member.GetGetMethod(true); if (method != null && method.GetParameters().Length == 0) { values.Add(new KeyValue <PropertyIndex, MethodInfo>(property, method)); } } } } } return(values); }
static TypeSerializer() { Type type = typeof(T); MethodInfo methodInfo = JsonSerializer.GetSerializeMethod(type); if (methodInfo != null) { defaultSerializer = (Action <JsonSerializer, T>)Delegate.CreateDelegate(typeof(Action <JsonSerializer, T>), methodInfo); isValueType = true; return; } if (type.IsArray) { if (type.GetArrayRank() == 1) { Type elementType = type.GetElementType(); if (elementType.IsValueType && (!elementType.IsGenericType || elementType.GetGenericTypeDefinition() != typeof(Nullable <>))) { defaultSerializer = (Action <JsonSerializer, T>)StructGenericType.Get(elementType).JsonSerializeStructArrayMethod; } else { defaultSerializer = (Action <JsonSerializer, T>)GenericType.Get(elementType).JsonSerializeArrayMethod; } } else { defaultSerializer = (Action <JsonSerializer, T>)GenericType.Get(type).JsonSerializeNotSupportDelegate; } isValueType = true; return; } if (type.IsEnum) { defaultSerializer = EnumToString; isValueType = true; return; } if (type.isSerializeNotSupport()) { defaultSerializer = (Action <JsonSerializer, T>)GenericType.Get(type).JsonSerializeNotSupportDelegate; isValueType = true; return; } if (type.IsGenericType) { Type genericType = type.GetGenericTypeDefinition(); if (genericType == typeof(Dictionary <,>)) { defaultSerializer = (Action <JsonSerializer, T>)SerializeMethodCache.GetDictionary(type); isValueType = true; return; } if (genericType == typeof(Nullable <>)) { defaultSerializer = (Action <JsonSerializer, T>)StructGenericType.Get(type.GetGenericArguments()[0]).JsonSerializeNullableMethod; isValueType = true; return; } if (genericType == typeof(KeyValuePair <,>)) { defaultSerializer = (Action <JsonSerializer, T>)GenericType2.Get(type.GetGenericArguments()).JsonSerializeKeyValuePairMethod; isValueType = true; return; } } if ((methodInfo = SerializeMethodCache.GetCustom(type)) != null) { if (type.IsValueType) { #if NOJIT defaultSerializer = new CustomSerializer(methodInfo).Serialize; #else DynamicMethod dynamicMethod = new DynamicMethod("CustomJsonSerializer", null, new Type[] { typeof(JsonSerializer), type }, type, true); ILGenerator generator = dynamicMethod.GetILGenerator(); generator.Emit(OpCodes.Ldarga_S, 1); generator.Emit(OpCodes.Ldarg_0); generator.call(methodInfo); generator.Emit(OpCodes.Ret); defaultSerializer = (Action <JsonSerializer, T>)dynamicMethod.CreateDelegate(typeof(Action <JsonSerializer, T>)); #endif } else { defaultSerializer = (Action <JsonSerializer, T>)Delegate.CreateDelegate(typeof(Action <JsonSerializer, T>), methodInfo); } isValueType = true; } else { Delegate enumerableDelegate = SerializeMethodCache.GetIEnumerable(type); if (enumerableDelegate != null) { defaultSerializer = (Action <JsonSerializer, T>)enumerableDelegate; isValueType = true; } else { Type attributeType; attribute = type.customAttribute <JsonSerializeAttribute>(out attributeType) ?? (type.Name[0] == '<' ? JsonSerializeAttribute.AnonymousTypeMember : JsonSerializer.AllMemberAttribute); if (type.IsValueType) { isValueType = true; } else if (attribute != JsonSerializer.AllMemberAttribute && attributeType != type) { for (Type baseType = type.BaseType; baseType != typeof(object); baseType = baseType.BaseType) { JsonSerializeAttribute baseAttribute = baseType.customAttribute <JsonSerializeAttribute>(); if (baseAttribute != null) { if (baseAttribute.IsBaseType) { methodInfo = SerializeMethodCache.BaseSerializeMethod.MakeGenericMethod(baseType, type); defaultSerializer = (Action <JsonSerializer, T>)Delegate.CreateDelegate(typeof(Action <JsonSerializer, T>), methodInfo); return; } break; } } } LeftArray <FieldIndex> fields = SerializeMethodCache.GetFields(MemberIndexGroup <T> .GetFields(attribute.MemberFilters), attribute); LeftArray <KeyValue <PropertyIndex, MethodInfo> > properties = SerializeMethodCache.GetProperties(MemberIndexGroup <T> .GetProperties(attribute.MemberFilters), attribute); bool isBox = false; if (type.IsValueType && fields.Length + properties.Length == 1) { BoxSerializeAttribute boxSerialize = AutoCSer.Metadata.TypeAttribute.GetAttribute <BoxSerializeAttribute>(type); if (boxSerialize != null && boxSerialize.IsJson) { isBox = true; } } #if AutoCSer AutoCSer.WebView.ClientTypeAttribute clientType = isBox ? null : AutoCSer.Metadata.TypeAttribute.GetAttribute <AutoCSer.WebView.ClientTypeAttribute>(type); if (clientType != null) { if (clientType.MemberName == null) { viewClientTypeName = "new " + clientType.GetClientName(type) + "({"; } else { viewClientTypeName = clientType.GetClientName(type) + ".Get({"; } } #endif #if NOJIT if (isBox) { defaultSerializer = memberSerializer = new FieldPropertySerializer(ref fields, ref properties).SerializeBox; } else { memberSerializer = new FieldPropertySerializer(ref fields, ref properties).Serialize; memberMapSerializer = new MemberMapSerializer(ref fields, ref properties).Serialize; } #else SerializeMemberDynamicMethod dynamicMethod = new SerializeMemberDynamicMethod(type); SerializeMemberMapDynamicMethod memberMapDynamicMethod = isBox ? default(SerializeMemberMapDynamicMethod) : new SerializeMemberMapDynamicMethod(type); foreach (FieldIndex member in fields) { if (isBox) { dynamicMethod.PushBox(member); } else { dynamicMethod.Push(member); memberMapDynamicMethod.Push(member); } } foreach (KeyValue <PropertyIndex, MethodInfo> member in properties) { if (isBox) { dynamicMethod.PushBox(member.Key, member.Value); } else { dynamicMethod.Push(member.Key, member.Value); memberMapDynamicMethod.Push(member.Key, member.Value); } } memberSerializer = (Action <JsonSerializer, T>)dynamicMethod.Create <Action <JsonSerializer, T> >(); if (isBox) { defaultSerializer = memberSerializer; } else { memberMapSerializer = (Action <MemberMap, JsonSerializer, T, CharStream>)memberMapDynamicMethod.Create <Action <MemberMap, JsonSerializer, T, CharStream> >(); } #endif } } }