public override bool Enter(IObjectDescriptor value, IEmitter context) { var typeConverter = typeConverters.FirstOrDefault(t => t.Accepts(value.Type)); if (typeConverter != null) { typeConverter.WriteYaml(context, value.Value, value.Type); return false; } var convertible = value.Value as IYamlConvertible; if (convertible != null) { convertible.Write(context, nestedObjectSerializer); return false; } #pragma warning disable 0618 // IYamlSerializable is obsolete var serializable = value.Value as IYamlSerializable; if (serializable != null) { serializable.WriteYaml(context); return false; } #pragma warning restore return base.Enter(value, context); }
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); }
public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value) { var defaultValueAttribute = key.GetCustomAttribute<DefaultValueAttribute>(); var defaultValue = defaultValueAttribute != null ? defaultValueAttribute.Value : GetDefault(key.Type); return !_objectComparer.Equals(value.Value, defaultValue) && base.EnterMapping(key, value); }
public override bool Enter(IObjectDescriptor value) { var alias = aliasProvider.GetAlias(value.Value); if (alias != null && !emittedAliases.Add(alias)) { eventEmitter.Emit(new AliasEventInfo(value) { Alias = alias }); return false; } return base.Enter(value); }
private void EmitDocument(IEmitter emitter, IObjectDescriptor graph) { var traversalStrategy = CreateTraversalStrategy(); var eventEmitter = CreateEventEmitter(emitter); var emittingVisitor = CreateEmittingVisitor(emitter, traversalStrategy, eventEmitter, graph); emitter.Emit(new StreamStart()); emitter.Emit(new DocumentStart()); traversalStrategy.Traverse(graph, emittingVisitor); emitter.Emit(new DocumentEnd(true)); emitter.Emit(new StreamEnd()); }
protected override bool Enter(IObjectDescriptor value) { AnchorAssignment assignment; if (value.Value != null && assignments.TryGetValue(value.Value, out assignment)) { if (assignment.Anchor == null) { assignment.Anchor = "o" + nextId.ToString(CultureInfo.InvariantCulture); ++nextId; } return false; } return true; }
public override bool Enter(IObjectDescriptor value) { var typeConverter = typeConverters.FirstOrDefault(t => t.Accepts(value.Type)); if (typeConverter != null) { typeConverter.WriteYaml(emitter, value.Value, value.Type); return false; } var serializable = value as IYamlSerializable; if (serializable != null) { serializable.WriteYaml(emitter); return false; } return base.Enter(value); }
private IObjectGraphVisitor CreateEmittingVisitor(IEmitter emitter, IObjectGraphTraversalStrategy traversalStrategy, IEventEmitter eventEmitter, IObjectDescriptor graph) { IObjectGraphVisitor emittingVisitor = new EmittingObjectGraphVisitor(eventEmitter); emittingVisitor = new CustomSerializationObjectGraphVisitor(emitter, emittingVisitor, Converters); if (!IsOptionSet(SerializationOptions.DisableAliases)) { var anchorAssigner = new AnchorAssigner(); traversalStrategy.Traverse(graph, anchorAssigner); emittingVisitor = new AnchorAssigningObjectGraphVisitor(emittingVisitor, eventEmitter, anchorAssigner); } if (!IsOptionSet(SerializationOptions.EmitDefaults)) { emittingVisitor = new DefaultExclusiveObjectGraphVisitor(emittingVisitor); } return emittingVisitor; }
bool IObjectGraphVisitor.Enter(IObjectDescriptor value) { // Do not assign anchors to basic types if (value.Value == null || value.Type.GetTypeCode() != TypeCode.Object) { return false; } AnchorAssignment assignment; if (assignments.TryGetValue(value.Value, out assignment)) { if (assignment.Anchor == null) { assignment.Anchor = "o" + nextId.ToString(CultureInfo.InvariantCulture); ++nextId; } return false; } else { assignments.Add(value.Value, new AnchorAssignment()); return true; } }
public ScalarEventInfo(IObjectDescriptor source) : base(source) { }
protected ObjectEventInfo(IObjectDescriptor source) : base(source) { }
public virtual void VisitMappingStart(IObjectDescriptor mapping, Type keyType, Type valueType) { nextVisitor.VisitMappingStart(mapping, keyType, valueType); }
public virtual void VisitSequenceStart(IObjectDescriptor sequence, Type elementType) { nextVisitor.VisitSequenceStart(sequence, elementType); }
void IObjectGraphVisitor.VisitMappingStart(IObjectDescriptor mapping, Type keyType, Type valueType) { eventEmitter.Emit(new MappingStartEventInfo(mapping)); }
protected abstract bool EnterMapping(IObjectDescriptor key, IObjectDescriptor value);
protected abstract bool Enter(IObjectDescriptor value);
private IObjectGraphVisitor <IEmitter> CreateEmittingVisitor(IEmitter emitter, IObjectGraphTraversalStrategy traversalStrategy, IEventEmitter eventEmitter, IObjectDescriptor graph) { IObjectGraphVisitor <IEmitter> emittingVisitor = new EmittingObjectGraphVisitor(eventEmitter); ObjectSerializer nestedObjectSerializer = (v, t) => SerializeValue(emitter, v, t); emittingVisitor = new CustomSerializationObjectGraphVisitor(emittingVisitor, Converters, nestedObjectSerializer); if (!IsOptionSet(SerializationOptions.DisableAliases)) { var anchorAssigner = new AnchorAssigner(Converters); traversalStrategy.Traverse <Nothing>(graph, anchorAssigner, null); emittingVisitor = new AnchorAssigningObjectGraphVisitor(emittingVisitor, eventEmitter, anchorAssigner); } if (!IsOptionSet(SerializationOptions.EmitDefaults)) { emittingVisitor = new DefaultExclusiveObjectGraphVisitor(emittingVisitor); } return(emittingVisitor); }
protected virtual void Traverse <TContext>(object name, IObjectDescriptor value, IObjectGraphVisitor <TContext> visitor, TContext context, Stack <ObjectPathSegment> path) { if (path.Count >= maxRecursion) { var message = new StringBuilder(); message.AppendLine("Too much recursion when traversing the object graph."); message.AppendLine("The path to reach this recursion was:"); var lines = new Stack <KeyValuePair <string, string> >(path.Count); var maxNameLength = 0; foreach (var segment in path) { var segmentName = TypeConverter.ChangeType <string>(segment.name); maxNameLength = Math.Max(maxNameLength, segmentName.Length); lines.Push(new KeyValuePair <string, string>(segmentName, segment.value.Type.FullName)); } foreach (var line in lines) { message .Append(" -> ") .Append(line.Key.PadRight(maxNameLength)) .Append(" [") .Append(line.Value) .AppendLine("]"); } throw new MaximumRecursionLevelReachedException(message.ToString()); } if (!visitor.Enter(value, context)) { return; } path.Push(new ObjectPathSegment(name, value)); try { 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, context); break; case TypeCode.Empty: throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode)); default: if (value.IsDbNull()) { visitor.VisitScalar(new ObjectDescriptor(null, typeof(object), typeof(object)), context); } if (value.Value == null || value.Type == typeof(TimeSpan)) { visitor.VisitScalar(value, context); 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("Value", new ObjectDescriptor(value.Value, underlyingType, value.Type, value.ScalarStyle), visitor, context, path); } else { TraverseObject(value, visitor, context, path); } break; } } finally { path.Pop(); } }
public ObjectPathSegment(object name, IObjectDescriptor value) { this.name = name; this.value = value; }
void IObjectGraphTraversalStrategy.Traverse <TContext>(IObjectDescriptor graph, IObjectGraphVisitor <TContext> visitor, TContext context) { Traverse("<root>", graph, visitor, context, new Stack <ObjectPathSegment>(maxRecursion)); }
public virtual void VisitSequenceEnd(IObjectDescriptor sequence) { nextVisitor.VisitSequenceEnd(sequence); }
public SequenceStartEventInfo(IObjectDescriptor source) : base(source) { }
bool IObjectGraphVisitor.EnterMapping(IPropertyDescriptor key, IObjectDescriptor value) { return true; }
protected abstract void VisitMappingEnd(IObjectDescriptor mapping);
void IObjectGraphVisitor.VisitSequenceStart(IObjectDescriptor sequence, Type elementType) { eventEmitter.Emit(new SequenceStartEventInfo(sequence)); }
protected abstract void VisitMappingStart(IObjectDescriptor mapping, Type keyType, Type valueType);
protected abstract void VisitScalar(IObjectDescriptor scalar);
protected abstract void VisitSequenceEnd(IObjectDescriptor sequence);
protected abstract void VisitSequenceStart(IObjectDescriptor sequence, Type elementType);
public AliasEventInfo(IObjectDescriptor source, string alias) : base(source) { Alias = alias ?? throw new ArgumentNullException(nameof(alias)); }
bool IObjectGraphVisitor <Nothing> .EnterMapping(IObjectDescriptor key, IObjectDescriptor value, Nothing context) { return(EnterMapping(key, value)); }
public ScalarEventInfo(IObjectDescriptor source) : base(source) { Style = source.ScalarStyle; RenderedValue = string.Empty; }
void IObjectGraphVisitor <Nothing> .VisitMappingEnd(IObjectDescriptor mapping, Nothing context) { VisitMappingEnd(mapping); }
public virtual void VisitScalar(IObjectDescriptor scalar) { nextVisitor.VisitScalar(scalar); }
void IObjectGraphVisitor <Nothing> .VisitMappingStart(IObjectDescriptor mapping, Type keyType, Type valueType, Nothing context) { VisitMappingStart(mapping, keyType, valueType); }
public MappingEndEventInfo(IObjectDescriptor source) : base(source) { }
void IObjectGraphVisitor <Nothing> .VisitScalar(IObjectDescriptor scalar, Nothing context) { VisitScalar(scalar); }
bool IObjectGraphVisitor.Enter(IObjectDescriptor value) { return true; }
void IObjectGraphVisitor <Nothing> .VisitSequenceEnd(IObjectDescriptor sequence, Nothing context) { VisitSequenceEnd(sequence); }
void IObjectGraphVisitor.VisitScalar(IObjectDescriptor scalar) { eventEmitter.Emit(new ScalarEventInfo(scalar)); }
public override void VisitMappingStart(IObjectDescriptor mapping, Type keyType, Type valueType) { eventEmitter.Emit(new MappingStartEventInfo(mapping) { Anchor = aliasProvider.GetAlias(mapping.Value) }); }
void IObjectGraphVisitor.VisitMappingEnd(IObjectDescriptor mapping) { eventEmitter.Emit(new MappingEndEventInfo(mapping)); }
public override void VisitScalar(IObjectDescriptor scalar) { eventEmitter.Emit(new ScalarEventInfo(scalar) { Anchor = aliasProvider.GetAlias(scalar.Value) }); }
void IObjectGraphVisitor.VisitSequenceEnd(IObjectDescriptor sequence) { eventEmitter.Emit(new SequenceEndEventInfo(sequence)); }
public virtual bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value) { return nextVisitor.EnterMapping(key, value); }
public override bool EnterMapping(IObjectDescriptor key, IObjectDescriptor value) { return !_objectComparer.Equals(value, GetDefault(value.Type)) && base.EnterMapping(key, value); }
void IObjectGraphVisitor <Nothing> .VisitSequenceStart(IObjectDescriptor sequence, Type elementType, Nothing context) { VisitSequenceStart(sequence, elementType); }
public virtual void VisitMappingEnd(IObjectDescriptor mapping) { nextVisitor.VisitMappingEnd(mapping); }
public override void VisitSequenceStart(IObjectDescriptor sequence, Type elementType) { eventEmitter.Emit(new SequenceStartEventInfo(sequence) { Anchor = aliasProvider.GetAlias(sequence.Value) }); }
void IObjectGraphTraversalStrategy.Traverse<TContext>(IObjectDescriptor graph, IObjectGraphVisitor<TContext> visitor, TContext context) { Traverse(graph, visitor, 0, context); }
public virtual bool Enter(IObjectDescriptor value) { return nextVisitor.Enter(value); }
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)) { 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, context); break; case TypeCode.Empty: throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode)); default: if (value.IsDbNull()) { visitor.VisitScalar(new ObjectDescriptor(null, typeof(object), typeof(object)), context); } if (value.Value == null || value.Type == typeof(TimeSpan)) { visitor.VisitScalar(value, context); 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, context); } else { TraverseObject(value, visitor, currentDepth, context); } break; } }
public SequenceEndEventInfo(IObjectDescriptor source) : base(source) { }
protected EventInfo(IObjectDescriptor source) { Source = source ?? throw new ArgumentNullException(nameof(source)); }