MemberCache(MemberInfo memberInfo, Type memberType, string name) { MemberName = name; MemberType = memberType; MemberInfo = memberInfo; JsonDataType dt = Reflection.GetJsonDataType(memberType); DeserializeMethod = JsonDeserializer.GetReadJsonMethod(memberType); SerializeMethod = JsonSerializer.GetWriteJsonMethod(memberType); if (dt == JsonDataType.Array || dt == JsonDataType.MultiDimensionalArray) { ElementType = memberType.GetElementType(); } IsValueType = memberType.IsValueType; IsStruct = (IsValueType && !memberType.IsPrimitive && !memberType.IsEnum && typeof(decimal).Equals(memberType) == false); IsClass = memberType.IsClass; IsCollection = typeof(ICollection).IsAssignableFrom(memberType) && typeof(byte[]).Equals(memberType) == false; if (memberType.IsGenericType) { ElementType = memberType.GetGenericArguments()[0]; IsNullable = memberType.GetGenericTypeDefinition().Equals(typeof(Nullable <>)); } if (IsValueType) { ChangeType = IsNullable ? ElementType : memberType; } JsonDataType = dt; }
static RevertJsonValue[] RegisterMethods() { var r = new RevertJsonValue[Enum.GetNames(typeof(JsonDataType)).Length]; r[(int)JsonDataType.Array] = RevertArray; r[(int)JsonDataType.Bool] = RevertPrimitive; r[(int)JsonDataType.ByteArray] = RevertByteArray; r[(int)JsonDataType.Custom] = RevertCustom; r[(int)JsonDataType.DataSet] = RevertDataSet; r[(int)JsonDataType.DataTable] = RevertDataTable; r[(int)JsonDataType.DateTime] = RevertDateTime; r[(int)JsonDataType.Dictionary] = RevertDictionary; r[(int)JsonDataType.Double] = RevertPrimitive; r[(int)JsonDataType.Enum] = RevertEnum; r[(int)JsonDataType.List] = RevertList; r[(int)JsonDataType.Guid] = RevertGuid; r[(int)JsonDataType.Hashtable] = RevertHashTable; r[(int)JsonDataType.Int] = RevertInt32; r[(int)JsonDataType.Long] = RevertPrimitive; r[(int)JsonDataType.MultiDimensionalArray] = RevertMultiDimensionalArray; r[(int)JsonDataType.NameValue] = RevertNameValueCollection; r[(int)JsonDataType.Object] = RevertUndefined; r[(int)JsonDataType.Single] = RevertSingle; r[(int)JsonDataType.String] = RevertPrimitive; r[(int)JsonDataType.StringDictionary] = RevertStringDictionary; r[(int)JsonDataType.StringKeyDictionary] = RevertStringKeyDictionary; r[(int)JsonDataType.TimeSpan] = RevertTimeSpan; r[(int)JsonDataType.Undefined] = RevertUndefined; return(r); }
public SerializationInfo(ReflectionCache reflection) { Reflection = reflection; Constructor = reflection.Constructor; CommonType = reflection.CommonType; JsonDataType = reflection.JsonDataType; SerializeMethod = reflection.SerializeMethod; DeserializeMethod = reflection.DeserializeMethod; ItemDeserializeMethod = reflection.ItemDeserializer; AlwaysDeserializable = false; }
internal ReflectionCache(Type type) { Type = type; TypeName = type.FullName; AssemblyName = type.AssemblyQualifiedName; JsonDataType = Reflection.GetJsonDataType(type); SerializeMethod = JsonSerializer.GetWriteJsonMethod(type); DeserializeMethod = JsonDeserializer.GetReadJsonMethod(type); if (JsonDataType == JsonDataType.Enum) { IsFlaggedEnum = AttributeHelper.HasAttribute <FlagsAttribute> (type, false); return; } if (type.IsArray) { ArgumentTypes = new Type[] { type.GetElementType() }; CommonType = type.GetArrayRank() == 1 ? ComplexType.Array : ComplexType.MultiDimensionalArray; } else { var t = type; if (t.IsGenericType == false) { while ((t = t.BaseType) != null) { if (t.IsGenericType) { break; } } } if (t != null) { ArgumentTypes = t.GetGenericArguments(); var gt = t.GetGenericTypeDefinition(); if (gt.Equals(typeof(Dictionary <,>))) { CommonType = ComplexType.Dictionary; } else if (gt.Equals(typeof(List <>))) { CommonType = ComplexType.List; } else if (gt.Equals(typeof(Nullable <>))) { CommonType = ComplexType.Nullable; SerializeMethod = JsonSerializer.GetWriteJsonMethod(ArgumentTypes[0]); } } } if (typeof(IEnumerable).IsAssignableFrom(type)) { if (typeof(Array).IsAssignableFrom(type) == false) { AppendItem = Reflection.CreateWrapperMethod <AddCollectionItem> (Reflection.FindMethod(type, "Add", new Type[1] { null })); } if (ArgumentTypes != null && ArgumentTypes.Length == 1) { ItemSerializer = JsonSerializer.GetWriteJsonMethod(ArgumentTypes[0]); ItemDeserializer = JsonDeserializer.GetReadJsonMethod(ArgumentTypes[0]); } } if (ArgumentTypes != null) { ArgumentReflections = new ReflectionCache[ArgumentTypes.Length]; } if (CommonType != ComplexType.Array && CommonType != ComplexType.MultiDimensionalArray && CommonType != ComplexType.Nullable) { var t = type; if (type.IsNested == false && type.IsPublic == false) { ConstructorInfo |= ConstructorTypes.NonPublic; } else { while (t != null && t.IsNested) { if (t.IsNestedPublic == false) { ConstructorInfo |= ConstructorTypes.NonPublic; } t = t.DeclaringType; } } if (type.IsClass || type.IsValueType) { Constructor = Reflection.CreateConstructorMethod(type, type.IsVisible == false || typeof(DatasetSchema).Equals(type)); if (Constructor != null && Constructor.Method.IsPublic == false) { ConstructorInfo |= ConstructorTypes.NonPublic; } if (Constructor == null) { var c = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (c != null && c.Length > 0) { ConstructorInfo |= ConstructorTypes.Parametric; } } Members = Reflection.GetMembers(type); } } //if (typeof (IEnumerable).IsAssignableFrom (type)) { // return; //} //if (JsonDataType != JsonDataType.Undefined) { // return; //} }
public CompoundDeserializer(string collectionName, RevertJsonValue deserializeMethod) { CollectionName = collectionName; DeserializeMethod = deserializeMethod; }
void SetMultiDimensionalArrayValue(JsonArray data, ReflectionCache et, int[] upperBounds, int[] indexes, Array array, RevertJsonValue m, int rankIndex) { if (rankIndex + 1 == upperBounds.Length) { foreach (var item in data) { array.SetValue(m(this, item, et), indexes); ++indexes[rankIndex]; } return; } for (int i = 0; i < upperBounds[rankIndex]; i++) { var ob = data[indexes[rankIndex]] as JsonArray; if (ob == null) { continue; } else { for (int j = indexes.Length - 1; j > rankIndex; j--) { indexes[j] = 0; } SetMultiDimensionalArrayValue(ob, et, upperBounds, indexes, array, m, rankIndex + 1); ++indexes[rankIndex]; } } }
internal ReflectionCache(Type type) { Type = type; TypeName = type.FullName; AssemblyName = type.AssemblyQualifiedName; JsonDataType = Reflection.GetJsonDataType (type); SerializeMethod = JsonSerializer.GetWriteJsonMethod (type); DeserializeMethod = JsonDeserializer.GetReadJsonMethod (type); if (JsonDataType == JsonDataType.Enum) { IsFlaggedEnum = AttributeHelper.HasAttribute<FlagsAttribute> (type, false); return; } if (type.IsArray) { ArgumentTypes = new Type[] { type.GetElementType () }; CommonType = type.GetArrayRank () == 1 ? ComplexType.Array : ComplexType.MultiDimensionalArray; } else { var t = type; if (t.IsGenericType == false) { while ((t = t.BaseType) != null) { if (t.IsGenericType) { break; } } } if (t != null) { ArgumentTypes = t.GetGenericArguments (); var gt = t.GetGenericTypeDefinition (); if (gt.Equals (typeof (Dictionary<,>))) { CommonType = ComplexType.Dictionary; } else if (gt.Equals (typeof (List<>))) { CommonType = ComplexType.List; } else if (gt.Equals (typeof (Nullable<>))) { CommonType = ComplexType.Nullable; SerializeMethod = JsonSerializer.GetWriteJsonMethod (ArgumentTypes[0]); } } } if (typeof(IEnumerable).IsAssignableFrom (type)) { if (typeof(Array).IsAssignableFrom (type) == false) { AppendItem = Reflection.CreateWrapperMethod<AddCollectionItem> (Reflection.FindMethod (type, "Add", new Type[1] { null })); } if (ArgumentTypes != null && ArgumentTypes.Length == 1) { ItemSerializer = JsonSerializer.GetWriteJsonMethod (ArgumentTypes[0]); ItemDeserializer = JsonDeserializer.GetReadJsonMethod (ArgumentTypes[0]); } } if (ArgumentTypes != null) { ArgumentReflections = new ReflectionCache[ArgumentTypes.Length]; } if (CommonType != ComplexType.Array && CommonType != ComplexType.MultiDimensionalArray && CommonType != ComplexType.Nullable) { var t = type; if (type.IsNested == false && type.IsPublic == false) { ConstructorInfo |= ConstructorTypes.NonPublic; } else { while (t != null && t.IsNested) { if (t.IsNestedPublic == false) { ConstructorInfo |= ConstructorTypes.NonPublic; } t = t.DeclaringType; } } if (type.IsClass || type.IsValueType) { Constructor = Reflection.CreateConstructorMethod (type, type.IsVisible == false || typeof (DatasetSchema).Equals (type)); if (Constructor != null && Constructor.Method.IsPublic == false) { ConstructorInfo |= ConstructorTypes.NonPublic; } if (Constructor == null) { var c = type.GetConstructors (BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (c != null && c.Length > 0) { ConstructorInfo |= ConstructorTypes.Parametric; } } Members = Reflection.GetMembers (type); } } //if (typeof (IEnumerable).IsAssignableFrom (type)) { // return; //} //if (JsonDataType != JsonDataType.Undefined) { // return; //} }
MemberCache(MemberInfo memberInfo, Type memberType, string name) { MemberName = name; MemberType = memberType; MemberInfo = memberInfo; JsonDataType dt = Reflection.GetJsonDataType (memberType); DeserializeMethod = JsonDeserializer.GetReadJsonMethod (memberType); SerializeMethod = JsonSerializer.GetWriteJsonMethod (memberType); if (dt == JsonDataType.Array || dt == JsonDataType.MultiDimensionalArray) { ElementType = memberType.GetElementType (); } IsValueType = memberType.IsValueType; IsStruct = (IsValueType && !memberType.IsPrimitive && !memberType.IsEnum && typeof (decimal).Equals (memberType) == false); IsClass = memberType.IsClass; IsCollection = typeof (ICollection).IsAssignableFrom (memberType) && typeof (byte[]).Equals (memberType) == false; if (memberType.IsGenericType) { ElementType = memberType.GetGenericArguments ()[0]; IsNullable = memberType.GetGenericTypeDefinition ().Equals (typeof (Nullable<>)); } if (IsValueType) { ChangeType = IsNullable ? ElementType : memberType; } JsonDataType = dt; }