Skip to content

Add a System.Text.Json based IJsonHelper #9566

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 4 commits into from
Apr 21, 2019

Conversation

pranavkm
Copy link
Contributor

Fixes #9564

@pranavkm pranavkm requested review from mkArtakMSFT and rynowak April 19, 2019 17:22
public IHtmlContent Serialize(object value)
{
// JsonSerializer always encodes
var json = JsonSerializer.ToString(value, value.GetType(), _mvcOptions.SerializerOptions);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

System.Text.Json's default and only behavior is the equivalent of StringEscapeHandling.EscapeNonAscii (https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_StringEscapeHandling.htm). There isn't a way (as yet) to turn this behavior off.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One thing to note is that the extension method to IJsonHelper that accepts a setting is currently only available when you use the Json.NET based helper: https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.NewtonsoftJson/src/JsonHelperExtensions.cs#L30-L33.

We could consider adding it in the future if somebody asks for it. As of now, the extension primarily exists to maintain parity with 2.x

{
protected override IJsonHelper GetJsonHelper()
{
var mvcOptions = new MvcOptions { SerializerOptions = { PropertyNamingPolicy = JsonNamingPolicy.CamelCase } };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should get cleanup shortly.

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Apr 19, 2019
public IHtmlContent Serialize(object value)
{
// JsonSerializer always encodes
var json = JsonSerializer.ToString(value, value.GetType(), _mvcOptions.SerializerOptions);
Copy link
Member

Choose a reason for hiding this comment

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

This will null reference exception when value is null.

Add a test with fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch! Fixed and added test

@pranavkm pranavkm force-pushed the prkrishn/jsonresultexecutor branch from 955404c to 9176508 Compare April 19, 2019 23:31
@pranavkm pranavkm force-pushed the prkrishn/systemtextjsonhelper branch from 8f39208 to c138325 Compare April 19, 2019 23:31
public IHtmlContent Serialize(object value)
{
// JsonSerializer always encodes
var json = JsonSerializer.ToString(value, value?.GetType() ?? typeof(object), _mvcOptions.SerializerOptions);
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm, is there no overload on JsonSerializer that just takes a value?

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 was under the impression that https://github.com/dotnet/corefx/issues/36166 wasn't done. But it works for object value. Updated

@pranavkm pranavkm changed the base branch from prkrishn/jsonresultexecutor to master April 21, 2019 16:14
@pranavkm pranavkm merged commit e42f979 into master Apr 21, 2019
@pranavkm pranavkm deleted the prkrishn/systemtextjsonhelper branch April 22, 2019 16:02
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.

Add a System.Text.Json based IJsonHelper
4 participants