forked from Code-Sharp/uHttpSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonModelBinder.cs
More file actions
39 lines (28 loc) · 1.13 KB
/
Copy pathJsonModelBinder.cs
File metadata and controls
39 lines (28 loc) · 1.13 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
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();
}
}
}