1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00

Added specific type fields for NodeAccessor to prevent boxing

This commit is contained in:
JKorf 2024-03-21 14:09:49 +01:00
parent 926802d953
commit db9fba4cf2
3 changed files with 18 additions and 13 deletions

View File

@ -20,7 +20,7 @@ namespace CryptoExchange.Net.Converters.JsonNet
/// The json token loaded
/// </summary>
protected JToken? _token;
private static JsonSerializer _serializer = JsonSerializer.Create(SerializerOptions.WithConverters);
private static readonly JsonSerializer _serializer = JsonSerializer.Create(SerializerOptions.WithConverters);
/// <inheritdoc />
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
{

View File

@ -6,17 +6,23 @@
public struct NodeAccessor
{
/// <summary>
/// Value
/// Index
/// </summary>
public object? Value { get; }
public int? Index { get; }
/// <summary>
/// Property name
/// </summary>
public string? Property { get; }
/// <summary>
/// Type (0 = int, 1 = string, 2 = prop name)
/// </summary>
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 @@
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static NodeAccessor Int(int value) { return new NodeAccessor(value, 0); }
public static NodeAccessor Int(int value) { return new NodeAccessor(value, null, 0); }
/// <summary>
/// Create a string node accessor
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static NodeAccessor String(string value) { return new NodeAccessor(value, 1); }
public static NodeAccessor String(string value) { return new NodeAccessor(null, value, 1); }
/// <summary>
/// Create a property name node accessor
/// </summary>
/// <returns></returns>
public static NodeAccessor PropertyName() { return new NodeAccessor(null, 2); }
public static NodeAccessor PropertyName() { return new NodeAccessor(null, null, 2); }
}
}

View File

@ -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;
}