Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to deserialize a json string or stream to Dictionary<string,string> with System.Text.Json

I’m getting a string of json as a stream and trying to deserialize it to a Dictionary<string, string> the problem with that is that it chokes on numbers, even when the serialize options are set. How does one do this with System.Text?

Program.cs with .NET 6:

using System;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;


Console.WriteLine("Converting Json...");
var result = await DeserializeJson();
Console.WriteLine($"result: {result}");


async Task<Dictionary<string, string>> DeserializeJson()
{
    var jsonText = "{\"number\": 709, \"message\": \"My message here\",\"bool\": true}";
    var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonText));

    var options = new JsonSerializerOptions 
    { 
         PropertyNameCaseInsensitive = true,
         NumberHandling = JsonNumberHandling.WriteAsString 
    };
    var fullResponse = await JsonSerializer.DeserializeAsync<Dictionary<string, string>>(stream, options);

    return fullResponse;
}

The main error that results:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

---> System.InvalidOperationException: Cannot get the value of a token type 'Number' as a string.

This would make sense if it wasn’t for the fact that I set the handle numbers property of the serialization options. Is that known to be failing or is something wrong here?

>Solution :

Your json does not contain only strings. As a quick (but not performant one) fix you can try to deserealize to Dictionary<string, object> and then convert it to Dictionary<string, string>:

async Task<Dictionary<string, string>> DeserializeJson()
{
    var jsonText = "{\"number\": 709, \"message\": \"My message here\",\"bool\": true}";
    var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonText));

    var options = new JsonSerializerOptions 
    { 
        PropertyNameCaseInsensitive = true,
        NumberHandling = JsonNumberHandling.WriteAsString ,
        
    };
    var fullResponse = await JsonSerializer.DeserializeAsync<Dictionary<string, Object>>(stream, options);

    return fullResponse?.ToDictionary(pair => pair.Key, pair => pair.ToString());
}

Or parse the document manually:

async Task<Dictionary<string, string>> DeserializeJson()
{
    var jsonText = "{\"number\": 709, \"message\": \"My message here\",\"bool\": true}";
    var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonText));
  
    var jsonDocument = JsonDocument.Parse(stream);
    var dictionary = jsonDocument.RootElement
        .EnumerateObject()
        .ToDictionary(property => property.Name, property => property.Value.ToString());
    return dictionary;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading