-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Conversation
public IHtmlContent Serialize(object value) | ||
{ | ||
// JsonSerializer always encodes | ||
var json = JsonSerializer.ToString(value, value.GetType(), _mvcOptions.SerializerOptions); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 } }; |
There was a problem hiding this comment.
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.
public IHtmlContent Serialize(object value) | ||
{ | ||
// JsonSerializer always encodes | ||
var json = JsonSerializer.ToString(value, value.GetType(), _mvcOptions.SerializerOptions); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
955404c
to
9176508
Compare
8f39208
to
c138325
Compare
public IHtmlContent Serialize(object value) | ||
{ | ||
// JsonSerializer always encodes | ||
var json = JsonSerializer.ToString(value, value?.GetType() ?? typeof(object), _mvcOptions.SerializerOptions); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
Fixes #9564