Skip to content

Commit e0c559c

Browse files
committed
nit: cleanups
1 parent 6d7db76 commit e0c559c

File tree

2 files changed

+51
-66
lines changed

2 files changed

+51
-66
lines changed

src/A2A/JsonRpc/JsonRpcId.cs

Lines changed: 50 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics.CodeAnalysis;
21
using System.Text.Json;
32
using System.Text.Json.Serialization;
43

@@ -7,76 +6,65 @@ namespace A2A;
76
/// <summary>
87
/// Represents a JSON-RPC ID that can be either a string or a number, preserving the original type.
98
/// </summary>
10-
[JsonConverter(typeof(JsonRpcIdConverter))]
9+
[JsonConverter(typeof(Converter))]
1110
public readonly struct JsonRpcId : IEquatable<JsonRpcId>
1211
{
13-
private readonly object? _value;
14-
1512
/// <summary>
1613
/// Initializes a new instance of the <see cref="JsonRpcId"/> struct with a string value.
1714
/// </summary>
1815
/// <param name="value">The string value.</param>
19-
public JsonRpcId(string? value)
20-
{
21-
_value = value;
22-
}
16+
public JsonRpcId(string? value) => Value = value;
2317

2418
/// <summary>
2519
/// Initializes a new instance of the <see cref="JsonRpcId"/> struct with a numeric value.
2620
/// </summary>
2721
/// <param name="value">The numeric value.</param>
28-
public JsonRpcId(long value)
29-
{
30-
_value = value;
31-
}
22+
public JsonRpcId(long value) => Value = value;
3223

3324
/// <summary>
3425
/// Initializes a new instance of the <see cref="JsonRpcId"/> struct with a numeric value.
3526
/// </summary>
3627
/// <param name="value">The numeric value.</param>
37-
public JsonRpcId(int value)
38-
{
39-
_value = (long)value;
40-
}
28+
public JsonRpcId(int value) => Value = (long)value;
4129

4230
/// <summary>
4331
/// Gets a value indicating whether this ID has a value.
4432
/// </summary>
45-
public bool HasValue => _value is not null;
33+
public bool HasValue => Value is not null;
4634

4735
/// <summary>
4836
/// Gets a value indicating whether this ID is a string.
4937
/// </summary>
50-
public bool IsString => _value is string;
38+
public bool IsString => Value is string;
5139

5240
/// <summary>
5341
/// Gets a value indicating whether this ID is a number.
5442
/// </summary>
55-
public bool IsNumber => _value is long;
43+
public bool IsNumber => Value is long;
5644

5745
/// <summary>
5846
/// Gets the string value of this ID if it's a string.
5947
/// </summary>
6048
/// <returns>The string value, or null if not a string.</returns>
61-
public string? AsString() => _value as string;
49+
public string? AsString() => Value as string;
6250

6351
/// <summary>
6452
/// Gets the numeric value of this ID if it's a number.
6553
/// </summary>
6654
/// <returns>The numeric value, or null if not a number.</returns>
67-
public long? AsNumber() => _value as long?;
55+
public long? AsNumber() => Value as long?;
6856

6957
/// <summary>
7058
/// Gets the raw value as an object.
7159
/// </summary>
7260
/// <returns>The raw value as an object.</returns>
73-
public object? AsObject() => _value;
61+
public object? Value { get; }
7462

7563
/// <summary>
7664
/// Returns a string representation of this ID.
7765
/// </summary>
7866
/// <returns>A string representation of the ID.</returns>
79-
public override string? ToString() => _value?.ToString();
67+
public override string? ToString() => Value?.ToString();
8068

8169
/// <summary>
8270
/// Determines whether the specified object is equal to the current ID.
@@ -90,13 +78,13 @@ public JsonRpcId(int value)
9078
/// </summary>
9179
/// <param name="other">The ID to compare with the current ID.</param>
9280
/// <returns>true if the specified ID is equal to the current ID; otherwise, false.</returns>
93-
public bool Equals(JsonRpcId other) => Equals(_value, other._value);
81+
public bool Equals(JsonRpcId other) => Equals(Value, other.Value);
9482

9583
/// <summary>
9684
/// Returns the hash code for this ID.
9785
/// </summary>
9886
/// <returns>A hash code for the current ID.</returns>
99-
public override int GetHashCode() => _value?.GetHashCode() ?? 0;
87+
public override int GetHashCode() => Value?.GetHashCode() ?? 0;
10088

10189
/// <summary>
10290
/// Determines whether two IDs are equal.
@@ -134,49 +122,49 @@ public JsonRpcId(int value)
134122
/// <param name="value">The numeric value.</param>
135123
/// <returns>A JsonRpcId with the numeric value.</returns>
136124
public static implicit operator JsonRpcId(int value) => new(value);
137-
}
138125

