Example #1
0
 protected virtual void TraverseObject(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
 {
     if (typeof(IDictionary).IsAssignableFrom(value.Type))
     {
         this.TraverseDictionary(value, visitor, currentDepth, typeof(object), typeof(object));
     }
     else
     {
         Type implementedGenericInterface = ReflectionUtility.GetImplementedGenericInterface(value.Type, typeof(IDictionary <,>));
         if (implementedGenericInterface != null)
         {
             GenericDictionaryToNonGenericAdapter adapter = new GenericDictionaryToNonGenericAdapter(value.Value, implementedGenericInterface);
             Type[] genericArguments = implementedGenericInterface.GetGenericArguments();
             this.TraverseDictionary(new ObjectDescriptor(adapter, value.Type, value.StaticType, value.ScalarStyle), visitor, currentDepth, genericArguments[0], genericArguments[1]);
         }
         else if (typeof(IEnumerable).IsAssignableFrom(value.Type))
         {
             this.TraverseList(value, visitor, currentDepth);
         }
         else
         {
             this.TraverseProperties(value, visitor, currentDepth);
         }
     }
 }
Example #2
0
        protected virtual void TraverseObject <TContext>(IObjectDescriptor value, IObjectGraphVisitor <TContext> visitor, int currentDepth, TContext context)
        {
            if (typeof(IDictionary).IsAssignableFrom(value.Type))
            {
                TraverseDictionary(value, visitor, currentDepth, typeof(object), typeof(object), context);
                return;
            }

            var genericDictionaryType = ReflectionUtility.GetImplementedGenericInterface(value.Type, typeof(IDictionary <,>));

            if (genericDictionaryType != null)
            {
                var adaptedDictionary = new GenericDictionaryToNonGenericAdapter(value.Value, genericDictionaryType);
                var genericArguments  = genericDictionaryType.GetGenericArguments();
                TraverseDictionary(new ObjectDescriptor(adaptedDictionary, value.Type, value.StaticType, value.ScalarStyle), visitor, currentDepth, genericArguments[0], genericArguments[1], context);
                return;
            }

            if (typeof(IEnumerable).IsAssignableFrom(value.Type))
            {
                TraverseList(value, visitor, currentDepth, context);
                return;
            }

            TraverseProperties(value, visitor, currentDepth, context);
        }
        bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func <IParser, Type, object> nestedObjectDeserializer, out object value)
        {
            Type        implementedGenericInterface = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(IDictionary <, >));
            Type        tKey;
            Type        tValue;
            IDictionary dictionary;

            if (implementedGenericInterface != null)
            {
                Type[] genericArguments = implementedGenericInterface.GetGenericArguments();
                tKey       = genericArguments[0];
                tValue     = genericArguments[1];
                value      = _objectFactory.Create(expectedType);
                dictionary = (value as IDictionary);
                if (dictionary == null)
                {
                    dictionary = new GenericDictionaryToNonGenericAdapter(value, implementedGenericInterface);
                }
            }
            else
            {
                if (!typeof(IDictionary).IsAssignableFrom(expectedType))
                {
                    value = null;
                    return(false);
                }
                tKey       = typeof(object);
                tValue     = typeof(object);
                value      = _objectFactory.Create(expectedType);
                dictionary = (IDictionary)value;
            }
            DeserializeHelper(tKey, tValue, parser, nestedObjectDeserializer, dictionary);
            return(true);
        }
Example #4
0
        bool INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func <IParser, Type, object> nestedObjectDeserializer, out object value)
        {
            IDictionary dictionary;
            Type        keyType, valueType;
            var         genericDictionaryType = ReflectionUtility.GetImplementedGenericInterface(expectedType, typeof(IDictionary <,>));

            if (genericDictionaryType != null)
            {
                var genericArguments = genericDictionaryType.GetGenericArguments();
                keyType   = genericArguments[0];
                valueType = genericArguments[1];

                value = _objectFactory.Create(expectedType);

                dictionary = value as IDictionary;
                if (dictionary == null)
                {
                    dictionary = new GenericDictionaryToNonGenericAdapter(value, genericDictionaryType);
                }
            }
            else if (typeof(IDictionary).IsAssignableFrom(expectedType))
            {
                keyType   = typeof(object);
                valueType = typeof(object);

                value      = _objectFactory.Create(expectedType);
                dictionary = (IDictionary)value;
            }
            else
            {
                value = null;
                return(false);
            }

            DeserializeHelper(keyType, valueType, parser, nestedObjectDeserializer, dictionary);

            return(true);
        }