✅ 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
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
check out System.Text.Json
thats what im trying to get educated about here
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.
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...
thanks
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 JSONah
so that means
lets say this is my JSON
then i have a C# object
and an Author
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)
if you deserialize the top-level object it will automatically deserialize all its properties and so on into that one object
so like this?
you'd just
JsonSerializer.Deserialize<Post>(json);
yes but
why would it deserialize the author as well
because it's part of the object
dont i need to assign the JSON keys to the values
no, that's why you're using a serializer
it does that for you
but how would the JSON know where to put itself
?
it uses your C# object definitions
if i dont need to do anything how does it know what to assign
i kept reading smth abt a [JsonPropertyValue] thing
you don't have to do anything like that if you follow conventions
:SCgetoutofmyhead:
ill try smth
it will map JSON properties to C# object properties based on whatever naming convention you pick
and remember to use properties, not fields
:SCshocked:
i dont understand
read the articles linked earlier
for context ive never used the Newtonsoft.Json serializers either i manually assigned the values
so my knowledge abt this is 0
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
will it throw an exception on
null
?
or missing things
for example if "Title" in json isnt present at allit depends, look at the docs
ill try to turn my example into a real thing
its all null.
the docs tell you all the ways you can indicate a property must be present in the json
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
[JsonPropertyName("customName")]
on top of your propertyoki
Are you showing your user variable names lol?
nono i mean when i made a library
the devs are my users in that case
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
How to customize property names and values with System.Text.Json - ...
Learn how to customize property names and values when serializing with System.Text.Json in .NET.
i cant believe i havent used this before :SCgetoutofmyhead:
btw fields can be serialised, just not by default
but just use properties instead
thanks yall