✅ Serialize and Deserialize JSON using built in components

Im trying to get rid of my habit of blasting Newtonsoft.Json everywhere since many ppl here have told me its bad practice, how would i go on about serializing and deserializing Objects and JSON strings (for example turn a JSON string into a given Object)
40 Replies
The Fog from Human Resources
another question is how i could implement the principal on "Sub-Objects" for example if we have a Post Object, which has an "Author" variable, which is another Object how could i construct that sub object its something i work with pretty often so this is pretty much the most important to me
tera
tera10mo ago
check out System.Text.Json
The Fog from Human Resources
thats what im trying to get educated about here
CoRoys
CoRoys10mo ago
How to serialize JSON in C# - .NET
Learn how to use the System.Text.Json namespace to serialize to JSON in .NET. Includes sample code.
CoRoys
CoRoys10mo ago
System.Text.Json Namespace
Provides high-performance, low-allocating, and standards-compliant capabilities to process JavaScript Object Notation (JSON), which includes serializing objects to JSON text and deserializing JSON text to objects, with UTF-8 support built-in. It also provides types to read and write JSON text encoded as UTF-8, and to create an in-memory document...
The Fog from Human Resources
thanks
CoRoys
CoRoys10mo ago
Just make that Author variable the same type as the main Object It'll keep nesting it until there's no more data from your JSON
The Fog from Human Resources
ah so that means lets say this is my JSON
{
"Title": "The Best Post ever",
"Content": "Some Post Content",
"Author": {
"Name": "EpicName",
"Description": "Some Epic Description abt cats"
}

}
{
"Title": "The Best Post ever",
"Content": "Some Post Content",
"Author": {
"Name": "EpicName",
"Description": "Some Epic Description abt cats"
}

}
then i have a C# object
public string title;
public string content;
public Author author;
public string title;
public string content;
public Author author;
and an Author
public string name;
public string description;
public string name;
public string description;
do i just parse the "Author" key into the "author" variable and it will go into the object and create it based on the keys given? (assuming i have added the proper stuff to make the valid keys andstuff)
Jimmacle
Jimmacle10mo ago
if you deserialize the top-level object it will automatically deserialize all its properties and so on into that one object
The Fog from Human Resources
so like this?
Jimmacle
Jimmacle10mo ago
you'd just JsonSerializer.Deserialize<Post>(json);
The Fog from Human Resources
yes but why would it deserialize the author as well
Jimmacle
Jimmacle10mo ago
because it's part of the object
The Fog from Human Resources
dont i need to assign the JSON keys to the values
Jimmacle
Jimmacle10mo ago
no, that's why you're using a serializer it does that for you
The Fog from Human Resources
but how would the JSON know where to put itself
Jimmacle
Jimmacle10mo ago
? it uses your C# object definitions
The Fog from Human Resources
if i dont need to do anything how does it know what to assign i kept reading smth abt a [JsonPropertyValue] thing
Jimmacle
Jimmacle10mo ago
you don't have to do anything like that if you follow conventions
The Fog from Human Resources
:SCgetoutofmyhead: ill try smth
Jimmacle
Jimmacle10mo ago
it will map JSON properties to C# object properties based on whatever naming convention you pick
tera
tera10mo ago
and remember to use properties, not fields
The Fog from Human Resources
:SCshocked: i dont understand
Jimmacle
Jimmacle10mo ago
read the articles linked earlier
The Fog from Human Resources
for context ive never used the Newtonsoft.Json serializers either i manually assigned the values so my knowledge abt this is 0
Jimmacle
Jimmacle10mo ago
tl;dr the serializer is magic and knows how to turn json into C# objects and vice versa as long as the names match up according to the naming convention you tell it to use
The Fog from Human Resources
will it throw an exception on null? or missing things for example if "Title" in json isnt present at all
Jimmacle
Jimmacle10mo ago
it depends, look at the docs
The Fog from Human Resources
ill try to turn my example into a real thing its all null.
Jimmacle
Jimmacle10mo ago
the docs tell you all the ways you can indicate a property must be present in the json
The Fog from Human Resources
it works :SCshocked: this is magic :SCshocked: so now that that works. is there a way i can change the variable name cause i sometimes work with JSON that includes values that the user wouldnt understand
CoRoys
CoRoys10mo ago
[JsonPropertyName("customName")] on top of your property
CoRoys
CoRoys10mo ago
Are you showing your user variable names lol?
The Fog from Human Resources
nono i mean when i made a library the devs are my users in that case
CoRoys
CoRoys10mo ago
yeah either set a naming convention for your JSON field types (so that System.Text.Json knows how to translate it back to C#'s property name convention) or explicitly set each JSON field's name yourself
The Fog from Human Resources
i cant believe i havent used this before :SCgetoutofmyhead:
CoRoys
CoRoys10mo ago
btw fields can be serialised, just not by default but just use properties instead
The Fog from Human Resources
thanks yall
Want results from more Discord servers?
Add your server