-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathVersionJsonSchema.cs
77 lines (65 loc) · 2.76 KB
/
VersionJsonSchema.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
74
75
76
77
using Json.Schema.Generation;
using Json.Schema;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace CreateMatrix;
public class VersionJsonSchemaBase
{
[JsonPropertyName("$schema")]
[JsonPropertyOrder(-3)]
public string Schema { get; } = SchemaUri;
[JsonRequired]
[JsonPropertyOrder(-2)]
public int SchemaVersion { get; set; } = VersionJsonSchema.Version;
protected const string SchemaUri = "https://raw.githubusercontent.com/ptr727/NxWitness/main/CreateMatrix/JSON/Version.schema.json";
}
public class VersionJsonSchema : VersionJsonSchemaBase
{
public const int Version = 2;
[JsonRequired]
public List<ProductInfo> Products { get; set; } = [];
public static VersionJsonSchema FromFile(string path)
{
return FromJson(File.ReadAllText(path));
}
public static void ToFile(string path, VersionJsonSchema jsonSchema)
{
jsonSchema.SchemaVersion = Version;
File.WriteAllText(path, ToJson(jsonSchema));
}
private static string ToJson(VersionJsonSchema jsonSchema)
{
return JsonSerializer.Serialize(jsonSchema, MatrixJsonSchema.JsonWriteOptions);
}
private static VersionJsonSchema FromJson(string jsonString)
{
var versionJsonSchemaBase = JsonSerializer.Deserialize<VersionJsonSchemaBase>(jsonString, MatrixJsonSchema.JsonReadOptions);
ArgumentNullException.ThrowIfNull(versionJsonSchemaBase);
// Deserialize the correct version
var schemaVersion = versionJsonSchemaBase.SchemaVersion;
switch (schemaVersion)
{
case Version:
var schema = JsonSerializer.Deserialize<VersionJsonSchema>(jsonString, MatrixJsonSchema.JsonReadOptions);
ArgumentNullException.ThrowIfNull(schema);
return schema;
// case 1:
// VersionInfo::Uri was replaced with UriX64 and UriArm64 was added
// Breaking change, UriArm64 is required in ARM64 docker builds
// Unknown version
default:
throw new NotImplementedException();
}
}
public static void GenerateSchema(string path)
{
const string schemaVersion = "https://json-schema.org/draft/2020-12/schema";
var schemaBuilder = new JsonSchemaBuilder().FromType<VersionJsonSchema>(new SchemaGeneratorConfiguration { PropertyOrder = PropertyOrder.ByName })
.Title("CreateMatrix Version Schema")
.Id(new Uri(SchemaUri))
.Schema(new Uri(schemaVersion))
.Build();
var jsonSchema = JsonSerializer.Serialize(schemaBuilder, MatrixJsonSchema.JsonWriteOptions);
File.WriteAllText(path, jsonSchema);
}
}