forked from bonesoul/uhttpsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonModelBinder.cs
More file actions
54 lines (45 loc) · 1.28 KB
/
JsonModelBinder.cs
File metadata and controls
54 lines (45 loc) · 1.28 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
53
54
using System;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using uhttpsharp.Headers;
namespace uhttpsharp.ModelBinders
{
public class JsonModelBinder : IModelBinder
{
private readonly JsonSerializer _serializer;
public JsonModelBinder(JsonSerializer serializer)
{
_serializer = serializer;
}
public JsonModelBinder() : this(JsonSerializer.CreateDefault())
{
}
public T Get<T>(byte[] raw, string prefix)
{
var rawDecoded = Encoding.UTF8.GetString(raw);
if (raw.Length == 0)
{
return default(T);
}
if (prefix == null && typeof(T) == typeof(string))
{
return (T)(object)rawDecoded;
}
var jToken = JToken.Parse(rawDecoded);
if (prefix != null)
{
jToken = jToken.SelectToken(prefix);
}
return jToken.ToObject<T>(_serializer);
}
public T Get<T>(IHttpHeaders headers)
{
throw new NotSupportedException();
}
public T Get<T>(IHttpHeaders headers, string prefix)
{
throw new NotSupportedException();
}
}
}