Exemple #1
0
        private void SerializeValue(object o, StringBuilder sb, int depth, Hashtable objectsInUse, PropertyMapper propertyMapper = null)
        {
            if (++depth > this._recursionLimit)
            {
                throw new ArgumentException(AtlasWeb.JSON_DepthLimitExceeded);
            }

            // Check whether a custom converter is available for this type.
            JConverter converter = null;

            if (o != null && this.ConverterExistsForType(o.GetType(), out converter))
            {
                var dict = converter.Serialize(o, this);

                if (this.TypeResolver != null)
                {
                    var typeString = TypeResolver.ResolveTypeId(o.GetType());
                    if (typeString != null)
                    {
                        dict[ServerTypeFieldName] = typeString;
                    }
                }

                sb.Append(Serialize(dict));
                return;
            }

            this.SerializeValueInternal(o, sb, depth, objectsInUse, propertyMapper);
        }
Exemple #2
0
 internal bool ConverterExistsForType(Type t, out JConverter converter)
 {
     if (this._converters == null)
     {
         converter = null;
         return(false);
     }
     converter = this._converters.FirstOrDefault(c => c.IsSupported(t));
     return(converter != null);
 }
Exemple #3
0
        // Is this a type for which we want to instantiate based on the client stub
        internal static bool IsClientInstantiatableType(Type t, JSerializer serializer)
        {
            // Abstract classes and interfaces can't be instantiated
            //
            if (t == null || t.IsAbstract || t.IsInterface || t.IsArray)
            {
                return(false);
            }

            // Even though 'object' is instantiatable, it is never useful to do this
            if (t == typeof(object))
            {
                return(false);
            }

            // Return true if a converter is registered for the given type, so the converter
            // can generate code on the client to instantiate it.
            JConverter converter = null;

            if (serializer.ConverterExistsForType(t, out converter))
            {
                return(true);
            }

            // Value types are okay (i.e. structs);
            if (t.IsValueType)
            {
                return(true);
            }

            // Ignore types that don't have a public default ctor
            ConstructorInfo constructorInfo = t.GetConstructor(BindingFlags.Public | BindingFlags.Instance,
                                                               null, s_emptyTypeArray, null);

            if (constructorInfo == null)
            {
                return(false);
            }

            return(true);
        }
Exemple #4
0
        // Method that converts an IDictionary<string, object> to an object of the right type
        private static bool ConvertDictionaryToObject(IDictionary <string, object> dictionary, Type type, JSerializer serializer, bool throwOnError, out object convertedObject)
        {
            // The target type to instantiate.
            Type   targetType = type;
            object s;
            string serverTypeName = null;
            object o = dictionary;

            // Check if __serverType exists in the dictionary, use it as the type.
            if (dictionary.TryGetValue(JSerializer.ServerTypeFieldName, out s))
            {
                // Convert the __serverType value to a string.
                if (!ConvertObjectToTypeMain(s, typeof(String), serializer, throwOnError, out s))
                {
                    convertedObject = false;
                    return(false);
                }

                serverTypeName = (string)s;

                if (serverTypeName != null)
                {
                    // If we don't have the JavaScriptTypeResolver, we can't use it
                    if (serializer.TypeResolver != null)
                    {
                        // Get the actual type from the resolver.
                        targetType = serializer.TypeResolver.ResolveType(serverTypeName);

                        // In theory, we should always find the type.  If not, it may be some kind of attack.
                        if (targetType == null)
                        {
                            if (throwOnError)
                            {
                                throw new InvalidOperationException();
                            }

                            convertedObject = null;
                            return(false);
                        }
                    }

                    // Remove the serverType from the dictionary, even if the resolver was null
                    dictionary.Remove(JSerializer.ServerTypeFieldName);
                }
            }

            JConverter converter = null;

            if (targetType != null && serializer.ConverterExistsForType(targetType, out converter))
            {
                try
                {
                    convertedObject = converter.Deserialize(dictionary, targetType, serializer);
                    return(true);
                }
                catch
                {
                    if (throwOnError)
                    {
                        throw;
                    }

                    convertedObject = null;
                    return(false);
                }
            }

            // Instantiate the type if it's coming from the __serverType argument.
            if (serverTypeName != null || IsClientInstantiatableType(targetType, serializer))
            {
                // First instantiate the object based on the type.
                o = Activator.CreateInstance(targetType);
            }

            // Use a different collection to avoid modifying the original during keys enumeration.
            List <String> memberNames = new List <String>(dictionary.Keys);

            // Try to handle the IDictionary<K, V> case
            if (IsGenericDictionary(type))
            {
                Type keyType = type.GetGenericArguments()[0];
                if (keyType != typeof(string) && keyType != typeof(object))
                {
                    if (throwOnError)
                    {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DictionaryTypeNotSupported, type.FullName));
                    }

                    convertedObject = null;
                    return(false);
                }

                Type        valueType = type.GetGenericArguments()[1];
                IDictionary dict      = null;
                if (IsClientInstantiatableType(type, serializer))
                {
                    dict = (IDictionary)Activator.CreateInstance(type);
                }
                else
                {
                    // Get the strongly typed Dictionary<K, V>
                    Type t = _dictionaryGenericType.MakeGenericType(keyType, valueType);
                    dict = (IDictionary)Activator.CreateInstance(t);
                }

                if (dict != null)
                {
                    foreach (string memberName in memberNames)
                    {
                        object memberObject;
                        if (!ConvertObjectToTypeMain(dictionary[memberName], valueType, serializer, throwOnError, out memberObject))
                        {
                            convertedObject = null;
                            return(false);
                        }
                        dict[memberName] = memberObject;
                    }

                    convertedObject = dict;
                    return(true);
                }
            }

            // Fail if we know we cannot possibly return the required type.
            if (type != null && !type.IsAssignableFrom(o.GetType()))
            {
                if (!throwOnError)
                {
                    convertedObject = null;
                    return(false);
                }

                ConstructorInfo constructorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, s_emptyTypeArray, null);
                if (constructorInfo == null)
                {
                    throw new MissingMethodException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_NoConstructor, type.FullName));
                }

                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DeserializerTypeMismatch, type.FullName));
            }
            var typeMapper = TypeMapper.Create(o.GetType());

            foreach (string memberName in memberNames)
            {
                object propertyValue = dictionary[memberName];
                // Assign the value into a property or field of the object
                if (!AssignToProperty(typeMapper, propertyValue, o, memberName, serializer, throwOnError))
                {
                    convertedObject = null;
                    return(false);
                }
            }

            convertedObject = o;
            return(true);
        }