/// <summary> /// Read an object of the given type form the given HTTP message, and returns it. /// </summary> /// <typeparam name="T"> /// The type of object to return. This type must have previously been passed to the /// <see cref="M:CanRead"/> method of this interface, which must have returned <see langword="true"/>. /// </typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> public T Read <T>(IHttpInputMessage message) where T : class { // Get the message encoding MediaType contentType = message.Headers.ContentType; Encoding encoding = (contentType != null && contentType.CharSet != null) ? contentType.CharSet : DEFAULT_CHARSET; // Read from the message stream string body; using (StreamReader reader = new StreamReader(message.Body, encoding)) { body = reader.ReadToEnd(); } string[] pairs = body.Split('&'); NameValueCollection result = new NameValueCollection(pairs.Length); foreach (string pair in pairs) { int idx = pair.IndexOf('='); if (idx == -1) { result.Add(HttpUtils.FormDecode(pair), null); } else { string name = HttpUtils.FormDecode(pair.Substring(0, idx)); string value = HttpUtils.FormDecode(pair.Substring(idx + 1)); result.Add(name, value); } } return(result as T); }
/// <summary> /// Abstract template method that reads the actualy object. Invoked from <see cref="M:Read"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> protected override T ReadInternal <T>(IHttpInputMessage message) { // Read from the message stream using (MemoryStream tempStream = new MemoryStream()) { IoUtils.CopyStream(message.Body, tempStream); return(new ByteArrayResource(tempStream.ToArray()) as T); } }
/// <summary> /// Abstract template method that reads the actualy object. Invoked from <see cref="M:Read"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> protected override T ReadInternal <T>(IHttpInputMessage message) { // Read from the message stream using (StreamReader reader = new StreamReader(message.Body)) using (JsonTextReader jsonReader = new JsonTextReader(reader)) { JsonSerializer jsonSerializer = new JsonSerializer(); return(jsonSerializer.Deserialize <T>(jsonReader)); } }
/// <summary> /// Abstract template method that reads the actualy object. Invoked from <see cref="M:Read"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> protected override T ReadInternal <T>(IHttpInputMessage message) { XmlReaderSettings settings = this.GetXmlReaderSettings(); // Read from the message stream using (XmlReader xmlReader = XmlReader.Create(message.Body, settings)) { return(ReadXml <T>(xmlReader)); } }
/// <summary> /// Abstract template method that reads the actualy object. Invoked from <see cref="M:Read"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> protected override T ReadInternal <T>(IHttpInputMessage message) { // Get the message encoding Encoding encoding = GetContentTypeCharset(message.Headers.ContentType, DEFAULT_CHARSET); // Read from the message stream using (StreamReader reader = new StreamReader(message.Body, encoding)) { return(reader.ReadToEnd() as T); } }
/// <summary> /// Abstract template method that reads the actualy object. Invoked from <see cref="M:Read"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> protected override T ReadInternal <T>(IHttpInputMessage message) { // Read from the message stream //using (StreamReader reader = new StreamReader(message.Body)) //{ // var str = reader.ReadToEnd(); // return default(T); //} using (StreamReader reader = new StreamReader(message.Body)) using (JsonTextReader jsonReader = new JsonTextReader(reader)) { JsonSerializer jsonSerializer = new JsonSerializer(); return(jsonSerializer.Deserialize <T>(jsonReader)); } }
/// <summary> /// Abstract template method that reads the actualy object. Invoked from <see cref="M:Read"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> protected override T ReadInternal <T>(IHttpInputMessage message) { // Get the message encoding Encoding encoding = GetContentTypeCharset(message.Headers.ContentType, DEFAULT_CHARSET); // Read from the message stream using (StreamReader reader = new StreamReader(message.Body, encoding)) { // Parse JSON JsonValue jsonValue = JsonValue.Parse(reader.ReadToEnd()); if (typeof(T) == typeof(JsonValue)) { return(jsonValue as T); } // Map from JsonValue to object return(this.mapper.Deserialize <T>(jsonValue)); } }
/// <summary> /// Abstract template method that reads the actualy object. Invoked from <see cref="M:Read"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> protected override T ReadInternal <T>(IHttpInputMessage message) { DropboxFile file = new DropboxFile(); // Read file's content as an array of bytes file.Content = base.ReadInternal <byte[]>(message); // Read metadata content from 'x-dropbox-metadata' header string metadataHeaderContent = message.Headers["x-dropbox-metadata"]; if (metadataHeaderContent != null) { JsonValue metadataJsonValue; if (JsonValue.TryParse(metadataHeaderContent, out metadataJsonValue)) { file.Metadata = this.jsonMapper.Deserialize <Entry>(metadataJsonValue); } } return(file as T); }
/// <summary> /// Read an object of the given type form the given HTTP message, and returns it. /// </summary> /// <typeparam name="T"> /// The type of object to return. This type must have previously been passed to the /// <see cref="M:CanRead"/> method of this interface, which must have returned <see langword="true"/>. /// </typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> public override T Read <T>(IHttpInputMessage message) { throw new NotSupportedException(); }
/// <summary> /// Abstract template method that reads the actualy object. Invoked from <see cref="M:Read"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> protected override T ReadInternal <T>(IHttpInputMessage message) { DataContractJsonSerializer serializer = this.GetSerializer(typeof(T)); return((T)serializer.ReadObject(message.Body) as T); }
/// <summary> /// Abstract template method that reads the actualy object. Invoked from <see cref="M:Read"/>. /// </summary> /// <typeparam name="T">The type of object to return.</typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> protected abstract T ReadInternal <T>(IHttpInputMessage message) where T : class;
/// <summary> /// Read an object of the given type form the given HTTP message, and returns it. /// </summary> /// <remarks> /// This implementation simple delegates to <see cre="ReadInternal"/> method. /// Future implementations might add some default behavior, however. /// </remarks> /// <typeparam name="T"> /// The type of object to return. This type must have previously been passed to the /// <see cref="M:CanRead"/> method of this interface, which must have returned <see langword="true"/>. /// </typeparam> /// <param name="message">The HTTP message to read from.</param> /// <returns>The converted object.</returns> /// <exception cref="HttpMessageNotReadableException">In case of conversion errors</exception> public virtual T Read <T>(IHttpInputMessage message) where T : class { return(ReadInternal <T>(message)); }
protected override T ReadInternal <T>(IHttpInputMessage message) { return(Image.FromStream(message.Body) as T); }