From c470fd0b6a9eaa821a045abbafd79c99f479f326 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Tue, 31 Oct 2023 10:13:27 +0900 Subject: [PATCH] Fix deserialization problem as int64 --- VYaml.Core/Parser/Scalar.cs | 2 +- .../Formatters/PrimitiveObjectFormatter.cs | 5 +++++ VYaml.Tests/Serialization/SerializerTest.cs | 14 ++++++++++++++ VYaml.Unity/Assets/VYaml/Runtime/Parser/Scalar.cs | 2 +- .../Formatters/PrimitiveObjectFormatter.cs | 5 +++++ .../VYaml/Tests/Serialization/SerializerTest.cs | 14 ++++++++++++++ 6 files changed, 40 insertions(+), 2 deletions(-) diff --git a/VYaml.Core/Parser/Scalar.cs b/VYaml.Core/Parser/Scalar.cs index c647665..a5add8b 100644 --- a/VYaml.Core/Parser/Scalar.cs +++ b/VYaml.Core/Parser/Scalar.cs @@ -214,7 +214,7 @@ public bool TryGetInt32(out int value) public bool TryGetInt64(out long value) { - var span = buffer.AsSpan(); + var span = AsSpan(); if (Utf8Parser.TryParse(span, out value, out var bytesConsumed) && bytesConsumed == span.Length) { diff --git a/VYaml.Core/Serialization/Formatters/PrimitiveObjectFormatter.cs b/VYaml.Core/Serialization/Formatters/PrimitiveObjectFormatter.cs index af17e1a..4844cab 100644 --- a/VYaml.Core/Serialization/Formatters/PrimitiveObjectFormatter.cs +++ b/VYaml.Core/Serialization/Formatters/PrimitiveObjectFormatter.cs @@ -148,6 +148,11 @@ public void Serialize(ref Utf8YamlEmitter emitter, object? value, YamlSerializat parser.Read(); result = intValue; } + else if (parser.TryGetScalarAsInt64(out var int64Value)) + { + parser.Read(); + result = int64Value; + } else if (parser.TryGetScalarAsDouble(out var doubleValue)) { parser.Read(); diff --git a/VYaml.Tests/Serialization/SerializerTest.cs b/VYaml.Tests/Serialization/SerializerTest.cs index 57a2d74..1d3c7c3 100644 --- a/VYaml.Tests/Serialization/SerializerTest.cs +++ b/VYaml.Tests/Serialization/SerializerTest.cs @@ -18,5 +18,19 @@ public void DeserializeMultipleDocuments() Assert.That(documents[1]["Warning"], Is.EqualTo("A slightly different error message.")); Assert.That(documents[2]["Fatal"], Is.EqualTo("Unknown variable \"bar\"")); } + + [Test] + public void DeserializeHighDigitNumber() + { + var yamlBytes = StringEncoding.Utf8.GetBytes("id1: 8083928222794209684\n" + + "id2: 123\n" + + "id3: 8083928222794209684.123456789\n"); + var documents = YamlSerializer.Deserialize(yamlBytes); + Assert.That(documents.Count, Is.EqualTo(3)); + Assert.That(documents["id1"], Is.InstanceOf()); + Assert.That(documents["id1"], Is.EqualTo(8083928222794209684)); + Assert.That(documents["id2"], Is.InstanceOf()); + Assert.That(documents["id3"], Is.InstanceOf()); + } } } \ No newline at end of file diff --git a/VYaml.Unity/Assets/VYaml/Runtime/Parser/Scalar.cs b/VYaml.Unity/Assets/VYaml/Runtime/Parser/Scalar.cs index c647665..a5add8b 100644 --- a/VYaml.Unity/Assets/VYaml/Runtime/Parser/Scalar.cs +++ b/VYaml.Unity/Assets/VYaml/Runtime/Parser/Scalar.cs @@ -214,7 +214,7 @@ public bool TryGetInt32(out int value) public bool TryGetInt64(out long value) { - var span = buffer.AsSpan(); + var span = AsSpan(); if (Utf8Parser.TryParse(span, out value, out var bytesConsumed) && bytesConsumed == span.Length) { diff --git a/VYaml.Unity/Assets/VYaml/Runtime/Serialization/Formatters/PrimitiveObjectFormatter.cs b/VYaml.Unity/Assets/VYaml/Runtime/Serialization/Formatters/PrimitiveObjectFormatter.cs index af17e1a..4844cab 100644 --- a/VYaml.Unity/Assets/VYaml/Runtime/Serialization/Formatters/PrimitiveObjectFormatter.cs +++ b/VYaml.Unity/Assets/VYaml/Runtime/Serialization/Formatters/PrimitiveObjectFormatter.cs @@ -148,6 +148,11 @@ public void Serialize(ref Utf8YamlEmitter emitter, object? value, YamlSerializat parser.Read(); result = intValue; } + else if (parser.TryGetScalarAsInt64(out var int64Value)) + { + parser.Read(); + result = int64Value; + } else if (parser.TryGetScalarAsDouble(out var doubleValue)) { parser.Read(); diff --git a/VYaml.Unity/Assets/VYaml/Tests/Serialization/SerializerTest.cs b/VYaml.Unity/Assets/VYaml/Tests/Serialization/SerializerTest.cs index 57a2d74..1d3c7c3 100644 --- a/VYaml.Unity/Assets/VYaml/Tests/Serialization/SerializerTest.cs +++ b/VYaml.Unity/Assets/VYaml/Tests/Serialization/SerializerTest.cs @@ -18,5 +18,19 @@ public void DeserializeMultipleDocuments() Assert.That(documents[1]["Warning"], Is.EqualTo("A slightly different error message.")); Assert.That(documents[2]["Fatal"], Is.EqualTo("Unknown variable \"bar\"")); } + + [Test] + public void DeserializeHighDigitNumber() + { + var yamlBytes = StringEncoding.Utf8.GetBytes("id1: 8083928222794209684\n" + + "id2: 123\n" + + "id3: 8083928222794209684.123456789\n"); + var documents = YamlSerializer.Deserialize(yamlBytes); + Assert.That(documents.Count, Is.EqualTo(3)); + Assert.That(documents["id1"], Is.InstanceOf()); + Assert.That(documents["id1"], Is.EqualTo(8083928222794209684)); + Assert.That(documents["id2"], Is.InstanceOf()); + Assert.That(documents["id3"], Is.InstanceOf()); + } } } \ No newline at end of file