Skip to content

Commit

Permalink
Support different cultures
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardCooke committed Aug 23, 2023
1 parent 230666e commit e1986a4
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 84 deletions.
35 changes: 17 additions & 18 deletions Dockerfile.NonEnglish
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@ RUN dotnet restore YamlDotNet.sln

COPY . .

# RUN dotnet build -c Release --framework net35 YamlDotNet/YamlDotNet.csproj -o /output/net35
# RUN dotnet build -c Release --framework net40 YamlDotNet/YamlDotNet.csproj -o /output/net40
# RUN dotnet build -c Release --framework net45 YamlDotNet/YamlDotNet.csproj -o /output/net45
# RUN dotnet build -c Release --framework net47 YamlDotNet/YamlDotNet.csproj -o /output/net47
# RUN dotnet build -c Release --framework netstandard2.0 YamlDotNet/YamlDotNet.csproj -o /output/netstandard2.0
# RUN dotnet build -c Release --framework netstandard2.1 YamlDotNet/YamlDotNet.csproj -o /output/netstandard2.1
# RUN dotnet build -c Release --framework net60 YamlDotNet/YamlDotNet.csproj -o /output/net60
# RUN dotnet build -c Release --framework net70 YamlDotNet/YamlDotNet.csproj -o /output/net70

# RUN dotnet pack -c Release YamlDotNet/YamlDotNet.csproj -o /output/package /p:Version=$PACKAGE_VERSION
RUN dotnet build -c Release --framework net35 YamlDotNet/YamlDotNet.csproj -o /output/net35
RUN dotnet build -c Release --framework net40 YamlDotNet/YamlDotNet.csproj -o /output/net40
RUN dotnet build -c Release --framework net45 YamlDotNet/YamlDotNet.csproj -o /output/net45
RUN dotnet build -c Release --framework net47 YamlDotNet/YamlDotNet.csproj -o /output/net47
RUN dotnet build -c Release --framework netstandard2.0 YamlDotNet/YamlDotNet.csproj -o /output/netstandard2.0
RUN dotnet build -c Release --framework netstandard2.1 YamlDotNet/YamlDotNet.csproj -o /output/netstandard2.1
RUN dotnet build -c Release --framework net60 YamlDotNet/YamlDotNet.csproj -o /output/net60
RUN dotnet build -c Release --framework net70 YamlDotNet/YamlDotNet.csproj -o /output/net70

RUN dotnet pack -c Release YamlDotNet/YamlDotNet.csproj -o /output/package /p:Version=$PACKAGE_VERSION

ARG LOCALE_LANGUAGE="en"
ARG LOCALE_COUNTRY="DK"
Expand All @@ -65,12 +64,12 @@ RUN echo -n "${LC_ALL}" > /etc/locale.gen && \
locale-gen

RUN dotnet test -c Release YamlDotNet.sln --framework net70 --logger:"trx;LogFileName=/output/tests.net70.trx" --logger:"console;Verbosity=detailed"
#RUN dotnet test -c Release YamlDotNet.sln --framework net60 --logger:"trx;LogFileName=/output/tests.net60.trx" --logger:"console;Verbosity=detailed"
#RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework netcoreapp3.1 --logger:"trx;LogFileName=/output/tests.netcoreapp3.1.trx" --logger:"console;Verbosity=detailed"
#RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework net47 --logger:"trx;LogFileName=/output/tests.net47.trx" --logger:"console;Verbosity=detailed"
RUN dotnet test -c Release YamlDotNet.sln --framework net60 --logger:"trx;LogFileName=/output/tests.net60.trx" --logger:"console;Verbosity=detailed"
RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework netcoreapp3.1 --logger:"trx;LogFileName=/output/tests.netcoreapp3.1.trx" --logger:"console;Verbosity=detailed"
RUN dotnet test -c Release YamlDotNet.Test/YamlDotNet.Test.csproj --framework net47 --logger:"trx;LogFileName=/output/tests.net47.trx" --logger:"console;Verbosity=detailed"

#FROM alpine
#VOLUME /output
#WORKDIR /libraries
#COPY --from=build /output /libraries
#CMD [ "cp", "-r", "/libraries", "/output" ]
FROM alpine
VOLUME /output
WORKDIR /libraries
COPY --from=build /output /libraries
CMD [ "cp", "-r", "/libraries", "/output" ]
6 changes: 6 additions & 0 deletions YamlDotNet.Core7AoTCompileTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma warning disable CS8604 // Possible null reference argument.
#pragma warning disable CS8618 // Possible null reference argument.
#pragma warning disable CS8602 // Possible null reference argument.

using System;
using System.Collections;
Expand Down Expand Up @@ -208,3 +211,6 @@ public enum MyTestEnum
Z = 1,
}

