Beispiel #1
0
 internal static object RevertNameValueCollection(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(CreateNameValueCollection((JsonDict)value));
 }
Beispiel #2
0
 internal static object RevertHashTable(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(deserializer.RootHashTable((JsonArray)value));
 }
Beispiel #3
0
 internal static object RevertDictionary(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(deserializer.RootDictionary(value, type));
 }
Beispiel #4
0
 internal static object RevertList(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(deserializer.CreateList((JsonArray)value, type, null));
 }
Beispiel #5
0
 internal static object RevertDataTable(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(deserializer.CreateDataTable((JsonDict)value));
 }
Beispiel #6
0
 internal static object RevertPrimitive(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(value);
 }
Beispiel #7
0
 internal static object RevertMultiDimensionalArray(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(deserializer.CreateMultiDimensionalArray((JsonArray)value, type));
 }
Beispiel #8
0
 internal static object RevertGuid(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(CreateGuid(value));
 }
Beispiel #9
0
 internal static object RevertByteArray(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(Convert.FromBase64String((string)value));
 }
Beispiel #10
0
 internal static object RevertDecimal(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(value is double?(decimal)(double)value : value is long?(decimal)(long)value : value is string?(decimal)ValueConverter.ToDouble((string)value) : value is bool && (bool)value ? (decimal)1 : (decimal)0);
 }
Beispiel #11
0
        internal static object RevertChar(JsonDeserializer deserializer, object value, SerializationInfo type)
        {
            var s = value as string;

            return(s.Length > 0 ? s[0] : '\0');
        }
Beispiel #12
0
 internal static object RevertSingle(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(value is double?(float)(double)value : value is long?(float)(long)value : value is string?ValueConverter.ToSingle((string)value) : value is bool && (bool)value ? (float)1 : (float)0);
 }
Beispiel #13
0
 internal static object RevertUInt64(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(value is long?(ulong)(long)value : value is double?(ulong)(double)value : value is string?(ulong)ValueConverter.ToInt64((string)value) : value is bool && (bool)value ? (ulong)1 : (ulong)0);
 }
Beispiel #14
0
 internal static object RevertSByte(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(value is long?(sbyte)(long)value : value is double?(sbyte)(double)value : value is string?(sbyte)ValueConverter.ToInt32((string)value) : value is bool && (bool)value ? (sbyte)1 : (sbyte)0);
 }
Beispiel #15
0
 internal static object RevertStringKeyDictionary(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(deserializer.CreateStringKeyDictionary((JsonDict)value, type));
 }
Beispiel #16
0
 internal static object RevertDateTime(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(CreateDateTime(deserializer, value));
 }
Beispiel #17
0
 internal static object RevertEnum(JsonDeserializer deserializer, object value, SerializationInfo type)
 {
     return(deserializer.CreateEnum(value, type.Reflection.Type));
 }
Beispiel #18
0
        internal ReflectionCache(Type type)
        {
            Type                 = type;
            TypeName             = type.FullName;
            AssemblyName         = type.AssemblyQualifiedName;
            CircularReferencable = type.IsValueType == false || type != typeof(string);
            IsAbstract           = type.IsUndetermined();
            IsAnonymous          = type.IsAnonymous();

            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.IsPointer)
            {
                throw new JsonSerializationException("Can not serialize pointer type: " + type.AssemblyQualifiedName);
            }
            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[] { 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)
            {
                if (type.IsPubliclyAccessible() == false)
                {
                    ConstructorInfo |= ConstructorTypes.NonPublic;
                }
                if (type.IsClass || type.IsValueType)
                {
                    try {
                        Constructor = Reflection.CreateConstructorMethod(type, type.IsVisible == false || typeof(DatasetSchema).Equals(type));
                    }
                    catch (Exception ex) {
                        throw new JsonSerializationException("Error occurred when creating constructor method for type " + type.AssemblyQualifiedName, ex);
                    }
                    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;
            //}
        }