Skip to content
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

Circular reference detection reference equality handling #423

Closed
wants to merge 11 commits into from

Conversation

D3-LucaPiombino
Copy link

I' ve closed the previous pull request and opened a new one.
I still think that the current behavior(see #401) IS very fragile.
I undestand that if people are depending on it, the default
behavior cannot be changed, but maybe it should be at least overridable.
In this pull request i've added a new enum(ReferenceComparisonHandling) and a new property that allow to override the behavior, available to all the various levels available for other settings like ReferenceLoopHandling, ItemReferenceLoopHandling etc... that specify the method that will be used to compare references during the circular reference detection phase.
If the settings is not provided the old behavior is used.

new JsonSerializerSettings
{
    Formatting = Formatting.Indented,
    ReferenceComparisonHandling = ReferenceComparisonHandling.ReferenceEquals
}

[JsonObject(ItemReferenceComparisonHandling = ReferenceComparisonHandling.ReferenceEquals)]
[JsonProperty(ReferenceComparisonHandling = ReferenceComparisonHandling.ReferenceEquals)]
[JsonProperty(ItemReferenceComparisonHandling = ReferenceComparisonHandling.ReferenceEquals)]

and of course all the other classes that partecipate in the serialization pipeline(JsonProperty,JsonContract, etc...).
I've added a new set of unit test that verify the intended behavior as the guidelines indicates.
This could have also been implemented in a different way, by creating a new service (like IReferenceResolver) that allow to change the default behavior, but i didn't want to change the code to much. If you prefer this solution let me know , i can make another pull request.

Thanks.

… instances when checking for circular references in a object graph.
…changing the current bahavior unconditionally would cause a breaking change, this change set add the ability to override the behavior by providing a new setting when configuring the serializer. All the configuration paths are supported(JsonSerializerSettings, Json*Attribute, JsonContract, JsonProperty, etc...).

See the ReferenceComparisonHandling enum.
[ADDED]: ReferenceComparisonHandlingTests
… instances when checking for circular references in a object graph.
…changing the current bahavior unconditionally would cause a breaking change, this change set add the ability to override the behavior by providing a new setting when configuring the serializer. All the configuration paths are supported(JsonSerializerSettings, Json*Attribute, JsonContract, JsonProperty, etc...).

See the ReferenceComparisonHandling enum.
[ADDED]: ReferenceComparisonHandlingTests
JsonContainerAttribute.ReferenceComparisonHandling and JsonSerializerSettings.ReferenceComparisonHandling
[FIXED]: In JsonSerializer.applySerializerSettings, settings.ReferenceComparisonHandling was used
when checking if the user provided a value for the setting instead of using settings._referenceLoopHandling.
@iSynaptic
Copy link

I agree. I think the default behavior is wrong, but this solution is agreeable. Perhaps 7.0 can make the default use object.ReferenceEquals.

I'm on a project that needs this enhancement - any idea when it will be merged?

@JamesNK
Copy link
Owner

JamesNK commented Jun 14, 2015

Added EqualityComparer to JsonSerializer 3cc797c

@JamesNK JamesNK closed this Jun 14, 2015
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.

5 participants