Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Support for Ignore Default Values during JSON serialization in System.Text.Json #42288

Closed
wants to merge 23 commits into from

Conversation

judeleander
Copy link

No description provided.

@dnfclas
Copy link

dnfclas commented Nov 1, 2019

CLA assistant check
All CLA requirements met.

@maryamariyan
Copy link
Member

maryamariyan commented Nov 6, 2019

Thank you for your contribution. As announced in dotnet/coreclr#27549 this repository will be moving to dotnet/runtime on November 13. If you would like to continue working on this PR after this date, the easiest way to move the change to dotnet/runtime is:

  1. In your corefx repository clone, create patch by running git format-patch origin
  2. In your runtime repository clone, apply the patch by running git apply --directory src/libraries <path to the patch created in step 1>

Copy link
Contributor

@layomia layomia left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! API review is needed for this feature to be implemented in dotnet/runtime.

@@ -452,6 +452,7 @@ public sealed partial class JsonSerializerOptions
public int DefaultBufferSize { get { throw null; } set { } }
public System.Text.Json.JsonNamingPolicy DictionaryKeyPolicy { get { throw null; } set { } }
public System.Text.Encodings.Web.JavaScriptEncoder Encoder { get { throw null; } set { } }
public bool IgnoreDefaultValues { get { throw null; } set { } }
Copy link
Contributor

Choose a reason for hiding this comment

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

This requires API review: please see the process here: https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/api-review-process.md.

The overall "ignore default values" feature has to consider

  • if there should be options for the deserialization side (e.g. to populate object with default value when the corresponding JSON is missing). As a starting point, we'd consider the options Newtonsoft.Json provides.
  • if this it should apply only to properties or also to collection elements and root objects.
  • how the default values for value types should be serialized, and so on.

@@ -1,9 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: why this change?

// Check default value property.
if (options.IgnoreDefaultValues)
{
DefaultValueAttribute defaultValueAttribute = JsonPropertyInfo.GetAttribute<DefaultValueAttribute>(jsonPropertyInfo.PropertyInfo);
Copy link
Contributor

@layomia layomia Nov 12, 2019

Choose a reason for hiding this comment

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

We should cache the presence of this attribute for each property once during start-up (during the creation of JsonPropertyInfos) rather than every time we see a value.

@@ -96,7 +97,20 @@ private static bool WriteEndObject(ref WriteStack state)

if (jsonPropertyInfo.ClassType == ClassType.Value)
{
jsonPropertyInfo.Write(ref state, writer);
bool includeProperty = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to consider properties of other class types as well.

@@ -0,0 +1,80 @@
// Licensed to the .NET Foundation under one or more agreements.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: this should be named to something like DefaultValueHandlingTests.cs.


namespace System.Text.Json.Serialization.Tests
{
public static partial class DefaultTests
Copy link
Contributor

Choose a reason for hiding this comment

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

Based on API review, we might also need more tests for default value handling, e.g.

  • on deserialization
  • for collection properties on classes/structs
  • for collection elements
  • for nested collections

@ericstj
Copy link
Member

ericstj commented Nov 12, 2019

Thank you for the interest in contributing. This PR is attempting to add public API surface area, and that's something we have very strict processes around, outlined at https://github.com/dotnet/corefx/blob/master/Documentation/project-docs/api-review-process.md. We don't accept PRs for new surface area until an issue proposing the new APIs has been discussed, reviewed, and marked api-approved. As such, I'm going to close this PR. Please feel free to open such an issue, following the mentioned process. Once that’s approved we can accept a PR that implements the new API. Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants