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

17 posts, 0 answers
  1. Syed
    Syed avatar
    5 posts
    Member since:
    Nov 2010

    Posted 21 Oct 2019 Link to this post

    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.

     

  2. Syed
    Syed avatar
    5 posts
    Member since:
    Nov 2010

    Posted 21 Oct 2019 in reply to Syed Link to this post

    Forgot to attach the newtonsoft response.
  3. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1236 posts

    Posted 24 Oct 2019 Link to this post

    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.
  4. Syed
    Syed avatar
    5 posts
    Member since:
    Nov 2010

    Posted 25 Oct 2019 in reply to Veselin Tsvetanov Link to this post

    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

     

  5. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1236 posts

    Posted 30 Oct 2019 Link to this post

    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.
  6. Andrew
    Andrew avatar
    4 posts
    Member since:
    Jul 2013

    Posted 30 Oct 2019 Link to this post

    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. 
  7. Andrew
    Andrew avatar
    4 posts
    Member since:
    Jul 2013

    Posted 30 Oct 2019 Link to this post

    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();

     

     

  8. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1236 posts

    Posted 04 Nov 2019 Link to this post

    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.
  9. Andrew
    Andrew avatar
    4 posts
    Member since:
    Jul 2013

    Posted 04 Nov 2019 in reply to Veselin Tsvetanov Link to this post

    By Schema configuration do you mean the Entity Framework Classes / Database itself, or are you referencing some other Telerik option?
  10. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1236 posts

    Posted 07 Nov 2019 Link to this post

    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.
  11. Carl
    Carl avatar
    203 posts
    Member since:
    Oct 2012

    Posted 10 Nov 2019 Link to this post

    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.

  12. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1236 posts

    Posted 12 Nov 2019 Link to this post

    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.
  13. Carl
    Carl avatar
    203 posts
    Member since:
    Oct 2012

    Posted 28 May 2020 in reply to Veselin Tsvetanov Link to this post

    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?

  14. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1236 posts

    Posted 02 Jun 2020 Link to this post

    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.
  15. Marc Villella
    Marc Villella avatar
    2 posts
    Member since:
    Jul 2019

    Posted 26 Oct 2020 in reply to Veselin Tsvetanov Link to this post

    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?
  16. Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    1236 posts

    Posted 27 Oct 2020 Link to this post

    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/.

  17. Marc Villella
    Marc Villella avatar
    2 posts
    Member since:
    Jul 2019

    Posted 28 Oct 2020 in reply to Veselin Tsvetanov Link to this post

    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.
Back to Top