From 951b581d8eb8821ae53baeda06996d451fc73d02 Mon Sep 17 00:00:00 2001 From: Antoine Aubry Date: Wed, 3 Jul 2013 15:35:08 +0100 Subject: [PATCH] Move classes to their own files. --- .../Serialization/JsonEventEmitter.cs | 90 +++++++++++++ .../TypeAssigningEventEmitter.cs | 121 ------------------ .../Serialization/YamlFormatter.cs | 41 ++++++ .../YamlDotNet.RepresentationModel.csproj | 2 + 4 files changed, 133 insertions(+), 121 deletions(-) create mode 100644 YamlDotNet.RepresentationModel/Serialization/JsonEventEmitter.cs create mode 100644 YamlDotNet.RepresentationModel/Serialization/YamlFormatter.cs diff --git a/YamlDotNet.RepresentationModel/Serialization/JsonEventEmitter.cs b/YamlDotNet.RepresentationModel/Serialization/JsonEventEmitter.cs new file mode 100644 index 00000000..ee9bd2a8 --- /dev/null +++ b/YamlDotNet.RepresentationModel/Serialization/JsonEventEmitter.cs @@ -0,0 +1,90 @@ +using System; +using System.Globalization; +using YamlDotNet.Core; +using YamlDotNet.Core.Events; + +namespace YamlDotNet.RepresentationModel.Serialization +{ + public sealed class JsonEventEmitter : ChainedEventEmitter + { + public JsonEventEmitter(IEventEmitter nextEmitter) + : base(nextEmitter) + { + } + + public override void Emit(AliasEventInfo eventInfo) + { + throw new NotSupportedException("Aliases are not supported in JSON"); + } + + public override void Emit(ScalarEventInfo eventInfo) + { + eventInfo.IsPlainImplicit = true; + eventInfo.Style = ScalarStyle.Plain; + + var typeCode = eventInfo.SourceValue != null + ? Type.GetTypeCode(eventInfo.SourceType) + : TypeCode.Empty; + + switch (typeCode) + { + case TypeCode.Boolean: + eventInfo.RenderedValue = YamlFormatter.FormatBoolean(eventInfo.SourceValue); + break; + + case TypeCode.Byte: + case TypeCode.Int16: + case TypeCode.Int32: + case TypeCode.Int64: + case TypeCode.SByte: + case TypeCode.UInt16: + case TypeCode.UInt32: + case TypeCode.UInt64: + case TypeCode.Single: + case TypeCode.Double: + case TypeCode.Decimal: + eventInfo.RenderedValue = YamlFormatter.FormatNumber(eventInfo.SourceValue); + break; + + case TypeCode.String: + case TypeCode.Char: + eventInfo.RenderedValue = eventInfo.SourceValue.ToString(); + eventInfo.Style = ScalarStyle.DoubleQuoted; + break; + + case TypeCode.DateTime: + eventInfo.RenderedValue = YamlFormatter.FormatDateTime(eventInfo.SourceValue); + break; + + case TypeCode.Empty: + eventInfo.RenderedValue = "null"; + break; + + default: + if (eventInfo.SourceType == typeof(TimeSpan)) + { + eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(eventInfo.SourceValue); + break; + } + + throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode)); + } + + base.Emit(eventInfo); + } + + public override void Emit(MappingStartEventInfo eventInfo) + { + eventInfo.Style = MappingStyle.Flow; + + base.Emit(eventInfo); + } + + public override void Emit(SequenceStartEventInfo eventInfo) + { + eventInfo.Style = SequenceStyle.Flow; + + base.Emit(eventInfo); + } + } +} \ No newline at end of file diff --git a/YamlDotNet.RepresentationModel/Serialization/TypeAssigningEventEmitter.cs b/YamlDotNet.RepresentationModel/Serialization/TypeAssigningEventEmitter.cs index cdc984f9..62861001 100644 --- a/YamlDotNet.RepresentationModel/Serialization/TypeAssigningEventEmitter.cs +++ b/YamlDotNet.RepresentationModel/Serialization/TypeAssigningEventEmitter.cs @@ -1,93 +1,9 @@ using System; using System.Globalization; using YamlDotNet.Core; -using YamlDotNet.Core.Events; namespace YamlDotNet.RepresentationModel.Serialization { - public sealed class JsonEventEmitter : ChainedEventEmitter - { - public JsonEventEmitter(IEventEmitter nextEmitter) - : base(nextEmitter) - { - } - - public override void Emit(AliasEventInfo eventInfo) - { - throw new NotSupportedException("Aliases are not supported in JSON"); - } - - public override void Emit(ScalarEventInfo eventInfo) - { - eventInfo.IsPlainImplicit = true; - eventInfo.Style = ScalarStyle.Plain; - - var typeCode = eventInfo.SourceValue != null - ? Type.GetTypeCode(eventInfo.SourceType) - : TypeCode.Empty; - - switch (typeCode) - { - case TypeCode.Boolean: - eventInfo.RenderedValue = YamlFormatter.FormatBoolean(eventInfo.SourceValue); - break; - - case TypeCode.Byte: - case TypeCode.Int16: - case TypeCode.Int32: - case TypeCode.Int64: - case TypeCode.SByte: - case TypeCode.UInt16: - case TypeCode.UInt32: - case TypeCode.UInt64: - case TypeCode.Single: - case TypeCode.Double: - case TypeCode.Decimal: - eventInfo.RenderedValue = YamlFormatter.FormatNumber(eventInfo.SourceValue); - break; - - case TypeCode.String: - case TypeCode.Char: - eventInfo.RenderedValue = eventInfo.SourceValue.ToString(); - eventInfo.Style = ScalarStyle.DoubleQuoted; - break; - - case TypeCode.DateTime: - eventInfo.RenderedValue = YamlFormatter.FormatDateTime(eventInfo.SourceValue); - break; - - case TypeCode.Empty: - eventInfo.RenderedValue = "null"; - break; - - default: - if (eventInfo.SourceType == typeof(TimeSpan)) - { - eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(eventInfo.SourceValue); - break; - } - - throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode)); - } - - base.Emit(eventInfo); - } - - public override void Emit(MappingStartEventInfo eventInfo) - { - eventInfo.Style = MappingStyle.Flow; - - base.Emit(eventInfo); - } - - public override void Emit(SequenceStartEventInfo eventInfo) - { - eventInfo.Style = SequenceStyle.Flow; - - base.Emit(eventInfo); - } - } - public sealed class TypeAssigningEventEmitter : ChainedEventEmitter { public TypeAssigningEventEmitter(IEventEmitter nextEmitter) @@ -159,42 +75,5 @@ public override void Emit(ScalarEventInfo eventInfo) base.Emit(eventInfo); } - - } - - internal static class YamlFormatter - { - private static readonly NumberFormatInfo numberFormat = new NumberFormatInfo - { - CurrencyDecimalSeparator = ".", - CurrencyGroupSeparator = "_", - CurrencyGroupSizes = new[] { 3 }, - CurrencySymbol = string.Empty, - CurrencyDecimalDigits = 99, - NumberDecimalSeparator = ".", - NumberGroupSeparator = "_", - NumberGroupSizes = new[] { 3 }, - NumberDecimalDigits = 99 - }; - - public static string FormatNumber(object number) - { - return Convert.ToString(number, numberFormat); - } - - public static string FormatBoolean(object boolean) - { - return boolean.Equals(true) ? "true" : "false"; - } - - public static string FormatDateTime(object dateTime) - { - return ((DateTime)dateTime).ToString("o", CultureInfo.InvariantCulture); - } - - public static string FormatTimeSpan(object timeSpan) - { - return ((TimeSpan)timeSpan).ToString(); - } } } \ No newline at end of file diff --git a/YamlDotNet.RepresentationModel/Serialization/YamlFormatter.cs b/YamlDotNet.RepresentationModel/Serialization/YamlFormatter.cs new file mode 100644 index 00000000..18802bba --- /dev/null +++ b/YamlDotNet.RepresentationModel/Serialization/YamlFormatter.cs @@ -0,0 +1,41 @@ +using System; +using System.Globalization; + +namespace YamlDotNet.RepresentationModel.Serialization +{ + internal static class YamlFormatter + { + private static readonly NumberFormatInfo numberFormat = new NumberFormatInfo + { + CurrencyDecimalSeparator = ".", + CurrencyGroupSeparator = "_", + CurrencyGroupSizes = new[] { 3 }, + CurrencySymbol = string.Empty, + CurrencyDecimalDigits = 99, + NumberDecimalSeparator = ".", + NumberGroupSeparator = "_", + NumberGroupSizes = new[] { 3 }, + NumberDecimalDigits = 99 + }; + + public static string FormatNumber(object number) + { + return Convert.ToString(number, numberFormat); + } + + public static string FormatBoolean(object boolean) + { + return boolean.Equals(true) ? "true" : "false"; + } + + public static string FormatDateTime(object dateTime) + { + return ((DateTime)dateTime).ToString("o", CultureInfo.InvariantCulture); + } + + public static string FormatTimeSpan(object timeSpan) + { + return ((TimeSpan)timeSpan).ToString(); + } + } +} \ No newline at end of file diff --git a/YamlDotNet.RepresentationModel/YamlDotNet.RepresentationModel.csproj b/YamlDotNet.RepresentationModel/YamlDotNet.RepresentationModel.csproj index 08fbad32..2c44fd1b 100644 --- a/YamlDotNet.RepresentationModel/YamlDotNet.RepresentationModel.csproj +++ b/YamlDotNet.RepresentationModel/YamlDotNet.RepresentationModel.csproj @@ -89,6 +89,7 @@ + @@ -118,6 +119,7 @@ +