#pragma warning restore CS8604 // Possible null reference argument.
#pragma warning restore CS8618 // Possible null reference argument.
#pragma warning restore CS8602 // Possible null reference argument.
2 changes: 1 addition & 1 deletion YamlDotNet.Test/Serialization/DeserializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public static IEnumerable<object[]> DeserializeScalarEdgeCases_TestCases
public void DeserializeScalarEdgeCases(IConvertible value, Type type)
{
var deserializer = new DeserializerBuilder().Build();
var result = deserializer.Deserialize(value.ToString(), type);
var result = deserializer.Deserialize(value.ToString(YamlFormatter.Default.NumberFormat), type);

result.Should().Be(value);
}
Expand Down
13 changes: 13 additions & 0 deletions YamlDotNet/Serialization/BuilderSkeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public abstract class BuilderSkeleton<TBuilder>
internal bool ignoreFields;
internal bool includeNonPublicProperties = false;
internal Settings settings;
internal YamlFormatter yamlFormatter = YamlFormatter.Default;

internal BuilderSkeleton(ITypeResolver typeResolver)
{
Expand Down Expand Up @@ -295,6 +296,18 @@ public TBuilder WithoutTypeInspector(Type inspectorType)
return Self;
}

/// <summary>
/// Override the default yaml formatter with the one passed in
/// </summary>
/// <param name="formatter"><seealso cref="YamlFormatter"/>to use when serializing and deserializing objects.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public TBuilder WithYamlFormatter(YamlFormatter formatter)
{
yamlFormatter = formatter ?? throw new ArgumentNullException(nameof(formatter));
return Self;
}

protected IEnumerable<IYamlTypeConverter> BuildTypeConverters()
{
return typeConverterFactories.BuildComponentList();
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/DeserializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public DeserializerBuilder()
{ typeof(YamlSerializableNodeDeserializer), _ => new YamlSerializableNodeDeserializer(objectFactory.Value) },
{ typeof(TypeConverterNodeDeserializer), _ => new TypeConverterNodeDeserializer(BuildTypeConverters()) },
{ typeof(NullNodeDeserializer), _ => new NullNodeDeserializer() },
{ typeof(ScalarNodeDeserializer), _ => new ScalarNodeDeserializer(attemptUnknownTypeDeserialization, typeConverter) },
{ typeof(ScalarNodeDeserializer), _ => new ScalarNodeDeserializer(attemptUnknownTypeDeserialization, typeConverter, yamlFormatter) },
{ typeof(ArrayNodeDeserializer), _ => new ArrayNodeDeserializer() },
{ typeof(DictionaryNodeDeserializer), _ => new DictionaryNodeDeserializer(objectFactory.Value, duplicateKeyChecking) },
{ typeof(CollectionNodeDeserializer), _ => new CollectionNodeDeserializer(objectFactory.Value) },
Expand Down
20 changes: 14 additions & 6 deletions YamlDotNet/Serialization/EventEmitters/JsonEventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ namespace YamlDotNet.Serialization.EventEmitters
{
public sealed class JsonEventEmitter : ChainedEventEmitter
{
public JsonEventEmitter(IEventEmitter nextEmitter)
private readonly YamlFormatter formatter;

public JsonEventEmitter(IEventEmitter nextEmitter, YamlFormatter formatter)
: base(nextEmitter)
{
this.formatter = formatter;
}

public JsonEventEmitter(IEventEmitter nextEmitter)
: this(nextEmitter, YamlFormatter.Default)
{
}

Expand All @@ -53,7 +61,7 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
switch (typeCode)
{
case TypeCode.Boolean:
eventInfo.RenderedValue = YamlFormatter.FormatBoolean(value);
eventInfo.RenderedValue = formatter.FormatBoolean(value);
break;

case TypeCode.Byte:
Expand All @@ -72,13 +80,13 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
break;
}

eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
eventInfo.RenderedValue = formatter.FormatNumber(value);
break;

case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
eventInfo.RenderedValue = formatter.FormatNumber(value);
break;

case TypeCode.String:
Expand All @@ -88,7 +96,7 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
break;

case TypeCode.DateTime:
eventInfo.RenderedValue = YamlFormatter.FormatDateTime(value);
eventInfo.RenderedValue = formatter.FormatDateTime(value);
break;

case TypeCode.Empty:
Expand All @@ -98,7 +106,7 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
default:
if (eventInfo.Source.Type == typeof(TimeSpan))
{
eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(value);
eventInfo.RenderedValue = formatter.FormatTimeSpan(value);
break;
}

Expand Down
59 changes: 31 additions & 28 deletions YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using YamlDotNet.Core;
using YamlDotNet.Serialization.Schemas;
Expand Down Expand Up @@ -66,22 +68,14 @@ public sealed class TypeAssigningEventEmitter : ChainedEventEmitter
+ @")$";

private readonly ScalarStyle defaultScalarStyle = ScalarStyle.Any;
private readonly YamlFormatter formatter;

public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings, bool quoteNecessaryStrings, bool quoteYaml1_1Strings, ScalarStyle defaultScalarStyle)
: this(nextEmitter, requireTagWhenStaticAndActualTypesAreDifferent, tagMappings, quoteNecessaryStrings, quoteYaml1_1Strings)
{
this.defaultScalarStyle = defaultScalarStyle;
}

public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings, bool quoteNecessaryStrings, ScalarStyle defaultScalarStyle)
: this(nextEmitter, requireTagWhenStaticAndActualTypesAreDifferent, tagMappings, quoteNecessaryStrings)
public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings, bool quoteNecessaryStrings, bool quoteYaml1_1Strings, ScalarStyle defaultScalarStyle, YamlFormatter formatter)
: base(nextEmitter)
{
this.defaultScalarStyle = defaultScalarStyle;
}

