-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSerializedMemberCustomDescriptionConverter.cs
More file actions
153 lines (136 loc) · 6.54 KB
/
SerializedMemberCustomDescriptionConverter.cs
File metadata and controls
153 lines (136 loc) · 6.54 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using com.IvanMurzak.ReflectorNet.Json;
using com.IvanMurzak.ReflectorNet.Model;
using com.IvanMurzak.ReflectorNet.Utils;
namespace com.IvanMurzak.ReflectorNet.Tests.Model
{
public class SerializedMemberCustomDescriptionConverter : JsonSchemaConverter<SerializedMember>, IJsonSchemaConverter
{
public const string CustomDescription = "Custom description, used for testing purposes.";
public static JsonNode Schema => new JsonObject
{
[JsonSchema.Type] = JsonSchema.Object,
[JsonSchema.Properties] = new JsonObject
{
[nameof(SerializedMember.typeName)] = new JsonObject
{
[JsonSchema.Type] = JsonSchema.String,
[JsonSchema.Description] = CustomDescription
},
[nameof(SerializedMember.name)] = new JsonObject
{
[JsonSchema.Type] = JsonSchema.String,
[JsonSchema.Description] = CustomDescription
},
[SerializedMember.ValueName] = new JsonObject
{
[JsonSchema.Type] = JsonSchema.Object,
[JsonSchema.Description] = CustomDescription
},
[nameof(SerializedMember.fields)] = new JsonObject
{
[JsonSchema.Type] = JsonSchema.Array,
[JsonSchema.Items] = new JsonObject
{
[JsonSchema.Ref] = JsonSchema.RefValue + StaticId,
[JsonSchema.Description] = "Nested field value."
},
[JsonSchema.Description] = CustomDescription
},
[nameof(SerializedMember.props)] = new JsonObject
{
[JsonSchema.Type] = JsonSchema.Array,
[JsonSchema.Items] = new JsonObject
{
[JsonSchema.Ref] = JsonSchema.RefValue + StaticId,
[JsonSchema.Description] = "Nested property value."
},
[JsonSchema.Description] = CustomDescription
}
},
[JsonSchema.Required] = new JsonArray { nameof(SerializedMember.typeName), SerializedMember.ValueName },
[JsonSchema.AdditionalProperties] = false
};
public static JsonNode SchemaRef => new JsonObject
{
[JsonSchema.Ref] = JsonSchema.RefValue + StaticId
};
readonly Reflector _reflector;
public SerializedMemberCustomDescriptionConverter(Reflector reflector)
{
_reflector = reflector ?? throw new ArgumentNullException(nameof(reflector));
}
public override JsonNode GetSchemaRef() => SchemaRef;
public override JsonNode GetSchema() => Schema;
public override SerializedMember? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return null;
if (reader.TokenType != JsonTokenType.StartObject)
throw new JsonException($"Expected start of object, but got {reader.TokenType}");
var member = new SerializedMember();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
return member;
if (reader.TokenType == JsonTokenType.PropertyName)
{
var propertyName = reader.GetString();
reader.Read(); // Move to the value token
switch (propertyName)
{
case nameof(SerializedMember.name):
member.name = reader.GetString();
break;
case nameof(SerializedMember.typeName):
member.typeName = reader.GetString() ?? "[FAILED TO READ]";
break;
case SerializedMember.ValueName:
if (!JsonElement.TryParseValue(ref reader, out member.valueJsonElement))
throw new JsonException($"Failed to parse value for property '{SerializedMember.ValueName}'.");
break;
case nameof(SerializedMember.fields):
member.fields = _reflector.JsonSerializer.Deserialize<SerializedMemberList>(ref reader, options);
break;
case nameof(SerializedMember.props):
member.props = _reflector.JsonSerializer.Deserialize<SerializedMemberList>(ref reader, options);
break;
default:
throw new JsonException($"Unexpected property name: '{propertyName}'. "
+ $"Did you want to use '{nameof(SerializedMember.name)}', '{nameof(SerializedMember.typeName)}', '{SerializedMember.ValueName}', '{nameof(SerializedMember.fields)}' or '{nameof(SerializedMember.props)}'?");
}
}
}
throw new JsonException("Unexpected end of JSON while reading SerializedMember.");
}
public override void Write(Utf8JsonWriter writer, SerializedMember value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
return;
}
writer.WriteStartObject();
writer.WriteString(nameof(SerializedMember.name), value.name);
writer.WriteString(nameof(SerializedMember.typeName), value.typeName);
if (value.valueJsonElement.HasValue)
{
writer.WritePropertyName(SerializedMember.ValueName);
value.valueJsonElement.Value.WriteTo(writer);
}
if (value.fields != null && value.fields.Count > 0)
{
writer.WritePropertyName(nameof(SerializedMember.fields));
System.Text.Json.JsonSerializer.Serialize(writer, value.fields, options);
}
if (value.props != null && value.props.Count > 0)
{
writer.WritePropertyName(nameof(SerializedMember.props));
System.Text.Json.JsonSerializer.Serialize(writer, value.props, options);
}
writer.WriteEndObject();
}
}
}