Skip to content

Commit

Permalink
Merge pull request #55 from hadashiA/ku/fix-int64
Browse files Browse the repository at this point in the history
Fix deserialization problem as int64
  • Loading branch information
hadashiA authored Oct 31, 2023
2 parents 239d6a9 + c470fd0 commit bca52b1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion VYaml.Core/Parser/Scalar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions VYaml.Tests/Serialization/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dynamic>(yamlBytes);
Assert.That(documents.Count, Is.EqualTo(3));
Assert.That(documents["id1"], Is.InstanceOf<long>());
Assert.That(documents["id1"], Is.EqualTo(8083928222794209684));
Assert.That(documents["id2"], Is.InstanceOf<int>());
Assert.That(documents["id3"], Is.InstanceOf<double>());
}
}
}
2 changes: 1 addition & 1 deletion VYaml.Unity/Assets/VYaml/Runtime/Parser/Scalar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions VYaml.Unity/Assets/VYaml/Tests/Serialization/SerializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dynamic>(yamlBytes);
Assert.That(documents.Count, Is.EqualTo(3));
Assert.That(documents["id1"], Is.InstanceOf<long>());
Assert.That(documents["id1"], Is.EqualTo(8083928222794209684));
Assert.That(documents["id2"], Is.InstanceOf<int>());
Assert.That(documents["id3"], Is.InstanceOf<double>());
}
}
}

0 comments on commit bca52b1

Please sign in to comment.