Skip to content

Commit

Permalink
Update unity assets
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed May 17, 2024
1 parent 81abc25 commit bc05817
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Buffers;
using System.Text;
using VYaml.Emitter;
using VYaml.Parser;

Expand Down Expand Up @@ -34,5 +35,81 @@ public void Serialize(ref Utf8YamlEmitter emitter, byte[]? value, YamlSerializat
return Convert.FromBase64String(str!);
}
}

public class ByteMemoryFormatter : IYamlFormatter<Memory<byte>>
{
public static readonly ByteMemoryFormatter Instance = new();

public void Serialize(ref Utf8YamlEmitter emitter, Memory<byte> value, YamlSerializationContext context)
{
emitter.WriteString(
Convert.ToBase64String(value.Span, Base64FormattingOptions.None),
ScalarStyle.Plain);
}

public Memory<byte> Deserialize(ref YamlParser parser, YamlDeserializationContext context)
{
var str = parser.ReadScalarAsString();
return Convert.FromBase64String(str!);
}
}

public class ByteReadOnlyMemoryFormatter : IYamlFormatter<ReadOnlyMemory<byte>>
{
public static readonly ByteReadOnlyMemoryFormatter Instance = new();

public void Serialize(ref Utf8YamlEmitter emitter, ReadOnlyMemory<byte> value, YamlSerializationContext context)
{
emitter.WriteString(
Convert.ToBase64String(value.Span, Base64FormattingOptions.None),
ScalarStyle.Plain);
}

public ReadOnlyMemory<byte> Deserialize(ref YamlParser parser, YamlDeserializationContext context)
{
var str = parser.ReadScalarAsString();
return Convert.FromBase64String(str!);
}
}

public class ByteReadOnlySequenceFormatter : IYamlFormatter<ReadOnlySequence<byte>>
{
public static readonly ByteReadOnlySequenceFormatter Instance = new();

public void Serialize(ref Utf8YamlEmitter emitter, ReadOnlySequence<byte> value, YamlSerializationContext context)
{
var builder = new StringBuilder((int)value.Length);
foreach (var segment in value)
{
builder.Append(Convert.ToBase64String(segment.Span, Base64FormattingOptions.None));
}
emitter.WriteString(builder.ToString(), ScalarStyle.Plain);
}

public ReadOnlySequence<byte> Deserialize(ref YamlParser parser, YamlDeserializationContext context)
{
var str = parser.ReadScalarAsString();
var bytes = Convert.FromBase64String(str!);
return new ReadOnlySequence<byte>(bytes);
}
}

