This is a migrated thread and some comments may be shown as answers.

data binding and serialization issue with System.Text.Json core 3.0

16 Answers 1863 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Syed
Top achievements
Rank 1
Syed asked on 21 Oct 2019, 04:23 PM

I am using the new System.Text.Json in asp.net core and I noticed that the grid does not bind to the model when using the new .net Core Json library.  If I use Newtonsoft.Json the bind binding works fine.

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(IISDefaults.AuthenticationScheme);
            services.AddKendo();
            services.AddControllersWithViews();

//.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
        }

 @(Html.Kendo().Grid<TestModel>()

        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(e => e.email);
            columns.Bound(e => e.username);
        })
    
    .HtmlAttributes(new { style = "height:430px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(100)
            .Read(read => read.Action("Read", "home"))
        )
        )

Model

public class TestModel
    {
       
        public string archiveGuid { get; set; }
        public string orgUid { get; set; }
        public string orgName { get; set; }
        public string orgExtRef { get; set; }
        public string userUid { get; set; }
        public string username { get; set; }
        public string email { get; set; }
       
    }

Here is what is returned with NewtonSoft JSON.  I also noticed that D in the data response sent is capitalized when using Newtonsoft.json.  Any ideas?  It could be due to the contract resolver not being set when we are using the System.Text.Json as posted here https://www.telerik.com/forums/grid---binding-doesn't-work but I don't think there is a contract resolver option for the System.Text.Json.

 

16 Answers, 1 is accepted

Sort by
0
Syed
Top achievements
Rank 1
answered on 21 Oct 2019, 04:24 PM
Forgot to attach the newtonsoft response.
0
Veselin Tsvetanov
Telerik team
answered on 24 Oct 2019, 11:05 AM

Hello Syed,

The issue that you have observed is exactly the one discussed in the forum thread in question. If you would like to skip the Newtonsoft.Json in your project, you should define a Custom() DataSource with a schema. Just like Viktor has suggested in his latest post:

https://www.telerik.com/forums/grid---binding-doesn't-work#nvbEGC71DE6jUybCHyH_Bg

Regards,
Veselin Tsvetanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Syed
Top achievements
Rank 1
answered on 25 Oct 2019, 12:51 PM

Thanks Veselin.  Is this information documented somewhere?  I found a reference to this on stackoverflow but the talking point 4 does not mention setting up serialization option for JSON

 

0
Veselin Tsvetanov
Telerik team
answered on 30 Oct 2019, 09:57 AM

Hello Syed,

The ToDataSourceResult() call will return a Kendo.Mvc.UI.DataSourceResult which have all its properties capitalized. As a result, the DataSource utility on the client would expect the Data member to be with a capital letter.

You are correct that the Serialization options configuration is not present in our Getting started articles and that is something we should take care of. By the way, as you will be using the System.Text.Json namespace, here is how to configure it, so that it does not change the casing of the properties:

services
    .AddMvc(options => options.EnableEndpointRouting = false)
    .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
    .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);

The above will allow you to avoid the Custom DataSource definition, as it will keep the leading capital letters.

Regards,
Veselin Tsvetanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
1
Andrew
Top achievements
Rank 1
answered on 30 Oct 2019, 04:19 PM
Is there a plan / timeline to make the Telerik objects read the JSON response compatible with the default .NET Core 3 serialization approach?  I'd rather not force my API to return what is essentially non-standard JSON just so that the Telerik controls can read it. 
0
Andrew
Top achievements
Rank 1
answered on 30 Oct 2019, 05:07 PM

For anyone else like me who was trying to use this in Razor Pages / Controllers, the fix for json casing provided by Veselin will look more like this...it brought my grid to life anyway:

services.AddRazorPages();
services.AddControllers()
    .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
services.AddKendo();

 

 

0
Veselin Tsvetanov
Telerik team
answered on 04 Nov 2019, 09:15 AM

Hi Andrew,

We will consider the implementation of a modified DataSource type that would respect the .Net Core default serialization configuration. That is, however, currently not scheduled for implementation. What needs to be done in that case is to modify the Schema configuration to use camalCase properties instead of PascalCase.

Regards,
Veselin Tsvetanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Andrew
Top achievements
Rank 1
answered on 04 Nov 2019, 02:45 PM
By Schema configuration do you mean the Entity Framework Classes / Database itself, or are you referencing some other Telerik option?
0
Veselin Tsvetanov
Telerik team
answered on 07 Nov 2019, 10:59 AM

Hello Andrew,

I am referring to the DataSource Schema configuration which will need to be automatically generated when the DataSource is defined within an HTML helper with a defined model like the following:

