-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMatrixJsonSchema.cs
More file actions
61 lines (52 loc) · 1.96 KB
/
Copy pathMatrixJsonSchema.cs
File metadata and controls
61 lines (52 loc) · 1.96 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
namespace CreateMatrix;
internal class MatrixJsonSchemaBase
{
[JsonRequired]
[JsonPropertyOrder(-2)]
public int SchemaVersion { get; set; } = MatrixJsonSchema.Version;
}
internal class MatrixJsonSchema : MatrixJsonSchemaBase
{
public const int Version = 2;
[JsonRequired]
public List<ImageInfo> Images { get; set; } = [];
public static void ToFile(string path, MatrixJsonSchema json)
{
json.SchemaVersion = Version;
File.WriteAllText(path, ToJson(json));
}
private static string ToJson(MatrixJsonSchema json) =>
JsonSerializer.Serialize(json, MatrixJsonContext.Default.MatrixJsonSchema);
}
internal sealed class LowercaseEnumConverter<T> : JsonConverter<T>
where T : struct, Enum
{
public override T Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
string? value =
reader.GetString() ?? throw new JsonException($"Expected {typeof(T).Name} value.");
return !Enum.TryParse(value, true, out T result)
? throw new JsonException($"Invalid {typeof(T).Name} value '{value}'.")
: result;
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.ToString().ToLowerInvariant());
}
[JsonSourceGenerationOptions(
AllowTrailingCommas = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
IncludeFields = true,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
PreferredObjectCreationHandling = JsonObjectCreationHandling.Populate,
ReadCommentHandling = JsonCommentHandling.Skip,
UseStringEnumConverter = true,
WriteIndented = true,
NewLine = "\r\n"
)]
[JsonSerializable(typeof(MatrixJsonSchema))]
[JsonSerializable(typeof(MatrixJsonSchemaBase))]
internal partial class MatrixJsonContext : JsonSerializerContext;