Skip to content

Conversation

@jozkee
Copy link
Owner

@jozkee jozkee commented Nov 25, 2019

@ahsonkhan PTAL

Copy link

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback.


Community is heavily requesting this feature since is consider by many as a very common scenario, specially when serializing POCOs that came from an ORM Framework, such as Entity Framework; even though JSON specifiacation does not support reference loops by default. Therefore this will be shipped as an opt-in feature.

The current solution to deal with reference loops is to rely in MaxDepth and throw a JsonException after it is exceeded. Now, this is a decent and cheap solution but we will also offer other not-so-cheap options to deal with this problem while keeping the current one in order to not affect the out-of-the-box performance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The current solution to deal with reference loops is to rely in MaxDepth and throw a JsonException after it is exceeded. Now, this is a decent and cheap solution but we will also offer other not-so-cheap options to deal with this problem while keeping the current one in order to not affect the out-of-the-box performance.
The current solution to deal with cycles in the object graph while serializing is to rely on `MaxDepth` and throw a `JsonException` after it is exceeded. This was done to avoid perf overhead for cycle detection in the common case. The goal is to enable the new opt-in feature with minimal impact to existing performance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current solution to deal with cycles in the object graph while serializing is to rely on `MaxDepth` and throw a `JsonException` after it is exceeded. This was done to avoid perf overhead for cycle detection in the common case. The goal is to enable the new opt-in feature with minimal impact to existing performance.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did those HTML tags came from?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know. Something broke with the suggestions. That's why I copied the suggestion as regular text.


# Prior art

Json.Net contains settings that you can enable to deal with such problems.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Newtonsoft.Json everywhere rather than Json.NET. Json.Net is confusing to readers (they tend to think that's the built-in library).


The current solution to deal with reference loops is to rely in MaxDepth and throw a JsonException after it is exceeded. Now, this is a decent and cheap solution but we will also offer other not-so-cheap options to deal with this problem while keeping the current one in order to not affect the out-of-the-box performance.

# Prior art

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about what other languages/libraries do? I would add that here too.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given we are basically talking about what other libraries do, when you add more info here, consider wording the title as "Other libraries" like you had before.


1. MaxDepth validation will not be affected by `ReferenceHandling.Preserve`.
2. We are merging the Json.Net types [`ReferenceLoopHandling`]("https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_ReferenceLoopHandling.htm") and [`PreserveReferencesHandling`]("https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_PreserveReferencesHandling.htm") (we are also not including the granularity on this one) into one single class; `ReferenceHandling`.
3. While Immutable types and `System.Array`s can be Serialized with Preserve semantics, they will not be supported when trying to Deserialize them as a reference.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain why.

1. MaxDepth validation will not be affected by `ReferenceHandling.Preserve`.
2. We are merging the Json.Net types [`ReferenceLoopHandling`]("https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_ReferenceLoopHandling.htm") and [`PreserveReferencesHandling`]("https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_PreserveReferencesHandling.htm") (we are also not including the granularity on this one) into one single class; `ReferenceHandling`.
3. While Immutable types and `System.Array`s can be Serialized with Preserve semantics, they will not be supported when trying to Deserialize them as a reference.
4. Value types, such as structs that contain preserve semantics, will not be supported when Deserialized as well.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Why?


With ReferenceHandling being a class, we can exclude things that, as of now, we are not sure if they need to be shipped, in example, the `ReferenceLoopHandling.Ignore` option or the Object and Array granularity of Json.Net's `PreserveReferencesHandling`.

In a future, we might also be able to extend the Deserialization behaviors, in order to provide the granularity of Serialization on Deserialization. Or we could also be able to define independant behaviors for both Serialization and Deserialization; i.e: someone may want to emit payloads with preserved references but he does not want to read them.
Copy link

@ahsonkhan ahsonkhan Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the support matrix here. Along with what the support matrix could look like in the future (also consider including read ahead in your matrix of things we don't support).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the support matrix here.

Just to clarify, referring to something like this:
https://github.com/dotnet/corefx/issues/38579#issuecomment-556923546

}
```

* Metadata property **after** `$ref`:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add rule about JsonExtensionData where metadata won't get added to it ever (even if we ignore it with a setting).

Copy link
Owner Author

@jozkee jozkee Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though we agreed that metadata will be added to JsonExtensionData if we choose to ignore it. There might be people that depend on such properties existing in it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though we agreed that metadata will be added to JsonExtensionData if we choose to ignore it.

Is ignoring metadata the default behavior on deserialization? I am fine with that. Please document it as such.


This is a heavily requested feature since it is consider by many as a very common scenario, specially when serializing POCOs that came from an ORM Framework, such as Entity Framework; even though the JSON specification does not support reference loops by default. Therefore, this will be implemented as an opt-in feature (for both serialization and deserialization).

The current solution to deal with reference loops is to rely in MaxDepth and throw a JsonException after it is exceeded. Now, this is a decent and cheap solution but we will also offer other not-so-cheap options to deal with this problem while keeping the current one in order to not affect the out-of-the-box performance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The current solution to deal with reference loops is to rely in MaxDepth and throw a JsonException after it is exceeded. Now, this is a decent and cheap solution but we will also offer other not-so-cheap options to deal with this problem while keeping the current one in order to not affect the out-of-the-box performance.
The current solution to deal with cycles in the object graph while serializing is to rely on `MaxDepth` and throw a `JsonException` after it is exceeded. This was done to avoid perf overhead for cycle detection in the common case. The goal is to enable the new opt-in feature with minimal impact to existing performance.


Currently there is no mechanism to prevent infinite looping in circular objects (while serializing) nor to preserve references that round-trip when using System.Text.Json.

This is a heavily requested feature since it is consider by many as a very common scenario, specially when serializing POCOs that came from an ORM Framework, such as Entity Framework; even though the JSON specification does not support reference loops by default. Therefore, this will be implemented as an opt-in feature (for both serialization and deserialization).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is a heavily requested feature since it is consider by many as a very common scenario, specially when serializing POCOs that came from an ORM Framework, such as Entity Framework; even though the JSON specification does not support reference loops by default. Therefore, this will be implemented as an opt-in feature (for both serialization and deserialization).
This is a heavily requested feature since it is considered by many as a very common scenario, specially when serializing POCOs that came from an ORM Framework, such as Entity Framework; even though the JSON specification does not support reference loops by default. Therefore, this will be implemented as an opt-in feature (for both serialization and deserialization).


This API is exposing the `ReferenceHandling` property as a class, to be extensible in the future; and provide built-in static instances of `Default` and `Preserve` that are useful to enable the most common behaviors by just setting those in `JsonSerializerOptions.ReferenceHandling`.

With `ReferenceHandling` being a class, we can exclude things that, as of now, we are not sure are required and add them later based on customer feedback. For example, the `Object` and `Array` granularity of `Newtonsoft.Json's` ``PreserveReferencesHandling` or the `ReferenceLoopHandling.Ignore` option.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
With `ReferenceHandling` being a class, we can exclude things that, as of now, we are not sure are required and add them later based on customer feedback. For example, the `Object` and `Array` granularity of `Newtonsoft.Json's` ``PreserveReferencesHandling` or the `ReferenceLoopHandling.Ignore` option.
With `ReferenceHandling` being a class, we can exclude things that, as of now, we are not sure are required and add them later based on customer feedback. For example, the `Object` and `Array` granularity of `Newtonsoft.Json's` `PreserveReferencesHandling` or the `ReferenceLoopHandling.Ignore` option.

@jozkee jozkee closed this Dec 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants