public override PackageIdentifier ReadJson(global::Newtonsoft.Json.JsonReader reader, global::System.Type objectType, PackageIdentifier existingValue, bool hasExistingValue, global::Newtonsoft.Json.JsonSerializer serializer) { if (serializer is null) { throw new global::System.ArgumentNullException(nameof(serializer)); } var deserialize = serializer.Deserialize <string>(reader); return(new PackageIdentifier(deserialize)); }
/// <summary> /// Deserializes the specified response. /// </summary> /// <typeparam name="T">The response type.</typeparam> /// <param name="response">The response.</param> /// <returns>The strongly-typed deserialized response.</returns> public T Deserialize <T>(IRestResponse response) { var content = response.Content; using (var stringReader = new StringReader(content)) { using (var jsonTextReader = new JsonTextReader(stringReader)) { return(_serializer.Deserialize <T>(jsonTextReader)); } } }
public static Document ToDocument(this JToken jToken, MediaType mediaType, global::Newtonsoft.Json.JsonSerializer serializer) { Type documentType; Document document; if (TypeUtil.TryGetTypeForMediaType(mediaType, out documentType)) { if (mediaType.IsJson) { document = (Document)serializer.Deserialize(jToken.CreateReader(), documentType); } else if (jToken != null) { var parseFunc = TypeUtil.GetParseFuncForType(documentType); document = (Document)parseFunc(jToken.ToString()); } else { document = (Document)Activator.CreateInstance(documentType); } } else { if (mediaType.IsJson) { var contentJsonObject = jToken as IDictionary <string, JToken>; if (contentJsonObject != null) { var contentDictionary = contentJsonObject.ToDictionary(k => k.Key, v => v.Value.GetTokenValue()); document = new JsonDocument(contentDictionary, mediaType); } else { throw new ArgumentException("The property is not a JSON"); } } else { document = new PlainDocument(jToken.ToString(), mediaType); } } return(document); }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="T:Newtonsoft.Json.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, global::Newtonsoft.Json.JsonSerializer serializer) { object target = null; if (reader.TokenType != JsonToken.Null) { JObject jObject = JObject.Load(reader); var command = new Command(); serializer.Populate(jObject.CreateReader(), command); if (jObject[Command.TYPE_KEY] != null) { var resourceMediaType = jObject[Command.TYPE_KEY].ToObject <MediaType>(); Type documentType; if (TypeUtil.TryGetTypeForMediaType(resourceMediaType, out documentType)) { if (resourceMediaType.IsJson) { command.Resource = (Document)Activator.CreateInstance(documentType); if (jObject[Command.RESOURCE_KEY] != null) { var resourceReader = jObject[Command.RESOURCE_KEY].CreateReader(); command.Resource = (Document)serializer.Deserialize(resourceReader, documentType); } } else if (jObject[Command.RESOURCE_KEY] != null) { var parseFunc = TypeUtil.GetParseFuncForType(documentType); command.Resource = (Document)parseFunc(jObject[Command.RESOURCE_KEY].ToString()); } else { command.Resource = (Document)Activator.CreateInstance(documentType); } } else { if (resourceMediaType.IsJson) { var contentJsonObject = jObject[Command.RESOURCE_KEY] as IDictionary <string, JToken>; if (contentJsonObject != null) { var contentDictionary = contentJsonObject.ToDictionary(k => k.Key, v => (object)v.Value.ToString()); command.Resource = new JsonDocument(contentDictionary, resourceMediaType); } else { throw new ArgumentException("The property is not a JSON"); } } else { command.Resource = new PlainDocument( jObject[Command.RESOURCE_KEY].ToString(), resourceMediaType); } } } target = command; } return(target); }
/// <summary> /// Deserializes the specified response data. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="response">The response.</param> /// <returns></returns> public virtual T Deserialize <T>(IRestResponse response) { using (var stringReader = new StringReader(response.Content)) using (var jsonTextReader = new JsonTextReader(stringReader)) return(_serializer.Deserialize <T>(jsonTextReader)); }