I am calling an API that expects my data in a format that looks like this:
{
"cartId":null,
"id":"944990015513953203",
"quantity":"3",
"modifier":{
"1033306667720114200":1033308953984892900
}
}
But I am having issues generating a c# class from that. The modifier objects first property name (1033306667720114200) is not always the same. Does anyone know how I can create a class that when deserialized will output the same json as I have shown in my example?
You can pack your modifier values into a dictionary.
public class MyObject
{
public long Id {get;set;}
public long? CartId {get;set;}
public int Quantity {get;set;}
public Dictionary<object, object> Modifier {get;set;}
}
EDIT
Based on comment this should be close
public class RootObject
{
public object cartId { get; set; }
public string id { get; set; }
public string quantity { get; set; }
public Modifier modifier { get; set; }
}
public class Modifier
{
public long _1033306667720114200 { get; set; }
}
The easiest and most flexible way to handle custom json format would be to implement custom JSON.Net converter. Below is example implementation that works for your json message.
First is Cart type
public class Cart
{
public long? cartId { get; set; }
public string id { get; set; }
public string quantity { get; set; }
public CartModifier modifier { get; set; }
}
[JsonConverter(typeof(CartModifierSerializer))]
public class CartModifier
{
public CartModifier()
{
Values = new Dictionary<string, long>();
}
public Dictionary<string, long> Values { get; set; }
}
and next is custom json converter for CartModifier class
public class CartModifierSerializer : JsonConverter {
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var modifier = value as CartModifier;
writer.WriteStartObject();
foreach (var pair in modifier.Values)
{
writer.WritePropertyName(pair.Key);
writer.WriteValue(pair.Value);
}
writer.WriteEndObject();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var properties = jsonObject.Properties().ToList();
return new CartModifier
{
Values = properties.ToDictionary(x => x.Name, x => (long) x.Value)
};
}
public override bool CanConvert(Type objectType)
{
return typeof(CartModifier).IsAssignableFrom(objectType);
}
}
and below are example serialization/deserialization usages:
[Test]
public void TestSerialization()
{
var cart = new Cart()
{
id = "944990015513953203",
quantity = "3",
modifier = new CartModifier()
{
Values =
{
{"1033306667720114200", 1033308953984892900}
}
}
};
Console.WriteLine(JsonConvert.SerializeObject(cart));
}
[Test]
public void TestDeseriazliation()
{
var data = "{\"cartId\":null, \"id\":\"944990015513953203\",\"quantity\":\"3\",\"modifier\":{ \"1033306667720114200\":1033308953984892900 }}";
var cart = JsonConvert.DeserializeObject<Cart>(data);
Assert.AreEqual(cart.modifier.Values["1033306667720114200"], 1033308953984892900);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With