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

View File

@@ -0,0 +1,32 @@
// 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.
namespace YamlDotNet.Serialization.Utilities
{
/// <summary>
/// Indicates that a class used as deserialization state
/// needs to be notified after deserialization.
/// </summary>
public interface IPostDeserializationCallback
{
void OnDeserialization();
}
}

View File

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

View File

@@ -0,0 +1,75 @@
// 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.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using YamlDotNet.Core;
namespace YamlDotNet.Serialization.Utilities
{
internal sealed class ObjectAnchorCollection
{
private readonly IDictionary<string, object> objectsByAnchor = new Dictionary<string, object>();
private readonly IDictionary<object, string> anchorsByObject = new Dictionary<object, string>();
/// <summary>
/// Adds the specified anchor.
/// </summary>
/// <param name="anchor">The anchor.</param>
/// <param name="object">The @object.</param>
public void Add(string anchor, object @object)
{
objectsByAnchor.Add(anchor, @object);
if (@object != null)
{
anchorsByObject.Add(@object, anchor);
}
}
/// <summary>
/// Gets the anchor for the specified object.
/// </summary>
/// <param name="object">The object.</param>
/// <param name="anchor">The anchor.</param>
/// <returns></returns>
public bool TryGetAnchor(object @object, [MaybeNullWhen(false)] out string? anchor)
{
return anchorsByObject.TryGetValue(@object, out anchor);
}
/// <summary>
/// Gets the <see cref="object"/> with the specified anchor.
/// </summary>
/// <value></value>
public object this[string anchor]
{
get
{
if (objectsByAnchor.TryGetValue(anchor, out var value))
{
return value;
}
throw new AnchorNotFoundException($"The anchor '{anchor}' does not exists");
}
}
}
}

View File

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

View File

@@ -0,0 +1,54 @@
// 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;
namespace YamlDotNet.Serialization.Utilities
{
internal static class ReflectionUtility
{
public static Type? GetImplementedGenericInterface(Type type, Type genericInterfaceType)
{
foreach (var interfacetype in GetImplementedInterfaces(type))
{
if (interfacetype.IsGenericType() && interfacetype.GetGenericTypeDefinition() == genericInterfaceType)
{
return interfacetype;
}
}
return null;
}
public static IEnumerable<Type> GetImplementedInterfaces(Type type)
{
if (type.IsInterface())
{
yield return type;
}
foreach (var implementedInterface in type.GetInterfaces())
{
yield return implementedInterface;
}
}
}
}

View File

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

View File

@@ -0,0 +1,67 @@
// 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;
namespace YamlDotNet.Serialization.Utilities
{
/// <summary>
/// A generic container that is preserved during the entire deserialization process.
/// Any disposable object added to this collection will be disposed when this object is disposed.
/// </summary>
public sealed class SerializerState : IDisposable
{
private readonly IDictionary<Type, object> items = new Dictionary<Type, object>();
public T Get<T>()
where T : class, new()
{
if (!items.TryGetValue(typeof(T), out var value))
{
value = new T();
items.Add(typeof(T), value);
}
return (T)value;
}
/// <summary>
/// Invokes <see cref="IPostDeserializationCallback.OnDeserialization" /> on all
/// objects added to this collection that implement <see cref="IPostDeserializationCallback" />.
/// </summary>
public void OnDeserialization()
{
foreach (var callback in items.Values.OfType<IPostDeserializationCallback>())
{
callback.OnDeserialization();
}
}
public void Dispose()
{
foreach (var disposable in items.Values.OfType<IDisposable>())
{
disposable.Dispose();
}
}
}
}

View File

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

View File

