-
Notifications
You must be signed in to change notification settings - Fork 2
Add bigintconverter to serialize int64 and uint64 to string #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c5eb190
Add BigIntConverter to serialize BigInt types to string and update se…
matnun-br a749d31
Fix BigIntConverter: use Utf8JsonReader APIs, invariant culture, and …
Copilot 5289739
Revert "Fix BigIntConverter: use Utf8JsonReader APIs, invariant cultu…
matnun-br ee8be6a
Refactor BigIntConverter and Serializer: make default converter and o…
matnun-br 1fb4b20
Rename LongConverter and ULongConverter to Int64ToStringConverter and…
matnun-br 2ada0b2
Fix BigIntConverter to use invariant culture for conversions and upda…
matnun-br 55d4baa
minor mechanical change
matnun-br bd5d6b6
Rename
clement911 360cddb
Update documentation for JsonSerializerOptions to clarify default con…
matnun-br File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
| using System.Globalization; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| public class Int64ToStringConverter : LongToStringConverter<long> | ||
| { | ||
| } | ||
|
|
||
| public class UInt64ToStringConverter : LongToStringConverter<ulong> | ||
| { | ||
| } | ||
|
|
||
| public abstract class LongToStringConverter<T> : JsonConverter<T> | ||
| { | ||
| private readonly static JsonConverter<T> _defaultConverter = (JsonConverter<T>)JsonSerializerOptions.Default.GetConverter(typeof(T)); | ||
| public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.String) | ||
| return (T)Convert.ChangeType(reader.GetString(), typeof(T), CultureInfo.InvariantCulture); | ||
|
|
||
| return _defaultConverter.Read(ref reader, typeToConvert, options); | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) | ||
| { | ||
| writer.WriteStringValue(((IFormattable)value).ToString(null, CultureInfo.InvariantCulture)); | ||
| } | ||
matnun-br marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,50 +1,61 @@ | ||
| using System.Text.Json.Serialization; | ||
| using System.Text.Json; | ||
| using System.Collections.Concurrent; | ||
|
|
||
| namespace GraphQLSharp; | ||
|
|
||
| #nullable enable | ||
|
|
||
| public static class Serializer | ||
| { | ||
| public static readonly JsonSerializerOptions Options; | ||
|
|
||
| public static readonly JsonSerializerOptions OptionsIndented; | ||
| private static readonly ConcurrentDictionary<(bool indent, bool serializeInt64ToString), JsonSerializerOptions> _optionsToJsonOptions = new(); | ||
|
|
||
| static Serializer() | ||
| { | ||
| Options = new JsonSerializerOptions | ||
| } | ||
|
|
||
| public static JsonSerializerOptions CreateOptions(bool indent, bool serializeInt64ToString) | ||
| { | ||
| var options = new JsonSerializerOptions | ||
| { | ||
| NumberHandling = JsonNumberHandling.AllowReadingFromString, | ||
| Converters = { new JsonStringEnumConverter() }, | ||
| Converters = { | ||
| new JsonStringEnumConverter(), | ||
| new SafeDateTimeConverter(), | ||
| new SafeDateTimeOffsetConverter() | ||
| }, | ||
| DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
| }; | ||
| Options.Converters.Add(new SafeDateTimeConverter()); | ||
| Options.Converters.Add(new SafeDateTimeOffsetConverter()); | ||
|
|
||
| OptionsIndented = new JsonSerializerOptions(Options) | ||
| if (serializeInt64ToString) | ||
| { | ||
| WriteIndented = true | ||
| }; | ||
| options.Converters.Add(new Int64ToStringConverter()); | ||
| options.Converters.Add(new UInt64ToStringConverter()); | ||
| } | ||
|
|
||
| if (indent) | ||
| options.WriteIndented = true; | ||
|
|
||
| return options; | ||
| } | ||
|
|
||
| public static JsonSerializerOptions GetOptions(bool indent) | ||
| public static JsonSerializerOptions GetOptions(bool indent = false, bool serializeInt64ToString = true) | ||
| { | ||
| return indent ? OptionsIndented : Options; | ||
| return _optionsToJsonOptions.GetOrAdd((indent, serializeInt64ToString), _ => CreateOptions(indent, serializeInt64ToString)); | ||
| } | ||
matnun-br marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static string Serialize(object obj, bool indent = false) | ||
| public static string Serialize(object obj, bool indent = false, bool serializeInt64ToString = true) | ||
| { | ||
| return JsonSerializer.Serialize(obj, obj.GetType(), GetOptions(indent)); | ||
| return JsonSerializer.Serialize(obj, obj.GetType(), GetOptions(indent, serializeInt64ToString)); | ||
| } | ||
|
|
||
| public static object? Deserialize(string json, Type type, bool indent = false) | ||
| public static object? Deserialize(string json, Type type, bool indent = false, bool serializeInt64ToString = true) | ||
| { | ||
| return JsonSerializer.Deserialize(json, type, GetOptions(indent)); | ||
| return JsonSerializer.Deserialize(json, type, GetOptions(indent, serializeInt64ToString)); | ||
| } | ||
|
|
||
| public static T? Deserialize<T>(string json, bool indent = false) | ||
| public static T? Deserialize<T>(string json, bool indent = false, bool serializeInt64ToString = true) | ||
| { | ||
| return JsonSerializer.Deserialize<T>(json, GetOptions(indent)); | ||
| return JsonSerializer.Deserialize<T>(json, GetOptions(indent, serializeInt64ToString)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.