public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings, bool quoteNecessaryStrings, bool quoteYaml1_1Strings)
: this(nextEmitter, requireTagWhenStaticAndActualTypesAreDifferent, tagMappings)
{
this.formatter = formatter;
this.tagMappings = tagMappings;
this.quoteNecessaryStrings = quoteNecessaryStrings;

var specialStringValuePattern = quoteYaml1_1Strings
Expand All @@ -94,23 +88,32 @@ public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenS
#endif
}

public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings, bool quoteNecessaryStrings)
: this(nextEmitter, requireTagWhenStaticAndActualTypesAreDifferent, tagMappings)
public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings, bool quoteNecessaryStrings, bool quoteYaml1_1Strings, ScalarStyle defaultScalarStyle)
: this(nextEmitter, requireTagWhenStaticAndActualTypesAreDifferent, tagMappings, quoteNecessaryStrings, quoteYaml1_1Strings, defaultScalarStyle, YamlFormatter.Default)
{
this.quoteNecessaryStrings = quoteNecessaryStrings;
}

#if NET40
isSpecialStringValue_Regex = new Regex(SpecialStrings_Pattern);
#else
isSpecialStringValue_Regex = new Regex(SpecialStrings_Pattern, RegexOptions.Compiled);
#endif
public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings, bool quoteNecessaryStrings, ScalarStyle defaultScalarStyle)
: this(nextEmitter, requireTagWhenStaticAndActualTypesAreDifferent, tagMappings, quoteNecessaryStrings, false, defaultScalarStyle, YamlFormatter.Default)
{
}

public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings, bool quoteNecessaryStrings, bool quoteYaml1_1Strings)
: this(nextEmitter, requireTagWhenStaticAndActualTypesAreDifferent, tagMappings, quoteNecessaryStrings, quoteYaml1_1Strings, ScalarStyle.Any)
{
}

public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings, bool quoteNecessaryStrings)
: this(nextEmitter, requireTagWhenStaticAndActualTypesAreDifferent, tagMappings, quoteNecessaryStrings, false)
{
}

public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings)
: base(nextEmitter)
{
this.requireTagWhenStaticAndActualTypesAreDifferent = requireTagWhenStaticAndActualTypesAreDifferent;
this.tagMappings = tagMappings ?? throw new ArgumentNullException(nameof(tagMappings));
formatter = YamlFormatter.Default;
}

public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
Expand All @@ -130,7 +133,7 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
{
case TypeCode.Boolean:
eventInfo.Tag = JsonSchema.Tags.Bool;
eventInfo.RenderedValue = YamlFormatter.FormatBoolean(value);
eventInfo.RenderedValue = formatter.FormatBoolean(value);
break;

case TypeCode.Byte:
Expand Down Expand Up @@ -159,23 +162,23 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
else
{
eventInfo.Tag = JsonSchema.Tags.Int;
eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
eventInfo.RenderedValue = formatter.FormatNumber(value);
}
break;

case TypeCode.Single:
eventInfo.Tag = JsonSchema.Tags.Float;
eventInfo.RenderedValue = YamlFormatter.FormatNumber((float)value);
eventInfo.RenderedValue = formatter.FormatNumber((float)value);
break;

case TypeCode.Double:
eventInfo.Tag = JsonSchema.Tags.Float;
eventInfo.RenderedValue = YamlFormatter.FormatNumber((double)value);
eventInfo.RenderedValue = formatter.FormatNumber((double)value);
break;

case TypeCode.Decimal:
eventInfo.Tag = JsonSchema.Tags.Float;
eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
eventInfo.RenderedValue = formatter.FormatNumber(value);
break;

case TypeCode.String:
Expand All @@ -196,7 +199,7 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)

case TypeCode.DateTime:
eventInfo.Tag = DefaultSchema.Tags.Timestamp;
eventInfo.RenderedValue = YamlFormatter.FormatDateTime(value);
eventInfo.RenderedValue = formatter.FormatDateTime(value);
break;

case TypeCode.Empty:
Expand All @@ -207,7 +210,7 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
default:
if (eventInfo.Source.Type == typeof(TimeSpan))
{
eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(value);
eventInfo.RenderedValue = formatter.FormatTimeSpan(value);
break;
}

Expand Down
Loading

0 comments on commit e1986a4

Please sign in to comment.