/// <summary> /// Map /// </summary> /// <param name="value">value</param> /// <param name="updateId">updateId</param> /// <param name="updateObject">updateObject</param> public static void Map(object value, Action <string> updateId, Action <T> updateObject) { if (value is JObject) { var item = default(T); string objectValue = ((JObject)value).SelectToken("object")?.ToString(); Type concreteType = TelnyxTypeRegistry.GetConcreteType(typeof(T), objectValue); if (concreteType != null) { item = (T)((JToken)value).ToObject(concreteType); } if (item != null) { updateId(item.Id); updateObject(item); } else { // We were unable to deserialize the object, but make a last attempt to grab // the ID from the raw JObject. var id = ((JObject)value).SelectToken("id")?.ToString(); updateId(id); updateObject(default);
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } var jsonObject = JObject.Load(reader); var objectValue = (string)jsonObject["object"]; Type concreteType = TelnyxTypeRegistry.GetConcreteType(objectType, objectValue); if (concreteType == null) { // Couldn't find a concrete type to instantiate, return null. return(null); } var value = Activator.CreateInstance(concreteType); using (var subReader = jsonObject.CreateReader()) { serializer.Populate(subReader, value); } return(value); }