33using System . Text . RegularExpressions ;
44using System . Xml ;
55
6+ #if NET8_0_OR_GREATER
7+ using System . Text . Json . Serialization . Metadata ;
8+ #endif
9+
610namespace k8s
711{
812 public static class KubernetesJson
913 {
10- private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions ( ) ;
14+ internal static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions ( ) ;
1115
12- private sealed class Iso8601TimeSpanConverter : JsonConverter < TimeSpan >
16+ public sealed class Iso8601TimeSpanConverter : JsonConverter < TimeSpan >
1317 {
1418 public override TimeSpan Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
1519 {
@@ -24,11 +28,11 @@ public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializer
2428 }
2529 }
2630
27- private sealed class KubernetesDateTimeOffsetConverter : JsonConverter < DateTimeOffset >
31+ public sealed class KubernetesDateTimeOffsetConverter : JsonConverter < DateTimeOffset >
2832 {
29- private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffK " ;
30- private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffK " ;
31- private const string RFC3339Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK " ;
33+ private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffZ " ;
34+ private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffZ " ;
35+ private const string RFC3339Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ " ;
3236
3337 public override DateTimeOffset Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
3438 {
@@ -50,13 +54,22 @@ public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConver
5054 throw new FormatException ( $ "Unable to parse { originalstr } as RFC3339 RFC3339Micro or RFC3339Nano") ;
5155 }
5256
57+
5358 public override void Write ( Utf8JsonWriter writer , DateTimeOffset value , JsonSerializerOptions options )
5459 {
55- writer . WriteStringValue ( value . ToString ( RFC3339MicroFormat ) ) ;
60+ // Output as RFC3339Micro
61+ var date = value . ToUniversalTime ( ) ;
62+
63+ var basePart = date . ToString ( "yyyy-MM-dd'T'HH:mm:ss" , CultureInfo . InvariantCulture ) ;
64+ var frac = date . ToString ( ".ffffff" , CultureInfo . InvariantCulture )
65+ . TrimEnd ( '0' )
66+ . TrimEnd ( '.' ) ;
67+
68+ writer . WriteStringValue ( basePart + frac + "Z" ) ;
5669 }
5770 }
5871
59- private sealed class KubernetesDateTimeConverter : JsonConverter < DateTime >
72+ public sealed class KubernetesDateTimeConverter : JsonConverter < DateTime >
6073 {
6174 private static readonly JsonConverter < DateTimeOffset > UtcConverter = new KubernetesDateTimeOffsetConverter ( ) ;
6275 public override DateTime Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
@@ -72,13 +85,22 @@ public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializer
7285
7386 static KubernetesJson ( )
7487 {
88+ #if K8S_AOT
89+ // Uses Source Generated IJsonTypeInfoResolver
90+ JsonSerializerOptions . TypeInfoResolver = SourceGenerationContext . Default ;
91+ #else
92+ #if NET8_0_OR_GREATER
93+ // Uses Source Generated IJsonTypeInfoResolver when available and falls back to reflection
94+ JsonSerializerOptions . TypeInfoResolver = JsonTypeInfoResolver . Combine ( SourceGenerationContext . Default , new DefaultJsonTypeInfoResolver ( ) ) ;
95+ #endif
96+ JsonSerializerOptions . Converters . Add ( new JsonStringEnumConverter ( ) ) ;
97+ #endif
7598 JsonSerializerOptions . DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull ;
7699 JsonSerializerOptions . PropertyNamingPolicy = JsonNamingPolicy . CamelCase ;
77100 JsonSerializerOptions . Converters . Add ( new Iso8601TimeSpanConverter ( ) ) ;
78101 JsonSerializerOptions . Converters . Add ( new KubernetesDateTimeConverter ( ) ) ;
79102 JsonSerializerOptions . Converters . Add ( new KubernetesDateTimeOffsetConverter ( ) ) ;
80103 JsonSerializerOptions . Converters . Add ( new V1Status . V1StatusObjectViewConverter ( ) ) ;
81- JsonSerializerOptions . Converters . Add ( new JsonStringEnumConverter ( ) ) ;
82104 }
83105
84106 /// <summary>
@@ -99,47 +121,92 @@ public static void AddJsonOptions(Action<JsonSerializerOptions> configure)
99121
100122 public static TValue Deserialize < TValue > ( string json , JsonSerializerOptions jsonSerializerOptions = null )
101123 {
124+ #if NET8_0_OR_GREATER
125+ var info = ( JsonTypeInfo < TValue > ) ( jsonSerializerOptions ?? JsonSerializerOptions ) . GetTypeInfo ( typeof ( TValue ) ) ;
126+ return JsonSerializer . Deserialize ( json , info ) ;
127+ #else
102128 return JsonSerializer . Deserialize < TValue > ( json , jsonSerializerOptions ?? JsonSerializerOptions ) ;
129+ #endif
103130 }
104131
105132 public static TValue Deserialize < TValue > ( Stream json , JsonSerializerOptions jsonSerializerOptions = null )
106133 {
134+ #if NET8_0_OR_GREATER
135+ var info = ( JsonTypeInfo < TValue > ) ( jsonSerializerOptions ?? JsonSerializerOptions ) . GetTypeInfo ( typeof ( TValue ) ) ;
136+ return JsonSerializer . Deserialize ( json , info ) ;
137+ #else
107138 return JsonSerializer . Deserialize < TValue > ( json , jsonSerializerOptions ?? JsonSerializerOptions ) ;
139+ #endif
108140 }
109141
110142 public static TValue Deserialize < TValue > ( JsonDocument json , JsonSerializerOptions jsonSerializerOptions = null )
111143 {
144+ #if NET8_0_OR_GREATER
145+ var info = ( JsonTypeInfo < TValue > ) ( jsonSerializerOptions ?? JsonSerializerOptions ) . GetTypeInfo ( typeof ( TValue ) ) ;
146+ return JsonSerializer . Deserialize ( json , info ) ;
147+ #else
112148 return JsonSerializer . Deserialize < TValue > ( json , jsonSerializerOptions ?? JsonSerializerOptions ) ;
149+ #endif
113150 }
114151
115152 public static TValue Deserialize < TValue > ( JsonElement json , JsonSerializerOptions jsonSerializerOptions = null )
116153 {
154+ #if NET8_0_OR_GREATER
155+ var info = ( JsonTypeInfo < TValue > ) ( jsonSerializerOptions ?? JsonSerializerOptions ) . GetTypeInfo ( typeof ( TValue ) ) ;
156+ return JsonSerializer . Deserialize ( json , info ) ;
157+ #else
117158 return JsonSerializer . Deserialize < TValue > ( json , jsonSerializerOptions ?? JsonSerializerOptions ) ;
159+ #endif
118160 }
119161
120162 public static TValue Deserialize < TValue > ( JsonNode json , JsonSerializerOptions jsonSerializerOptions = null )
121163 {
164+ #if NET8_0_OR_GREATER
165+ var info = ( JsonTypeInfo < TValue > ) ( jsonSerializerOptions ?? JsonSerializerOptions ) . GetTypeInfo ( typeof ( TValue ) ) ;
166+ return JsonSerializer . Deserialize ( json , info ) ;
167+ #else
122168 return JsonSerializer . Deserialize < TValue > ( json , jsonSerializerOptions ?? JsonSerializerOptions ) ;
169+ #endif
123170 }
124171
125172 public static string Serialize ( object value , JsonSerializerOptions jsonSerializerOptions = null )
126173 {
174+ #if NET8_0_OR_GREATER
175+ var info = ( jsonSerializerOptions ?? JsonSerializerOptions ) . GetTypeInfo ( value . GetType ( ) ) ;
176+ return JsonSerializer . Serialize ( value , info ) ;
177+ #else
127178 return JsonSerializer . Serialize ( value , jsonSerializerOptions ?? JsonSerializerOptions ) ;
179+ #endif
128180 }
129181
130182 public static string Serialize ( JsonDocument value , JsonSerializerOptions jsonSerializerOptions = null )
131183 {
184+ #if NET8_0_OR_GREATER
185+ var info = ( jsonSerializerOptions ?? JsonSerializerOptions ) . GetTypeInfo ( value . GetType ( ) ) ;
186+ return JsonSerializer . Serialize ( value , info ) ;
187+ #else
132188 return JsonSerializer . Serialize ( value , jsonSerializerOptions ?? JsonSerializerOptions ) ;
189+ #endif
133190 }
134191
135192 public static string Serialize ( JsonElement value , JsonSerializerOptions jsonSerializerOptions = null )
136193 {
194+ #if NET8_0_OR_GREATER
195+ var info = ( jsonSerializerOptions ?? JsonSerializerOptions ) . GetTypeInfo ( value . GetType ( ) ) ;
196+ return JsonSerializer . Serialize ( value , info ) ;
197+ #else
137198 return JsonSerializer . Serialize ( value , jsonSerializerOptions ?? JsonSerializerOptions ) ;
199+ #endif
138200 }
139201
140202 public static string Serialize ( JsonNode value , JsonSerializerOptions jsonSerializerOptions = null )
141203 {
204+ #if NET8_0_OR_GREATER
205+ var info = ( jsonSerializerOptions ?? JsonSerializerOptions ) . GetTypeInfo ( value . GetType ( ) ) ;
206+ return JsonSerializer . Serialize ( value , info ) ;
207+ #else
142208 return JsonSerializer . Serialize ( value , jsonSerializerOptions ?? JsonSerializerOptions ) ;
209+ #endif
143210 }
144211 }
145212}
0 commit comments