-3

I know there are many similar questions on SO, however all the ones I've found require a shared base class in order to work.

With a stream of JSON data like this:

[
  {
    "webhookType": "order",
    "data": {
      "id": "eeiefj393",
      "orderProperty": "Value"
    }
  },
  {
    "webhookType": "customer",
    "data": {
      "id": 29238,
      "customerProperty": "Value"
    }
  }
]

I wish to deserialize this into two containers, List<Customer> and List<Order>. Where the two classes are as follows:

class Order
{
    public string Id { get; set; }
    public string OrderProperty { get; set; }
    [...]
}

class Customer
{
    public long Id { get; set; }
    public string CustomerProperty { get; set; }
    [...]
}

There may be shared property names however there are no shared properties + type between these two classes and so the solutions involing a sub class aren't working for me.

Alasdair
  • 19
  • 4

1 Answers1

1

You need to create a JsonConverter.

DataConverter

public class WebHookConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.StartObject)
        {
            JObject item = JObject.Load(reader);

            if (item["webhookType"].Value<string>() == "order")
            {
                var webhook = new WebHook
                {
                    Type = item["webhookType"].Value<string>(),
                    Data = item["data"].ToObject<Order>()
                };

                return webhook;
            }
            else if (item["webhookType"].Value<string>() == "customer")
            {
                var webhook = new WebHook
                {
                    Type = item["webhookType"].Value<string>(),
                    Data = item["data"].ToObject<Customer>()
                };

                return webhook;
            }
        }

        return null;
    }

    public override bool CanConvert(Type objectType)
    {
        throw new NotImplementedException();
    }
}

Objects

[JsonConverter(typeof(WebHookConverter))]
public class WebHook
{
    [JsonProperty("webhookType")]
    public string Type { get; set; }
    public object Data { get; set; }
}

public class Order
{
    public string Id { get; set; }

    [JsonProperty("orderProperty")]
    public string Property { get; set; }
}

public class Customer
{
    public long Id { get; set; }

    [JsonProperty("customerProperty")]
    public string Property { get; set; }
}

Serialization

var json = File.ReadAllText("json1.json");
var obj = JsonConvert.DeserializeObject<List<WebHook>>(json);

var orderList = obj.Where(o => o.Type == "order").Select(o => o.Data).ToList();
var customerList = obj.Where(o => o.Type == "customer").Select(o => o.Data).ToList();

Output:
enter image description here

tontonsevilla
  • 2,649
  • 1
  • 11
  • 18