1

Recently I have decided to go from ASP.NET Core 2.2 to ASP.NET Core 3.1 and did not anticipate all the breaking changes; nearly every part of my application broke as most parts rely on JSON.

To safeguard against future JSON related problems, would it be possible to create an interface, mimicking the current Json implementation and override the default behaviour.

Most of my code relies on these two methods:

Json.Serialize() // used in my razor 
Json() // returns an IActionResult

Use case: a Razor Page : Json.Serialize Doc

<script>
   var myModel = @Html.Raw(Json.Serialize(Model))
</script>

Use case: a Controller

public async Task<IActionResult> AjaxGetRoleDetails(int id)
{
        return Json(await GetUserRoles(id));
}

Here are the methods that I would like, when the above methods are called respectively.

JsonConvert.SerializeObject() // override Json.Serialize
Content(JsonConvert.SerializeObject(), new MediaTypeHeaderValue("application/json")) // override Json()

How can override the system implementation, and call my own implementation for now, and later easily revert to the system's implementation when ASP.NET settles on a JSON implementation.

Wayne
  • 3,359
  • 3
  • 30
  • 50
  • Does this answer your question? [Where did IMvcBuilder AddJsonOptions go in .Net Core 3.0?](https://stackoverflow.com/questions/55666826/where-did-imvcbuilder-addjsonoptions-go-in-net-core-3-0) – Ian Kemp Mar 27 '20 at 09:09
  • No this does not answer the question as I was able to locate the option, my problem is that all my json fields end up being lower cased, and one could easily with allot of pain convert each method call. But since this is a bad option I am seeking a way to have a global setting. And the options does exist, but it is not entirely clear what the options should be. The DefaultContractResolver() is the one that preserves the case whereas CamelCasePropertyNamesContractResolver produces all lower case. It sounds strange because it is. – Wayne Mar 27 '20 at 09:20

1 Answers1

2

The default JSON serializer for ASP.NET Core is now System.Text.Json

So you could migrate over to using it.

Or, if you want to continue using Newtonsoft.Json in ASP.NET Core 3.0 and above, you can update your Startup.ConfigureServices to call AddNewtonsoftJson.

If you require things just like before, for example, in ASP.NET Core 2.2 then you can use the default contract resolver. E.g.

services.AddControllers().AddNewtonsoftJson(options =>
{
    options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
fuzzy_logic
  • 896
  • 11
  • 14
  • I have several files (+50 ) using this: @Html.Raw(Json.Serialize(Model)) and also several statements returning Json(); In my Startup I am already using .AddNewtonsoftJson(); And this does not have a global effect for me. Should it? – Wayne Mar 27 '20 at 06:40
  • services.AddControllers().AddNewtonsoftJson(); has no effect on @Html.Raw(Json.Serialize(Model)); the result is that all the fields are lowercase, and of course imagine the impact of that change. – Wayne Mar 27 '20 at 07:36
  • @Wayne Yes as far as I'm aware it should be global.. I just upgraded a 2.2 project with System.Text.Json causing too many issues. This change applied across my whole project.. How about updating the options in Startup.cs? `.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver());` – fuzzy_logic Mar 27 '20 at 08:10
  • @Wayne Also.. As per the [doco](https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#use-newtonsoftjson-in-an-aspnet-core-30-mvc-project) Did you install the [Microsoft.AspNetCore.Mvc.NewtonsoftJson](https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson) package? – fuzzy_logic Mar 27 '20 at 08:16
  • In my project assets.json I have this: "Microsoft.AspNetCore.Mvc.NewtonsoftJson >= 3.1.3" and in my Startup I have this: services.AddControllers().AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver()); And then in a .cshtml file I have this: The result is: formData: {"id":86,"email":"w@w.com","cell":"01234567890","name":"w","surname":"ww"} All the fields are lower case. – Wayne Mar 27 '20 at 08:20
  • 2
    Hmm.. How about this [answer](https://stackoverflow.com/a/55666898/1657427) which says "has an overload that allows you to configure the Json.NET options like you were used to with AddJsonOptions in ASP.NET Core 2.x.". I.e. maybe the default contract resolver: `services.AddControllers() .AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); });` – fuzzy_logic Mar 27 '20 at 08:55
  • fuzzy_logic please update your answer.. As the last option you mentioned works perfectly for me. – Wayne Mar 27 '20 at 09:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210425/discussion-between-fuzzy-logic-and-wayne). – fuzzy_logic Mar 27 '20 at 10:42