示例#1
0
        protected virtual void TraverseObject <TContext>(IObjectDescriptor value, IObjectGraphVisitor <TContext> visitor, TContext context, Stack <ObjectPathSegment> path)
        {
            if (typeof(IDictionary).IsAssignableFrom(value.Type))
            {
                TraverseDictionary(value, visitor, typeof(object), typeof(object), context, path);
                return;
            }

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

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

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

            TraverseProperties(value, visitor, context, path);
        }
示例#2
0
        private void TraverseList(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
        {
            Type implementedGenericInterface = ReflectionUtility.GetImplementedGenericInterface(value.Type, typeof(IEnumerable <>));
            Type elementType = (implementedGenericInterface == null) ? typeof(object) : implementedGenericInterface.GetGenericArguments()[0];

            visitor.VisitSequenceStart(value, elementType);
            IEnumerator enumerator = ((IEnumerable)value.Value).GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    object current = enumerator.Current;
                    this.Traverse(this.GetObjectDescriptor(current, elementType), visitor, currentDepth);
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            visitor.VisitSequenceEnd(value);
        }
示例#3
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);
         }
     }
 }
        protected virtual void TraverseObject(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
        {
            Action <IObjectDescriptor, IObjectGraphVisitor, int> action;

            if (!_behaviorCache.TryGetValue(value.Type, out action))
            {
                if (typeof(IDictionary).IsAssignableFrom(value.Type))
                {
                    action = TraverseDictionary;
                }
                else
                {
                    var dictionaryType = ReflectionUtility.GetImplementedGenericInterface(value.Type, typeof(IDictionary <,>));
                    if (dictionaryType != null)
                    {
                        action = (v, vi, d) => TraverseGenericDictionary(v, dictionaryType, vi, d);
                    }
                    else if (typeof(IEnumerable).IsAssignableFrom(value.Type))
                    {
                        action = TraverseList;
                    }
                    else
                    {
                        action = TraverseProperties;
                    }
                }
                _behaviorCache[value.Type] = action;
            }
            action(value, visitor, currentDepth);
        }
示例#5
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);
        }
示例#6
0
        protected virtual void Traverse(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
        {
            if (++currentDepth > this.maxRecursion)
            {
                throw new InvalidOperationException("Too much recursion when traversing the object graph");
            }
            if (visitor.Enter(value))
            {
                TypeCode typeCode = value.Type.GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Empty:
                {
                    object[] args = new object[] { typeCode };
                    throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", args));
                }

                case TypeCode.DBNull:
                    visitor.VisitScalar(new ObjectDescriptor(null, typeof(object), typeof(object)));
                    break;

                case TypeCode.Boolean:
                case TypeCode.Char:
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.DateTime:
                case TypeCode.String:
                    visitor.VisitScalar(value);
                    break;

                default:
                    if ((value.Value == null) || ReferenceEquals(value.Type, typeof(TimeSpan)))
                    {
                        visitor.VisitScalar(value);
                    }
                    else
                    {
                        Type underlyingType = Nullable.GetUnderlyingType(value.Type);
                        if (underlyingType != null)
                        {
                            this.Traverse(new ObjectDescriptor(value.Value, underlyingType, value.Type, value.ScalarStyle), visitor, currentDepth);
                        }
                        else
                        {
                            this.TraverseObject(value, visitor, currentDepth);
                        }
                    }
                    break;
                }
            }
        }
示例#7
0
 public EmissionPhaseObjectGraphVisitorArgs(IObjectGraphVisitor <IEmitter> innerVisitor, IEventEmitter eventEmitter, IEnumerable <IObjectGraphVisitor <Nothing> > preProcessingPhaseVisitors, IEnumerable <IYamlTypeConverter> typeConverters, ObjectSerializer nestedObjectSerializer)
 {
     if (innerVisitor == null)
     {
         throw new ArgumentNullException("innerVisitor");
     }
     InnerVisitor = innerVisitor;
     if (eventEmitter == null)
     {
         throw new ArgumentNullException("eventEmitter");
     }
     EventEmitter = eventEmitter;
     if (preProcessingPhaseVisitors == null)
     {
         throw new ArgumentNullException("preProcessingPhaseVisitors");
     }
     this.preProcessingPhaseVisitors = preProcessingPhaseVisitors;
     if (typeConverters == null)
     {
         throw new ArgumentNullException("typeConverters");
     }
     TypeConverters = typeConverters;
     if (nestedObjectSerializer == null)
     {
         throw new ArgumentNullException("nestedObjectSerializer");
     }
     NestedObjectSerializer = nestedObjectSerializer;
 }
