System.Text.Json – Dictionary Serialize and Deserialize

SystemTextJson Dictionary Serialize and Deserialize

Today in this article, we will learn how to use System.Text.Json – Dictionary Serialize and Deserialize.

As we know the new .NET /ASP.NET Core 3.1 0r ASP.NET 6.0 framework has removed the dependency on JSON.NET and uses its own JSON serializer i.e ‘System.Text.Json‘.

This new library has so far given many surprises to developers by not supporting dozens of good features which were readily available using JSON.NET (Newtonsoft).

There are known limitations in the System.Text.Json serializer which are as per specification and design considering performance and security aspects.

That means the best alternative is to find alternatives or write a custom converter for not supported features.

We shall see below aspects in today’s article,

Let’s see a few examples of how to perform JSON serialization and deserialization of Dictionary type using a new serializer.

Dictionary support in System.Text.Json

System.Text.Json has built-in support for only Dictionary types that matches Dictionary< string, TValue>.

That means built-in support works fine for the below dictionary samples,

Examples:

Dictionary<string, string> dictEmp = new Dictionary<string, string>();

Dictionary<string, int> dictEmp = new Dictionary<string, int>();

The below code produces JSON output and works fine,

var jsonoutPut = JsonSerializer.Serialize(dictEmp);

Dictionary types not supported in System.Text.Json

System.Text.Json doesn’t serialize/deserialize non-string values like Int, Boolean, or enum as Key types by default.

That means if the Key value is non-string, it throws NotSupportedException.

Examples of not supported Dictionary types,

Dictionary<int, string> dictEmployess = new Dictionary<int, string>();

Dictionary<object, string> dictEmployess = new Dictionary<int, string>();

Supporting Dictionary with non-string key

To allow non-string key serialization, please create the custom DictionaryConverter as a serializer option.

Creating a Custom Converter is pretty simple. You need to derive a class from the exact type( JsonConverter<T>) which you want to convert and provide the logic for the conversion in the Read and Write overridable methods.

Here T could be of any type as in below example,

Examples:

JsonConverter<Dictionary<object, string>>
JsonConverter<Dictionary<int, string>>
JsonConverter<Dictionary<Guid, string>>

I have a sample dictionary of type as below,

Dictionary<int, string> dictEmployess = new Dictionary<int, string>();

If you already have a custom converter please register your converter using JsonSerializerOptions.

I was able to serialize it using the custom converter DictionaryInt32Converter using below,

            var serializeOptions = new JsonSerializerOptions();
            serializeOptions.Converters.Add(new DictionaryInt32Converter());
            jsonoutPut = JsonSerializer.Serialize(dictEmployess);

There are already a few readily available sample converters on Microsoft documentation here.

Additionally, DictionaryInt32Converter example too which you can refer to.

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *