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
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 >============ } |