示例#8
0
        protected virtual void TraverseDictionary(IObjectDescriptor dictionary, IObjectGraphVisitor visitor, int currentDepth, Type keyType, Type valueType)
        {
            visitor.VisitMappingStart(dictionary, keyType, valueType);
            bool flag = dictionary.Type.FullName.Equals("System.Dynamic.ExpandoObject");
            IDictionaryEnumerator enumerator = ((IDictionary)dictionary.Value).GetEnumerator();

            try
            {
                while (enumerator.MoveNext())
                {
                    DictionaryEntry current = (DictionaryEntry)enumerator.Current;
                    string          str     = !flag?current.Key.ToString() : this.namingConvention.Apply(current.Key.ToString());

                    IObjectDescriptor objectDescriptor = this.GetObjectDescriptor(str, keyType);
                    IObjectDescriptor descriptor2      = this.GetObjectDescriptor(current.Value, valueType);
                    if (visitor.EnterMapping(objectDescriptor, descriptor2))
                    {
                        this.Traverse(objectDescriptor, visitor, currentDepth);
                        this.Traverse(descriptor2, visitor, currentDepth);
                    }
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            visitor.VisitMappingEnd(dictionary);
        }
 public CustomSerializationObjectGraphVisitor(IEmitter emitter, IObjectGraphVisitor nextVisitor, IEnumerable <IYamlTypeConverter> typeConverters)
     : base(nextVisitor)
 {
     this.emitter        = emitter;
     this.typeConverters = typeConverters != null
                         ? typeConverters.ToList()
                         : Enumerable.Empty <IYamlTypeConverter>();
 }
		public CustomSerializationObjectGraphVisitor(IEmitter emitter, IObjectGraphVisitor nextVisitor, IEnumerable<IYamlTypeConverter> typeConverters)
			: base(nextVisitor)
		{
			this.emitter = emitter;
			this.typeConverters = typeConverters != null
				? typeConverters.ToList()
				: Enumerable.Empty<IYamlTypeConverter>();
		}
        protected override void SerializeProperties(object value, Type type, IObjectGraphVisitor visitor, int currentDepth)
        {
            if (!ReflectionUtility.HasDefaultConstructor(type) && !serializer.Converters.Any(c => c.Accepts(type)))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Type '{0}' cannot be deserialized because it does not have a default constructor or a type converter.", type));
            }

            base.SerializeProperties(value, type, visitor, currentDepth);
        }
        protected override void TraverseProperties(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
        {
            if (!value.Type.HasDefaultConstructor() && !serializer.Converters.Any(c => c.Accepts(value.Type)))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Type '{0}' cannot be deserialized because it does not have a default constructor or a type converter.", value.Type));
            }

            base.TraverseProperties(value, visitor, currentDepth);
        }
        protected override void TraverseProperties(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
        {
            if (!ReflectionUtility.HasDefaultConstructor(value.Type) && !serializer.Converters.Any(c => c.Accepts(value.Type)))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Type '{0}' cannot be deserialized because it does not have a default constructor or a type converter.", value.Type));
            }

            base.TraverseProperties(value, visitor, currentDepth);
        }
        protected override void SerializeProperties(object value, Type type, IObjectGraphVisitor visitor, int currentDepth)
        {
            if (!ReflectionUtility.HasDefaultConstructor(type))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Type '{0}' cannot be deserialized because it does not have a default constructor.", type));
            }

            base.SerializeProperties(value, type, visitor, currentDepth);
        }
        public CustomSerializationObjectGraphVisitor(IObjectGraphVisitor<IEmitter> nextVisitor, IEnumerable<IYamlTypeConverter> typeConverters, ObjectSerializer nestedObjectSerializer)
            : base(nextVisitor)
        {
            this.typeConverters = typeConverters != null
                ? typeConverters.ToList()
                : Enumerable.Empty<IYamlTypeConverter>();

            this.nestedObjectSerializer = nestedObjectSerializer;
        }
