Use JsonConverter to deserialize JToken to bool

I have an odd JSON object I need to deserialize.

{
  "data": {
    "id": 123,
    "permissions": {
      "appName": {
        "data": {
          "1": {
            "2021-08-01": {
              "2020": {
                "isAllowed": {}
              }
            }
          }
        }
      }
    }
  }
}

I've gotten most of it to work with a wrapper and nested dictionaries but the final part isn't working as expected. It works fine if I create a class like this, but using it is clunky.

public class DataWrapper<T>
{
   public T Data { get; set;}
}

public class PermissionValues
{
   public JToken IsAllowed { get; set; }
}

DataWrapper<Dictionary<int, Dictionary<DateTime, Dictionary<int, PermissionValues>>>>

I'd like to change JToken to a bool. When I do that and add a JsonConverter

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    return reader.Value != null;
}

I get an error:

Newtonsoft.Json.JsonSerializationException: 'Could not convert string 'isAllowed' to dictionary key type 'System.Int32'. Create a TypeConverter to convert from the string to the key type object. Path 'data.permissions.appName.data.1.2021-08-01.2020.isAllowed'

Any idea what I am missing?



from Recent Questions - Stack Overflow https://ift.tt/39Eo0mF
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation