Json / C # serialization issues
I am calling an API that expects my data to be in a format that looks like this:
{
"cartId":null,
"id":"944990015513953203",
"quantity":"3",
"modifier":{
"1033306667720114200":1033308953984892900
}
}
But I am having trouble creating a C # class. The first property name of the modifier object (1033306667720114200) is not always the same. Does anyone know how I can create a class that, when deserialized, will output the same json as in my example?
source to share
The easiest and most flexible way to handle custom json format is to implement a custom JSON.Net converter. Below is an example implementation that works for your json message.
First type of basket
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 then - 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 examples of using serialization / deserialization:
[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);
}
source to share
You can wrap the 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 the 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; }
}
source to share
Use LinkedHashMap
LinkedHashMap<String, String> jsonOrderedMap = new LinkedHashMap<String, String>();
jsonOrderedMap.put("1","red");
jsonOrderedMap.put("2","blue");
jsonOrderedMap.put("3","green");
JSONObject orderedJson = new JSONObject(jsonOrderedMap);
JSONArray jsonArray = new JSONArray(Arrays.asList(orderedJson));
source to share