示例#16
0
        protected virtual void Traverse <TContext>(IObjectDescriptor value, IObjectGraphVisitor <TContext> visitor, int currentDepth, TContext context)
        {
            if (++currentDepth > maxRecursion)
            {
                throw new InvalidOperationException("Too much recursion when traversing the object graph");
            }
            if (visitor.Enter(value, context))
            {
                TypeCode typeCode = value.Type.GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                case TypeCode.Char:
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.DateTime:
                case TypeCode.String:
                    visitor.VisitScalar(value, context);
                    break;

                case TypeCode.Empty:
                    throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode));

                default:
                    if (value.IsDbNull())
                    {
                        visitor.VisitScalar((IObjectDescriptor) new ObjectDescriptor(null, typeof(object), typeof(object)), context);
                    }
                    if (value.Value == null || value.Type == typeof(TimeSpan))
                    {
                        visitor.VisitScalar(value, context);
                    }
                    else
                    {
                        Type underlyingType = Nullable.GetUnderlyingType(value.Type);
                        if (underlyingType != null)
                        {
                            Traverse(new ObjectDescriptor(value.Value, underlyingType, value.Type, value.ScalarStyle), visitor, currentDepth, context);
                        }
                        else
                        {
                            TraverseObject(value, visitor, currentDepth, context);
                        }
                    }
                    break;
                }
            }
        }
示例#17
0
        public YamlIEnumerableSkipEmptyObjectGraphVisitor(IObjectGraphVisitor <IEmitter> nextVisitor, IEnumerable <IYamlTypeConverter> typeConverters, ObjectSerializer nestedObjectSerializer)
            : base(nextVisitor)
        {
            this.typeConverters = typeConverters != null
                                ? typeConverters.ToList()
                                : Enumerable.Empty <IYamlTypeConverter>();

            this.nestedObjectSerializer = nestedObjectSerializer;
        }
示例#18
0
        private void EmitDocument(IEmitter emitter, IObjectDescriptor graph)
        {
            IObjectGraphTraversalStrategy traversalStrategy = this.CreateTraversalStrategy();
            IObjectGraphVisitor           visitor           = this.CreateEmittingVisitor(emitter, traversalStrategy, this.CreateEventEmitter(emitter), graph);

            emitter.Emit(new StreamStart());
            emitter.Emit(new DocumentStart());
            traversalStrategy.Traverse(graph, visitor);
            emitter.Emit(new DocumentEnd(true));
            emitter.Emit(new StreamEnd());
        }
        protected virtual void Traverse(object value, Type type, IObjectGraphVisitor visitor, int currentDepth)
        {
            if (++currentDepth > maxRecursion)
            {
                throw new InvalidOperationException("Too much recursion when traversing the object graph");
            }

            if (!visitor.Enter(value, type))
            {
                return;
            }

            var typeCode = Type.GetTypeCode(type);

            switch (typeCode)
            {
            case TypeCode.Boolean:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.SByte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
            case TypeCode.String:
            case TypeCode.Char:
            case TypeCode.DateTime:
                visitor.VisitScalar(value, type);
                break;

            case TypeCode.DBNull:
                visitor.VisitScalar(null, type);
                break;

            case TypeCode.Empty:
                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode));

            default:
                if (value == null)
                {
                    visitor.VisitScalar(value, type);
                }
                else
                {
                    TraverseObject(value, type, visitor, currentDepth);
                }
                break;
            }
        }
 private void TraverseGenericDictionaryHelper <TKey, TValue>(
     IDictionary <TKey, TValue> value,
     IObjectGraphVisitor visitor, int currentDepth)
 {
     foreach (var entry in value)
     {
         if (visitor.EnterMapping(entry.Key, typeof(TKey), entry.Value, typeof(TValue)))
         {
             Traverse(entry.Key, typeof(TKey), visitor, currentDepth);
             Traverse(entry.Value, typeof(TValue), visitor, currentDepth);
         }
     }
 }
 public EmissionPhaseObjectGraphVisitorArgs(
     IObjectGraphVisitor <IEmitter> innerVisitor,
     IEventEmitter eventEmitter,
     IEnumerable <IObjectGraphVisitor <Nothing> > preProcessingPhaseVisitors,
     IEnumerable <IYamlTypeConverter> typeConverters,
     ObjectSerializer nestedObjectSerializer
     )
 {
     InnerVisitor = innerVisitor ?? throw new ArgumentNullException(nameof(innerVisitor));
     EventEmitter = eventEmitter ?? throw new ArgumentNullException(nameof(eventEmitter));
     this.preProcessingPhaseVisitors = preProcessingPhaseVisitors ?? throw new ArgumentNullException(nameof(preProcessingPhaseVisitors));
     TypeConverters         = typeConverters ?? throw new ArgumentNullException(nameof(typeConverters));
     NestedObjectSerializer = nestedObjectSerializer ?? throw new ArgumentNullException(nameof(nestedObjectSerializer));
 }