@@ -0,0 +1,79 @@
// 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.Text.RegularExpressions;
namespace YamlDotNet.Serialization.Utilities
{
/// <summary>
/// Various string extension methods
/// </summary>
internal static class StringExtensions
{
private static string ToCamelOrPascalCase(string str, Func<char, char> firstLetterTransform)
{
var text = Regex.Replace(str, "([_\\-])(?<char>[a-z])", match => match.Groups["char"].Value.ToUpperInvariant(), RegexOptions.IgnoreCase);
return firstLetterTransform(text[0]) + text.Substring(1);
}
/// <summary>
/// Convert the string with underscores (this_is_a_test) or hyphens (this-is-a-test) to
/// camel case (thisIsATest). Camel case is the same as Pascal case, except the first letter
/// is lowercase.
/// </summary>
/// <param name="str">String to convert</param>
/// <returns>Converted string</returns>
public static string ToCamelCase(this string str)
{
return ToCamelOrPascalCase(str, char.ToLowerInvariant);
}
/// <summary>
/// Convert the string with underscores (this_is_a_test) or hyphens (this-is-a-test) to
/// pascal case (ThisIsATest). Pascal case is the same as camel case, except the first letter
/// is uppercase.
/// </summary>
/// <param name="str">String to convert</param>
/// <returns>Converted string</returns>
public static string ToPascalCase(this string str)
{
return ToCamelOrPascalCase(str, char.ToUpperInvariant);
}
/// <summary>
/// Convert the string from camelcase (thisIsATest) to a hyphenated (this-is-a-test) or
/// underscored (this_is_a_test) string
/// </summary>
/// <param name="str">String to convert</param>
/// <param name="separator">Separator to use between segments</param>
/// <returns>Converted string</returns>
public static string FromCamelCase(this string str, string separator)
{
// Ensure first letter is always lowercase
str = char.ToLower(str[0]) + str.Substring(1);
str = Regex.Replace(str.ToCamelCase(), "(?<char>[A-Z])", match => separator + match.Groups["char"].Value.ToLowerInvariant());
return str;
}
}
}

View File

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

View File

