public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { return(_innerFormatter.ReadFromStreamAsync(type, readStream, content, formatterLogger) .ContinueWith(antecedent => { var o = antecedent.Result; FilterObject(o); return o; })); }
/// <summary> /// Deserialises the specified data. /// </summary> /// <typeparam name="T">The type.</typeparam> /// <param name="data">The data.</param> /// <returns>Deserialised object.</returns> public T Deserialise <T>(string data) { var task = _formatter.ReadFromStreamAsync(typeof(T), new MemoryStream(Encoding.Default.GetBytes(data)), null, null); task.Wait(); return((T)task.Result); }
private static async Task <T> ReadAsAsyncCore <T>(HttpContent content, Type type, IFormatterLogger formatterLogger, MediaTypeFormatter formatter) { Stream stream = await content.ReadAsStreamAsync(); object result = await formatter.ReadFromStreamAsync(type, stream, content, formatterLogger); return((T)result); }
public async Task ReadFromStreamAsync_Traces() { // Arrange Mock <TFormatter> mockFormatter = new Mock <TFormatter>() { CallBase = true }; mockFormatter .Setup( f => f.ReadFromStreamAsync( It.IsAny <Type>(), It.IsAny <Stream>(), It.IsAny <HttpContent>(), It.IsAny <IFormatterLogger>() ) ) .Returns(Task.FromResult <object>("sampleValue")); TestTraceWriter traceWriter = new TestTraceWriter(); HttpRequestMessage request = new HttpRequestMessage(); request.Content = new StringContent(""); MediaTypeFormatter tracer = CreateTracer(mockFormatter.Object, request, traceWriter); TraceRecord[] expectedTraces = new TraceRecord[] { new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ReadFromStreamAsync" }, new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "ReadFromStreamAsync" } }; // Act Task <object> task = tracer.ReadFromStreamAsync( typeof(string), new MemoryStream(), request.Content, null ); string result = (await task)as string; // Assert Assert.Equal <TraceRecord>( expectedTraces, traceWriter.Traces, new TraceRecordComparer() ); Assert.Equal("sampleValue", result); }
private static async Task <T> ReadAsAsyncCore <T>(HttpContent content, Type type, MediaTypeFormatter formatter, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var stream = await content.ReadAsStreamAsync(); var result = await formatter.ReadFromStreamAsync(type, stream, content, null, cancellationToken); return((T)result); }
private async Task <TEntity> ParseAsync(int separatorIndex, CancellationToken cancellationToken) { using (var subStream = new MemoryStream(_buffer, _startIndex, count: separatorIndex - _startIndex)) { var result = (TEntity)await _serializer.ReadFromStreamAsync(typeof(TEntity), subStream, _content, null, cancellationToken); _startIndex = separatorIndex + _separatorPattern.Length; return(result); } }
public static async Task <T> DeserializeAsync <T>(this MediaTypeFormatter formatter, string str) where T : class { Stream stream = new MemoryStream(); var writer = new StreamWriter(stream); await writer.WriteAsync(str); await writer.FlushAsync(); stream.Position = 0; return(await formatter.ReadFromStreamAsync(typeof(T), stream, null, null) as T); }
private static T Deserialize <T>(MediaTypeFormatter formatter, string str) where T : class { var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(str); writer.Flush(); stream.Position = 0; return(formatter.ReadFromStreamAsync(typeof(T), stream, null, null).Result as T); }
private static object Read(MemoryStream ms, Type tSource, MediaTypeFormatter formatter) { bool f = formatter.CanReadType(tSource); Assert.True(f); object o = formatter.ReadFromStreamAsync(tSource, ms, content: null, formatterLogger: null).Result; Assert.True(tSource.IsAssignableFrom(o.GetType())); return(o); }
internal T Deserialize <T>(MediaTypeFormatter formatter, string str) where T : class { // Write the serialized string to a memory stream. Stream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.Write(str); writer.Flush(); stream.Position = 0; // Deserialize to an object of type T return(formatter.ReadFromStreamAsync(typeof(T), stream, null, null).Result as T); }
private async Task <T> DeserializeResponseAsync <T>(HttpResponseMessage response) { var responseStream = await response.Content.ReadAsStreamAsync(); var responseObject = await _formatter.ReadFromStreamAsync( typeof(T), responseStream, response.Content, _formatterLogger); return((T)responseObject); }
public override Task <object> ReadFromStreamAsync( Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger, CancellationToken cancellationToken) { var result = formatter.ReadFromStreamAsync (type, readStream, content, formatterLogger, cancellationToken); readStream.Seek(0, SeekOrigin.Begin); return(result); }
private T Deserialize <T>(MediaTypeFormatter formatter, string str, MediaTypeHeaderValue mediaType) where T : class { Stream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); HttpContent content = new StreamContent(stream); content.Headers.ContentType = mediaType; writer.Write(str); writer.Flush(); stream.Position = 0; return(formatter.ReadFromStreamAsync(typeof(T), stream, content, null).Result as T); }
private IEnumerable <T> DeserializeCollection <T>(MediaTypeFormatter formatter, string str, MediaTypeHeaderValue mediaType) where T : class { Stream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); HttpContent content = new StreamContent(stream); content.Headers.ContentType = mediaType; writer.Write(str); writer.Flush(); stream.Position = 0; return(formatter.ReadFromStreamAsync(typeof(List <T>), stream, content, logFormatter).Result as IEnumerable <T>); }
public static async Task <T> Deserialize <T>(this string str, MediaTypeFormatter formatter) { // Write the serialized string to a memory stream. using (Stream stream = new MemoryStream()) { using (StreamWriter writer = new StreamWriter(stream)) { writer.Write(str); writer.Flush(); stream.Position = 0; dynamic obj = await formatter.ReadFromStreamAsync(typeof(T), stream, null, null); // Sem Cast return(obj); } } }
public void ReadFromStreamAsync_Traces_And_Faults_When_Inner_Faults() { // Arrange InvalidOperationException exception = new InvalidOperationException("test"); Mock <TFormatter> mockFormatter = new Mock <TFormatter>() { CallBase = true }; TaskCompletionSource <object> tcs = new TaskCompletionSource <object>(); tcs.TrySetException(exception); mockFormatter.Setup( f => f.ReadFromStreamAsync(It.IsAny <Type>(), It.IsAny <Stream>(), It.IsAny <HttpContent>(), It.IsAny <IFormatterLogger>())). Returns(tcs.Task); TestTraceWriter traceWriter = new TestTraceWriter(); HttpRequestMessage request = new HttpRequestMessage(); request.Content = new StringContent(""); MediaTypeFormatter tracer = CreateTracer(mockFormatter.Object, request, traceWriter); TraceRecord[] expectedTraces = new TraceRecord[] { new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ReadFromStreamAsync" }, new TraceRecord(request, TraceCategories.FormattingCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "ReadFromStreamAsync" } }; // Act Task <object> task = tracer.ReadFromStreamAsync(typeof(string), new MemoryStream(), request.Content, null); // Assert Exception thrown = Assert.Throws <InvalidOperationException>(() => task.Wait()); Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer()); Assert.Same(exception, thrown); Assert.Same(exception, traceWriter.Traces[1].Exception); }
public Task <T> ReadAsync() { return(this.ReadAsStreamAsync() .ContinueWith <object>(streamTask => _formatter.ReadFromStreamAsync(typeof(T), streamTask.Result, _inboundContent.Headers, new FormatterContext(new MediaTypeHeaderValue("application/bogus"), false))) .ContinueWith <T>(objectTask => (T)((Task <object>)(objectTask.Result)).Result)); }
public override Task <object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger) { return(_internalFormatter.ReadFromStreamAsync(type, readStream, content, formatterLogger)); }