示例#22
0
 protected virtual void TraverseProperties(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
 {
     visitor.VisitMappingStart(value, typeof(string), typeof(object));
     foreach (IPropertyDescriptor descriptor in this.typeDescriptor.GetProperties(value.Type, value.Value))
     {
         IObjectDescriptor descriptor2 = descriptor.Read(value.Value);
         if (visitor.EnterMapping(descriptor, descriptor2))
         {
             this.Traverse(new ObjectDescriptor(descriptor.Name, typeof(string), typeof(string)), visitor, currentDepth);
             this.Traverse(descriptor2, visitor, currentDepth);
         }
     }
     visitor.VisitMappingEnd(value);
 }
示例#23
0
        private void TraverseList <TContext>(IObjectDescriptor value, IObjectGraphVisitor <TContext> visitor, int currentDepth, TContext context)
        {
            var enumerableType = ReflectionUtility.GetImplementedGenericInterface(value.Type, typeof(IEnumerable <>));
            var itemType       = enumerableType != null?enumerableType.GetGenericArguments()[0] : typeof(object);

            visitor.VisitSequenceStart(value, itemType, context);

            foreach (var item in (IEnumerable)value.Value)
            {
                Traverse(GetObjectDescriptor(item, itemType), visitor, currentDepth, context);
            }

            visitor.VisitSequenceEnd(value, context);
        }
        private void SerializeList(object value, Type type, IObjectGraphVisitor visitor, int currentDepth)
        {
            var enumerableType = ReflectionUtility.GetImplementedGenericInterface(type, typeof(IEnumerable <>));
            var itemType       = enumerableType != null?enumerableType.GetGenericArguments()[0] : typeof(object);

            visitor.VisitSequenceStart(value, type, itemType);

            foreach (var item in (IEnumerable)value)
            {
                Traverse(item, itemType, visitor, currentDepth);
            }

            visitor.VisitSequenceEnd(value, type);
        }
 private static void TraverseGenericDictionaryHelper <TKey, TValue>(
     // "this" is passed as parameter so that we can cache the generic method definition
     FullObjectGraphTraversalStrategy self,
     IDictionary <TKey, TValue> value,
     IObjectGraphVisitor visitor, int currentDepth)
 {
     foreach (var entry in value)
     {
         if (visitor.EnterMapping(entry.Key, typeof(TKey), entry.Value, typeof(TValue)))
         {
             self.Traverse(entry.Key, typeof(TKey), visitor, currentDepth);
             self.Traverse(entry.Value, typeof(TValue), visitor, currentDepth);
         }
     }
 }
示例#26
0
        private void TraverseGenericDictionaryHelper <TKey, TValue>(
            IDictionary <TKey, TValue> dictionary,
            IObjectGraphVisitor visitor, int currentDepth)
        {
            foreach (var entry in dictionary)
            {
                var key   = GetObjectDescriptor(entry.Key, typeof(TKey));
                var value = GetObjectDescriptor(entry.Value, typeof(TValue));

                if (visitor.EnterMapping(key, value))
                {
                    Traverse(key, visitor, currentDepth);
                    Traverse(value, visitor, currentDepth);
                }
            }
        }
        protected virtual void SerializeProperties(object value, Type type, IObjectGraphVisitor visitor, int currentDepth)
        {
            visitor.VisitMappingStart(value, type, typeof(string), typeof(object));

            foreach (var propertyDescriptor in typeDescriptor.GetProperties(type))
            {
                var propertyValue = propertyDescriptor.Property.GetValue(value, null);

                if (visitor.EnterMapping(propertyDescriptor, propertyValue))
                {
                    Traverse(propertyDescriptor.Name, typeof(string), visitor, currentDepth);
                    Traverse(propertyValue, propertyDescriptor.Property.PropertyType, visitor, currentDepth);
                }
            }

            visitor.VisitMappingEnd(value, type);
        }
示例#28
0
        private void TraverseList <TContext>(IObjectDescriptor value, IObjectGraphVisitor <TContext> visitor, TContext context, Stack <ObjectPathSegment> path)
        {
            var enumerableType = ReflectionUtility.GetImplementedGenericInterface(value.Type, typeof(IEnumerable <>));
            var itemType       = enumerableType != null?enumerableType.GetGenericArguments()[0] : typeof(object);

            visitor.VisitSequenceStart(value, itemType, context);

            var index = 0;

            foreach (var item in (IEnumerable)value.NonNullValue())
            {
                Traverse(index, GetObjectDescriptor(item, itemType), visitor, context, path);
                ++index;
            }

            visitor.VisitSequenceEnd(value, context);
        }
示例#29
0
        protected virtual void TraverseProperties(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
        {
            visitor.VisitMappingStart(value, typeof(string), typeof(object));

            foreach (var propertyDescriptor in typeDescriptor.GetProperties(value.Type, value.Value))
            {
                var propertyValue = propertyDescriptor.Read(value.Value);

                if (visitor.EnterMapping(propertyDescriptor, propertyValue))
                {
                    Traverse(new ObjectDescriptor(propertyDescriptor.Name, typeof(string), typeof(string)), visitor, currentDepth);
                    Traverse(propertyValue, visitor, currentDepth);
                }
            }

            visitor.VisitMappingEnd(value);
        }
        protected virtual void TraverseDictionary(object value, Type type, IObjectGraphVisitor visitor, int currentDepth)
        {
            visitor.VisitMappingStart(value, type, typeof(object), typeof(object));

            foreach (DictionaryEntry entry in (IDictionary)value)
            {
                var keyType   = GetObjectType(entry.Key);
                var valueType = GetObjectType(entry.Value);
                if (visitor.EnterMapping(entry.Key, keyType, entry.Value, valueType))
                {
                    Traverse(entry.Key, keyType, visitor, currentDepth);
                    Traverse(entry.Value, valueType, visitor, currentDepth);
                }
            }

            visitor.VisitMappingEnd(value, type);
        }
示例#31
0
        protected virtual void TraverseDictionary(IObjectDescriptor dictionary, IObjectGraphVisitor visitor, int currentDepth)
        {
            visitor.VisitMappingStart(dictionary, typeof(object), typeof(object));

            foreach (DictionaryEntry entry in (IDictionary)dictionary.Value)
            {
                var key   = GetObjectDescriptor(entry.Key, typeof(object));
                var value = GetObjectDescriptor(entry.Value, typeof(object));

                if (visitor.EnterMapping(key, value))
                {
                    Traverse(key, visitor, currentDepth);
                    Traverse(value, visitor, currentDepth);
                }
            }

            visitor.VisitMappingEnd(dictionary);
        }
示例#32
0
        private void TraverseGenericDictionaryHelper <TKey, TValue>(
            IDictionary <TKey, TValue> dictionary,
            IObjectGraphVisitor visitor, int currentDepth, INamingConvention namingConvention)
        {
            var isDynamic = dictionary.GetType().FullName.Equals("System.Dynamic.ExpandoObject");

            foreach (var entry in dictionary)
            {
                var keyString = isDynamic ? namingConvention.Apply(entry.Key.ToString()) : entry.Key.ToString();
                var key       = GetObjectDescriptor(keyString, typeof(TKey));
                var value     = GetObjectDescriptor(entry.Value, typeof(TValue));

                if (visitor.EnterMapping(key, value))
                {
                    Traverse(key, visitor, currentDepth);
                    Traverse(value, visitor, currentDepth);
                }
            }
        }
示例#33
0
            public void SerializeValue(IEmitter emitter, object value, Type type)
            {
                Type             type2      = (type != null) ? type : ((value == null) ? typeof(object) : value.GetType());
                Type             staticType = type ?? typeof(object);
                ObjectDescriptor graph      = new ObjectDescriptor(value, type2, staticType);
                List <IObjectGraphVisitor <Nothing> > preProcessingPhaseObjectGraphVisitors = preProcessingPhaseObjectGraphVisitorFactories.BuildComponentList(typeConverters);

                foreach (IObjectGraphVisitor <Nothing> item in preProcessingPhaseObjectGraphVisitors)
                {
                    traversalStrategy.Traverse(graph, item, null);
                }
                ObjectSerializer nestedObjectSerializer = delegate(object v, Type t)
                {
                    SerializeValue(emitter, v, t);
                };
                IObjectGraphVisitor <IEmitter> visitor = emissionPhaseObjectGraphVisitorFactories.BuildComponentChain(new EmittingObjectGraphVisitor(eventEmitter), (IObjectGraphVisitor <IEmitter> inner) => new EmissionPhaseObjectGraphVisitorArgs(inner, eventEmitter, preProcessingPhaseObjectGraphVisitors, typeConverters, nestedObjectSerializer));

                traversalStrategy.Traverse(graph, visitor, emitter);
            }
示例#34
0
        protected virtual void TraverseDictionary(IObjectDescriptor dictionary, IObjectGraphVisitor visitor, int currentDepth, Type keyType, Type valueType)
        {
            visitor.VisitMappingStart(dictionary, keyType, valueType);

            var isDynamic = dictionary.Type.FullName.Equals("System.Dynamic.ExpandoObject");

            foreach (DictionaryEntry entry in (IDictionary)dictionary.Value)
            {
                var keyString = isDynamic ? namingConvention.Apply(entry.Key.ToString()) : entry.Key.ToString();
                var key       = GetObjectDescriptor(keyString, keyType);
                var value     = GetObjectDescriptor(entry.Value, valueType);

                if (visitor.EnterMapping(key, value))
                {
                    Traverse(key, visitor, currentDepth);
                    Traverse(value, visitor, currentDepth);
                }
            }

            visitor.VisitMappingEnd(dictionary);
        }
示例#35
0
        private void EmitDocument(Emitter emitter, IObjectGraphTraversalStrategy traversalStrategy, IObjectGraphVisitor emittingVisitor, object graph, Type type)
        {
            emitter.Emit(new StreamStart());
            emitter.Emit(new DocumentStart());

            traversalStrategy.Traverse(graph, type, emittingVisitor);

            emitter.Emit(new DocumentEnd(true));
            emitter.Emit(new StreamEnd());
        }
 protected ChainedObjectGraphVisitor(IObjectGraphVisitor nextVisitor)
 {
     this.nextVisitor = nextVisitor;
 }
		public AnchorAssigningObjectGraphVisitor(IObjectGraphVisitor nextVisitor, IEventEmitter eventEmitter, IAliasProvider aliasProvider)
			: base(nextVisitor)
		{
			this.eventEmitter = eventEmitter;
			this.aliasProvider = aliasProvider;
		}
		public DefaultExclusiveObjectGraphVisitor(IObjectGraphVisitor nextVisitor)
			: base(nextVisitor)
		{
		}
        protected virtual void TraverseObject(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
        {
            if (typeof(IDictionary).IsAssignableFrom(value.Type))
            {
                TraverseDictionary(value, visitor, currentDepth, typeof(object), typeof(object));
                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]);
                return;
            }

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

            TraverseProperties(value, visitor, currentDepth);
        }
        protected virtual void TraverseProperties(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
        {
            visitor.VisitMappingStart(value, typeof(string), typeof(object));

            foreach (var propertyDescriptor in typeDescriptor.GetProperties(value.Type, value.Value))
            {
                var propertyValue = propertyDescriptor.Read(value.Value);

                if (visitor.EnterMapping(propertyDescriptor, propertyValue))
                {
                    Traverse(new ObjectDescriptor(propertyDescriptor.Name, typeof(string), typeof(string)), visitor, currentDepth);
                    Traverse(propertyValue, visitor, currentDepth);
                }
            }

            visitor.VisitMappingEnd(value);
        }
        protected virtual void TraverseDictionary(IObjectDescriptor dictionary, IObjectGraphVisitor visitor, int currentDepth, Type keyType, Type valueType)
        {
            visitor.VisitMappingStart(dictionary, keyType, valueType);

            var isDynamic = dictionary.Type.FullName.Equals("System.Dynamic.ExpandoObject");
            foreach (DictionaryEntry entry in (IDictionary)dictionary.Value)
            {
                var keyString = isDynamic ? namingConvention.Apply(entry.Key.ToString()) : entry.Key.ToString();
                var key = GetObjectDescriptor(keyString, keyType);
                var value = GetObjectDescriptor(entry.Value, valueType);

                if (visitor.EnterMapping(key, value))
                {
                    Traverse(key, visitor, currentDepth);
                    Traverse(value, visitor, currentDepth);
                }
            }

            visitor.VisitMappingEnd(dictionary);
        }
        private void TraverseList(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
        {
            var enumerableType = ReflectionUtility.GetImplementedGenericInterface(value.Type, typeof(IEnumerable<>));
            var itemType = enumerableType != null ? enumerableType.GetGenericArguments()[0] : typeof(object);

            visitor.VisitSequenceStart(value, itemType);

            foreach (var item in (IEnumerable)value.Value)
            {
                Traverse(GetObjectDescriptor(item, itemType), visitor, currentDepth);
            }

            visitor.VisitSequenceEnd(value);
        }
 void IObjectGraphTraversalStrategy.Traverse(IObjectDescriptor graph, IObjectGraphVisitor visitor)
 {
     Traverse(graph, visitor, 0);
 }
        protected virtual void Traverse(IObjectDescriptor value, IObjectGraphVisitor visitor, int currentDepth)
        {
            if (++currentDepth > maxRecursion)
            {
                throw new InvalidOperationException("Too much recursion when traversing the object graph");
            }

            if (!visitor.Enter(value))
            {
                return;
            }

            var typeCode = value.Type.GetTypeCode();
            switch (typeCode)
            {
                case TypeCode.Boolean:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.SByte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.String:
                case TypeCode.Char:
                case TypeCode.DateTime:
                    visitor.VisitScalar(value);
                    break;

                case TypeCode.DBNull:
                    visitor.VisitScalar(new ObjectDescriptor(null, typeof(object), typeof(object)));
                    break;

                case TypeCode.Empty:
                    throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode));

                default:
                    if (value.Value == null || value.Type == typeof(TimeSpan))
                    {
                        visitor.VisitScalar(value);
                        break;
                    }

                    var underlyingType = Nullable.GetUnderlyingType(value.Type);
                    if (underlyingType != null)
                    {
                        // This is a nullable type, recursively handle it with its underlying type.
                        // Note that if it contains null, the condition above already took care of it
                        Traverse(new ObjectDescriptor(value.Value, underlyingType, value.Type, value.ScalarStyle), visitor, currentDepth);
                    }
                    else
                    {
                        TraverseObject(value, visitor, currentDepth);
                    }
                    break;
            }
        }
示例#45
0
 public override object Accept(IObjectGraphVisitor visitor)
 {
     return visitor.Visit(this);
 }