-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from graphql-dotnet/fix-immutableconverter
Fix ImmutableConverter and ErrorPath
- Loading branch information
Showing
9 changed files
with
288 additions
and
50 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/GraphQL.Client.Serializer.SystemTextJson/ConverterHelperExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Numerics; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace GraphQL.Client.Serializer.SystemTextJson | ||
{ | ||
public static class ConverterHelperExtensions | ||
{ | ||
public static object ReadNumber(this ref Utf8JsonReader reader) | ||
{ | ||
if (reader.TryGetInt32(out int i)) | ||
return i; | ||
else if (reader.TryGetInt64(out long l)) | ||
return l; | ||
else if (reader.TryGetDouble(out double d)) | ||
return reader.TryGetBigInteger(out var bi) && bi != new BigInteger(d) | ||
? bi | ||
: (object)d; | ||
else if (reader.TryGetDecimal(out decimal dd)) | ||
return reader.TryGetBigInteger(out var bi) && bi != new BigInteger(dd) | ||
? bi | ||
: (object)dd; | ||
|
||
throw new NotImplementedException($"Unexpected Number value. Raw text was: {reader.GetRawString()}"); | ||
} | ||
|
||
public static bool TryGetBigInteger(this ref Utf8JsonReader reader, out BigInteger bi) => BigInteger.TryParse(reader.GetRawString(), out bi); | ||
|
||
public static string GetRawString(this ref Utf8JsonReader reader) | ||
{ | ||
var byteArray = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan.ToArray(); | ||
return Encoding.UTF8.GetString(byteArray); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/GraphQL.Client.Serializer.SystemTextJson/ErrorPathConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace GraphQL.Client.Serializer.SystemTextJson | ||
{ | ||
public class ErrorPathConverter : JsonConverter<ErrorPath> | ||
{ | ||
|
||
public override ErrorPath Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => | ||
new ErrorPath(ReadArray(ref reader)); | ||
|
||
public override void Write(Utf8JsonWriter writer, ErrorPath value, JsonSerializerOptions options) | ||
=> throw new NotImplementedException( | ||
"This converter currently is only intended to be used to read a JSON object into a strongly-typed representation."); | ||
|
||
private IEnumerable<object?> ReadArray(ref Utf8JsonReader reader) | ||
{ | ||
if (reader.TokenType != JsonTokenType.StartArray) | ||
{ | ||
throw new JsonException("This converter can only parse when the root element is a JSON Array."); | ||
} | ||
|
||
var array = new List<object?>(); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndArray) | ||
break; | ||
|
||
array.Add(ReadValue(ref reader)); | ||
} | ||
|
||
return array; | ||
} | ||
|
||
private object? ReadValue(ref Utf8JsonReader reader) | ||
=> reader.TokenType switch | ||
{ | ||
JsonTokenType.None => null, | ||
JsonTokenType.String => reader.GetString(), | ||
JsonTokenType.Number => reader.ReadNumber(), | ||
JsonTokenType.True => true, | ||
JsonTokenType.False => false, | ||
JsonTokenType.Null => null, | ||
_ => throw new InvalidOperationException($"Unexpected token type: {reader.TokenType}") | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace GraphQL | ||
{ | ||
public class ErrorPath : List<object> | ||
{ | ||
public ErrorPath() | ||
{ | ||
} | ||
|
||
public ErrorPath(IEnumerable<object> collection) : base(collection) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.