Serialization turns an object in memory into text you can send or store. Deserialization turns that text back into an object. In C# the default tool for JSON is System.Text.Json, which shipped with .NET Core 3.0 and is now the one to reach for. Newtonsoft.Json is still everywhere, still excellent, and still the answer when you need something System.Text.Json does not do yet.
Most JSON problems in C# come down to naming. C# properties are PascalCase, most JSON is camelCase, and almost everything in the first section below exists to close that gap.
JSON first below, then XML and YAML.
Naming, Ordering and What Gets Included
The part that causes the most trouble: making the JSON look the way the other side expects.
- System.Text.Json CamelCase Serialization
- Property Ordering in C# JSON Serialization
- Custom Naming Policy for JSON
- Polymorphic Serialization and Deserialization with System.Text.Json
- How to Exclude Properties From JSON Serialization
- How to Retrieve JSON Property Names
XML and YAML
The same jobs in the other two formats, including converting between JSON and XML.
- XML Deserialization
- Serialization and Deserialization with YamlDotNet
- Selecting Xml Nodes With XPath
- How to Read XML Documents
- Serializing Objects to XML
- How to Create XML Files
- How to Convert JSON to XML or XML to JSON
Deserializing JSON to Objects
JSON in, object out, including the case where you do not have a class to put it in.
- Serialization and Deserialization
- JSON Deserialization to a POCO Class
- How to Deserialize JSON Into Dynamic Object
- How to Read and Parse a JSON File
- How to Deserialize a Complex JSON Object
Serializing Objects to JSON
Object in, JSON out.
- How to Serialize Enum to a String
- How to Serialize a List to JSON
- How to Serialize a Dictionary to JSON
- How to Turn a C# Object Into a JSON String
- How to Serialize an Object into Query String Format
- How to Write a JSON Into a File
- How to Convert DataTable to JSON
- How to Get Formatted JSON
Reading and Changing JSON Directly
Sometimes you do not want a class at all, you just want a value out of a document.
- How to Get Value by Key from JObject
- How to Iterate Over JSON Objects
- How to Ensure a String Is Valid JSON
- Fastest Way to Convert a JObject to a Dictionary
- How to Compare Two Json Objects
- How to Convert JSON to DataTable
- How to Insert a Key Value Pair Into an Existing JSON
Where to Go Next
JSON usually arrives over the wire or out of a file:
Write the class you want first and let the serializer complain. Shaping a class around a setting you have not needed yet costs more than fixing it once you have.
