mirror of https://github.com/dotnet/tye.git
Browse Source
* Created a object graph visitor to ignore also empty collections on serialization. Unify creation of serilizer/deserialization objects with specified configuration * Update OmitDefaultAndEmptyArrayObjectGraphVisitor.cs * Update YamlSerializer.cs * Fix tests with EOL issues on different SO #235 Co-authored-by: Justin Kotalik <jkotalik12@gmail.com>pull/242/head
committed by
GitHub
7 changed files with 99 additions and 22 deletions
@ -0,0 +1,46 @@ |
|||||
|
// Licensed to the .NET Foundation under one or more agreements.
|
||||
|
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
// See the LICENSE file in the project root for more information.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections; |
||||
|
using System.ComponentModel; |
||||
|
using System.Reflection; |
||||
|
using YamlDotNet.Core; |
||||
|
using YamlDotNet.Serialization; |
||||
|
using YamlDotNet.Serialization.ObjectGraphVisitors; |
||||
|
|
||||
|
namespace Microsoft.Tye.Serialization |
||||
|
{ |
||||
|
internal sealed class OmitDefaultAndEmptyArrayObjectGraphVisitor |
||||
|
: ChainedObjectGraphVisitor |
||||
|
{ |
||||
|
public OmitDefaultAndEmptyArrayObjectGraphVisitor(IObjectGraphVisitor<IEmitter> nextVisitor) |
||||
|
: base(nextVisitor) { } |
||||
|
|
||||
|
private static object? GetDefault(Type type) |
||||
|
{ |
||||
|
return type.GetTypeInfo().IsValueType ? Activator.CreateInstance(type) : null; |
||||
|
} |
||||
|
|
||||
|
private static bool IsEmptyArray(Type type, object? value) |
||||
|
{ |
||||
|
return value is object |
||||
|
&& value is ICollection |
||||
|
&& ((ICollection)value).Count == 0; |
||||
|
} |
||||
|
|
||||
|
public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context) |
||||
|
{ |
||||
|
var defaultValueAttribute = key.GetCustomAttribute<DefaultValueAttribute>(); |
||||
|
|
||||
|
var defaultValue = defaultValueAttribute != null |
||||
|
? defaultValueAttribute.Value |
||||
|
: GetDefault(key.Type); |
||||
|
|
||||
|
return !Equals(value.Value, defaultValue) |
||||
|
&& !IsEmptyArray(value.Type, value.Value) |
||||
|
&& base.EnterMapping(key, value, context); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
// Licensed to the .NET Foundation under one or more agreements.
|
||||
|
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
// See the LICENSE file in the project root for more information.
|
||||
|
|
||||
|
using YamlDotNet.Serialization; |
||||
|
using YamlDotNet.Serialization.NamingConventions; |
||||
|
|
||||
|
namespace Microsoft.Tye.Serialization |
||||
|
{ |
||||
|
public static class YamlSerializer |
||||
|
{ |
||||
|
public static ISerializer CreateSerializer() |
||||
|
{ |
||||
|
return new SerializerBuilder() |
||||
|
.WithNamingConvention(CamelCaseNamingConvention.Instance) |
||||
|
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults) |
||||
|
.WithEmissionPhaseObjectGraphVisitor(args => new OmitDefaultAndEmptyArrayObjectGraphVisitor(args.InnerVisitor)) |
||||
|
.Build(); |
||||
|
} |
||||
|
|
||||
|
public static IDeserializer CreateDeserializer() |
||||
|
{ |
||||
|
return new DeserializerBuilder() |
||||
|
.WithNamingConvention(CamelCaseNamingConvention.Instance) |
||||
|
.Build(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
namespace System |
||||
|
{ |
||||
|
internal static class StringExtensions |
||||
|
{ |
||||
|
public static string NormalizeNewLines(this string value) |
||||
|
{ |
||||
|
return value |
||||
|
.Replace("\r\n", "\n") |
||||
|
.Replace("\n", Environment.NewLine); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue