From db9fba4cf2cbc91526f76e3a9402592f5335885c Mon Sep 17 00:00:00 2001 From: JKorf Date: Thu, 21 Mar 2024 14:09:49 +0100 Subject: [PATCH] Added specific type fields for NodeAccessor to prevent boxing --- .../JsonNet/JsonNetMessageAccessor.cs | 6 +++--- .../Converters/MessageParsing/MessageNode.cs | 21 ++++++++++++------- .../SystemTextJsonMessageAccessor.cs | 4 ++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/CryptoExchange.Net/Converters/JsonNet/JsonNetMessageAccessor.cs b/CryptoExchange.Net/Converters/JsonNet/JsonNetMessageAccessor.cs index 0c6d749..9aa1e80 100644 --- a/CryptoExchange.Net/Converters/JsonNet/JsonNetMessageAccessor.cs +++ b/CryptoExchange.Net/Converters/JsonNet/JsonNetMessageAccessor.cs @@ -20,7 +20,7 @@ namespace CryptoExchange.Net.Converters.JsonNet /// The json token loaded /// protected JToken? _token; - private static JsonSerializer _serializer = JsonSerializer.Create(SerializerOptions.WithConverters); + private static readonly JsonSerializer _serializer = JsonSerializer.Create(SerializerOptions.WithConverters); /// public bool IsJson { get; protected set; } @@ -174,7 +174,7 @@ namespace CryptoExchange.Net.Converters.JsonNet if (node.Type == 0) { // Int value - var val = (int)node.Value!; + var val = node.Index!.Value; if (currentToken!.Type != JTokenType.Array || ((JArray)currentToken).Count <= val) return null; @@ -186,7 +186,7 @@ namespace CryptoExchange.Net.Converters.JsonNet if (currentToken!.Type != JTokenType.Object) return null; - currentToken = currentToken[(string)node.Value!]; + currentToken = currentToken[node.Property!]; } else { diff --git a/CryptoExchange.Net/Converters/MessageParsing/MessageNode.cs b/CryptoExchange.Net/Converters/MessageParsing/MessageNode.cs index 01b23bf..2a9782e 100644 --- a/CryptoExchange.Net/Converters/MessageParsing/MessageNode.cs +++ b/CryptoExchange.Net/Converters/MessageParsing/MessageNode.cs @@ -6,17 +6,23 @@ public struct NodeAccessor { /// - /// Value + /// Index /// - public object? Value { get; } + public int? Index { get; } + /// + /// Property name + /// + public string? Property { get; } + /// /// Type (0 = int, 1 = string, 2 = prop name) /// public int Type { get; } - private NodeAccessor(object? value, int type) + private NodeAccessor(int? index, string? property, int type) { - Value = value; + Index = index; + Property = property; Type = type; } @@ -25,20 +31,19 @@ /// /// /// - public static NodeAccessor Int(int value) { return new NodeAccessor(value, 0); } + public static NodeAccessor Int(int value) { return new NodeAccessor(value, null, 0); } /// /// Create a string node accessor /// /// /// - public static NodeAccessor String(string value) { return new NodeAccessor(value, 1); } + public static NodeAccessor String(string value) { return new NodeAccessor(null, value, 1); } /// /// Create a property name node accessor /// /// - public static NodeAccessor PropertyName() { return new NodeAccessor(null, 2); } + public static NodeAccessor PropertyName() { return new NodeAccessor(null, null, 2); } } - } diff --git a/CryptoExchange.Net/Converters/SystemTextJson/SystemTextJsonMessageAccessor.cs b/CryptoExchange.Net/Converters/SystemTextJson/SystemTextJsonMessageAccessor.cs index 020003d..c65be56 100644 --- a/CryptoExchange.Net/Converters/SystemTextJson/SystemTextJsonMessageAccessor.cs +++ b/CryptoExchange.Net/Converters/SystemTextJson/SystemTextJsonMessageAccessor.cs @@ -147,7 +147,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson if (node.Type == 0) { // Int value - var val = (int)node.Value!; + var val = node.Index!.Value; if (currentToken!.Value.ValueKind != JsonValueKind.Array || currentToken.Value.GetArrayLength() <= val) return null; @@ -159,7 +159,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson if (currentToken!.Value.ValueKind != JsonValueKind.Object) return null; - if (!currentToken.Value.TryGetProperty((string)node.Value!, out var token)) + if (!currentToken.Value.TryGetProperty(node.Property!, out var token)) return null; currentToken = token; }