Import YamlDotNet.

This commit is contained in:
2022-12-12 22:23:49 +08:00
parent f559cea826
commit 1477e907e6
462 changed files with 27142 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7d3f73aa22328f342ab53072ea83da7d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 272f43609d820fd4aa820cc0636fe694
folderAsset: yes
timeCreated: 1427145262
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,115 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using System;
using System.Globalization;
using System.Linq;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
namespace YamlDotNet.Serialization.Converters
{
/// <summary>
/// This represents the YAML converter entity for <see cref="DateTime"/>.
/// </summary>
public class DateTimeConverter : IYamlTypeConverter
{
private readonly DateTimeKind kind;
private readonly IFormatProvider provider;
private readonly string[] formats;
/// <summary>
/// Initializes a new instance of the <see cref="DateTimeConverter"/> class.
/// </summary>
/// <param name="kind"><see cref="DateTimeKind"/> value. Default value is <see cref="DateTimeKind.Utc"/>. <see cref="DateTimeKind.Unspecified"/> is considered as <see cref="DateTimeKind.Utc"/>.</param>
/// <param name="provider"><see cref="IFormatProvider"/> instance. Default value is <see cref="CultureInfo.InvariantCulture"/>.</param>
/// <param name="formats">List of date/time formats for parsing. Default value is "<c>G</c>".</param>
/// <remarks>On deserializing, all formats in the list are used for conversion, while on serializing, the first format in the list is used.</remarks>
public DateTimeConverter(DateTimeKind kind = DateTimeKind.Utc, IFormatProvider? provider = null, params string[] formats)
{
this.kind = kind == DateTimeKind.Unspecified ? DateTimeKind.Utc : kind;
this.provider = provider ?? CultureInfo.InvariantCulture;
this.formats = formats.DefaultIfEmpty("G").ToArray();
}
/// <summary>
/// Gets a value indicating whether the current converter supports converting the specified type.
/// </summary>
/// <param name="type"><see cref="Type"/> to check.</param>
/// <returns>Returns <c>True</c>, if the current converter supports; otherwise returns <c>False</c>.</returns>
public bool Accepts(Type type)
{
return type == typeof(DateTime);
}
/// <summary>
/// Reads an object's state from a YAML parser.
/// </summary>
/// <param name="parser"><see cref="IParser"/> instance.</param>
/// <param name="type"><see cref="Type"/> to convert.</param>
/// <returns>Returns the <see cref="DateTime"/> instance converted.</returns>
/// <remarks>On deserializing, all formats in the list are used for conversion.</remarks>
public object ReadYaml(IParser parser, Type type)
{
var value = parser.Consume<Scalar>().Value;
var style = this.kind == DateTimeKind.Local ? DateTimeStyles.AssumeLocal : DateTimeStyles.AssumeUniversal;
var dt = DateTime.ParseExact(value, this.formats, this.provider, style);
dt = EnsureDateTimeKind(dt, this.kind);
return dt;
}
/// <summary>
/// Writes the specified object's state to a YAML emitter.
/// </summary>
/// <param name="emitter"><see cref="IEmitter"/> instance.</param>
/// <param name="value">Value to write.</param>
/// <param name="type"><see cref="Type"/> to convert.</param>
/// <remarks>On serializing, the first format in the list is used.</remarks>
public void WriteYaml(IEmitter emitter, object? value, Type type)
{
var dt = (DateTime)value!;
var adjusted = this.kind == DateTimeKind.Local ? dt.ToLocalTime() : dt.ToUniversalTime();
var formatted = adjusted.ToString(this.formats.First(), this.provider); // Always take the first format of the list.
emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, formatted, ScalarStyle.Any, true, false));
}
private static DateTime EnsureDateTimeKind(DateTime dt, DateTimeKind kind)
{
DateTime ensured;
if (dt.Kind == DateTimeKind.Local && kind == DateTimeKind.Utc)
{
ensured = dt.ToUniversalTime();
return ensured;
}
if (dt.Kind == DateTimeKind.Utc && kind == DateTimeKind.Local)
{
ensured = dt.ToLocalTime();
return ensured;
}
return dt;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 59f426f3754afee449a7e275b7a0c119
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,57 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
namespace YamlDotNet.Serialization.Converters
{
/// <summary>
/// Converter for System.Guid.
/// </summary>
public class GuidConverter : IYamlTypeConverter
{
private readonly bool jsonCompatible;
public GuidConverter(bool jsonCompatible)
{
this.jsonCompatible = jsonCompatible;
}
public bool Accepts(Type type)
{
return type == typeof(Guid);
}
public object ReadYaml(IParser parser, Type type)
{
var value = parser.Consume<Scalar>().Value;
return new Guid(value);
}
public void WriteYaml(IEmitter emitter, object? value, Type type)
{
var guid = (Guid)value!;
emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, guid.ToString("D"), jsonCompatible ? ScalarStyle.DoubleQuoted : ScalarStyle.Any, true, false));
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 2034b2a36cbe85a438addb075cff648b
timeCreated: 1427145263
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,53 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
namespace YamlDotNet.Serialization.Converters
{
/// <summary>
/// Converter for System.Type.
/// </summary>
/// <remarks>
/// Converts <see cref="System.Type" /> to a scalar containing the assembly qualified name of the type.
/// </remarks>
public class SystemTypeConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return typeof(Type).IsAssignableFrom(type);
}
public object ReadYaml(IParser parser, Type type)
{
var value = parser.Consume<Scalar>().Value;
return Type.GetType(value, throwOnError: true)!; // Will throw instead of returning null
}
public void WriteYaml(IEmitter emitter, object? value, Type type)
{
var systemType = (Type)value!;
emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, systemType.AssemblyQualifiedName!, ScalarStyle.Any, true, false));
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: eb56a8270f28ae3438392cb2cfebe3b6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,52 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using System;
namespace YamlDotNet.Serialization
{
/// <summary>
/// Specifies the strategy to handle default and null values during serialization of properties.
/// </summary>
[Flags]
public enum DefaultValuesHandling
{
/// <summary>
/// Specifies that all properties are to be emitted regardless of their value. This is the default behavior.
/// </summary>
Preserve = 0,
/// <summary>
/// Specifies that properties that contain null references or a null Nullable&lt;T&gt; are to be omitted.
/// </summary>
OmitNull = 1,
/// <summary>
/// Specifies that properties that that contain their default value, either default(T) or the value specified in DefaultValueAttribute are to be omitted.
/// </summary>
OmitDefaults = 2,
/// <summary>
/// Specifies that properties that that contain collections/arrays/enumerations that are empty are to be omitted.
/// </summary>
OmitEmptyCollections = 4,
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 16ab81ef49026d74abc0c7d9dd2addf1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,149 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using System;
using System.IO;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization.Utilities;
namespace YamlDotNet.Serialization
{
/// <summary>
/// Deserializes objects from the YAML format.
/// To customize the behavior of <see cref="Deserializer" />,
/// use the <see cref="DeserializerBuilder" /> class.
/// </summary>
public sealed class Deserializer : IDeserializer
{
private readonly IValueDeserializer valueDeserializer;
/// <summary>
/// Initializes a new instance of <see cref="Deserializer" /> using the default configuration.
/// </summary>
/// <remarks>
/// To customize the behavior of the deserializer, use <see cref="DeserializerBuilder" />.
/// </remarks>
public Deserializer()
: this(new DeserializerBuilder().BuildValueDeserializer())
{
}
/// <remarks>
/// This constructor is private to discourage its use.
/// To invoke it, call the <see cref="FromValueDeserializer"/> method.
/// </remarks>
private Deserializer(IValueDeserializer valueDeserializer)
{
this.valueDeserializer = valueDeserializer ?? throw new ArgumentNullException(nameof(valueDeserializer));
}
/// <summary>
/// Creates a new <see cref="Deserializer" /> that uses the specified <see cref="IValueDeserializer" />.
/// This method is available for advanced scenarios. The preferred way to customize the behavior of the
/// deserializer is to use <see cref="DeserializerBuilder" />.
/// </summary>
public static Deserializer FromValueDeserializer(IValueDeserializer valueDeserializer)
{
return new Deserializer(valueDeserializer);
}
public T Deserialize<T>(string input)
{
using var reader = new StringReader(input);
return Deserialize<T>(reader);
}
public T Deserialize<T>(TextReader input)
{
return Deserialize<T>(new Parser(input));
}
public object? Deserialize(TextReader input)
{
return Deserialize(input, typeof(object));
}
public object? Deserialize(string input, Type type)
{
using var reader = new StringReader(input);
return Deserialize(reader, type);
}
public object? Deserialize(TextReader input, Type type)
{
return Deserialize(new Parser(input), type);
}
public T Deserialize<T>(IParser parser)
{
return (T)Deserialize(parser, typeof(T))!; // We really want an exception if we are trying to deserialize null into a non-nullable type
}
public object? Deserialize(IParser parser)
{
return Deserialize(parser, typeof(object));
}
/// <summary>
/// Deserializes an object of the specified type.
/// </summary>
/// <param name="parser">The <see cref="IParser" /> from where to deserialize the object.</param>
/// <param name="type">The static type of the object to deserialize.</param>
/// <returns>Returns the deserialized object.</returns>
public object? Deserialize(IParser parser, Type type)
{
if (parser == null)
{
throw new ArgumentNullException(nameof(parser));
}
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
var hasStreamStart = parser.TryConsume<StreamStart>(out var _);
var hasDocumentStart = parser.TryConsume<DocumentStart>(out var _);
object? result = null;
if (!parser.Accept<DocumentEnd>(out var _) && !parser.Accept<StreamEnd>(out var _))
{
using var state = new SerializerState();
result = valueDeserializer.DeserializeValue(parser, type, state, valueDeserializer);
state.OnDeserialization();
}
if (hasDocumentStart)
{
parser.Consume<DocumentEnd>();
}
if (hasStreamStart)
{
parser.Consume<StreamEnd>();
}
return result;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: cee7b8e2c23b44c4c9d8b6fa9029e350
timeCreated: 1427145266
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0ef2383a78598ce4b921ed59f9cbbceb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,82 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using YamlDotNet.Core;
namespace YamlDotNet.Serialization
{
public sealed class EmissionPhaseObjectGraphVisitorArgs
{
/// <summary>
/// Gets the next visitor that should be called by the current visitor.
/// </summary>
public IObjectGraphVisitor<IEmitter> InnerVisitor { get; private set; }
/// <summary>
/// Gets the <see cref="IEventEmitter" /> that is to be used for serialization.
/// </summary>
public IEventEmitter EventEmitter { get; private set; }
/// <summary>
/// Gets a function that, when called, serializes the specified object.
/// </summary>
public ObjectSerializer NestedObjectSerializer { get; private set; }
public IEnumerable<IYamlTypeConverter> TypeConverters { get; private set; }
private readonly IEnumerable<IObjectGraphVisitor<Nothing>> preProcessingPhaseVisitors;
public EmissionPhaseObjectGraphVisitorArgs(
IObjectGraphVisitor<IEmitter> innerVisitor,
IEventEmitter eventEmitter,
IEnumerable<IObjectGraphVisitor<Nothing>> preProcessingPhaseVisitors,
IEnumerable<IYamlTypeConverter> typeConverters,
ObjectSerializer nestedObjectSerializer
)
{
InnerVisitor = innerVisitor ?? throw new ArgumentNullException(nameof(innerVisitor));
EventEmitter = eventEmitter ?? throw new ArgumentNullException(nameof(eventEmitter));
this.preProcessingPhaseVisitors = preProcessingPhaseVisitors ?? throw new ArgumentNullException(nameof(preProcessingPhaseVisitors));
TypeConverters = typeConverters ?? throw new ArgumentNullException(nameof(typeConverters));
NestedObjectSerializer = nestedObjectSerializer ?? throw new ArgumentNullException(nameof(nestedObjectSerializer));
}
/// <summary>
/// Gets the visitor of type <typeparamref name="T" /> that was used during the pre-processing phase.
/// </summary>
/// <typeparam name="T">The type of the visitor.s</typeparam>
/// <returns></returns>
/// <exception cref="InvalidOperationException">
/// No visitor of that type has been registered,
/// or ore than one visitors registered are of type <typeparamref name="T"/>.
/// </exception>
public T GetPreProcessingPhaseObjectGraphVisitor<T>()
where T : IObjectGraphVisitor<Nothing>
{
return preProcessingPhaseVisitors
.OfType<T>()
.Single();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e2a250c3185b3314da07c8ab685d5325
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: e9ef080653208014a9cc235469180344
folderAsset: yes
timeCreated: 1427145262
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,70 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using System;
using YamlDotNet.Core;
namespace YamlDotNet.Serialization.EventEmitters
{
/// <summary>
/// Provided the base implementation for an IEventEmitter that is a
/// decorator for another IEventEmitter.
/// </summary>
public abstract class ChainedEventEmitter : IEventEmitter
{
protected readonly IEventEmitter nextEmitter;
protected ChainedEventEmitter(IEventEmitter nextEmitter)
{
this.nextEmitter = nextEmitter ?? throw new ArgumentNullException(nameof(nextEmitter));
}
public virtual void Emit(AliasEventInfo eventInfo, IEmitter emitter)
{
nextEmitter.Emit(eventInfo, emitter);
}
public virtual void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
{
nextEmitter.Emit(eventInfo, emitter);
}
public virtual void Emit(MappingStartEventInfo eventInfo, IEmitter emitter)
{
nextEmitter.Emit(eventInfo, emitter);
}
public virtual void Emit(MappingEndEventInfo eventInfo, IEmitter emitter)
{
nextEmitter.Emit(eventInfo, emitter);
}
public virtual void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter)
{
nextEmitter.Emit(eventInfo, emitter);
}
public virtual void Emit(SequenceEndEventInfo eventInfo, IEmitter emitter)
{
nextEmitter.Emit(eventInfo, emitter);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: ccb97ea0d5b5aff4d93a807363ef3ec7
timeCreated: 1427145266
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,126 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
namespace YamlDotNet.Serialization.EventEmitters
{
public sealed class JsonEventEmitter : ChainedEventEmitter
{
public JsonEventEmitter(IEventEmitter nextEmitter)
: base(nextEmitter)
{
}
public override void Emit(AliasEventInfo eventInfo, IEmitter emitter)
{
eventInfo.NeedsExpansion = true;
}
public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
{
eventInfo.IsPlainImplicit = true;
eventInfo.Style = ScalarStyle.Plain;
var value = eventInfo.Source.Value;
if (value == null)
{
eventInfo.RenderedValue = "null";
}
else
{
var typeCode = eventInfo.Source.Type.GetTypeCode();
switch (typeCode)
{
case TypeCode.Boolean:
eventInfo.RenderedValue = YamlFormatter.FormatBoolean(value);
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:
var valueIsEnum = eventInfo.Source.Type.IsEnum();
if (valueIsEnum)
{
eventInfo.RenderedValue = value.ToString()!;
eventInfo.Style = ScalarStyle.DoubleQuoted;
break;
}
eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
break;
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
break;
case TypeCode.String:
case TypeCode.Char:
eventInfo.RenderedValue = value.ToString()!;
eventInfo.Style = ScalarStyle.DoubleQuoted;
break;
case TypeCode.DateTime:
eventInfo.RenderedValue = YamlFormatter.FormatDateTime(value);
break;
case TypeCode.Empty:
eventInfo.RenderedValue = "null";
break;
default:
if (eventInfo.Source.Type == typeof(TimeSpan))
{
eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(value);
break;
}
throw new NotSupportedException($"TypeCode.{typeCode} is not supported.");
}
}
base.Emit(eventInfo, emitter);
}
public override void Emit(MappingStartEventInfo eventInfo, IEmitter emitter)
{
eventInfo.Style = MappingStyle.Flow;
base.Emit(eventInfo, emitter);
}
public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter)
{
eventInfo.Style = SequenceStyle.Flow;
base.Emit(eventInfo, emitter);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 62580a3e5f0e5314e83b62758257428e
timeCreated: 1427495771
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,155 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using System;
using System.Collections.Generic;
using YamlDotNet.Core;
using YamlDotNet.Serialization.Schemas;
namespace YamlDotNet.Serialization.EventEmitters
{
public sealed class TypeAssigningEventEmitter : ChainedEventEmitter
{
private readonly bool requireTagWhenStaticAndActualTypesAreDifferent;
private readonly IDictionary<Type, TagName> tagMappings;
public TypeAssigningEventEmitter(IEventEmitter nextEmitter, bool requireTagWhenStaticAndActualTypesAreDifferent, IDictionary<Type, TagName> tagMappings)
: base(nextEmitter)
{
this.requireTagWhenStaticAndActualTypesAreDifferent = requireTagWhenStaticAndActualTypesAreDifferent;
this.tagMappings = tagMappings ?? throw new ArgumentNullException(nameof(tagMappings));
}
public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
{
var suggestedStyle = ScalarStyle.Plain;
var value = eventInfo.Source.Value;
if (value == null)
{
eventInfo.Tag = JsonSchema.Tags.Null;
eventInfo.RenderedValue = "";
}
else
{
var typeCode = eventInfo.Source.Type.GetTypeCode();
switch (typeCode)
{
case TypeCode.Boolean:
eventInfo.Tag = JsonSchema.Tags.Bool;
eventInfo.RenderedValue = YamlFormatter.FormatBoolean(value);
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:
eventInfo.Tag = JsonSchema.Tags.Int;
eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
break;
case TypeCode.Single:
eventInfo.Tag = JsonSchema.Tags.Float;
eventInfo.RenderedValue = YamlFormatter.FormatNumber((float)value);
break;
case TypeCode.Double:
eventInfo.Tag = JsonSchema.Tags.Float;
eventInfo.RenderedValue = YamlFormatter.FormatNumber((double)value);
break;
case TypeCode.Decimal:
eventInfo.Tag = JsonSchema.Tags.Float;
eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
break;
case TypeCode.String:
case TypeCode.Char:
eventInfo.Tag = FailsafeSchema.Tags.Str;
eventInfo.RenderedValue = value.ToString()!;
suggestedStyle = ScalarStyle.Any;
break;
case TypeCode.DateTime:
eventInfo.Tag = DefaultSchema.Tags.Timestamp;
eventInfo.RenderedValue = YamlFormatter.FormatDateTime(value);
break;
case TypeCode.Empty:
eventInfo.Tag = JsonSchema.Tags.Null;
eventInfo.RenderedValue = "";
break;
default:
if (eventInfo.Source.Type == typeof(TimeSpan))
{
eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(value);
break;
}
throw new NotSupportedException($"TypeCode.{typeCode} is not supported.");
}
}
eventInfo.IsPlainImplicit = true;
if (eventInfo.Style == ScalarStyle.Any)
{
eventInfo.Style = suggestedStyle;
}
base.Emit(eventInfo, emitter);
}
public override void Emit(MappingStartEventInfo eventInfo, IEmitter emitter)
{
AssignTypeIfNeeded(eventInfo);
base.Emit(eventInfo, emitter);
}
public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter)
{
AssignTypeIfNeeded(eventInfo);
base.Emit(eventInfo, emitter);
}
private void AssignTypeIfNeeded(ObjectEventInfo eventInfo)
{
if (tagMappings.TryGetValue(eventInfo.Source.Type, out var tag))
{
eventInfo.Tag = tag;
}
else if (requireTagWhenStaticAndActualTypesAreDifferent && eventInfo.Source.Value != null && eventInfo.Source.Type != eventInfo.Source.StaticType)
{
throw new YamlException(
$"Cannot serialize type '{eventInfo.Source.Type.FullName}' where a '{eventInfo.Source.StaticType.FullName}' was expected "
+ $"because no tag mapping has been registered for '{eventInfo.Source.Type.FullName}', "
+ $"which means that it won't be possible to deserialize the document.\n"
+ $"Register a tag mapping using the SerializerBuilder.WithTagMapping method.\n\n"
+ $"E.g: builder.WithTagMapping(\"!{eventInfo.Source.Type.Name}\", typeof({eventInfo.Source.Type.FullName}));"
);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: e1b82bb57f4eb7f4ba0ffad467f82555
timeCreated: 1427145266
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,59 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
namespace YamlDotNet.Serialization.EventEmitters
{
public sealed class WriterEventEmitter : IEventEmitter
{
void IEventEmitter.Emit(AliasEventInfo eventInfo, IEmitter emitter)
{
emitter.Emit(new AnchorAlias(eventInfo.Alias));
}
void IEventEmitter.Emit(ScalarEventInfo eventInfo, IEmitter emitter)
{
emitter.Emit(new Scalar(eventInfo.Anchor, eventInfo.Tag, eventInfo.RenderedValue, eventInfo.Style, eventInfo.IsPlainImplicit, eventInfo.IsQuotedImplicit));
}
void IEventEmitter.Emit(MappingStartEventInfo eventInfo, IEmitter emitter)
{
emitter.Emit(new MappingStart(eventInfo.Anchor, eventInfo.Tag, eventInfo.IsImplicit, eventInfo.Style));
}
void IEventEmitter.Emit(MappingEndEventInfo eventInfo, IEmitter emitter)
{
emitter.Emit(new MappingEnd());
}
void IEventEmitter.Emit(SequenceStartEventInfo eventInfo, IEmitter emitter)
{
emitter.Emit(new SequenceStart(eventInfo.Anchor, eventInfo.Tag, eventInfo.IsImplicit, eventInfo.Style));
}
void IEventEmitter.Emit(SequenceEndEventInfo eventInfo, IEmitter emitter)
{
emitter.Emit(new SequenceEnd());
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f2ea019325fb629499443d6af969a295
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,117 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using System;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
namespace YamlDotNet.Serialization
{
public abstract class EventInfo
{
public IObjectDescriptor Source { get; }
protected EventInfo(IObjectDescriptor source)
{
Source = source ?? throw new ArgumentNullException(nameof(source));
}
}
public class AliasEventInfo : EventInfo
{
public AliasEventInfo(IObjectDescriptor source, AnchorName alias)
: base(source)
{
if (alias.IsEmpty)
{
throw new ArgumentNullException(nameof(alias));
}
Alias = alias;
}
public AnchorName Alias { get; }
public bool NeedsExpansion { get; set; }
}
public class ObjectEventInfo : EventInfo
{
protected ObjectEventInfo(IObjectDescriptor source)
: base(source)
{
}
public AnchorName Anchor { get; set; }
public TagName Tag { get; set; }
}
public sealed class ScalarEventInfo : ObjectEventInfo
{
public ScalarEventInfo(IObjectDescriptor source)
: base(source)
{
Style = source.ScalarStyle;
RenderedValue = string.Empty;
}
public string RenderedValue { get; set; }
public ScalarStyle Style { get; set; }
public bool IsPlainImplicit { get; set; }
public bool IsQuotedImplicit { get; set; }
}
public sealed class MappingStartEventInfo : ObjectEventInfo
{
public MappingStartEventInfo(IObjectDescriptor source)
: base(source)
{
}
public bool IsImplicit { get; set; }
public MappingStyle Style { get; set; }
}
public sealed class MappingEndEventInfo : EventInfo
{
public MappingEndEventInfo(IObjectDescriptor source)
: base(source)
{
}
}
public sealed class SequenceStartEventInfo : ObjectEventInfo
{
public SequenceStartEventInfo(IObjectDescriptor source)
: base(source)
{
}
public bool IsImplicit { get; set; }
public SequenceStyle Style { get; set; }
}
public sealed class SequenceEndEventInfo : EventInfo
{
public SequenceEndEventInfo(IObjectDescriptor source)
: base(source)
{
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: e72f96c4049a0dc4d97ad0d44f6b766d
timeCreated: 1427145267
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,30 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// 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.
using YamlDotNet.Core;
namespace YamlDotNet.Serialization
{
public interface IAliasProvider
{
AnchorName GetAlias(object target);
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 3a934679d1d7f204e9d25f18f7f397bb
timeCreated: 1427145263
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Some files were not shown because too many files have changed in this diff Show More