This commit is contained in:
Virgile Bello 2015-07-09 00:51:38 +09:00
Родитель ceaa7f9d52
Коммит 4932314103
9 изменённых файлов: 101 добавлений и 24 удалений

Просмотреть файл

@ -0,0 +1,12 @@
using System;
namespace SharpYaml.Serialization.Logging
{
/// <summary>
/// Logger interface.
/// </summary>
public interface ILogger
{
void Log(LogLevel level, Exception ex, string message);
}
}

Просмотреть файл

@ -0,0 +1,11 @@
namespace SharpYaml.Serialization.Logging
{
/// <summary>
/// Severity log level.
/// </summary>
public enum LogLevel
{
Error = 0,
Warning = 1,
}
}

Просмотреть файл

@ -110,11 +110,12 @@ namespace SharpYaml.Serialization
/// </summary>
/// <param name="graph">The graph.</param>
/// <param name="expectedType">The expected type.</param>
/// <param name="contextSettings">The context settings.</param>
/// <returns>A YAML string of the object.</returns>
public string Serialize(object graph, Type expectedType)
public string Serialize(object graph, Type expectedType, SerializerContextSettings contextSettings = null)
{
var stringWriter = new StringWriter();
Serialize(stringWriter, graph, expectedType);
Serialize(stringWriter, graph, expectedType, contextSettings);
return stringWriter.ToString();
}
@ -133,12 +134,13 @@ namespace SharpYaml.Serialization
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="graph">The object to serialize.</param>
public void Serialize(Stream stream, object graph, Type expectedType)
/// <param name="contextSettings">The context settings.</param>
public void Serialize(Stream stream, object graph, Type expectedType, SerializerContextSettings contextSettings = null)
{
var writer = new StreamWriter(stream);
try
{
Serialize(writer, graph, expectedType);
Serialize(writer, graph, expectedType, contextSettings);
}
finally
{
@ -168,9 +170,10 @@ namespace SharpYaml.Serialization
/// <param name="writer">The <see cref="TextWriter" /> where to serialize the object.</param>
/// <param name="graph">The object to serialize.</param>
/// <param name="type">The static type of the object to serialize.</param>
public void Serialize(TextWriter writer, object graph, Type type)
/// <param name="contextSettings">The context settings.</param>
public void Serialize(TextWriter writer, object graph, Type type, SerializerContextSettings contextSettings = null)
{
Serialize(new Emitter(writer, Settings.PreferredIndent), graph, type);
Serialize(new Emitter(writer, Settings.PreferredIndent), graph, type, contextSettings);
}
/// <summary>
@ -189,7 +192,8 @@ namespace SharpYaml.Serialization
/// <param name="emitter">The <see cref="IEmitter" /> where to serialize the object.</param>
/// <param name="graph">The object to serialize.</param>
/// <param name="type">The static type of the object to serialize.</param>
public void Serialize(IEmitter emitter, object graph, Type type)
/// <param name="contextSettings">The context settings.</param>
public void Serialize(IEmitter emitter, object graph, Type type, SerializerContextSettings contextSettings = null)
{
if (emitter == null)
{
@ -210,7 +214,7 @@ namespace SharpYaml.Serialization
defaultEmitter.ForceIndentLess = settings.IndentLess;
}
var context = new SerializerContext(this) {Emitter = emitter, Writer = CreateEmitter(emitter)};
var context = new SerializerContext(this, contextSettings) { Emitter = emitter, Writer = CreateEmitter(emitter) };
// Serialize the document
context.Writer.StreamStart();
@ -245,13 +249,14 @@ namespace SharpYaml.Serialization
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="expectedType">The expected type.</param>
/// <param name="contextSettings">The context settings.</param>
/// <returns>A deserialized object.</returns>
/// <exception cref="System.ArgumentNullException">stream</exception>
public object Deserialize(Stream stream, Type expectedType)
public object Deserialize(Stream stream, Type expectedType, SerializerContextSettings contextSettings = null)
{
if (stream == null) throw new ArgumentNullException("stream");
return Deserialize(new StreamReader(stream), expectedType);
return Deserialize(new StreamReader(stream), expectedType, contextSettings);
}
/// <summary>
@ -271,12 +276,13 @@ namespace SharpYaml.Serialization
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="expectedType">The expected type.</param>
/// <param name="contextSettings">The context settings.</param>
/// <returns>A deserialized object.</returns>
/// <exception cref="System.ArgumentNullException">reader</exception>
public object Deserialize(TextReader reader, Type expectedType)
public object Deserialize(TextReader reader, Type expectedType, SerializerContextSettings contextSettings = null)
{
if (reader == null) throw new ArgumentNullException("reader");
return Deserialize(new EventReader(new Parser(reader)), expectedType);
return Deserialize(new EventReader(new Parser(reader)), expectedType, contextSettings);
}
/// <summary>
@ -343,11 +349,12 @@ namespace SharpYaml.Serialization
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="expectedType">The expected type.</param>
/// <param name="contextSettings">The context settings.</param>
/// <returns>A deserialized object.</returns>
/// <exception cref="System.ArgumentNullException">reader</exception>
public object Deserialize(EventReader reader, Type expectedType)
public object Deserialize(EventReader reader, Type expectedType, SerializerContextSettings contextSettings = null)
{
return Deserialize(reader, null, expectedType);
return Deserialize(reader, null, expectedType, contextSettings);
}
/// <summary>
@ -356,9 +363,10 @@ namespace SharpYaml.Serialization
/// <param name="reader">The reader.</param>
/// <param name="value">The value.</param>
/// <param name="expectedType">The expected type.</param>
/// <param name="contextSettings">The context settings.</param>
/// <returns>A deserialized object.</returns>
/// <exception cref="System.ArgumentNullException">reader</exception>
public object Deserialize(EventReader reader, object value, Type expectedType)
public object Deserialize(EventReader reader, object value, Type expectedType, SerializerContextSettings contextSettings = null)
{
if (reader == null) throw new ArgumentNullException("reader");
@ -368,7 +376,7 @@ namespace SharpYaml.Serialization
object result = null;
if (!reader.Accept<DocumentEnd>() && !reader.Accept<StreamEnd>())
{
var context = new SerializerContext(this) {Reader = reader};
var context = new SerializerContext(this, contextSettings) { Reader = reader };
result = context.ReadYaml(value, expectedType);
}

Просмотреть файл

@ -59,13 +59,15 @@ namespace SharpYaml.Serialization
private readonly ITagTypeRegistry tagTypeRegistry;
private readonly ITypeDescriptorFactory typeDescriptorFactory;
private IEmitter emitter;
private readonly SerializerContextSettings contextSettings;
internal int AnchorCount;
/// <summary>
/// Initializes a new instance of the <see cref="SerializerContext"/> class.
/// </summary>
/// <param name="serializer">The serializer.</param>
internal SerializerContext(Serializer serializer)
/// <param name="serializerContextSettings">The serializer context settings.</param>
internal SerializerContext(Serializer serializer, SerializerContextSettings serializerContextSettings)
{
Serializer = serializer;
settings = serializer.Settings;
@ -75,6 +77,7 @@ namespace SharpYaml.Serialization
Schema = Settings.Schema;
ObjectSerializer = serializer.ObjectSerializer;
typeDescriptorFactory = serializer.TypeDescriptorFactory;
contextSettings = serializerContextSettings ?? SerializerContextSettings.Default;
}
/// <summary>
@ -86,6 +89,17 @@ namespace SharpYaml.Serialization
get { return Writer != null; }
}
/// <summary>
/// Gets the context settings.
/// </summary>
/// <value>
/// The context settings.
/// </value>
public SerializerContextSettings ContextSettings
{
get { return contextSettings; }
}
/// <summary>
/// Gets the settings.
/// </summary>

Просмотреть файл

@ -0,0 +1,20 @@
using SharpYaml.Serialization.Logging;
namespace SharpYaml.Serialization
{
/// <summary>
/// Some parameters that can be transmitted from caller
/// </summary>
public class SerializerContextSettings
{
public static readonly SerializerContextSettings Default = new SerializerContextSettings();
/// <summary>
/// Gets the logger.
/// </summary>
/// <value>
/// The logger.
/// </value>
public ILogger Logger { get; set; }
}
}

Просмотреть файл

@ -47,6 +47,7 @@ using System.Collections;
using System.Collections.Generic;
using SharpYaml.Events;
using SharpYaml.Serialization.Descriptors;
using SharpYaml.Serialization.Logging;
namespace SharpYaml.Serialization.Serializers
{
@ -165,9 +166,11 @@ namespace SharpYaml.Serialization.Serializers
{
ReadAddCollectionItem(ref objectContext, elementType, collectionDescriptor, thisObject, index);
}
catch (YamlException)
catch (YamlException ex)
{
// TODO: Warning?
var logger = objectContext.SerializerContext.ContextSettings.Logger;
if (logger != null)
logger.Log(LogLevel.Warning, ex, "Ignored collection item that could not be deserialized");
objectContext.Reader.Skip(currentDepth);
}
}

Просмотреть файл

@ -47,6 +47,7 @@ using System.Collections.Generic;
using System.Linq;
using SharpYaml.Events;
using SharpYaml.Serialization.Descriptors;
using SharpYaml.Serialization.Logging;
using SharpYaml.Tokens;
using Scalar = SharpYaml.Events.Scalar;
@ -140,10 +141,12 @@ namespace SharpYaml.Serialization.Serializers
var keyValue = ReadDictionaryItem(ref objectContext, new KeyValuePair<Type, Type>(dictionaryDescriptor.KeyType, dictionaryDescriptor.ValueType));
dictionaryDescriptor.AddToDictionary(objectContext.Instance, keyValue.Key, keyValue.Value);
}
catch (YamlException)
catch (YamlException ex)
{
// TODO: Warning?
objectContext.Reader.Skip(currentDepth);
var logger = objectContext.SerializerContext.ContextSettings.Logger;
if (logger != null)
logger.Log(LogLevel.Warning, ex, "Ignored dictionary item that could not be deserialized");
objectContext.Reader.Skip(currentDepth);
}
}
else

Просмотреть файл

@ -45,6 +45,7 @@
using System;
using SharpYaml.Events;
using SharpYaml.Serialization.Logging;
namespace SharpYaml.Serialization.Serializers
{
@ -195,9 +196,11 @@ namespace SharpYaml.Serialization.Serializers
{
ReadMemberCore(ref objectContext);
}
catch (YamlException)
catch (YamlException ex)
{
// TODO: Warning?
var logger = objectContext.SerializerContext.ContextSettings.Logger;
if (logger != null)
logger.Log(LogLevel.Warning, ex, "Ignored object member that could not be deserialized");
objectContext.Reader.Skip(currentDepth);
}
}

Просмотреть файл

@ -59,10 +59,13 @@
<Compile Include="MemoryParser.cs" />
<Compile Include="Serialization\AnchorEventEmitter.cs" />
<Compile Include="Serialization\Descriptors\DefaultKeyComparer.cs" />
<Compile Include="Serialization\Logging\ILogger.cs" />
<Compile Include="Serialization\IOrderedDictionary.cs" />
<Compile Include="Serialization\ITagTypeResolver.cs" />
<Compile Include="Serialization\Logging\LogLevel.cs" />
<Compile Include="Serialization\ObjectContext.cs" />
<Compile Include="Serialization\OrderedDictionary.cs" />
<Compile Include="Serialization\SerializerContextSettings.cs" />
<Compile Include="Serialization\Serializers\DefaultObjectSerializerBackend.cs" />
<Compile Include="Serialization\IObjectSerializerBackend.cs" />
<Compile Include="YamlStyle.cs" />