139-
/// <summary>
140-
/// JSON converter for JsonRpcId that preserves the original type during serialization/deserialization.
141-
/// </summary>
142-
internal sealed class JsonRpcIdConverter : JsonConverter<JsonRpcId>
143-
{
144-
public override JsonRpcId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
126+
/// <summary>
127+
/// JSON converter for JsonRpcId that preserves the original type during serialization/deserialization.
128+
/// </summary>
129+
internal sealed class Converter : JsonConverter<JsonRpcId>
145130
{
146-
switch (reader.TokenType)
131+
public override JsonRpcId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
147132
{
148-
case JsonTokenType.String:
149-
return new JsonRpcId(reader.GetString());
150-
case JsonTokenType.Number:
151-
if (reader.TryGetInt64(out var longValue))
152-
{
153-
return new JsonRpcId(longValue);
154-
}
155-
throw new JsonException("Invalid numeric value for JSON-RPC ID.");
156-
case JsonTokenType.Null:
157-
return new JsonRpcId((string?)null);
158-
default:
159-
throw new JsonException("Invalid JSON-RPC ID format. Must be string, number, or null.");
133+
switch (reader.TokenType)
134+
{
135+
case JsonTokenType.String:
136+
return new JsonRpcId(reader.GetString());
137+
case JsonTokenType.Number:
138+
if (reader.TryGetInt64(out var longValue))
139+
{
140+
return new JsonRpcId(longValue);
141+
}
142+
throw new JsonException("Invalid numeric value for JSON-RPC ID.");
143+
case JsonTokenType.Null:
144+
return new JsonRpcId(null);
145+
default:
146+
throw new JsonException("Invalid JSON-RPC ID format. Must be string, number, or null.");
147+
}
160148
}
161-
}
162149

163-
public override void Write(Utf8JsonWriter writer, JsonRpcId value, JsonSerializerOptions options)
164-
{
165-
if (!value.HasValue)
166-
{
167-
writer.WriteNullValue();
168-
}
169-
else if (value.IsString)
170-
{
171-
writer.WriteStringValue(value.AsString());
172-
}
173-
else if (value.IsNumber)
174-
{
175-
writer.WriteNumberValue(value.AsNumber()!.Value);
176-
}
177-
else
150+
public override void Write(Utf8JsonWriter writer, JsonRpcId value, JsonSerializerOptions options)
178151
{
179-
writer.WriteNullValue();
152+
if (!value.HasValue)
153+
{
154+
writer.WriteNullValue();
155+
}
156+
else if (value.IsString)
157+
{
158+
writer.WriteStringValue(value.AsString());
159+
}
160+
else if (value.IsNumber)
161+
{
162+
writer.WriteNumberValue(value.AsNumber()!.Value);
163+
}
164+
else
165+
{
166+
writer.WriteNullValue();
167+
}
180168
}
181169
}
182170
}

src/A2A/JsonRpc/JsonRpcRequestConverter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ internal sealed class JsonRpcRequestConverter : JsonConverter<JsonRpcRequest>
1616

1717
public override JsonRpcRequest? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1818
{
19-
string? requestId = null;
20-
2119
try
2220
{
2321
// Create JsonElement from Utf8JsonReader
@@ -26,8 +24,7 @@ internal sealed class JsonRpcRequestConverter : JsonConverter<JsonRpcRequest>
2624

2725
// Validate the JSON-RPC request structure
2826
var idField = ReadAndValidateIdField(rootElement);
29-
requestId = idField.ToString();
30-
27+
var requestId = idField.ToString();
3128
return new JsonRpcRequest
3229
{
3330
Id = idField,

0 commit comments

Comments
 (0)