System.Text.Json
is a namespace in the .NET framework that provides functionality for working with JSON (JavaScript Object Notation) data. JSON is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate.
In .NET, System.Text.Json
includes classes and methods for both serializing (converting objects to JSON) and deserializing (converting JSON back to objects). It is the default JSON serialization library in .NET starting from version 5.0.
Key classes and features in System.Text.Json
include:
- JsonSerializer: This class provides methods for serializing and deserializing objects to and from JSON. It supports a wide range of options for controlling the serialization process.
- JsonDocument: Represents an immutable JSON document. It allows you to parse and navigate JSON data without the need to deserialize it into strongly-typed objects.
- JsonElement: Represents a single JSON value within a
JsonDocument
. It provides methods for navigating and extracting data from a JSON document. - JsonConverter: A base class for creating custom converters to handle serialization and deserialization of specific types.
Here’s a simple example of using System.Text.Json
to serialize an object to JSON and then deserialize it back:
using System;
using System.Text.Json;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main()
{
// Serialize
Person person = new Person { Name = "John Doe", Age = 30 };
string json = JsonSerializer.Serialize(person);
Console.WriteLine(json);
// Deserialize
Person deserializedPerson = JsonSerializer.Deserialize<Person>(json);
Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}");
}
}
This example demonstrates basic serialization and deserialization using System.Text.Json
. The library is quite flexible and can handle more complex scenarios and customization as needed.
System.Text.Json
in .NET offers various features and options for JSON serialization and deserialization. Below is a list of some key features along with examples:
Simple Serialization/Deserialization:
Example:
string jsonString = JsonSerializer.Serialize(myObject);
MyClass deserializedObject = JsonSerializer.Deserialize<MyClass>(jsonString);
Custom Naming Policy:
Example:
JsonSerializerOptions options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
string jsonString = JsonSerializer.Serialize(myObject, options);
Ignore Null Values:
Example:
JsonSerializerOptions options = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
string jsonString = JsonSerializer.Serialize(myObject, options);
Indented Formatting:
Example:
JsonSerializerOptions options = new JsonSerializerOptions
{
WriteIndented = true,
};
string jsonString = JsonSerializer.Serialize(myObject, options);
Handling Enums:
Example:
JsonSerializerOptions options = new JsonSerializerOptions
{
Converters = { new JsonStringEnumConverter() },
};
string jsonString = JsonSerializer.Serialize(myObject, options);
Custom Converters:
Example:
public class CustomConverter : JsonConverter<MyType>
{
// Implement Read and Write methods
}
JsonSerializerOptions options = new JsonSerializerOptions
{
Converters = { new CustomConverter() },
};
string jsonString = JsonSerializer.Serialize(myObject, options);
Handling Polymorphism:
Example:
JsonSerializerOptions options = new JsonSerializerOptions
{
DefaultDiscriminatorValue = "discriminator",
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
string jsonString = JsonSerializer.Serialize(myObject, options);
Ignoring Properties:
Example:
public class MyClass
{
[JsonIgnore]
public string IgnoredProperty { get; set; }
}
Handling Circular References:
Example:
JsonSerializerOptions options = new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.Preserve,
};
string jsonString = JsonSerializer.Serialize(myObject, options);
Reading and Writing JSON Documents:
Example:
JsonDocument jsonDoc = JsonDocument.Parse(jsonString);
JsonElement root = jsonDoc.RootElement;
These examples cover a range of scenarios, and there are more options and features available in System.Text.Json
. The specific features you need will depend on the requirements of your application and how you want to handle JSON serialization and deserialization.