← Back to C# Course | Chapter 14: File I/O & Streams | Lesson 6 of 6

Serialization Basics

In this page:

  1. Serialization Basics

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

markup
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}");
    }
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.