-
Notifications
You must be signed in to change notification settings - Fork 1
/
KVNodeConverter.cs
74 lines (70 loc) · 2.26 KB
/
KVNodeConverter.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
using Antlr4.Runtime.Misc;
using System;
using System.Collections;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace antlr4_fortran_parser
{
public class KVNodeConverter : JsonConverter<KVNode>
{
public override KVNode Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, KVNode value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName(value.Key);
WriteValue(writer, value.Value, options);
writer.WriteEndObject();
}
public void WriteValue(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
}
else if (value is KVNode)
{
Write(writer, value as KVNode, options);
}
else if (value is ArrayList<object>)
{
var a = value as ArrayList<object>;
writer.WriteStartArray();
foreach (var av in a)
{
WriteValue(writer, av, options);
}
writer.WriteEndArray();
}
else if (value is KVDict)
{
var d = value as KVDict;
foreach (DictionaryEntry e in d)
{
string k = e.Key.ToString();
writer.WriteStartObject();
writer.WritePropertyName(k);
WriteValue(writer, e.Value, options);
writer.WriteEndObject();
}
}
else if (value is string)
{
writer.WriteStringValue(value.ToString());
}
else if (value is int)
{
writer.WriteNumberValue((int)value);
}
else if (value is double)
{
writer.WriteNumberValue((double)value);
}
else
Debug.Fail("Unexpected value in KVNode");
}
}
}