11using System . Text . Json ;
2+ using System . Text . Json . Nodes ;
23using System . Text . Json . Serialization ;
4+ using System . Text . Json . Serialization . Metadata ;
35
46namespace Devolutions . Now . Policy . Api ;
57
@@ -17,14 +19,79 @@ public static class BrokerJson
1719 /// (via explicit <c>[JsonPropertyName]</c> attributes), PascalCase enum values, and
1820 /// null optionals omitted (mirroring the Rust <c>skip_serializing_if = "Option::is_none"</c>).
1921 /// </summary>
20- public static readonly JsonSerializerOptions Options = new ( )
22+ public static readonly JsonSerializerOptions Options = new ( BrokerJsonSerializerContext . Default . Options )
2123 {
22- DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull ,
23- WriteIndented = false ,
2424 } ;
2525
2626 public static readonly JsonSerializerOptions PrettyOptions = new ( Options )
2727 {
2828 WriteIndented = true ,
2929 } ;
30- }
30+
31+ public static string Serialize < T > ( T value ) =>
32+ JsonSerializer . Serialize ( value , TypeInfo < T > ( ) ) ;
33+
34+ public static T ? Deserialize < T > ( string json ) =>
35+ JsonSerializer . Deserialize ( json , TypeInfo < T > ( ) ) ;
36+
37+ public static T ? DeserializeStrict < T > ( string json ) =>
38+ JsonSerializer . Deserialize ( json , StrictTypeInfo < T > ( ) ) ;
39+
40+ private static JsonTypeInfo < T > TypeInfo < T > ( ) =>
41+ typeof ( T ) == typeof ( PackageRequest ) ? Cast < T > ( BrokerJsonSerializerContext . Default . PackageRequest ) :
42+ typeof ( T ) == typeof ( StatusRequest ) ? Cast < T > ( BrokerJsonSerializerContext . Default . StatusRequest ) :
43+ typeof ( T ) == typeof ( HealthResponse ) ? Cast < T > ( BrokerJsonSerializerContext . Default . HealthResponse ) :
44+ typeof ( T ) == typeof ( CapabilitiesResponse ) ? Cast < T > ( BrokerJsonSerializerContext . Default . CapabilitiesResponse ) :
45+ typeof ( T ) == typeof ( EvaluationResponse ) ? Cast < T > ( BrokerJsonSerializerContext . Default . EvaluationResponse ) :
46+ typeof ( T ) == typeof ( ExecutionResponse ) ? Cast < T > ( BrokerJsonSerializerContext . Default . ExecutionResponse ) :
47+ typeof ( T ) == typeof ( StatusResponse ) ? Cast < T > ( BrokerJsonSerializerContext . Default . StatusResponse ) :
48+ typeof ( T ) == typeof ( ErrorResponse ) ? Cast < T > ( BrokerJsonSerializerContext . Default . ErrorResponse ) :
49+ throw new NotSupportedException ( $ "Broker JSON serialization for { typeof ( T ) . FullName } is not source-generated.") ;
50+
51+ private static JsonTypeInfo < T > StrictTypeInfo < T > ( ) =>
52+ typeof ( T ) == typeof ( PackageRequest ) ? Cast < T > ( BrokerJsonStrictSerializerContext . Default . PackageRequest ) :
53+ typeof ( T ) == typeof ( StatusRequest ) ? Cast < T > ( BrokerJsonStrictSerializerContext . Default . StatusRequest ) :
54+ typeof ( T ) == typeof ( HealthResponse ) ? Cast < T > ( BrokerJsonStrictSerializerContext . Default . HealthResponse ) :
55+ typeof ( T ) == typeof ( CapabilitiesResponse ) ? Cast < T > ( BrokerJsonStrictSerializerContext . Default . CapabilitiesResponse ) :
56+ typeof ( T ) == typeof ( EvaluationResponse ) ? Cast < T > ( BrokerJsonStrictSerializerContext . Default . EvaluationResponse ) :
57+ typeof ( T ) == typeof ( ExecutionResponse ) ? Cast < T > ( BrokerJsonStrictSerializerContext . Default . ExecutionResponse ) :
58+ typeof ( T ) == typeof ( StatusResponse ) ? Cast < T > ( BrokerJsonStrictSerializerContext . Default . StatusResponse ) :
59+ typeof ( T ) == typeof ( ErrorResponse ) ? Cast < T > ( BrokerJsonStrictSerializerContext . Default . ErrorResponse ) :
60+ throw new NotSupportedException ( $ "Strict broker JSON deserialization for { typeof ( T ) . FullName } is not source-generated.") ;
61+
62+ private static JsonTypeInfo < T > Cast < T > ( JsonTypeInfo jsonTypeInfo ) =>
63+ ( JsonTypeInfo < T > ) jsonTypeInfo ;
64+ }
65+
66+ [ JsonSourceGenerationOptions (
67+ DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull ,
68+ WriteIndented = false ) ]
69+ [ JsonSerializable ( typeof ( PackageRequest ) ) ]
70+ [ JsonSerializable ( typeof ( StatusRequest ) ) ]
71+ [ JsonSerializable ( typeof ( HealthResponse ) ) ]
72+ [ JsonSerializable ( typeof ( CapabilitiesResponse ) ) ]
73+ [ JsonSerializable ( typeof ( EvaluationResponse ) ) ]
74+ [ JsonSerializable ( typeof ( ExecutionResponse ) ) ]
75+ [ JsonSerializable ( typeof ( StatusResponse ) ) ]
76+ [ JsonSerializable ( typeof ( ErrorResponse ) ) ]
77+ [ JsonSerializable ( typeof ( JsonNode ) ) ]
78+ [ JsonSerializable ( typeof ( JsonObject ) ) ]
79+ [ JsonSerializable ( typeof ( JsonArray ) ) ]
80+ internal sealed partial class BrokerJsonSerializerContext : JsonSerializerContext ;
81+
82+ [ JsonSourceGenerationOptions (
83+ DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull ,
84+ WriteIndented = false ,
85+ UnmappedMemberHandling = JsonUnmappedMemberHandling . Disallow ) ]
86+ [ JsonSerializable ( typeof ( PackageRequest ) ) ]
87+ [ JsonSerializable ( typeof ( StatusRequest ) ) ]
88+ [ JsonSerializable ( typeof ( HealthResponse ) ) ]
89+ [ JsonSerializable ( typeof ( CapabilitiesResponse ) ) ]
90+ [ JsonSerializable ( typeof ( EvaluationResponse ) ) ]
91+ [ JsonSerializable ( typeof ( ExecutionResponse ) ) ]
92+ [ JsonSerializable ( typeof ( StatusResponse ) ) ]
93+ [ JsonSerializable ( typeof ( ErrorResponse ) ) ]
94+ [ JsonSerializable ( typeof ( JsonNode ) ) ]
95+ [ JsonSerializable ( typeof ( JsonObject ) ) ]
96+ [ JsonSerializable ( typeof ( JsonArray ) ) ]
97+ internal sealed partial class BrokerJsonStrictSerializerContext : JsonSerializerContext ;
0 commit comments