-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFormatter.cs
More file actions
52 lines (46 loc) · 1.73 KB
/
Formatter.cs
File metadata and controls
52 lines (46 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
namespace ScriptCs.WebApi
{
internal class Formatter : MediaTypeFormatter
{
private Func<Type, bool> _canReadType;
private Func<Type, bool> _canWriteType;
private Func<ReadFromStreamArgs, Task<object>> _readFromStream;
private Func<WriteToStreamArgs, Task> _writeToStream;
public Formatter(
Func<Type, bool> canReadType,
Func<Type, bool> canWriteType,
Func<ReadFromStreamArgs, Task<object>> readFromStream,
Func<WriteToStreamArgs, Task> writeToStream
)
{
_canReadType = canReadType;
_canWriteType = canWriteType;
_readFromStream = readFromStream;
_writeToStream = writeToStream;
}
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
return _readFromStream(new ReadFromStreamArgs(type, readStream, content, formatterLogger));
}
public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content,
TransportContext transportContext, CancellationToken cancellationToken)
{
return _writeToStream(new WriteToStreamArgs(type, value, writeStream, content, transportContext, cancellationToken));
}
public override bool CanReadType(Type type)
{
return _canReadType(type);
}
public override bool CanWriteType(Type type)
{
return _canWriteType(type);
}
}
}