Skip to content

Commit b6572d9

Browse files
authored
Remove deprecated functionaliaty from the new SystemTextJson.FromJson methods (graphql-dotnet#2530)
1 parent 42b8382 commit b6572d9

File tree

2 files changed

+19
-39
lines changed

2 files changed

+19
-39
lines changed

src/GraphQL.SystemTextJson/StringExtensions.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ public static class StringExtensions
2525
}
2626
};
2727

28+
private static readonly JsonSerializerOptions _jsonOptions2 = new JsonSerializerOptions
29+
{
30+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
31+
Converters =
32+
{
33+
new InputsConverter(),
34+
new JsonConverterBigInteger(),
35+
}
36+
};
37+
2838
/// <summary>
2939
/// Converts a JSON-formatted string into a dictionary.
3040
/// </summary>
3141
/// <param name="json">A JSON formatted string.</param>
3242
/// <returns>Inputs.</returns>
3343
public static Inputs ToInputs(this string json)
34-
=> json != null ? JsonSerializer.Deserialize<Inputs>(json, _jsonOptions) : Inputs.Empty;
44+
=> json != null ? JsonSerializer.Deserialize<Inputs>(json, _jsonOptions2) : Inputs.Empty;
3545

3646
/// <summary>
3747
/// Converts a JSON-formatted string into a dictionary of objects of their actual type.
@@ -50,7 +60,7 @@ public static Dictionary<string, object> ToDictionary(this string json)
5060
/// Property names are converted to camel case and matched based on a case sensitive comparison (the default for System.Text.Json).
5161
/// </summary>
5262
public static T FromJson<T>(this string json)
53-
=> JsonSerializer.Deserialize<T>(json, _jsonOptions);
63+
=> JsonSerializer.Deserialize<T>(json, _jsonOptions2);
5464

5565
/// <summary>
5666
/// Deserializes a JSON-formatted stream of data into the specified type.
@@ -60,7 +70,7 @@ public static T FromJson<T>(this string json)
6070
/// Property names are converted to camel case and matched based on a case sensitive comparison (the default for System.Text.Json).
6171
/// </summary>
6272
public static ValueTask<T> FromJsonAsync<T>(this System.IO.Stream stream, CancellationToken cancellationToken = default)
63-
=> JsonSerializer.DeserializeAsync<T>(stream, _jsonOptions, cancellationToken);
73+
=> JsonSerializer.DeserializeAsync<T>(stream, _jsonOptions2, cancellationToken);
6474

6575
/// <summary>
6676
/// Converts a JSON element into an <see cref="Inputs"/> object.

src/GraphQL.Tests/Serialization/SystemTextJson/StringExtensionTests.cs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.IO;
43
using System.Linq;
@@ -62,45 +61,22 @@ public void StringToInputs()
6261
Verify(actual);
6362
}
6463

65-
[Fact]
66-
public void FromJson()
67-
{
68-
var test = $"{{\"query\":\"hello\",\"variables\":{_exampleJson}}}";
69-
var actual = test.FromJson<TestClass1>();
70-
actual.query.ShouldBe("hello");
71-
Verify(actual.variables);
72-
}
73-
7464
[Fact]
7565
public void FromJson_Null()
7666
{
7767
var test = $"{{\"query\":\"hello\",\"variables\":null}}";
78-
var actual = test.FromJson<TestClass1>();
79-
actual.query.ShouldBe("hello");
80-
actual.variables.ShouldBeNull();
68+
var actual = test.FromJson<TestClass3>();
69+
actual.Query.ShouldBe("hello");
70+
actual.Variables.ShouldBeNull();
8171
}
8272

8373
[Fact]
8474
public void FromJson_Missing()
8575
{
8676
var test = $"{{\"query\":\"hello\"}}";
87-
var actual = test.FromJson<TestClass1>();
88-
actual.query.ShouldBe("hello");
89-
actual.variables.ShouldBeNull();
90-
}
91-
92-
[Fact]
93-
public async Task FromJsonAsync()
94-
{
95-
var test = $"{{\"query\":\"hello\",\"variables\":{_exampleJson}}}";
96-
var testData = new MemoryStream(Encoding.UTF8.GetBytes(test));
97-
var actual = await testData.FromJsonAsync<TestClass1>();
98-
actual.query.ShouldBe("hello");
99-
Verify(actual.variables);
100-
// verify that the stream has not been disposed
101-
testData.ReadByte().ShouldBe(-1);
102-
testData.Dispose();
103-
Should.Throw<ObjectDisposedException>(() => testData.ReadByte());
77+
var actual = test.FromJson<TestClass3>();
78+
actual.Query.ShouldBe("hello");
79+
actual.Variables.ShouldBeNull();
10480
}
10581

10682
[Fact]
@@ -198,12 +174,6 @@ public void ToInputsReturnsEmptyForNull()
198174
((string)null).ToInputs().ShouldNotBeNull().Count.ShouldBe(0);
199175
}
200176

201-
private class TestClass1
202-
{
203-
public string query { get; set; }
204-
public Dictionary<string, object> variables { get; set; }
205-
}
206-
207177
private class TestClass2
208178
{
209179
public string Query { get; set; }

0 commit comments

Comments
 (0)