Skip to content

React to new System.Text.Json features #9572

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 1 commit into from
Apr 22, 2019
Merged

Conversation

pranavkm
Copy link
Contributor

No description provided.


// Ignore casing when serializing values. Setting this to true keeps parity with Json.NET's
// behavior.
PropertyNameCaseInsensitive = true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

How do you feel about this? In the absence of this property, it is required to send camel-case values to successfully deserialize. Most of our tests use pascal-case when sending data.

We can update tests, but I'm not sure how sad users would be when random casing doesn't work.

Copy link
Member

Choose a reason for hiding this comment

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

I think true is the right default for MVC. Also it keeps closer to Json.NET so less friction when upgrading. If someone wants to be strict then they can always change the setting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just spoke to @rynowak offline and he's of the opinion that having it be case sensitive by default would be the right way to go. Users are always free to change it.

Copy link
Member

Choose a reason for hiding this comment

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

We should use the best default regardless of migration. You can always turn this ON for migrating.

I'm interested to hear arguments about case-insensitive is the right default for a REST API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made this case sensitive now and updated tests to be 🐫 cased

Copy link
Member

Choose a reason for hiding this comment

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

I'm interested to hear arguments about case-insensitive is the right default for a REST API.

Nothing other than it slightly reduces issues when using an API and I don't see much disadvantage. I don't feel strongly about it. As long as it is configurable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could always revisit this once we hear what users have to say with the preview5 bits.

Copy link
Member

Choose a reason for hiding this comment

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

Nothing other than it slightly reduces issues when using an API and I don't see much disadvantage. I don't feel strongly about it. As long as it is configurable

APIs document their contract and are precise about what they support. JSON is a case sensitive format, and it's reasonable to follow along with that. I expect if we asked API users they would overwhelmingly support us following the spec.

Copy link
Member

@JamesNK JamesNK Apr 22, 2019

Choose a reason for hiding this comment

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

JSON being case sensitive in the browser is prior art, not a spec. There is no spec about how JSON should map to a .NET object 😋

When it comes to JSON my philosophy has been be flexible in what you accept and exact in what you produce. It appeals to software devs to be strict but when it comes time to use the new serializer I suspect a lot of people will turn case insensitive on to ease the way.

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Apr 19, 2019
@pranavkm pranavkm force-pushed the prkrishn/systemtextjsonhelper branch from 8f39208 to c138325 Compare April 19, 2019 23:31
@pranavkm pranavkm force-pushed the prkrishn/set-options branch from fe648e5 to e577bcc Compare April 19, 2019 23:35
@pranavkm pranavkm changed the base branch from prkrishn/systemtextjsonhelper to master April 21, 2019 19:12
@pranavkm pranavkm force-pushed the prkrishn/set-options branch from e577bcc to dbe4b80 Compare April 21, 2019 19:20
@rynowak
Copy link
Member

rynowak commented Apr 22, 2019

@ahsonkhan @steveharter FYI

@pranavkm pranavkm merged commit 418f3d8 into master Apr 22, 2019
@pranavkm pranavkm deleted the prkrishn/set-options branch April 22, 2019 03:48
}

[Fact]
public override async Task Formatting_CollectionType()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why remove these tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They were overriding the baseline tests https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/test/Mvc.FunctionalTests/JsonOutputFormatterTestBase.cs#L117-L129 because they were different from what we would want (i.e did not do camel-case). Now that it matches the baseline, we can have it call the one on the base.

}

[Fact]
public virtual void RoundTripTest_StringThatIsNotCompliantGuid()
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to verify, we are ok with this, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, this is fine. We just need to able to document "a" behavior so we don't find it confusing \ surprising.

}
else
{
deserializedValue = stringValue;
deserializedValue = item.Value.GetString();
Copy link
Contributor

@ahsonkhan ahsonkhan Apr 24, 2019

Choose a reason for hiding this comment

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

Will the JSON strings more often than not be GUID/DateTimes (or will they primarily be regular strings)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd expect the latter. It's not uncommon for users to JSON serialize on complex models so that they could put it in this dictionary. Is there a way to improve this?

@@ -194,8 +195,8 @@ protected async Task ReadAsync_ReadsValidArray_AsList(Type requestedType)

// Assert
Assert.False(result.HasError);
var integers = Assert.IsType<List<int>>(result.Model);
Assert.Equal(new int[] { 0, 23, 300 }, integers);
Copy link
Contributor

Choose a reason for hiding this comment

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

What motivated this change?

Copy link
Contributor Author

@pranavkm pranavkm Apr 24, 2019

Choose a reason for hiding this comment

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

Good one. I should have brought this up - @layomia was there a reason System.Text.Json returns a type that wraps List<T> rather than just returning a List<T>? Not that it ultimately matters, but if I remember correctly we agreed on just doing a List<T> like Json.NET does.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also cc @steveharter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants