https://github.com/jsakamoto/dictionarystringobjectjsonconverter
A converter for System.Text.Json to deserialize a JSON string into a Dictionary<string, object> object as humans would expect.
https://github.com/jsakamoto/dictionarystringobjectjsonconverter
converter json
Last synced: 4 months ago
JSON representation
A converter for System.Text.Json to deserialize a JSON string into a Dictionary<string, object> object as humans would expect.
- Host: GitHub
- URL: https://github.com/jsakamoto/dictionarystringobjectjsonconverter
- Owner: jsakamoto
- License: mpl-2.0
- Created: 2022-09-12T12:18:12.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-12T13:15:31.000Z (almost 4 years ago)
- Last Synced: 2025-08-01T02:01:00.001Z (11 months ago)
- Topics: converter, json
- Language: C#
- Homepage:
- Size: 25.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DictionaryStringObjectJsonConverter [](https://www.nuget.org/packages/Toolbelt.Text.Json.Serialization.DictionaryStringObjectJsonConverter/) [](https://github.com/jsakamoto/DictionaryStringObjectJsonConverter/actions/workflows/unit-tests.yml)
## Summary
This is an attribute and a converter on the `System.Text.Json` infrastructure to deserialize a JSON string to a `Dictionary` object with each value of appropriate basic type, not `System.Text.JsonElement`.
## Usage
1. Add the `Toolbelt.Text.Json.Serialization.DictionaryStringObjectJsonConverter` NuGet package to your .NET project.
```shell
dotnet add package Toolbelt.Text.Json.Serialization.DictionaryStringObjectJsonConverter
```
2. Add the `[DictionaryStringObjectJsonConverter]` attribute to a property of the `Dictionary` type.
```csharp
using Toolbelt.Text.Json.Serialization;
public class MyType
{
[DictionaryStringObjectJsonConverter]
public Dictionary Items { get; set; } = new();
}
```
3. You can deserialize a JSON string to an object of the `Dictionary` type.
```csharp
using System.Text.Json;
var json = @"{\"items\": {
\"Lorem\": 1,
\"ipsum\": \"Two\",
\"amets\": [3.4, 5.6],
\"dolor\": true
}
}";
var obj = JsonSerializer.Deserialize(json);
obj.Items["Lorem"]; // -> 1, typeof(int)
obj.Items["ipsum"]; // -> "Two", typeof(string)
obj.Items["amets"]; // -> new[]{ 3.4, 5.6 }, typeof(double[])
obj.Items["dolor"]; // -> true, typeof(bool)
```
If you didn't add the `[DictionaryStringObjectJsonConverter]` attribute to a property of the `Dictionary` type, you would get all of the values in that dictionary object as the `System.Text.JsonElement` struct value instead of an appropriate basic type value of the .NET.
## License
[Mozilla Public License, version 2.0](https://github.com/jsakamoto/DictionaryStringObjectJsonConverter/blob/main/LICENSE)