-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeJsonConverter.cs
More file actions
91 lines (76 loc) · 3.67 KB
/
Copy pathNodeJsonConverter.cs
File metadata and controls
91 lines (76 loc) · 3.67 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
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Contentstack.Utils.Models;
namespace Contentstack.Utils.Converters
{
public class NodeJsonConverter : JsonConverter<Node>
{
public override Node Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using (JsonDocument doc = JsonDocument.ParseValue(ref reader))
{
JsonElement root = doc.RootElement;
bool hasType = root.TryGetProperty("type", out JsonElement typeEl) && typeEl.ValueKind != JsonValueKind.Null;
if (!hasType)
{
TextNode textNode = new TextNode();
textNode.type = "text";
PopulateTextNode(root, textNode, options);
return textNode;
}
Node node = new Node();
PopulateNode(root, node, options);
return node;
}
}
public override void Write(Utf8JsonWriter writer, Node value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, value?.GetType() ?? typeof(Node), CloneWithoutNodeConverter(options));
}
private static void PopulateNode(JsonElement root, Node node, JsonSerializerOptions options)
{
if (root.TryGetProperty("type", out JsonElement typeProp))
node.type = typeProp.GetString();
if (root.TryGetProperty("attrs", out JsonElement attrs))
node.attrs = JsonSerializer.Deserialize<IDictionary<string, object>>(attrs.GetRawText(), options);
if (root.TryGetProperty("children", out JsonElement children))
node.children = JsonSerializer.Deserialize<List<Node>>(children.GetRawText(), options);
}
private static void PopulateTextNode(JsonElement root, TextNode node, JsonSerializerOptions options)
{
PopulateNode(root, node, options);
if (root.TryGetProperty("text", out JsonElement text))
node.text = text.GetString();
TryBindBool(root, "bold", v => node.bold = v);
TryBindBool(root, "italic", v => node.italic = v);
TryBindBool(root, "underline", v => node.underline = v);
TryBindBool(root, "strikethrough", v => node.strikethrough = v);
TryBindBool(root, "inlineCode", v => node.inlineCode = v);
TryBindBool(root, "subscript", v => node.subscript = v);
TryBindBool(root, "superscript", v => node.superscript = v);
if (root.TryGetProperty("classname", out JsonElement cn))
node.classname = cn.ValueKind == JsonValueKind.Null ? null : cn.GetString();
if (root.TryGetProperty("id", out JsonElement id))
node.id = id.ValueKind == JsonValueKind.Null ? null : id.GetString();
}
private static void TryBindBool(JsonElement root, string name, Action<bool> set)
{
if (!root.TryGetProperty(name, out JsonElement el))
return;
if (el.ValueKind == JsonValueKind.True || el.ValueKind == JsonValueKind.False)
set(el.GetBoolean());
}
private static JsonSerializerOptions CloneWithoutNodeConverter(JsonSerializerOptions options)
{
JsonSerializerOptions inner = new JsonSerializerOptions(options);
for (int i = inner.Converters.Count - 1; i >= 0; i--)
{
if (inner.Converters[i] is NodeJsonConverter)
inner.Converters.RemoveAt(i);
}
return inner;
}
}
}