-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add specification for ReferenceHandling #354
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 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
85259e9
Add specification for ReferenceHandling
jozkee f5f85c1
Address more feedback, corrected types, and included table of contents
jozkee e12a8f6
Add missing access modifiers to class properties
jozkee 24c6d17
Apply suggestions from code review
jozkee 6b4a91b
Apply more suggestions from code review
jozkee 095d5a9
Add PR suggestions.
jozkee bc2701c
Apply even more suggestions from code review
jozkee cc1ac66
Add missing semi colons on samples and standardize error messages on …
jozkee 95d2636
Address changes discussed on API review
jozkee 94021e1
Add note to describe unsupported round-tripping capability on JsonExt…
jozkee c23959d
Replace i.e. for e.g. where meant to quote an example.
jozkee 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
Add PR suggestions.
- Loading branch information
commit 095d5a9e1dd5101cdf12a30e5c46afb85ce6751e
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -49,6 +49,14 @@ The current solution to deal with cycles in the object graph while serializing i | |||||
| # Proposal | ||||||
|
|
||||||
| ```cs | ||||||
| namespace System.Text.Json | ||||||
| { | ||||||
| public partial class JsonSerializerOptions | ||||||
| { | ||||||
| public ReferenceHandling ReferenceHandling { get; set; } = ReferenceHandling.Default; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| namespace System.Text.Json.Serialization | ||||||
| { | ||||||
| /// <summary> | ||||||
|
|
@@ -63,14 +71,6 @@ namespace System.Text.Json.Serialization | |||||
| public static ReferenceHandling Ignore { get; } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| namespace System.Text.Json | ||||||
| { | ||||||
| public partial class JsonSerializerOptions | ||||||
| { | ||||||
| public ReferenceHandling ReferenceHandling { get; set; } = ReferenceHandling.Default; | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
| See also the [internal implementation details](https://gist.github.com/Jozkee/b0922ef609f7a942f00ac2c93a976ff1). | ||||||
jozkee marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
|
|
@@ -111,7 +111,45 @@ Notes: | |||||
|
|
||||||
| # Examples | ||||||
|
|
||||||
| Let's assume you have the following class: | ||||||
| ## Using Default on Deserialize | ||||||
| ```cs | ||||||
| class Employee | ||||||
| { | ||||||
| [JsonPropertyName("$id")] | ||||||
| public string Identifier { get; set; } | ||||||
| public Employee Manager { get; set; } | ||||||
|
|
||||||
| [JsonExtensionData] | ||||||
| public IDictionary<string, object> ExtensionData { get; set; } | ||||||
| } | ||||||
|
|
||||||
| private const string json = | ||||||
| @"{ | ||||||
| ""$id"": ""1"", | ||||||
| ""Name"": ""Angela"", | ||||||
| ""Manager"": { | ||||||
| ""$id"": ""2"", | ||||||
| ""Name"": ""Bob"", | ||||||
| ""Manager"": { | ||||||
| ""$ref"": ""2"" | ||||||
| } | ||||||
| } | ||||||
| }"; | ||||||
| ``` | ||||||
|
|
||||||
| ```cs | ||||||
| public static void ReadObject() | ||||||
| { | ||||||
| Employee angela = JsonSerializer.Deserialize<Employee>(json); | ||||||
| Console.WriteLine(angela.Identifier) //prints: "1". | ||||||
ahsonkhan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jozkee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| Console.WriteLine(angela.Manager.Identifier) //prints: "2". | ||||||
jozkee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| Console.WriteLine(angela.Manager.Manager.ExtensionData["$ref"]) //prints: "2". | ||||||
jozkee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Note how you can annotate .Net properties to use properties that are meant for metadata and are added to the `JsonExtensionData` overflow dictionary, in case there is any, when opting-out of the `ReferenceHanding.Preserve` feature. | ||||||
|
|
||||||
| For the next samples let's assume you have the following class: | ||||||
jozkee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| ```cs | ||||||
| class Employee | ||||||
| { | ||||||
|
|
@@ -126,6 +164,9 @@ class Employee | |||||
| private Employee bob = new Employee { Name = "Bob" }; | ||||||
| private Employee angela = new Employee { Name = "Angela" }; | ||||||
|
|
||||||
| angela.Manager = bob; | ||||||
| bob.Subordinates = new List<Employee>{ angela }; | ||||||
|
|
||||||
| public static void WriteObject() | ||||||
| { | ||||||
| string json = JsonSerializer.Serialize(angela, options); | ||||||
|
|
@@ -604,6 +645,9 @@ Since these types are created with the help of an internal converter, and they a | |||||
|
|
||||||
| With that said, the deserializer will throw when it reads `$id` on any of these types; but regardless of that, when writing those types, they are going to be preserved as any other collection type (`{ "$id": "1", "$values": [...] }`) since those types can still being parsed into a collection type that it is supported. | ||||||
jozkee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| Note: By the same principle, `Newtonsoft.Json` does not support parsing JSON arrays into immutables as well. | ||||||
| Note 2: When using immutable types and `ReferenceHandling.Preserve`, you will not be able to generate payloads that are capables of round-tripping. | ||||||
|
|
||||||
| * **Immutable types**: i.e: `ImmutableList` and `ImmutableDictionary` | ||||||
|
||||||
| * **Immutable types**: i.e: `ImmutableList` and `ImmutableDictionary` | |
| * **Immutable types**: e.g. `ImmutableList` and `ImmutableDictionary` |
Member
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always though i.e. stands for "in example".
jozkee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jozkee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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.