diff --git a/src/Nethermind/Nethermind.Core.Test/Json/DoubleArrayConverterTests.cs b/src/Nethermind/Nethermind.Core.Test/Json/DoubleArrayConverterTests.cs new file mode 100644 index 00000000000..d177b45f40d --- /dev/null +++ b/src/Nethermind/Nethermind.Core.Test/Json/DoubleArrayConverterTests.cs @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited +// SPDX-License-Identifier: LGPL-3.0-only + +using System; +using System.Text.Json; +using Nethermind.Serialization.Json; + +using NUnit.Framework; + +namespace Nethermind.Core.Test.Json; + +[TestFixture] +public class DoubleArrayConverterTests : ConverterTestBase +{ + static readonly DoubleArrayConverter converter = new(); + + [Test] + public void Test_roundtrip() + { + TestConverter(new double[] { -0.5, 0.5, 1.0, 1.5, 2.0, 2.5 }, (a, b) => a.AsSpan().SequenceEqual(b), converter); + TestConverter(new double[] { 1, 1, 1, 1 }, (a, b) => a.AsSpan().SequenceEqual(b), converter); + TestConverter(new double[] { 0, 0, 0, 0 }, (a, b) => a.AsSpan().SequenceEqual(b), converter); + TestConverter(Array.Empty(), (a, b) => a.AsSpan().SequenceEqual(b), converter); + } +} diff --git a/src/Nethermind/Nethermind.Serialization.Json/DoubleConverter.cs b/src/Nethermind/Nethermind.Serialization.Json/DoubleConverter.cs index d62578904da..044a661a8d2 100644 --- a/src/Nethermind/Nethermind.Serialization.Json/DoubleConverter.cs +++ b/src/Nethermind/Nethermind.Serialization.Json/DoubleConverter.cs @@ -5,7 +5,7 @@ namespace Nethermind.Serialization.Json { - using System.Collections.Generic; + using Nethermind.Core.Collections; using System.Runtime.CompilerServices; using System.Text.Json; using System.Text.Json.Serialization; @@ -47,19 +47,21 @@ public override double[] Read( { throw new JsonException(); } - List values = null; - reader.Read(); - while (reader.TokenType == JsonTokenType.Number) + using ArrayPoolList values = new ArrayPoolList(16); + while (reader.Read() && reader.TokenType == JsonTokenType.Number) { - values ??= new List(); values.Add(reader.GetDouble()); } if (reader.TokenType != JsonTokenType.EndArray) { throw new JsonException(); } - reader.Read(); - return values?.ToArray() ?? Array.Empty(); + + if (values.Count == 0) return Array.Empty(); + + double[] result = new double[values.Count]; + values.CopyTo(result, 0); + return result; } [SkipLocalsInit]