@(Html.Kendo().Grid<TestModel>()

When receiving item properties with lower case, that schema should properly map the camelCase field to the PascalCase property from the model. To do that we will need to implement a new type of DataSource, that will make the above conversion automatically. 

Regards,
Veselin Tsvetanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Carl
Top achievements
Rank 1
answered on 11 Nov 2019, 12:36 AM

How about a blog post about what will be recommended going forward in the future? Should we just continue to use NewtonSoft.Json or should we use the new Microsoft version of Json?  Should we have to worry about things breaking in the data grid?  I have already lost a considerable amount of wasted time before I realized that the problem I had with my broken Telerik Grids was my failure to address the JSON problem, and differences between NewtonSoft and Microsoft when migrating from ASP.NET Core 2.2 to Core 3.1.

Since Telerik always advertises that they make things easy for developers, why not support both bindings out-of-the-box for the Telerik Data Grid control?  Then you could just add a property switch for NewtonSoft JSON versus Microsoft JSON.  Or alternatively, a property corresponding to an Enum with at least 3 different options: NewtonSoft, Microsoft and Custom.

Thanks for considering this suggestion.

0
Veselin Tsvetanov
Telerik team
answered on 12 Nov 2019, 12:49 PM

Hi Carl,

Your idea to create a resource explaining how the System.Text.Json could be configured to substitute the Newtonsoft.Json in a .Net Core 3.0 project is indeed a very good suggestion. We will include that task in our implementation queue and will create that resource as soon as our priorities allow that. 

As per the suggested property switch in the Grid (or better its DataSource), that would be something similar to what we have discussed with Andrew. Note that the default behavior of changing the beginning letter of the fields does not differ in System.Text.Json compared to Newtonsoft.Json. The default serialization in .Net Core applications (since version 1.0) has always been changing PascalCase fields to camelCase. Therefore, concerning the data that is being transferred between the server and the client, there are no changes in the default format. What has changed, and needs to be explained, is the change in the default Json resolver. In consequence, when the Sytem.Text.Json is used, .Net Core 3.0 projects require a different configuration compared to previous .Net Core versions.

Regards,
Veselin Tsvetanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Carl
Top achievements
Rank 1
answered on 28 May 2020, 05:56 PM

What is latest update from Telerik on status of use of NewtonSoft.Json versus Microsoft System.Text.Json?

Microsoft has documentation at

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to

But what is Telerik's position vis-a-vis use of JSON libraries by Telerik UI for Net Core?  And for Net 5 compatible UI libraries for use when Net 5 is released next November?

Has Telerik prepared a blog post discussing these questions?

0
Veselin Tsvetanov
Telerik team
answered on 02 Jun 2020, 09:51 AM

Hello Carl,

We have included the following documentation article on the serialization topic:

https://docs.telerik.com/aspnet-core/compatibility/json-serialization

Since .Net Core 3 and the release of the System.Text.Json library Newtonsoft.Json is no longer needed in projects using Telerik UI for ASP.NET Core.

Regards,
Veselin Tsvetanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
n/a
Top achievements
Rank 1
answered on 26 Oct 2020, 03:04 PM
I'm using Telerik.Reporting R3 2020 and it appears Newtonsoft.Json is still required (and incompatible) with System.Text.Json in terms of being used at the same time (I've got another grid that is leveraging System.Text.Json).  Can you confirm this is true and advise if there's any solution to enable Telerik.Reporting in aspnet core 3.0 to get both to work in parallel?
0
Veselin Tsvetanov
Telerik team
answered on 27 Oct 2020, 06:54 AM

Hi Marc,

In order to preserve the casing of the property names, you should configure the contract resolver/property naming policy used by the application. In .Net Core 3.0 that would be:

            // Add framework services.
            services
                .AddControllersWithViews()
                .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
                // Maintain property names during serialization. See:
                // https://github.com/aspnet/Announcements/issues/194
               .AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

While in .Net Core 3.1:

            services.AddMvc(s => s.EnableEndpointRouting = false)
               .AddJsonOptions(o=> { o.JsonSerializerOptions.PropertyNamingPolicy = null; });

Then you could use the following endpoint for the Grid Read action, which will keep the casing of the properties in the items:

public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)
{
    DataSourceResult result =  orders.ToDataSourceResult(request);
    return Json(result);
} 

Regards,
Veselin Tsvetanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
n/a
Top achievements
Rank 1
answered on 28 Oct 2020, 05:01 PM
Thanks, Veselin! I was using another grid component (not Telerik), and was able to configure their component to use Newtonsoft.  As such, I resolved my issue that way for the time being.
Tags
Grid
Asked by
Syed
Top achievements
Rank 1
Answers by
Syed
Top achievements
Rank 1
Veselin Tsvetanov
Telerik team
Andrew
Top achievements
Rank 1
Carl
Top achievements
Rank 1
n/a
Top achievements
Rank 1
Share this question
or