Serializer now takes SerializationOptions and INamingConvention in the constructor instead of in the Serialize() methods.

This commit is contained in:
Antoine Aubry 2013-05-29 10:13:07 +01:00
Родитель f4182bd8cd
Коммит 7464c7b02f
3 изменённых файлов: 65 добавлений и 62 удалений

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

@ -74,8 +74,19 @@ namespace YamlDotNet.RepresentationModel.Serialization
{
internal IList<IYamlTypeConverter> Converters { get; private set; }
public Serializer()
private readonly SerializationOptions options;
private readonly INamingConvention namingConvention;
/// <summary>
///
/// </summary>
/// <param name="options">Options that control how the serialization is to be performed.</param>
/// <param name="namingConvention">Naming strategy to use for serialized property names</param>
public Serializer(SerializationOptions options = SerializationOptions.None, INamingConvention namingConvention = null)
{
this.options = options;
this.namingConvention = namingConvention ?? new NullNamingConvention();
Converters = new List<IYamlTypeConverter>();
}
@ -92,11 +103,9 @@ namespace YamlDotNet.RepresentationModel.Serialization
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> where to serialize the object.</param>
/// <param name="graph">The object to serialize.</param>
/// <param name="options">Options that control how the serialization is to be performed.</param>
/// <param name="namingConvention">Naming strategy to use for serialized property names</param>
public void Serialize(TextWriter writer, object graph, SerializationOptions options = SerializationOptions.None, INamingConvention namingConvention = null)
public void Serialize(TextWriter writer, object graph)
{
Serialize(new Emitter(writer), graph, options, namingConvention);
Serialize(new Emitter(writer), graph);
}
/// <summary>
@ -105,11 +114,9 @@ namespace YamlDotNet.RepresentationModel.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>
/// <param name="options">Options that control how the serialization is to be performed.</param>
/// <param name="namingConvention">Naming strategy to use for serialized property names</param>
public void Serialize(TextWriter writer, object graph, Type type, SerializationOptions options = SerializationOptions.None, INamingConvention namingConvention = null)
public void Serialize(TextWriter writer, object graph, Type type)
{
Serialize(new Emitter(writer), graph, type, options, namingConvention);
Serialize(new Emitter(writer), graph, type);
}
/// <summary>
@ -117,11 +124,9 @@ namespace YamlDotNet.RepresentationModel.Serialization
/// </summary>
/// <param name="emitter">The <see cref="Emitter" /> where to serialize the object.</param>
/// <param name="graph">The object to serialize.</param>
/// <param name="options">Options that control how the serialization is to be performed.</param>
/// <param name="namingConvention">Naming strategy to use for serialized property names</param>
public void Serialize(Emitter emitter, object graph, SerializationOptions options = SerializationOptions.None, INamingConvention namingConvention = null)
public void Serialize(Emitter emitter, object graph)
{
Serialize(emitter, graph, graph != null ? graph.GetType() : typeof(object), options, namingConvention);
Serialize(emitter, graph, graph != null ? graph.GetType() : typeof(object));
}
/// <summary>
@ -130,9 +135,7 @@ namespace YamlDotNet.RepresentationModel.Serialization
/// <param name="emitter">The <see cref="Emitter" /> 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>
/// <param name="options">Options that control how the serialization is to be performed.</param>
/// <param name="namingConvention">Naming strategy to use for serialized property names</param>
public void Serialize(Emitter emitter, object graph, Type type, SerializationOptions options = SerializationOptions.None, INamingConvention namingConvention = null)
public void Serialize(Emitter emitter, object graph, Type type)
{
if (emitter == null)
{
@ -144,9 +147,9 @@ namespace YamlDotNet.RepresentationModel.Serialization
throw new ArgumentNullException("type");
}
var traversalStrategy = CreateTraversalStrategy(options, namingConvention ?? new NullNamingConvention());
var eventEmitter = CreateEventEmitter(emitter, options);
var emittingVisitor = CreateEmittingVisitor(emitter, options, traversalStrategy, eventEmitter, graph, type);
var traversalStrategy = CreateTraversalStrategy();
var eventEmitter = CreateEventEmitter(emitter);
var emittingVisitor = CreateEmittingVisitor(emitter, traversalStrategy, eventEmitter, graph, type);
EmitDocument(emitter, traversalStrategy, emittingVisitor, graph, type);
}
@ -161,7 +164,7 @@ namespace YamlDotNet.RepresentationModel.Serialization
emitter.Emit(new StreamEnd());
}
private IObjectGraphVisitor CreateEmittingVisitor(Emitter emitter, SerializationOptions options, IObjectGraphTraversalStrategy traversalStrategy, IEventEmitter eventEmitter, object graph, Type type)
private IObjectGraphVisitor CreateEmittingVisitor(Emitter emitter, IObjectGraphTraversalStrategy traversalStrategy, IEventEmitter eventEmitter, object graph, Type type)
{
IObjectGraphVisitor emittingVisitor = new EmittingObjectGraphVisitor(eventEmitter);
@ -183,7 +186,7 @@ namespace YamlDotNet.RepresentationModel.Serialization
return emittingVisitor;
}
private IEventEmitter CreateEventEmitter(Emitter emitter, SerializationOptions options)
private IEventEmitter CreateEventEmitter(Emitter emitter)
{
var writer = new WriterEventEmitter(emitter);
@ -197,7 +200,7 @@ namespace YamlDotNet.RepresentationModel.Serialization
}
}
private IObjectGraphTraversalStrategy CreateTraversalStrategy(SerializationOptions options, INamingConvention namingConvention)
private IObjectGraphTraversalStrategy CreateTraversalStrategy()
{
ITypeDescriptor typeDescriptor;
if ((options & SerializationOptions.Roundtrip) != 0)

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

@ -185,8 +185,8 @@ namespace YamlDotNet.RepresentationModel.Serialization
[Obsolete("This method exists for source compatibility with older code. You should use the Serializer class instead.")]
public void Serialize(TextWriter output, object graph)
{
var serializer = new Serializer();
serializer.Serialize(output, graph, (SerializationOptions)mode);
var serializer = new Serializer((SerializationOptions)mode);
serializer.Serialize(output, graph);
}
#endregion

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

@ -172,13 +172,13 @@ namespace YamlDotNet.UnitTests.RepresentationModel
[Fact]
public void Roundtrip()
{
var serializer = new Serializer();
{
var serializer = new Serializer(SerializationOptions.Roundtrip);
using (StringWriter buffer = new StringWriter())
{
X original = new X();
serializer.Serialize(buffer, original, SerializationOptions.Roundtrip);
serializer.Serialize(buffer, original);
Console.WriteLine(buffer.ToString());
@ -200,13 +200,13 @@ namespace YamlDotNet.UnitTests.RepresentationModel
[Fact]
public void RoundtripWithDefaults()
{
var serializer = new Serializer();
{
var serializer = new Serializer(SerializationOptions.Roundtrip | SerializationOptions.EmitDefaults);
using (StringWriter buffer = new StringWriter())
{
X original = new X();
serializer.Serialize(buffer, original, SerializationOptions.Roundtrip | SerializationOptions.EmitDefaults);
serializer.Serialize(buffer, original);
Console.WriteLine(buffer.ToString());
@ -261,8 +261,8 @@ namespace YamlDotNet.UnitTests.RepresentationModel
[Fact]
public void CircularReference()
{
var serializer = new Serializer();
{
var serializer = new Serializer(SerializationOptions.Roundtrip);
using (StringWriter buffer = new StringWriter())
{
@ -273,7 +273,7 @@ namespace YamlDotNet.UnitTests.RepresentationModel
Child2 = original
};
serializer.Serialize(buffer, original, typeof(Y), SerializationOptions.Roundtrip);
serializer.Serialize(buffer, original, typeof(Y));
Console.WriteLine(buffer.ToString());
}
@ -392,8 +392,8 @@ namespace YamlDotNet.UnitTests.RepresentationModel
[Fact]
public void RoundtripList()
{
var serializer = new Serializer();
{
var serializer = new Serializer(SerializationOptions.Roundtrip);
var deserializer = new Deserializer();
using (StringWriter buffer = new StringWriter())
@ -402,7 +402,7 @@ namespace YamlDotNet.UnitTests.RepresentationModel
original.Add(2);
original.Add(4);
original.Add(6);
serializer.Serialize(buffer, original, typeof(List<int>), SerializationOptions.Roundtrip);
serializer.Serialize(buffer, original, typeof(List<int>));
Console.WriteLine(buffer.ToString());
@ -629,11 +629,11 @@ namespace YamlDotNet.UnitTests.RepresentationModel
[Fact]
public void RoundtripWithTypeConverter()
{
SomeCustomeType x = new SomeCustomeType("Yo");
var serializer = new Serializer();
SomeCustomeType x = new SomeCustomeType("Yo");
var serializer = new Serializer(SerializationOptions.Roundtrip);
serializer.RegisterTypeConverter(new CustomTypeConverter());
StringWriter buffer = new StringWriter();
serializer.Serialize(buffer, x, SerializationOptions.Roundtrip);
serializer.Serialize(buffer, x);
Console.WriteLine(buffer.ToString());
@ -691,13 +691,13 @@ namespace YamlDotNet.UnitTests.RepresentationModel
[Fact]
public void SerializationIncludesNullWhenAsked_BugFix()
{
var serializer = new Serializer();
{
var serializer = new Serializer(SerializationOptions.EmitDefaults);
using (StringWriter buffer = new StringWriter())
{
var original = new { MyString = (string)null };
serializer.Serialize(buffer, original, original.GetType(), SerializationOptions.EmitDefaults);
serializer.Serialize(buffer, original, original.GetType());
Console.WriteLine(buffer.ToString());
@ -707,13 +707,13 @@ namespace YamlDotNet.UnitTests.RepresentationModel
[Fact]
public void SerializationIncludesNullWhenAsked()
{
var serializer = new Serializer();
{
var serializer = new Serializer(SerializationOptions.EmitDefaults);
using (StringWriter buffer = new StringWriter())
{
X original = new X { MyString = null };
serializer.Serialize(buffer, original, typeof(X), SerializationOptions.EmitDefaults);
serializer.Serialize(buffer, original, typeof(X));
Console.WriteLine(buffer.ToString());
@ -729,7 +729,7 @@ namespace YamlDotNet.UnitTests.RepresentationModel
using (StringWriter buffer = new StringWriter())
{
X original = new X { MyString = null };
serializer.Serialize(buffer, original, typeof(X), SerializationOptions.None);
serializer.Serialize(buffer, original, typeof(X));
Console.WriteLine(buffer.ToString());
@ -739,13 +739,13 @@ namespace YamlDotNet.UnitTests.RepresentationModel
[Fact]
public void SerializationOfNullWorksInJson()
{
var serializer = new Serializer();
{
var serializer = new Serializer(SerializationOptions.EmitDefaults | SerializationOptions.JsonCompatible);
using (StringWriter buffer = new StringWriter())
{
X original = new X { MyString = null };
serializer.Serialize(buffer, original, typeof(X), SerializationOptions.EmitDefaults | SerializationOptions.JsonCompatible);
serializer.Serialize(buffer, original, typeof(X));
Console.WriteLine(buffer.ToString());
@ -755,14 +755,14 @@ namespace YamlDotNet.UnitTests.RepresentationModel
[Fact]
public void DeserializationOfNullWorksInJson()
{
var serializer = new Serializer();
{
var serializer = new Serializer(SerializationOptions.EmitDefaults | SerializationOptions.JsonCompatible | SerializationOptions.Roundtrip);
var deserializer = new Deserializer();
using (StringWriter buffer = new StringWriter())
{
X original = new X { MyString = null };
serializer.Serialize(buffer, original, typeof(X), SerializationOptions.EmitDefaults | SerializationOptions.JsonCompatible | SerializationOptions.Roundtrip);
serializer.Serialize(buffer, original, typeof(X));
Console.WriteLine(buffer.ToString());
@ -848,10 +848,10 @@ namespace YamlDotNet.UnitTests.RepresentationModel
private string SerializeWithNaming<T>(T input, INamingConvention naming)
{
var serializer = new Serializer();
var serializer = new Serializer(namingConvention: naming);
using (var writer = new StringWriter())
{
serializer.Serialize(writer, input, typeof(T), SerializationOptions.None, naming);
serializer.Serialize(writer, input, typeof(T));
return writer.ToString();
}
}
@ -978,11 +978,11 @@ namespace YamlDotNet.UnitTests.RepresentationModel
[Fact]
public void DefaultValueAttributeIsIgnoredWhenPresentWithEmitDefaults()
{
var input = new HasDefaults { Value = HasDefaults.DefaultValue };
var serializer = new Serializer();
var input = new HasDefaults { Value = HasDefaults.DefaultValue };
var serializer = new Serializer(SerializationOptions.EmitDefaults);
var writer = new StringWriter();
serializer.Serialize(writer, input, SerializationOptions.EmitDefaults);
serializer.Serialize(writer, input);
var serialized = writer.ToString();
Console.WriteLine(serialized);
@ -1081,11 +1081,11 @@ Name: Charles
[Fact]
public void NullValuesInListsAreAlwaysEmittedWithEmitDefaults()
{
var input = new string[] { "foo", null, "bar" };
var serializer = new Serializer();
var input = new string[] { "foo", null, "bar" };
var serializer = new Serializer(SerializationOptions.EmitDefaults);
var writer = new StringWriter();
serializer.Serialize(writer, input, SerializationOptions.EmitDefaults);
serializer.Serialize(writer, input);
var serialized = writer.ToString();
Console.WriteLine(serialized);