-
-
Notifications
You must be signed in to change notification settings - Fork 741
Fix API break: public API must not expose JsonElement objects #938
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
3 commits
Select commit
Hold shift + click to select a range
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
126 changes: 126 additions & 0 deletions
126
src/ElectronNET.API/Serialization/JsonToBoxedPrimitivesConverter.cs
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,126 @@ | ||
| namespace ElectronNET.Serialization | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| public sealed class JsonToBoxedPrimitivesConverter : JsonConverter<object> | ||
| { | ||
| public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
| { | ||
| return ReadValue(ref reader); | ||
| } | ||
|
|
||
| private static object ReadValue(ref Utf8JsonReader r) | ||
| { | ||
| switch (r.TokenType) | ||
| { | ||
| case JsonTokenType.StartObject: | ||
|
|
||
| var obj = new Dictionary<string, object>(); | ||
| while (r.Read()) | ||
| { | ||
| if (r.TokenType == JsonTokenType.EndObject) | ||
| { | ||
| return obj; | ||
| } | ||
|
|
||
| if (r.TokenType != JsonTokenType.PropertyName) | ||
| { | ||
| throw new JsonException("Expected property name."); | ||
| } | ||
|
|
||
| string name = r.GetString()!; | ||
| if (!r.Read()) | ||
| { | ||
| throw new JsonException("Unexpected end while reading property value."); | ||
| } | ||
|
|
||
| obj[name] = ReadValue(ref r); | ||
| } | ||
|
|
||
| throw new JsonException("Unexpected end while reading object."); | ||
|
|
||
| case JsonTokenType.StartArray: | ||
|
|
||
| var list = new List<object>(); | ||
| while (r.Read()) | ||
| { | ||
| if (r.TokenType == JsonTokenType.EndArray) | ||
| { | ||
| return list; | ||
| } | ||
|
|
||
| list.Add(ReadValue(ref r)); | ||
| } | ||
|
|
||
| throw new JsonException("Unexpected end while reading array."); | ||
|
|
||
| case JsonTokenType.True: return true; | ||
| case JsonTokenType.False: return false; | ||
| case JsonTokenType.Null: return null; | ||
|
|
||
| case JsonTokenType.Number: | ||
|
|
||
| if (r.TryGetInt32(out int i)) | ||
| { | ||
| return i; | ||
| } | ||
|
|
||
| if (r.TryGetInt64(out long l)) | ||
| { | ||
| return l; | ||
| } | ||
|
|
||
| if (r.TryGetDouble(out double d)) | ||
| { | ||
| return d; | ||
| } | ||
|
|
||
| return r.GetDecimal(); | ||
|
|
||
| case JsonTokenType.String: | ||
|
|
||
| string s = r.GetString()!; | ||
|
|
||
| if (DateTimeOffset.TryParse(s, out var dto)) | ||
| { | ||
| return dto; | ||
| } | ||
|
|
||
| if (DateTime.TryParse(s, out var dt)) | ||
| { | ||
| return dt; | ||
| } | ||
|
|
||
| if (TimeSpan.TryParse(s, out var ts)) | ||
| { | ||
| return ts; | ||
| } | ||
|
|
||
| if (Guid.TryParse(s, out var g)) | ||
| { | ||
| return g; | ||
| } | ||
|
|
||
softworkz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return s; | ||
|
|
||
| default: | ||
| throw new JsonException($"Unsupported token {r.TokenType}"); | ||
| } | ||
| } | ||
|
|
||
| public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) | ||
| { | ||
| if (value is null) | ||
| { | ||
| writer.WriteNullValue(); | ||
| return; | ||
| } | ||
|
|
||
| writer.WriteStartObject(); | ||
| writer.WriteEndObject(); | ||
softworkz 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
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
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.