Summary - Serialization
Combine Serialize, Deserialize and options to write a config file.
Summary - Serialization
In this group you learned serialization: JsonSerializer.Serialize turns an object into text, Deserialize<T> turns text into an object, [JsonPropertyName] customizes key names, JsonSerializerOptions controls formatting, List<T> serializes as an array.
In the final lesson you will write a config file flow: convert an object to text, save to a file, read it back and convert to an object. This is the foundation of data storage in real applications.
Exercise Save and load settings
The Settings class below is ready. In Main: (1) create a Settings instance (Theme = "Dark", Volume = 80); (2) serialize with JsonSerializer.Serialize; (3) save with File.WriteAllText("settings.txt", text); (4) read back with File.ReadAllText("settings.txt"); (5) deserialize with JsonSerializer.Deserialize<Settings>; (6) print loaded.Theme with Console.WriteLine.
Show example solution
var s = new Settings { Theme = "Dark", Volume = 80 }; string text = JsonSerializer.Serialize(s); File.WriteAllText("ayarlar.txt", text); string loaded = File.ReadAllText("ayarlar.txt"); var settings = JsonSerializer.Deserialize<Settings>(loaded); Console.WriteLine(settings.Theme);
Example solution:
using System;
using System.IO;
using System.Text.Json;
class Settings
{
public string Theme { get; set; }
public int Volume { get; set; }
}
class Program
{
static void Main()
{
var s = new Settings { Theme = "Dark", Volume = 80 };
string text = JsonSerializer.Serialize(s);
File.WriteAllText("ayarlar.txt", text);
string loaded = File.ReadAllText("ayarlar.txt");
var settings = JsonSerializer.Deserialize<Settings>(loaded);
Console.WriteLine(settings.Theme);
}
}
Exercise Save and load a list
Serialize the people list with JsonSerializer.Serialize, save to people.txt with File.WriteAllText. Then read back with File.ReadAllText and convert to a list with JsonSerializer.Deserialize<List<Person>>. Print the name of the first element with Console.WriteLine.
Show example solution
string text = JsonSerializer.Serialize(people); File.WriteAllText("people.txt", text); string loaded = File.ReadAllText("people.txt"); var list = JsonSerializer.Deserialize<List<Person>>(loaded); Console.WriteLine(list[0].Name);
Example solution:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
var people = new List<Person>
{
new Person { Name = "Alex", Age = 30 },
new Person { Name = "Sam", Age = 25 }
};
string text = JsonSerializer.Serialize(people);
File.WriteAllText("people.txt", text);
string loaded = File.ReadAllText("people.txt");
var list = JsonSerializer.Deserialize<List<Person>>(loaded);
Console.WriteLine(list[0].Name);
}
}
Test yourself
-
Which turns an object into text?
- Deserialize<T>
- Serialize
- JsonPropertyName
- WriteIndented
Show answer
JsonPropertyName — Correct. Serialize turns an object into text.
-
Match each method to its purpose
- Serialize
- Object to text
- Deserialize<T>
- Text to object
- JsonPropertyName
- Key name
- JsonSerializerOptions
- Format setting