Import YamlDotNet.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ccb97ea0d5b5aff4d93a807363ef3ec7
|
||||
timeCreated: 1427145266
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 62580a3e5f0e5314e83b62758257428e
|
||||
timeCreated: 1427495771
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -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}));"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1b82bb57f4eb7f4ba0ffad467f82555
|
||||
timeCreated: 1427145266
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f2ea019325fb629499443d6af969a295
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user