Skip to content

Commit

Permalink
Merge 89fcd44 into 1613584
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih authored Dec 18, 2023
2 parents 1613584 + 89fcd44 commit ffab603
Show file tree
Hide file tree
Showing 20 changed files with 36 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static T ResolveContent<T>(T item, Func<T, T> itemBuilder) where T : IOv
{
YamlUtility.Serialize(sw, item);
using var sr = new StringReader(sw.ToString());
var serializer = new YamlDeserializer(ignoreUnmatched: true);
var serializer = new YamlDeserializer();
var placeholderValueDeserializer = new PlaceholderValueDeserializer(serializer.ValueDeserializer, item.Conceptual);
item = serializer.Deserialize<T>(sr, placeholderValueDeserializer);
if (placeholderValueDeserializer.ContainPlaceholder)
Expand Down
4 changes: 2 additions & 2 deletions src/Docfx.Common/YamlUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace Docfx.Common;

public static class YamlUtility
{
private static readonly ThreadLocal<YamlSerializer> serializer = new(() => new YamlSerializer(SerializationOptions.DisableAliases));
private static readonly ThreadLocal<YamlDeserializer> deserializer = new(() => new YamlDeserializer(ignoreUnmatched: true));
private static readonly ThreadLocal<YamlSerializer> serializer = new(() => new YamlSerializer());
private static readonly ThreadLocal<YamlDeserializer> deserializer = new(() => new YamlDeserializer());

public static void Serialize(TextWriter writer, object graph)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

namespace Docfx.YamlSerialization.NodeDeserializers;

public class EmitArrayNodeDeserializer : INodeDeserializer
class EmitArrayNodeDeserializer : INodeDeserializer
{
private static readonly MethodInfo DeserializeHelperMethod =
typeof(EmitArrayNodeDeserializer).GetMethod(nameof(DeserializeHelper));
private static readonly ConcurrentDictionary<Type, Func<IParser, Type, Func<IParser, Type, object>, object>> _funcCache =
new();
private static readonly MethodInfo DeserializeHelperMethod = typeof(EmitArrayNodeDeserializer).GetMethod(nameof(DeserializeHelper));
private static readonly ConcurrentDictionary<Type, Func<IParser, Type, Func<IParser, Type, object>, object>> _funcCache = new();

bool INodeDeserializer.Deserialize(IParser reader, Type expectedType, Func<IParser, Type, object> nestedObjectDeserializer, out object value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Docfx.YamlSerialization.NodeDeserializers;

public class EmitGenericCollectionNodeDeserializer : INodeDeserializer
class EmitGenericCollectionNodeDeserializer : INodeDeserializer
{
private static readonly MethodInfo DeserializeHelperMethod =
typeof(EmitGenericCollectionNodeDeserializer).GetMethod(nameof(DeserializeHelper));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Docfx.YamlSerialization.NodeDeserializers;

public class EmitGenericDictionaryNodeDeserializer : INodeDeserializer
class EmitGenericDictionaryNodeDeserializer : INodeDeserializer
{
private static readonly MethodInfo DeserializeHelperMethod =
typeof(EmitGenericDictionaryNodeDeserializer).GetMethod(nameof(DeserializeHelper));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Docfx.YamlSerialization.NodeDeserializers;

public sealed class ExtensibleObjectNodeDeserializer : INodeDeserializer
class ExtensibleObjectNodeDeserializer : INodeDeserializer
{
private readonly IObjectFactory _objectFactory;
private readonly ITypeInspector _typeDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Docfx.YamlSerialization.ObjectDescriptors;

public class BetterObjectDescriptor : IObjectDescriptor
class BetterObjectDescriptor : IObjectDescriptor
{
public BetterObjectDescriptor(object value, Type type, Type staticType)
: this(value, type, staticType, ScalarStyle.Any)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Docfx.YamlSerialization.ObjectFactories;

public class DefaultEmitObjectFactory : ObjectFactoryBase
class DefaultEmitObjectFactory : ObjectFactoryBase
{
private readonly Dictionary<Type, Func<object>> _cache = new();
private static Type[] EmptyTypes => Type.EmptyTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Docfx.YamlSerialization.ObjectGraphTraversalStrategies;
/// An implementation of <see cref="IObjectGraphTraversalStrategy"/> that traverses
/// readable properties, collections and dictionaries.
/// </summary>
public class FullObjectGraphTraversalStrategy : IObjectGraphTraversalStrategy
class FullObjectGraphTraversalStrategy : IObjectGraphTraversalStrategy
{
private static MethodInfo TraverseGenericDictionaryHelperMethod { get; } =
typeof(FullObjectGraphTraversalStrategy).GetMethod(nameof(TraverseGenericDictionaryHelper));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Docfx.YamlSerialization.ObjectGraphTraversalStrategies;
/// properties that are read/write, collections and dictionaries, while ensuring that
/// the graph can be regenerated from the resulting document.
/// </summary>
public class RoundtripObjectGraphTraversalStrategy : FullObjectGraphTraversalStrategy
class RoundtripObjectGraphTraversalStrategy : FullObjectGraphTraversalStrategy
{
public RoundtripObjectGraphTraversalStrategy(YamlSerializer serializer, ITypeInspector typeDescriptor, ITypeResolver typeResolver, int maxRecursion)
: base(serializer, typeDescriptor, typeResolver, maxRecursion, null)
Expand Down
2 changes: 1 addition & 1 deletion src/Docfx.YamlSerialization/SerializationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Docfx.YamlSerialization;

/// <summary>Options that control the serialization process.</summary>
[Flags]
public enum SerializationOptions
enum SerializationOptions
{
/// <summary>Serializes using the default options</summary>
None = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Docfx.YamlSerialization.TypeInspectors;

public class EmitTypeInspector : ExtensibleTypeInspectorSkeleton
class EmitTypeInspector : ExtensibleTypeInspectorSkeleton
{
private static readonly ConcurrentDictionary<Type, CachingItem> _cache = new();
private static readonly ConcurrentDictionary<Type, List<IPropertyDescriptor>> _propertyDescriptorCache = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Docfx.YamlSerialization.TypeInspectors;

public sealed class ExtensibleNamingConventionTypeInspector : ExtensibleTypeInspectorSkeleton
class ExtensibleNamingConventionTypeInspector : ExtensibleTypeInspectorSkeleton
{
private readonly IExtensibleTypeInspector innerTypeDescriptor;
private readonly INamingConvention namingConvention;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Docfx.YamlSerialization.TypeInspectors;

public sealed class ExtensibleReadableAndWritablePropertiesTypeInspector : ExtensibleTypeInspectorSkeleton
class ExtensibleReadableAndWritablePropertiesTypeInspector : ExtensibleTypeInspectorSkeleton
{
private readonly IExtensibleTypeInspector _innerTypeDescriptor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Docfx.YamlSerialization.TypeInspectors;

public abstract class ExtensibleTypeInspectorSkeleton : ITypeInspector, IExtensibleTypeInspector
abstract class ExtensibleTypeInspectorSkeleton : ITypeInspector, IExtensibleTypeInspector
{
public abstract IEnumerable<IPropertyDescriptor> GetProperties(Type type, object container);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Docfx.YamlSerialization.TypeInspectors;
/// <summary>
/// Applies the <see cref="YamlMemberAttribute"/> to another <see cref="ITypeInspector"/>.
/// </summary>
public sealed class ExtensibleYamlAttributesTypeInspector : ExtensibleTypeInspectorSkeleton
sealed class ExtensibleYamlAttributesTypeInspector : ExtensibleTypeInspectorSkeleton
{
private readonly IExtensibleTypeInspector innerTypeDescriptor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Docfx.YamlSerialization.TypeInspectors;

public interface IExtensibleTypeInspector : ITypeInspector
interface IExtensibleTypeInspector : ITypeInspector
{
IPropertyDescriptor GetProperty(Type type, object container, string name);
}
54 changes: 9 additions & 45 deletions src/Docfx.YamlSerialization/YamlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public sealed class YamlDeserializer
private readonly IValueDeserializer _valueDeserializer;
private readonly ITypeConverter _reflectionTypeConverter = new ReflectionTypeConverter();

public IList<INodeDeserializer> NodeDeserializers { get; private set; }
public IList<INodeTypeResolver> TypeResolvers { get; private set; }
internal IList<INodeDeserializer> NodeDeserializers { get; private set; }
internal IList<INodeTypeResolver> TypeResolvers { get; private set; }
public IValueDeserializer ValueDeserializer => _valueDeserializer;

private sealed class TypeDescriptorProxy : ITypeInspector
Expand All @@ -58,14 +58,10 @@ public IPropertyDescriptor GetProperty(Type type, object container, string name,
}
}

public YamlDeserializer(
IObjectFactory objectFactory = null,
INamingConvention namingConvention = null,
bool ignoreUnmatched = false,
bool ignoreNotFoundAnchor = true)
public YamlDeserializer()
{
objectFactory ??= new DefaultEmitObjectFactory();
namingConvention ??= NullNamingConvention.Instance;
var objectFactory = new DefaultEmitObjectFactory();
var namingConvention = NullNamingConvention.Instance;

_typeDescriptor.TypeDescriptor =
new ExtensibleYamlAttributesTypeInspector(
Expand Down Expand Up @@ -96,7 +92,7 @@ public YamlDeserializer(
new EmitGenericCollectionNodeDeserializer(objectFactory),
new CollectionNodeDeserializer(objectFactory),
new EnumerableNodeDeserializer(),
new ExtensibleObjectNodeDeserializer(objectFactory, _typeDescriptor, ignoreUnmatched)
new ExtensibleObjectNodeDeserializer(objectFactory, _typeDescriptor, ignoreUnmatched: true)
};
_tagMappings = new Dictionary<TagName, Type>(PredefinedTagMappings);
TypeResolvers = new List<INodeTypeResolver>
Expand All @@ -107,58 +103,26 @@ public YamlDeserializer(
};

NodeValueDeserializer nodeValueDeserializer = new(NodeDeserializers, TypeResolvers, _reflectionTypeConverter);
if (ignoreNotFoundAnchor)
{
_valueDeserializer = new LooseAliasValueDeserializer(nodeValueDeserializer);
}
else
{
_valueDeserializer = new AliasValueDeserializer(nodeValueDeserializer);
}
}

public void RegisterTagMapping(string tag, Type type)
{
_tagMappings.Add(tag, type);
}

public void RegisterTypeConverter(IYamlTypeConverter typeConverter)
{
_converters.Add(typeConverter);
_valueDeserializer = new LooseAliasValueDeserializer(nodeValueDeserializer);
}

public T Deserialize<T>(TextReader input, IValueDeserializer deserializer = null)
{
return (T)Deserialize(input, typeof(T), deserializer);
}

public object Deserialize(TextReader input, IValueDeserializer deserializer = null)
{
return Deserialize(input, typeof(object), deserializer);
}

public object Deserialize(TextReader input, Type type, IValueDeserializer deserializer = null)
private object Deserialize(TextReader input, Type type, IValueDeserializer deserializer = null)
{
return Deserialize(new Parser(input), type, deserializer);
}

public T Deserialize<T>(IParser reader, IValueDeserializer deserializer = null)
{
return (T)Deserialize(reader, typeof(T), deserializer);
}

public object Deserialize(IParser reader, IValueDeserializer deserializer = null)
{
return Deserialize(reader, typeof(object), deserializer);
}

/// <summary>
/// Deserializes an object of the specified type.
/// </summary>
/// <param name="parser">The <see cref="IParser" /> where to deserialize the object.</param>
/// <param name="type">The static type of the object to deserialize.</param>
/// <returns>Returns the deserialized object.</returns>
public object Deserialize(IParser parser, Type type, IValueDeserializer deserializer = null)
private object Deserialize(IParser parser, Type type, IValueDeserializer deserializer = null)
{
ArgumentNullException.ThrowIfNull(parser);
ArgumentNullException.ThrowIfNull(type);
Expand Down
20 changes: 5 additions & 15 deletions src/Docfx.YamlSerialization/YamlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ namespace Docfx.YamlSerialization;
public class YamlSerializer
{
internal IList<IYamlTypeConverter> Converters { get; set; }
private readonly SerializationOptions _options;
private readonly INamingConvention _namingConvention;
private readonly SerializationOptions _options = SerializationOptions.DisableAliases;
private readonly INamingConvention _namingConvention = NullNamingConvention.Instance;
private readonly ITypeResolver _typeResolver;

public YamlSerializer(SerializationOptions options = SerializationOptions.None, INamingConvention namingConvention = null)
public YamlSerializer()
{
_options = options;
_namingConvention = namingConvention ?? NullNamingConvention.Instance;

Converters = new List<IYamlTypeConverter>();
foreach (IYamlTypeConverter yamlTypeConverter in YamlTypeConverters.BuiltInConverters)
{
Expand All @@ -47,14 +44,7 @@ private bool IsOptionSet(SerializationOptions option)

public void Serialize(TextWriter writer, object graph)
{
Serialize(new Emitter(writer), graph);
}

public void Serialize(IEmitter emitter, object graph)
{
ArgumentNullException.ThrowIfNull(emitter);

EmitDocument(emitter, new BetterObjectDescriptor(graph, graph != null ? graph.GetType() : typeof(object), typeof(object)));
EmitDocument(new Emitter(writer), new BetterObjectDescriptor(graph, graph != null ? graph.GetType() : typeof(object), typeof(object)));
}

private void EmitDocument(IEmitter emitter, IObjectDescriptor graph)
Expand Down Expand Up @@ -96,7 +86,7 @@ private IObjectGraphVisitor<IEmitter> CreateEmittingVisitor(IEmitter emitter, IO
return emittingVisitor;
}

public void SerializeValue(IEmitter emitter, object value, Type type)
private void SerializeValue(IEmitter emitter, object value, Type type)
{
var graph = type != null
? new BetterObjectDescriptor(value, type, type)
Expand Down
Loading

0 comments on commit ffab603

Please sign in to comment.