-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathJsonResponse.cs
More file actions
135 lines (116 loc) · 4.45 KB
/
Copy pathJsonResponse.cs
File metadata and controls
135 lines (116 loc) · 4.45 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace AustinHarris.JsonRpc
{
/// <summary>
/// Represents a Json Rpc Response
/// </summary>
[JsonConverter(typeof(JsonResponseConverter))]
public class JsonResponse
{
public string Jsonrpc { get; set; } = "2.0";
public object Result { get; set; }
public JsonRpcException Error { get; set; }
public JsonElement Id { get; set; }
}
/// <summary>
/// Represents a Json Rpc Response
/// </summary>
public class JsonResponse<T>
{
public string Jsonrpc { get; set; } = "2.0";
public T Result { get; set; }
public JsonRpcException Error { get; set; }
public JsonElement Id { get; set; }
}
public class JsonResponseConverter : JsonConverter<JsonResponse>
{
public override JsonResponse Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var jres = new JsonResponse();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
if (reader.TokenType == JsonTokenType.PropertyName)
{
var propname = reader.GetString();
if (propname == "jsonrpc")
{
reader.Read();
jres.Jsonrpc = reader.GetString();
}
if (propname == "result")
{
reader.Read();
jres.Result = JsonDocument.ParseValue(ref reader);
}
if (propname == "id")
{
reader.Read();
jres.Id = JsonDocument.ParseValue(ref reader).RootElement.Clone();
}
if (propname == "error")
{
reader.Read();
if (reader.TokenType == JsonTokenType.Null || reader.TokenType == JsonTokenType.None)
{
}
else
{
JsonRpcExceptionConverter exc = new JsonRpcExceptionConverter();
jres.Error = exc.Read(ref reader, typeof(JsonRpcException), options);
}
}
}
}
return jres;
}
public override void Write(Utf8JsonWriter writer, JsonResponse value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString("jsonrpc", "2.0");
if (value.Error != null)
{
writer.WritePropertyName("error");
var spError =JsonSerializer.SerializeToUtf8Bytes(value.Error, options);
using (JsonDocument document = JsonDocument.Parse(spError))
{
document.RootElement.WriteTo(writer);
}
}
else
{
var spResult =JsonSerializer.SerializeToUtf8Bytes(value.Result, options);
using (JsonDocument document = JsonDocument.Parse(spResult))
{
if (document.RootElement.ValueKind == JsonValueKind.Object
|| document.RootElement.ValueKind == JsonValueKind.Array)
{
writer.WritePropertyName("result");
document.RootElement.WriteTo(writer);
}else if (document.RootElement.ValueKind == JsonValueKind.Null)
{
writer.WriteNull("result");
}
else
{
writer.WritePropertyName("result");
document.RootElement.Clone().WriteTo(writer);
}
}
}
if (value.Id.ValueKind != JsonValueKind.Null && value.Id.ValueKind != JsonValueKind.Undefined)
{
writer.WritePropertyName("id");
value.Id.Clone().WriteTo(writer);
}
writer.WriteEndObject();
writer.Flush();
}
}
}