Example #1
0
        /// <inheritdoc cref="MediaTypeFormatter.ReadFromStreamAsync(Type, Stream, HttpContent, IFormatterLogger, CancellationToken)"/>
        public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger, CancellationToken cancellationToken)
        {
            Contract.Requires <ArgumentException>(IsSirenDocumentType(type), "type");

            using (var txtReader = new StreamReader(readStream))
                using (var jsonReader = new JsonTextReader(txtReader))
                {
                    var sirenJson = JObject.ReadFrom(jsonReader);

                    var sirenDoc = SirenJson.Parse(sirenJson);

                    return(Task.FromResult <object>(sirenDoc));
                }

            return(Task.FromResult <object>(null));
        }
Example #2
0
        /// <inheritdoc cref="MediaTypeFormatter.WriteToStreamAsync(Type, object, Stream, HttpContent, TransportContext, CancellationToken)"/>
        public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
        {
            Contract.Requires <ArgumentException>(IsSirenDocumentType(type), "type");
            Contract.Requires <ArgumentException>(value is ISirenEntity);

            var sirenDocument = value as ISirenEntity;

            var jsonDocument = SirenJson.Unparse(sirenDocument);

            using (var writer = new StreamWriter(writeStream, this.SelectCharacterEncoding(content.Headers)))
                using (var jsonWriter = new JsonTextWriter(writer))
                {
                    jsonDocument.WriteTo(jsonWriter);

                    jsonWriter.Flush();
                    writer.Flush();
                }

            return(Task.FromResult <object>(null));
        }