Refactoring objects issue

So a long long time ago i implemented Objects and how they are set in a very stupid way, which i am now correcting by adapting them to be compatible with the default JSON Serializer and bringing the code to overall more structure by following C# conventions more My issue I want to structure the following object (shown in the screenshot) to use [JsonPropertyName] and such instead of the constructor with a JObject (from Newtonsoft JSON), which would be no problem, however, as you can see some values use a different key (for example ndcId is in "o":{"ndcId":0000}) while others are more nested (like "o":{"chatmessage":{"content"}}) What would be the proper approach to serialize them with the default serializer while keeping them in the same class as i dont want to split up my class for every branch of "o"
18 Replies
Pobiega
Pobiega2mo ago
You would turn it into a two-step process. Deserialize to a model that matches the JSON (several records), then map to a model with a flat structure the way you want it
Somgör from Human Resources
Do you have any examples of that? I don't quite understand
Pobiega
Pobiega2mo ago
Make classes that one-to-one reflect the JSON structure Deserialize to that
Somgör from Human Resources
But isn't that just creating like 3 classes for one object?
Pobiega
Pobiega2mo ago
Then, make a mapping method/constructor like above, that turns that nested structure into a flat structure as you want { means object in json
Pobiega
Pobiega2mo ago
You have more than one object here
Somgör from Human Resources
:SCcrying: Is there any other way.
Pobiega
Pobiega2mo ago
Any other way would be a hack ¯\_(ツ)_/¯
Somgör from Human Resources
What if I just deserialize it into an object from "o"/"chatMessage"
Pobiega
Pobiega2mo ago
I don't understand
Somgör from Human Resources
Like Instead of passing the whole JSON, I make the root point "o"/"chatMessage" so it can deserialize from there
Pobiega
Pobiega2mo ago
Sure
Somgör from Human Resources
Would that also be a hack.
Pobiega
Pobiega2mo ago
Not really?
Somgör from Human Resources
I really don't need the _type value it's more used in the socket that checks the value So I can safely skip it
Pobiega
Pobiega2mo ago
If you change the JSON structure, your deserializarion types reflect that change But how would you extract the o/chatMessage without deserializing the JSON? Anyways, gonna sleep. Nn
Somgör from Human Resources
There's some way to get JSON from a specific JSON key in the default serializer Good night
Want results from more Discord servers?
Add your server
More Posts
A problem while making a request for API in maui .neti try to do request to my api But the application stops I do not get any specific error, but the app✅ How to play an MP3 from byte data array instead of writing it to a file and then playing it?Hey, anyone know how I can play an MP3 directly from a byte array? This is what I currently have whI need help with my projectI need to create a password generator and checker and i need help to complete it. can you help me✅ Windows Subsystem for Android Alternativeson my win 11 pc i use WSA to compile my .NET MAUI code (C#). it shows a **desktop window** with the Access Token Expiry date suggestionsWhat does you guys think, should be the expiry of an access token? I set the expiry of the refresh tInterface implementing generic parametersHello, is there way to achieve something like this? ```cs interface IFoo<T> : T { } ``` I want to spHow to encapsulate the code that has been processed using the Newtonsoft Json library?I used the Newtonsoft Json library to write data to a JSON file, but I don't know how to encapsulateis this a bad practice```var result = await HttpContext.AuthenticateAsync( CookieAuthenticationDefaults.Aut✅ Read `launchsettings.json` environment variable from a terminal script in RiderIt looks like the only way to get Rider to reload real environment variable values is to restart thewhat is the best way to access the Model object in a razor view, in a javascript?``var model = @Html.Raw(Json.Encode(Model));`` ^ is this a good way