public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JObject j = null; try { j = JObject.Load(reader); JObject result = j["conversation"] as JObject; ConversationAppCount count = new ConversationAppCount(); count.assigned = result["assigned"].Value <int>(); count.unassigned = result["unassigned"].Value <int>(); count.open = result["open"].Value <int>(); count.closed = result["closed"].Value <int>(); return(count); } catch (Exception ex) { throw new JsonConverterException("Error while serializing ConversationAppCount endpoint json result.", ex) { Json = j == null ? String.Empty : j.ToString(), SerializationType = objectType.FullName }; } }
public void ReadJson_ForConversationAppCount_ReturnsValidCount() { String input = "{\"type\":\"count\",\"conversation\":{\"open\":24,\"closed\":35,\"unassigned\":11,\"assigned\":13}}"; StringReader stringReader = new StringReader(input); JsonReader reader = new JsonTextReader(stringReader); ConversationAppCount conversationAppCount = conversationAppCountJsonConverter.ReadJson(reader, typeof(ConversationAppCount), null, null) as ConversationAppCount; Assert.IsNotNull(conversationAppCount); Assert.AreEqual(13, conversationAppCount.assigned); Assert.AreEqual(11, conversationAppCount.unassigned); Assert.AreEqual(35, conversationAppCount.closed); Assert.AreEqual(24, conversationAppCount.open); }