@@ -0,0 +1,236 @@
// 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.
// Remarks: This file is imported from the SixPack library. This is ok because
// the copyright holder has agreed to redistribute this file under the license
// used in YamlDotNet.
using System;
using System.Globalization;
using System.Reflection;
using System.ComponentModel;
namespace YamlDotNet.Serialization.Utilities
{
/// <summary>
/// Performs type conversions using every standard provided by the .NET library.
/// </summary>
public static partial class TypeConverter
{
/// <summary>
/// Converts the specified value.
/// </summary>
/// <typeparam name="T">The type to which the value is to be converted.</typeparam>
/// <param name="value">The value to convert.</param>
/// <returns></returns>
public static T ChangeType<T>(object? value)
{
return (T)ChangeType(value, typeof(T))!; // This cast should always be valid
}
/// <summary>
/// Converts the specified value.
/// </summary>
/// <typeparam name="T">The type to which the value is to be converted.</typeparam>
/// <param name="value">The value to convert.</param>
/// <param name="provider">The provider.</param>
/// <returns></returns>
public static T ChangeType<T>(object? value, IFormatProvider provider)
{
return (T)ChangeType(value, typeof(T), provider)!; // This cast should always be valid
}
/// <summary>
/// Converts the specified value.
/// </summary>
/// <typeparam name="T">The type to which the value is to be converted.</typeparam>
/// <param name="value">The value to convert.</param>
/// <param name="culture">The culture.</param>
/// <returns></returns>
public static T ChangeType<T>(object? value, CultureInfo culture)
{
return (T)ChangeType(value, typeof(T), culture)!; // This cast should always be valid
}
/// <summary>
/// Converts the specified value using the invariant culture.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <param name="destinationType">The type to which the value is to be converted.</param>
/// <returns></returns>
public static object? ChangeType(object? value, Type destinationType)
{
return ChangeType(value, destinationType, CultureInfo.InvariantCulture);
}
/// <summary>
/// Converts the specified value.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <param name="destinationType">The type to which the value is to be converted.</param>
/// <param name="provider">The format provider.</param>
/// <returns></returns>
public static object? ChangeType(object? value, Type destinationType, IFormatProvider provider)
{
return ChangeType(value, destinationType, new CultureInfoAdapter(CultureInfo.CurrentCulture, provider));
}
/// <summary>
/// Converts the specified value.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <param name="destinationType">The type to which the value is to be converted.</param>
/// <param name="culture">The culture.</param>
/// <returns></returns>
public static object? ChangeType(object? value, Type destinationType, CultureInfo culture)
{
// Handle null and DBNull
if (value == null || value.IsDbNull())
{
return destinationType.IsValueType() ? Activator.CreateInstance(destinationType) : null;
}
var sourceType = value.GetType();
// If the source type is compatible with the destination type, no conversion is needed
if (destinationType == sourceType || destinationType.IsAssignableFrom(sourceType))
{
return value;
}
// Nullable types get a special treatment
if (destinationType.IsGenericType())
{
var genericTypeDefinition = destinationType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
var innerType = destinationType.GetGenericArguments()[0];
var convertedValue = ChangeType(value, innerType, culture);
return Activator.CreateInstance(destinationType, convertedValue);
}
}
// Enums also require special handling
if (destinationType.IsEnum())
{
return value is string valueText
? Enum.Parse(destinationType, valueText, true)
: value;
}
// Special case for booleans to support parsing "1" and "0". This is
// necessary for compatibility with XML Schema.
if (destinationType == typeof(bool))
{
if ("0".Equals(value))
{
return false;
}
if ("1".Equals(value))
{
return true;
}
}
// Try with the source type's converter
var sourceConverter = TypeDescriptor.GetConverter(sourceType);
if (sourceConverter != null && sourceConverter.CanConvertTo(destinationType))
{
return sourceConverter.ConvertTo(null, culture, value, destinationType);
}
// Try with the destination type's converter
var destinationConverter = TypeDescriptor.GetConverter(destinationType);
if (destinationConverter != null && destinationConverter.CanConvertFrom(sourceType))
{
return destinationConverter.ConvertFrom(null, culture, value);
}
// Try to find a casting operator in the source or destination type
foreach (var type in new[] { sourceType, destinationType })
{
foreach (var method in type.GetPublicStaticMethods())
{
var isCastingOperator =
method.IsSpecialName &&
(method.Name == "op_Implicit" || method.Name == "op_Explicit") &&
destinationType.IsAssignableFrom(method.ReturnParameter.ParameterType);
if (isCastingOperator)
{
var parameters = method.GetParameters();
var isCompatible =
parameters.Length == 1 &&
parameters[0].ParameterType.IsAssignableFrom(sourceType);
if (isCompatible)
{
try
{
return method.Invoke(null, new[] { value });
}
catch (TargetInvocationException ex)
{
throw ex.Unwrap();
}
}
}
}
}
// If source type is string, try to find a Parse or TryParse method
if (sourceType == typeof(string))
{
try
{
// Try with - public static T Parse(string, IFormatProvider)
var parseMethod = destinationType.GetPublicStaticMethod("Parse", typeof(string), typeof(IFormatProvider));
if (parseMethod != null)
{
return parseMethod.Invoke(null, new object[] { value, culture });
}
// Try with - public static T Parse(string)
parseMethod = destinationType.GetPublicStaticMethod("Parse", typeof(string));
if (parseMethod != null)
{
return parseMethod.Invoke(null, new object[] { value });
}
}
catch (TargetInvocationException ex)
{
throw ex.Unwrap();
}
}
// Handle TimeSpan
if (destinationType == typeof(TimeSpan))
{
return TimeSpan.Parse((string)ChangeType(value, typeof(string), CultureInfo.InvariantCulture)!);
}
// Default to the Convert class
return Convert.ChangeType(value, destinationType, CultureInfo.InvariantCulture);
}
}
}

View File

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