Skip to content

Commit

Permalink
Fix issue #26: Use the actual type of the objects instead of the stat…
Browse files Browse the repository at this point in the history
…ically detected one.
  • Loading branch information
aaubry committed Aug 29, 2013
1 parent 2383de4 commit 743527c
Show file tree
Hide file tree
Showing 35 changed files with 643 additions and 417 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ TestResults
*.user
*.userprefs
*.pidb
*.bak
Help/*
release/*
/_ReSharper.*
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2008, 2009, 2010, 2011, 2012 Antoine Aubry
Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Antoine Aubry

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
8 changes: 4 additions & 4 deletions YamlDotNet.Core/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private bool IsUnicode
private const int MaxBestIndent = 9;

/// <summary>
/// Initializes a new instance of the <see cref="IEmitter"/> class.
/// Initializes a new instance of the <see cref="Emitter"/> class.
/// </summary>
/// <param name="output">The <see cref="TextWriter"/> where the emitter will write.</param>
public Emitter(TextWriter output)
Expand All @@ -117,7 +117,7 @@ public Emitter(TextWriter output)
}

/// <summary>
/// Initializes a new instance of the <see cref="IEmitter"/> class.
/// Initializes a new instance of the <see cref="Emitter"/> class.
/// </summary>
/// <param name="output">The <see cref="TextWriter"/> where the emitter will write.</param>
/// <param name="bestIndent">The preferred indentation.</param>
Expand All @@ -127,7 +127,7 @@ public Emitter(TextWriter output, int bestIndent)
}

/// <summary>
/// Initializes a new instance of the <see cref="IEmitter"/> class.
/// Initializes a new instance of the <see cref="Emitter"/> class.
/// </summary>
/// <param name="output">The <see cref="TextWriter"/> where the emitter will write.</param>
/// <param name="bestIndent">The preferred indentation.</param>
Expand All @@ -139,7 +139,7 @@ public Emitter(TextWriter output, int bestIndent, int bestWidth)


/// <summary>
/// Initializes a new instance of the <see cref="IEmitter"/> class.
/// Initializes a new instance of the <see cref="Emitter"/> class.
/// </summary>
/// <param name="output">The <see cref="TextWriter"/> where the emitter will write.</param>
/// <param name="bestIndent">The preferred indentation.</param>
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet.Core/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private Token GetCurrentToken()
}

/// <summary>
/// Initializes a new instance of the <see cref="IParser"/> class.
/// Initializes a new instance of the <see cref="Parser"/> class.
/// </summary>
/// <param name="input">The input where the YAML stream is to be read.</param>
public Parser(TextReader input)
Expand Down
20 changes: 10 additions & 10 deletions YamlDotNet.RepresentationModel/Serialization/AnchorAssigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ private class AnchorAssignment
private readonly IDictionary<object, AnchorAssignment> assignments = new Dictionary<object, AnchorAssignment>();
private uint nextId;

bool IObjectGraphVisitor.Enter(object value, Type type)
bool IObjectGraphVisitor.Enter(IObjectDescriptor value)
{
// Do not assign anchors to basic types
if (value == null || Type.GetTypeCode(value.GetType()) != TypeCode.Object)
if (value.Value == null || Type.GetTypeCode(value.Type) != TypeCode.Object)
{
return false;
}

AnchorAssignment assignment;
if (assignments.TryGetValue(value, out assignment))
if (assignments.TryGetValue(value.Value, out assignment))
{
if (assignment.Anchor == null)
{
Expand All @@ -34,12 +34,12 @@ bool IObjectGraphVisitor.Enter(object value, Type type)
}
else
{
assignments.Add(value, new AnchorAssignment());
assignments.Add(value.Value, new AnchorAssignment());
return true;
}
}

bool IObjectGraphVisitor.EnterMapping(object key, Type keyType, object value, Type valueType)
bool IObjectGraphVisitor.EnterMapping(IObjectDescriptor key, IObjectDescriptor value)
{
return true;
}
Expand All @@ -49,11 +49,11 @@ bool IObjectGraphVisitor.EnterMapping(IPropertyDescriptor key, object value)
return true;
}

void IObjectGraphVisitor.VisitScalar(object scalar, Type scalarType) { }
void IObjectGraphVisitor.VisitMappingStart(object mapping, Type mappingType, Type keyType, Type valueType) { }
void IObjectGraphVisitor.VisitMappingEnd(object mapping, Type mappingType) { }
void IObjectGraphVisitor.VisitSequenceStart(object sequence, Type sequenceType, Type elementType) { }
void IObjectGraphVisitor.VisitSequenceEnd(object sequence, Type sequenceType) { }
void IObjectGraphVisitor.VisitScalar(IObjectDescriptor scalar) { }
void IObjectGraphVisitor.VisitMappingStart(IObjectDescriptor mapping, Type keyType, Type valueType) { }
void IObjectGraphVisitor.VisitMappingEnd(IObjectDescriptor mapping) { }
void IObjectGraphVisitor.VisitSequenceStart(IObjectDescriptor sequence, Type elementType) { }
void IObjectGraphVisitor.VisitSequenceEnd(IObjectDescriptor sequence) { }

string IAliasProvider.GetAlias(object target)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ public AnchorAssigningObjectGraphVisitor(IObjectGraphVisitor nextVisitor, IEvent
this.aliasProvider = aliasProvider;
}

public override bool Enter(object value, Type type)
public override bool Enter(IObjectDescriptor value)
{
var alias = aliasProvider.GetAlias(value);
var alias = aliasProvider.GetAlias(value.Value);
if (alias != null && !emittedAliases.Add(alias))
{
eventEmitter.Emit(new AliasEventInfo(value, type) { Alias = alias });
eventEmitter.Emit(new AliasEventInfo(value) { Alias = alias });
return false;
}

return base.Enter(value, type);
return base.Enter(value);
}

public override void VisitMappingStart(object mapping, Type mappingType, Type keyType, Type valueType)
public override void VisitMappingStart(IObjectDescriptor mapping, Type keyType, Type valueType)
{
eventEmitter.Emit(new MappingStartEventInfo(mapping, mappingType) { Anchor = aliasProvider.GetAlias(mapping) });
eventEmitter.Emit(new MappingStartEventInfo(mapping) { Anchor = aliasProvider.GetAlias(mapping.Value) });
}

public override void VisitSequenceStart(object sequence, Type sequenceType, Type elementType)
public override void VisitSequenceStart(IObjectDescriptor sequence, Type elementType)
{
eventEmitter.Emit(new SequenceStartEventInfo(sequence, sequenceType) { Anchor = aliasProvider.GetAlias(sequence) });
eventEmitter.Emit(new SequenceStartEventInfo(sequence) { Anchor = aliasProvider.GetAlias(sequence.Value) });
}

public override void VisitScalar(object scalar, Type scalarType)
public override void VisitScalar(IObjectDescriptor scalar)
{
eventEmitter.Emit(new ScalarEventInfo(scalar, scalarType) { Anchor = aliasProvider.GetAlias(scalar) });
eventEmitter.Emit(new ScalarEventInfo(scalar) { Anchor = aliasProvider.GetAlias(scalar.Value) });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,44 @@ protected ChainedObjectGraphVisitor(IObjectGraphVisitor nextVisitor)
this.nextVisitor = nextVisitor;
}

public virtual bool Enter(object value, Type type)
public virtual bool Enter(IObjectDescriptor value)
{
return nextVisitor.Enter(value, type);
return nextVisitor.Enter(value);
}

public virtual bool EnterMapping(object key, Type keyType, object value, Type valueType)
public virtual bool EnterMapping(IObjectDescriptor key, IObjectDescriptor value)
{
return nextVisitor.EnterMapping(key, keyType, value, valueType);
return nextVisitor.EnterMapping(key, value);
}

public virtual bool EnterMapping(IPropertyDescriptor key, object value)
{
return nextVisitor.EnterMapping(key, value);
}

public virtual void VisitScalar(object scalar, Type scalarType)
public virtual void VisitScalar(IObjectDescriptor scalar)
{
nextVisitor.VisitScalar(scalar, scalarType);
nextVisitor.VisitScalar(scalar);
}

public virtual void VisitMappingStart(object mapping, Type mappingType, Type keyType, Type valueType)
public virtual void VisitMappingStart(IObjectDescriptor mapping, Type keyType, Type valueType)
{
nextVisitor.VisitMappingStart(mapping, mappingType, keyType, valueType);
nextVisitor.VisitMappingStart(mapping, keyType, valueType);
}

public virtual void VisitMappingEnd(object mapping, Type mappingType)
public virtual void VisitMappingEnd(IObjectDescriptor mapping)
{
nextVisitor.VisitMappingEnd(mapping, mappingType);
nextVisitor.VisitMappingEnd(mapping);
}

public virtual void VisitSequenceStart(object sequence, Type sequenceType, Type elementType)
public virtual void VisitSequenceStart(IObjectDescriptor sequence, Type elementType)
{
nextVisitor.VisitSequenceStart(sequence, sequenceType, elementType);
nextVisitor.VisitSequenceStart(sequence, elementType);
}

public virtual void VisitSequenceEnd(object sequence, Type sequenceType)
public virtual void VisitSequenceEnd(IObjectDescriptor sequence)
{
nextVisitor.VisitSequenceEnd(sequence, sequenceType);
nextVisitor.VisitSequenceEnd(sequence);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public CustomSerializationObjectGraphVisitor(IEmitter emitter, IObjectGraphVisit
: Enumerable.Empty<IYamlTypeConverter>();
}

public override bool Enter(object value, Type type)
public override bool Enter(IObjectDescriptor value)
{
var typeConverter = typeConverters.FirstOrDefault(t => t.Accepts(type));
var typeConverter = typeConverters.FirstOrDefault(t => t.Accepts(value.Type));
if (typeConverter != null)
{
typeConverter.WriteYaml(emitter, value, type);
typeConverter.WriteYaml(emitter, value.Value, value.Type);
return false;
}

Expand All @@ -35,7 +35,7 @@ public override bool Enter(object value, Type type)
return false;
}

return base.Enter(value, type);
return base.Enter(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,18 @@ private static object GetDefault(Type type)

private static readonly IEqualityComparer<object> _objectComparer = EqualityComparer<object>.Default;

public override bool Enter(object value, Type type)
public override bool EnterMapping(IObjectDescriptor key, IObjectDescriptor value)
{
return base.Enter(value, type);
}

public override bool EnterMapping(object key, Type keyType, object value, Type valueType)
{
return !_objectComparer.Equals(value, GetDefault(valueType))
&& base.EnterMapping(key, keyType, value, valueType);
return !_objectComparer.Equals(value, GetDefault(value.Type))
&& base.EnterMapping(key, value);
}

public override bool EnterMapping(IPropertyDescriptor key, object value)
{
var defaultValueAttribute = (DefaultValueAttribute)key.Property.GetCustomAttributes(typeof(DefaultValueAttribute), true).FirstOrDefault();
var defaultValueAttribute = key.GetCustomAttribute<DefaultValueAttribute>();
var defaultValue = defaultValueAttribute != null
? defaultValueAttribute.Value
: GetDefault(key.Property.PropertyType);
: GetDefault(key.Type);

return !_objectComparer.Equals(value, defaultValue)
&& base.EnterMapping(key, value);
Expand Down
16 changes: 10 additions & 6 deletions YamlDotNet.RepresentationModel/Serialization/Deserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public sealed class Deserializer
private class TypeDescriptorProxy : ITypeDescriptor
{
public ITypeDescriptor TypeDescriptor;
public IEnumerable<IPropertyDescriptor> GetProperties(Type type)

public IEnumerable<IPropertyDescriptor> GetProperties(Type type, object container)
{
return TypeDescriptor.GetProperties(type);
return TypeDescriptor.GetProperties(type, container);
}

public IPropertyDescriptor GetProperty(Type type, string name)
public IPropertyDescriptor GetProperty(Type type, object container, string name)
{
return TypeDescriptor.GetProperty(type, name);
return TypeDescriptor.GetProperty(type, container, name);
}
}

Expand All @@ -76,7 +76,11 @@ public Deserializer(IObjectFactory objectFactory = null, INamingConvention namin
typeDescriptor.TypeDescriptor =
new YamlAttributesTypeDescriptor(
new NamingConventionTypeDescriptor(
new ReadableAndWritablePropertiesTypeDescriptor(),
new ReadableAndWritablePropertiesTypeDescriptor(
new ReadablePropertiesTypeDescriptor(
new StaticTypeResolver()
)
),
namingConvention
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace YamlDotNet.RepresentationModel.Serialization
{
/// <summary>
/// The type returned will be the actual type of the value, if available.
/// </summary>
public sealed class DynamicTypeResolver : ITypeResolver
{
public Type Resolve(Type staticType, object actualValue)
{
return actualValue != null ? actualValue.GetType() : staticType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public EmittingObjectGraphVisitor(IEventEmitter eventEmitter)
this.eventEmitter = eventEmitter;
}

bool IObjectGraphVisitor.Enter(object value, Type type)
bool IObjectGraphVisitor.Enter(IObjectDescriptor value)
{
return true;
}

bool IObjectGraphVisitor.EnterMapping(object key, Type keyType, object value, Type valueType)
bool IObjectGraphVisitor.EnterMapping(IObjectDescriptor key, IObjectDescriptor value)
{
return true;
}
Expand All @@ -26,29 +26,29 @@ bool IObjectGraphVisitor.EnterMapping(IPropertyDescriptor key, object value)
return true;
}

void IObjectGraphVisitor.VisitScalar(object scalar, Type scalarType)
void IObjectGraphVisitor.VisitScalar(IObjectDescriptor scalar)
{
eventEmitter.Emit(new ScalarEventInfo(scalar, scalarType));
eventEmitter.Emit(new ScalarEventInfo(scalar));
}

void IObjectGraphVisitor.VisitMappingStart(object mapping, Type mappingType, Type type, Type valueType)
void IObjectGraphVisitor.VisitMappingStart(IObjectDescriptor mapping, Type keyType, Type valueType)
{
eventEmitter.Emit(new MappingStartEventInfo(mapping, mappingType));
eventEmitter.Emit(new MappingStartEventInfo(mapping));
}

void IObjectGraphVisitor.VisitMappingEnd(object mapping, Type mappingType)
void IObjectGraphVisitor.VisitMappingEnd(IObjectDescriptor mapping)
{
eventEmitter.Emit(new MappingEndEventInfo(mapping, mappingType));
eventEmitter.Emit(new MappingEndEventInfo(mapping));
}

void IObjectGraphVisitor.VisitSequenceStart(object sequence, Type sequenceType, Type elementType)
void IObjectGraphVisitor.VisitSequenceStart(IObjectDescriptor sequence, Type elementType)
{
eventEmitter.Emit(new SequenceStartEventInfo(sequence, sequenceType));
eventEmitter.Emit(new SequenceStartEventInfo(sequence));
}

void IObjectGraphVisitor.VisitSequenceEnd(object sequence, Type sequenceType)
void IObjectGraphVisitor.VisitSequenceEnd(IObjectDescriptor sequence)
{
eventEmitter.Emit(new SequenceEndEventInfo(sequence, sequenceType));
eventEmitter.Emit(new SequenceEndEventInfo(sequence));
}
}
}

0 comments on commit 743527c

Please sign in to comment.