forked from joelpob/betfairng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonConvert.cs
79 lines (65 loc) · 2.25 KB
/
JsonConvert.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
namespace BetfairNG
{
public class JsonConvert
{
public static JsonResponse<T> Import<T>(TextReader reader)
{
var jsonResponse = reader.ReadToEnd();
return Deserialize<JsonResponse<T>>(jsonResponse);
}
public static T Deserialize<T>(string json)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
}
public static void Export(JsonRequest request, TextWriter writer)
{
var json = Serialize<JsonRequest>(request);
writer.Write(json);
}
public static string Serialize<T>(T value)
{
var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
return Newtonsoft.Json.JsonConvert.SerializeObject(value, settings);
}
}
[JsonObject(MemberSerialization.OptIn)]
public class JsonRequest
{
public JsonRequest()
{
JsonRpc = "2.0";
}
[JsonProperty(PropertyName = "jsonrpc", NullValueHandling = NullValueHandling.Ignore)]
public string JsonRpc { get; set; }
[JsonProperty(PropertyName = "method")]
public string Method { get; set; }
[JsonProperty(PropertyName = "params")]
public object Params { get; set; }
[JsonProperty(PropertyName = "id")]
public object Id { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class JsonResponse<T>
{
[JsonProperty(PropertyName = "jsonrpc", NullValueHandling = NullValueHandling.Ignore)]
public string JsonRpc { get; set; }
[JsonProperty(PropertyName = "result", NullValueHandling = NullValueHandling.Ignore)]
public T Result { get; set; }
[JsonProperty(PropertyName = "error", NullValueHandling = NullValueHandling.Ignore)]
public Data.Exceptions.Exception Error { get; set; }
[JsonProperty(PropertyName = "id")]
public object Id { get; set; }
[JsonIgnore]
public bool HasError
{
get { return Error != null; }
}
}
}