-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathVersionJsonSchema.cs
More file actions
70 lines (61 loc) · 2.43 KB
/
VersionJsonSchema.cs
File metadata and controls
70 lines (61 loc) · 2.43 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
namespace CreateMatrix;
internal class VersionJsonSchemaBase
{
[JsonRequired]
[JsonPropertyOrder(-2)]
public int SchemaVersion { get; set; } = VersionJsonSchema.Version;
}
internal class VersionJsonSchema : VersionJsonSchemaBase
{
public const int Version = 2;
[JsonRequired]
public List<ProductInfo> Products { get; set; } = [];
public static VersionJsonSchema FromFile(string path) => FromJson(File.ReadAllText(path));
public static void ToFile(string path, VersionJsonSchema json)
{
json.SchemaVersion = Version;
File.WriteAllText(path, ToJson(json));
}
private static string ToJson(VersionJsonSchema json) =>
JsonSerializer.Serialize(json, VersionJsonContext.Default.VersionJsonSchema);
private static VersionJsonSchema FromJson(string json)
{
VersionJsonSchemaBase? versionJsonSchemaBase = JsonSerializer.Deserialize(
json,
VersionJsonContext.Default.VersionJsonSchemaBase
);
ArgumentNullException.ThrowIfNull(versionJsonSchemaBase);
// Deserialize the correct version
int schemaVersion = versionJsonSchemaBase.SchemaVersion;
switch (schemaVersion)
{
case Version:
VersionJsonSchema? schema = JsonSerializer.Deserialize(
json,
VersionJsonContext.Default.VersionJsonSchema
);
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();
}
}
}
[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(VersionJsonSchema))]
[JsonSerializable(typeof(VersionJsonSchemaBase))]
internal partial class VersionJsonContext : JsonSerializerContext;