JSON stands for Javascript Object Notation, which is a format that encodes objects in a string. In JSON there are two important parts. One is Serialization, and the other one is - you guessed it - Deserialization.
What does Serialization or Deserialization even mean?
The easiest way I can describe it is, serialization means to convert an object to a string and deserialization is converting the string to an object (the opposite one).
In a nutshell, Serialization is the process of converting an object into a stream of bytes to store an object or save it into memory or a database, or even a file.
If you want to know more about serialization you can see this.
In this app, we are going to get data from a public API. I like programming jokes so I scrape from this ππ (You can visit here to customize your public jokes API for fun though)
So to consume API from your app there are lots of packages. Each package comes with its pros and cons. We are going to use the default HttpClient.
string _apiUrl = "https://sv443.net/jokeapi/v2/joke/Programming?type=single";
using var httpClient = new HttpClient();
var response = await httpClient.GetAsync(_apiUrl);
We are using HttpClient so we are using GetAsync(URL) function to get the results from the API. As it is the async method we are using await to get the result from the API. There are many ways to read the content of the incoming response. For the simple purpose, we are using ReadAsStreamAsync() to read this as a stream.
var responses = await response.Content.ReadAsStreamAsync();
if everything goes well, we should see the response as a stream. Now we have to convert this to a class to use this without any hassle.
So to typecast it into a class we are going to need a class. Let's create this class depending on the property that we will be getting from that API and named it JokesApi.
class JokesApi
{
public int Id { get; set; }
public string Category { get; set; }
public string Type { get; set; }
public string Joke { get; set; }
public bool Error { get; set; }
}
Now the deserialization techniques come into play. To deserialize any kind of JSON object to a class or record or struct we have to specify a JsonSerializationOptions to configure JsonSerializer.
var options = new JsonSerializerOptions {
PropertyNameCaseInsensitive = true
};
PropertyNameCaseInsensitive is there to tell C# do not care about the case when mapping the property from a JSON object.
Now it's time to deserialize it.
var jokeResponse = await JsonSerializer.DeserializeAsync<JokesApi>(responses, options);
if everything goes well, you should get your response mapped to a class that you can print on.
Final Code
using System.Text.Json;
string _apiUrl = "https://sv443.net/jokeapi/v2/joke/Programming?type=single";
using
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(_apiUrl);
var options = new JsonSerializerOptions {
PropertyNameCaseInsensitive = true
};
if (response.IsSuccessStatusCode) {
var responses = await response.Content.ReadAsStreamAsync();
var jokeResponse = await JsonSerializer.DeserializeAsync < JokesApi > (responses, options);
Console.WriteLine($ "{jokeResponse?.Category} -- {jokeResponse?.Joke}");
Console.WriteLine("Finished");
}
record JokesApi {
public int Id {
get;
set;
}
public string Category {
get;
set;
}
public string Type {
get;
set;
}
public string Joke {
get;
set;
}
public bool Error {
get;
set;
}
}
Happy Coding β