Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Solved: Json JsonSerializer.Serialize of Data in NET6

03.01.2022 (👁3694)


 

Migrating von Newtonsoft Json Serial

 

New Version:

using System.Text.Json;

JsonSerializer.Serialize(menuItems, new JsonSerializerOptions(){IncludeFields = true}); 

 

Important:

The Serialization for Classes / Data Objects with Entity Framework will often be empty by default. But therefore the solution is to use the Option: new JsonSerializerOptions(){IncludeFields = true}); 

 

When  using Newtonsoft or Devextreme in previous Versions, then the serialization of Data was easy working.
but the migration to NET6 should delete Newtonsoft.

using Newtonsoft.Json;

 

Old Version: with Newtonsoft in Asp.Net and Net Core 1-3
JsonConvert.SerializeObject(menuItems);

 

Old Version with Devextreme:

JsonConvert.SerializeObject(DataSourceLoader.Load(menuItems, loadOptions));

 

 

 

 

 

using Microsoft.AspNetCore.Mvc;

using prada_drive.Models;

using prada_drive.Resources;

//using Newtonsoft.Json;

using System.Text.Json;

 

namespace .Controllers.Api

{

    //============< Namespace >============

    [Route("api/[controller]")]

    [ApiController]

    public class ApiSideMenu : ControllerBase

    {

        //========< Controller >========

        private LocalWords _localWords;

 

        public ApiSideMenu(LocalWords localWords)

        {

            _localWords = localWords;          

        }

 

        [HttpGet("GetMenuItems")]

        public object GetMenuItems() //(DataSourceLoadOptions loadOptions)

        {

            try

            {

                var menuItems = MenuModel.SideMenuItems;

 

                foreach (var item in menuItems)

                {

                    item.NameCurrentCulture = _localWords.GetWord(item.Name);

                }

 

           return JsonSerializer.Serialize(menuItems, new JsonSerializerOptions(){IncludeFields = true}); 

                //return JsonConvert.SerializeObject(menuItems);

                //return JsonConvert.SerializeObject(DataSourceLoader.Load(menuItems, loadOptions));

            }

            catch (Exception)

            {

                return BadRequest(_localWords.GetWord("ErrorRetrieveData").ToString());

            }

        }

        //========</ Controller >========

    }

 

 

 

    //============</ Namespace >============

}