-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathObjectRefConverter.cs
More file actions
106 lines (90 loc) · 3.97 KB
/
ObjectRefConverter.cs
File metadata and controls
106 lines (90 loc) · 3.97 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
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Nodes;
using com.IvanMurzak.ReflectorNet.Json;
using com.IvanMurzak.ReflectorNet.Utils;
namespace com.IvanMurzak.ReflectorNet.Tests.Model
{
public class ObjectRefConverter : JsonSchemaConverter<ObjectRef>, IJsonSchemaConverter
{
public override JsonNode GetSchema() => new JsonObject
{
[JsonSchema.Type] = JsonSchema.Object,
[JsonSchema.Properties] = new JsonObject
{
[nameof(ObjectRef.instanceID)] = new JsonObject { [JsonSchema.Type] = JsonSchema.Integer },
[nameof(ObjectRef.assetPath)] = new JsonObject { [JsonSchema.Type] = JsonSchema.String },
[nameof(ObjectRef.assetGuid)] = new JsonObject { [JsonSchema.Type] = JsonSchema.String }
},
[JsonSchema.Required] = new JsonArray { nameof(ObjectRef.instanceID) }
};
public override JsonNode GetSchemaRef() => new JsonObject
{
[JsonSchema.Ref] = JsonSchema.RefValue + Id
};
public override ObjectRef? 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 token.");
var objectRef = new ObjectRef();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
return objectRef;
if (reader.TokenType == JsonTokenType.PropertyName)
{
var propertyName = reader.GetString();
reader.Read(); // Move to the value token
switch (propertyName)
{
case nameof(ObjectRef.instanceID):
objectRef.instanceID = reader.GetInt32();
break;
case nameof(ObjectRef.assetPath):
objectRef.assetPath = reader.GetString();
break;
case nameof(ObjectRef.assetGuid):
objectRef.assetGuid = reader.GetString();
break;
default:
throw new JsonException($"Unexpected property name: {propertyName}. "
+ $"Expected '{nameof(ObjectRef.instanceID)}', '{nameof(ObjectRef.assetPath)}', or '{nameof(ObjectRef.assetGuid)}'.");
}
}
}
throw new JsonException("Expected end of object token.");
}
public override void Write(Utf8JsonWriter writer, ObjectRef value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteStartObject();
// Write the "instanceID" property
writer.WritePropertyName(nameof(ObjectRef.instanceID));
writer.WriteNumberValue(0);
writer.WriteEndObject();
return;
}
writer.WriteStartObject();
// Write the "instanceID" property
writer.WritePropertyName(nameof(ObjectRef.instanceID));
writer.WriteNumberValue(value.instanceID);
// Write the "assetPath" property
if (!string.IsNullOrEmpty(value.assetPath))
{
writer.WritePropertyName(nameof(ObjectRef.assetPath));
writer.WriteStringValue(value.assetPath);
}
// Write the "assetGuid" property
if (!string.IsNullOrEmpty(value.assetGuid))
{
writer.WritePropertyName(nameof(ObjectRef.assetGuid));
writer.WriteStringValue(value.assetGuid);
}
writer.WriteEndObject();
}
}
}