New Programming Model For Handling JSON In .NET 6

Introduction

.NET 6 is coming soon and it's packed with a lot of cool features. Writable JSON DOM API is one of those cool features that will be available from .NET 6 Preview 4 onwards. The purpose of adding a new feature is to ultimately reduce the pain of developers and provide flexibility to do development in a better way. So, this APIs ultimate goal is to provide a faster way to work with JSON Objects. This will give developers the flexibility to work with JSON objects. This will allow developers to skip writing POCO classes. That means it will save you from a lot of code that was created to develop multiple POCO classes for large objects and focus on business logic.

Following are the benefits of this API,

  1. A faster way to serialize objects when JSON Schema is not fixed. This means you can skip the creation of POCO classes.
  2. Large DOM subsections can be modified faster and efficiently. You can easily navigate to sections of the JSON structure. Also, You can use LINQ.
  3. Allow use of dynamic keyword, like dynamic obj = JsonNode.Parse(“my_json_object”) 
  4. Create a JSON Object using JsonObject Class.

Following types are available to work with,

Package Namespace to use is System.Text.Json.Node.

Class/Type Description Code Snippet
JsonNode Abstract class Used To Parse Json string and Get Values from node. JsonNode jsonNode = JsonNode.Parse("[JsonString]")
JsonObject Used to create Json object and can include JsonArray and Json Value var jsonObject = new JsonObject { ["childObject"] = new JsonObject { ["HelloProperty"] = "Hello", ["Array"] = new JsonArray(1, 2, 3) } };
JsonArray Used To Create JsonArray inside JsonObject var jsonArray = new JsonArray(1,2,3);
JsonValue Abstract class, when obtained any value from JsonNode this type is returned using Dictionary Format. var value = jsonNode[“HelloProperty”]; val value.GetType()


Project Structure

Program.cs

New Programming Model for Handling JSON in .NET 6

sample.json

New Programming Model for Handling JSON in .NET 6

Code Walkthrough

CREATE

Create a new JSON Object to store the list of employees.

var newJsonObject = new JsonObject {
    ["employeesList"] = new JsonArray("Aman", "Priyank", "Tejas", "Raj")
};
File.WriteAllText("employeesSample.json", newJsonObject.ToJsonString());

employeesSample.json

{"employeesList":["Aman","Priyank","Tejas","Raj"]}

READ

Read the Employee Name from sample.json,

var jsonString = File.ReadAllText("sample.json");
var jsonObject = JsonNode.Parse(jsonString);
System.Console.WriteLine(jsonObject["Employee"]["Name"]); // Output = Varun

UPDATE

Note
This feature is not possible with POCOs deserialization as they are fixed schema.

ADD PROPERTY IN NODE

Add Skills of the employee in Node in Existing JSON.

var jsonString = File.ReadAllText("sample.json");
var jsonObject = JsonNode.Parse(jsonString);
jsonObject["Employee"]["Skills"]=".NET, Azure, Angular";

ADD A NODE

Add Others Node containing personal information like age, country, etc.

jsonObject["Others"] = new JsonObject {
    ["Age"] = 29,
    ["Country"] = "India"
};
File.WriteAllText("sample.json", jsonObject.ToJsonString());

Sample.json

{"Employee":{"Name":"Varun","Designation":"Architect","Skills":".NET, Azure, Angular"},"Others":{"Age":29,"Country":"India"}}

Summary

This was just a glimpse of how this programming model works. We covered the following things,

  1. Features of Programming Model.
  2. Code walkthrough of how to implement and work with JSON. Operations such as,
    1. Create,
    2. Read and
    3. Update.

I will leave the delete operation of the node for you to explore. This will help you to learn more about this new programming model and its limitations.

That’s it! I hope this new programming model will help us understand the new ways of working with JSON in .NET. Thanks for staying until the end. Thanks for reading!!