public class ByteArraySegmentFormatter : IYamlFormatter<ArraySegment<byte>>
{
public static readonly ByteArraySegmentFormatter Instance = new();

public void Serialize(ref Utf8YamlEmitter emitter, ArraySegment<byte> value, YamlSerializationContext context)
{
emitter.WriteString(
Convert.ToBase64String(value, Base64FormattingOptions.None),
ScalarStyle.Plain);
}

public ArraySegment<byte> Deserialize(ref YamlParser parser, YamlDeserializationContext context)
{
var str = parser.ReadScalarAsString();
return Convert.FromBase64String(str!);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Numerics;
using VYaml.Emitter;
using VYaml.Parser;

namespace VYaml.Serialization
{
public class BigIntegerFormatter : IYamlFormatter<BigInteger>
{
public static readonly BigIntegerFormatter Instance = new();

public void Serialize(ref Utf8YamlEmitter emitter, BigInteger value, YamlSerializationContext context)
{
emitter.WriteString(value.ToString());
}

public BigInteger Deserialize(ref YamlParser parser, YamlDeserializationContext context)
{
if (parser.IsNullScalar())
{
return default;
}

var stringValue = parser.ReadScalarAsString();
return BigInteger.Parse(stringValue!);
}
}

public class ComplexFormatter : IYamlFormatter<Complex>
{
public static readonly ComplexFormatter Instance = new();

public void Serialize(ref Utf8YamlEmitter emitter, Complex value, YamlSerializationContext context)
{
emitter.WriteString($"{value.Real}+{value.Imaginary}i");
}

public Complex Deserialize(ref YamlParser parser, YamlDeserializationContext context)
{
if (parser.IsNullScalar())
{
return default;
}
var stringValue = parser.ReadScalarAsString()!;
var separatorIndex = stringValue.IndexOf('+');
var real = double.Parse(stringValue.AsSpan(0, separatorIndex));
var imaginary = double.Parse(stringValue.AsSpan(separatorIndex + 1, stringValue.Length - separatorIndex - 2));
return new Complex(real, imaginary);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#nullable enable
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace VYaml.Serialization
{
Expand Down Expand Up @@ -99,19 +98,19 @@ static FormatterCache()
{ typeof(object[]), new ArrayFormatter<object>() },
{ typeof(List<object>), new ListFormatter<object>() },

// { typeof(Memory<byte>), ByteMemoryFormatter.Instance },
// { typeof(Memory<byte>?), new StaticNullableFormatter<Memory<byte>>(ByteMemoryFormatter.Instance) },
// { typeof(ReadOnlyMemory<byte>), ByteReadOnlyMemoryFormatter.Instance },
// { typeof(ReadOnlyMemory<byte>?), new StaticNullableFormatter<ReadOnlyMemory<byte>>(ByteReadOnlyMemoryFormatter.Instance) },
// { typeof(ReadOnlySequence<byte>), ByteReadOnlySequenceFormatter.Instance },
// { typeof(ReadOnlySequence<byte>?), new StaticNullableFormatter<ReadOnlySequence<byte>>(ByteReadOnlySequenceFormatter.Instance) },
// { typeof(ArraySegment<byte>), ByteArraySegmentFormatter.Instance },
// { typeof(ArraySegment<byte>?), new StaticNullableFormatter<ArraySegment<byte>>(ByteArraySegmentFormatter.Instance) },

// { typeof(System.Numerics.BigInteger), BigIntegerFormatter.Instance },
// { typeof(System.Numerics.BigInteger?), new StaticNullableFormatter<System.Numerics.BigInteger>(BigIntegerFormatter.Instance) },
// { typeof(System.Numerics.Complex), ComplexFormatter.Instance },
// { typeof(System.Numerics.Complex?), new StaticNullableFormatter<System.Numerics.Complex>(ComplexFormatter.Instance) },
{ typeof(Memory<byte>), ByteMemoryFormatter.Instance },
{ typeof(Memory<byte>?), new StaticNullableFormatter<Memory<byte>>(ByteMemoryFormatter.Instance) },
{ typeof(ReadOnlyMemory<byte>), ByteReadOnlyMemoryFormatter.Instance },
{ typeof(ReadOnlyMemory<byte>?), new StaticNullableFormatter<ReadOnlyMemory<byte>>(ByteReadOnlyMemoryFormatter.Instance) },
{ typeof(ReadOnlySequence<byte>), ByteReadOnlySequenceFormatter.Instance },
{ typeof(ReadOnlySequence<byte>?), new StaticNullableFormatter<ReadOnlySequence<byte>>(ByteReadOnlySequenceFormatter.Instance) },
{ typeof(ArraySegment<byte>), ByteArraySegmentFormatter.Instance },
{ typeof(ArraySegment<byte>?), new StaticNullableFormatter<ArraySegment<byte>>(ByteArraySegmentFormatter.Instance) },

{ typeof(System.Numerics.BigInteger), BigIntegerFormatter.Instance },
{ typeof(System.Numerics.BigInteger?), new StaticNullableFormatter<System.Numerics.BigInteger>(BigIntegerFormatter.Instance) },
{ typeof(System.Numerics.Complex), ComplexFormatter.Instance },
{ typeof(System.Numerics.Complex?), new StaticNullableFormatter<System.Numerics.Complex>(ComplexFormatter.Instance) },
};

public static readonly Dictionary<Type, Type> KnownGenericTypes = new()
Expand Down
Binary file modified VYaml.Unity/Assets/VYaml/Runtime/VYaml.SourceGenerator.dll
Binary file not shown.

0 comments on commit bc05817

Please sign in to comment.