Question

This JSON:

{
    "Values": {
        "Category": "2",
        "Name": "Test",
        "Description": "Testing",
        "Expression": "[Total Items] * 100"
    }
}

Is being deserialized to this DTO using JsConfig.ConvertObjectTypesIntoStringDictionary = true;:

public class MyDto {
    public Dictionary<string, object> Values { get; set; }
}

Unexpectedly, the Expression property is deserialized as List<object> instead of string (see screenshot).

Screenshot

It seems the parser sees the opening bracket and tries to interpret the value as an array. Is there some way to turn this "type detection" off, or otherwise resolve the problem (without changing the DTO)?

I'm using ServiceStack v3.9.71.

Was it helpful?

Solution

Json.NET is able to deserialize your example JSON into an instance of your DTO class. It seems that the JSON parsing library you're using is buggy. Are you able to switch?

internal class Program
{
    private static void Main()
    {
        const string json = @"{
""Values"": {
    ""Category"": ""2"",
    ""Name"": ""Test"",
    ""Description"": ""Testing"",
    ""Expression"": ""[Total Items] * 100""
    }
}";
        var myDto = JsonConvert.DeserializeObject<MyDto>(json);
    }
}
public class MyDto
{
    public Dictionary<string, object> Values
    {
        get;
        set;
    }
}

Json.NET works

EDIT:

I switched my code to use ServiceStack.Text from NuGet and I was able to get the serialization to work perfectly:

var myDto = JsonSerializer.DeserializeFromString<MyDto>(json);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top