Serialization Basics
In this page:
Serialization Basics
Serialization converts an object into a storable or transmittable format, like JSON, and deserialization reverses the process. System.Text.Json provides JsonSerializer.Serialize and JsonSerializer.Deserialize for this in modern .NET. This is the standard way to save structured data to a file or send it over a network.
Note: Public properties (not fields) are what System.Text.Json serializes by default.
Example: Serialization Basics
using System;
using System.Text.Json;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
Person p = new Person { Name = "Nina", Age = 27 };
string json = JsonSerializer.Serialize(p);
Console.WriteLine(json);
Person restored = JsonSerializer.Deserialize<Person>(json);
Console.WriteLine($"{restored.Name}, {restored.Age}");
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: