RSCG – VYaml
| name | VYaml |
| nuget | https://www.nuget.org/packages/VYaml/ |
| link | https://github.com/hadashiA/VYaml |
| author | Hadashi A |
Serializing to/from YAML format
This is how you can use VYaml .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="VYaml" Version="1.2.0" />
<PackageReference Include="VYaml.Annotations" Version="1.2.0" />
</ItemGroup>
</Project>
The code that you will use is
using SerializerDemo;
using VYaml.Serialization;
var p= new Person() { Name= "Andrei Ignat" , Age=55};
var utf8Yaml = YamlSerializer.SerializeToString(p);
Console.WriteLine(utf8Yaml);
var p1 = YamlSerializer.Serialize<Person>(p);
var p2 = YamlSerializer.Deserialize<Person>(p1);
Console.WriteLine(p2.Name);
Console.WriteLine(p2.Age);
using VYaml.Annotations;
namespace SerializerDemo;
[YamlObject]
public partial class Person
{
public int Age { get; set; }
public string Name { get; set; } = string.Empty;
}
The code that is generated is
// <auto-generated />
#nullable enable
#pragma warning disable CS0162 // Unreachable code
#pragma warning disable CS0219 // Variable assigned but never used
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
#pragma warning disable CS8601 // Possible null reference assignment
#pragma warning disable CS8602 // Possible null return
#pragma warning disable CS8604 // Possible null reference argument for parameter
#pragma warning disable CS8619 // Possible null reference assignment fix
#pragma warning disable CS8631 // The type cannot be used as type parameter in the generic type or method
using System;
using VYaml.Annotations;
using VYaml.Parser;
using VYaml.Emitter;
using VYaml.Serialization;
namespace SerializerDemo
{
partial class Person
{
[VYaml.Annotations.Preserve]
public static void __RegisterVYamlFormatter()
{
global::VYaml.Serialization.GeneratedResolver.Register(new PersonGeneratedFormatter());
}
[VYaml.Annotations.Preserve]
public class PersonGeneratedFormatter : IYamlFormatter<global::SerializerDemo.Person?>
{
static readonly byte[] AgeKeyUtf8Bytes = { 97, 103, 101 }; // age
static readonly byte[] NameKeyUtf8Bytes = { 110, 97, 109, 101 }; // name
[VYaml.Annotations.Preserve]
public void Serialize(ref Utf8YamlEmitter emitter, global::SerializerDemo.Person? value, YamlSerializationContext context)
{
if (value is null)
{
emitter.WriteNull();
return;
}
emitter.BeginMapping();
if (context.Options.NamingConvention == global::VYaml.Annotations.NamingConvention.LowerCamelCase)
{
emitter.WriteScalar(AgeKeyUtf8Bytes);
}
else
{
global::VYaml.Serialization.NamingConventionMutator.MutateToThreadStaticBufferUtf8(AgeKeyUtf8Bytes, context.Options.NamingConvention, out var mutated, out var written);
emitter.WriteScalar(mutated.AsSpan(0, written));
}
context.Serialize(ref emitter, value.Age);
if (context.Options.NamingConvention == global::VYaml.Annotations.NamingConvention.LowerCamelCase)
{
emitter.WriteScalar(NameKeyUtf8Bytes);
}
else
{
global::VYaml.Serialization.NamingConventionMutator.MutateToThreadStaticBufferUtf8(NameKeyUtf8Bytes, context.Options.NamingConvention, out var mutated, out var written);
emitter.WriteScalar(mutated.AsSpan(0, written));
}
context.Serialize(ref emitter, value.Name);
emitter.EndMapping();
}
[VYaml.Annotations.Preserve]
public global::SerializerDemo.Person? Deserialize(ref YamlParser parser, YamlDeserializationContext context)
{
if (parser.IsNullScalar())
{
parser.Read();
return default;
}
parser.ReadWithVerify(ParseEventType.MappingStart);
var __Age__ = default(int);
var __Name__ = default(string);
while (!parser.End && parser.CurrentEventType != ParseEventType.MappingEnd)
{
if (parser.CurrentEventType != ParseEventType.Scalar)
{
throw new YamlSerializerException(parser.CurrentMark, "Custom type deserialization supports only string key");
}
if (!parser.TryGetScalarAsSpan(out var key))
{
throw new YamlSerializerException(parser.CurrentMark, "Custom type deserialization supports only string key");
}
if (context.Options.NamingConvention != global::VYaml.Annotations.NamingConvention.LowerCamelCase)
{
global::VYaml.Serialization.NamingConventionMutator.MutateToThreadStaticBufferUtf8(key, global::VYaml.Annotations.NamingConvention.LowerCamelCase, out var mutated, out var written);
key = mutated.AsSpan(0, written);
}
switch (key.Length)
{
case 3:
if (key.SequenceEqual(AgeKeyUtf8Bytes))
{
parser.Read(); // skip key
__Age__ = context.DeserializeWithAlias<int>(ref parser);
continue;
}
goto default;
case 4:
if (key.SequenceEqual(NameKeyUtf8Bytes))
{
parser.Read(); // skip key
__Name__ = context.DeserializeWithAlias<string>(ref parser);
continue;
}
goto default;
default:
parser.Read(); // skip key
parser.SkipCurrentNode(); // skip value
continue;
}
}
parser.ReadWithVerify(ParseEventType.MappingEnd);
return new Person
{
Age = __Age__,
Name = __Name__,
}
;
}
}
}
}
#pragma warning restore CS0162 // Unreachable code
#pragma warning restore CS0219 // Variable assigned but never used
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
#pragma warning restore CS8601 // Possible null reference assignment
#pragma warning restore CS8602 // Possible null return
#pragma warning restore CS8604 // Possible null reference argument for parameter
#pragma warning restore CS8631 // The type cannot be used as type parameter in the generic type or method
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/VYaml