From 9c618928e028cfd845ced511c6d1135a7b5d0686 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Tue, 21 Oct 2025 20:07:07 -0700 Subject: [PATCH 01/21] feat: initial source gen for json --- .../KubernetesClient.Aot.csproj | 5 +- src/KubernetesClient.Aot/KubernetesJson.cs | 103 ------------------ .../KubernetesClient.Classic.csproj | 4 +- src/KubernetesClient/KubernetesJson.cs | 56 ++++++---- .../SourceGenerationContext.cs | 2 +- .../SourceGenerationContext.cs.template | 4 +- 6 files changed, 42 insertions(+), 132 deletions(-) delete mode 100644 src/KubernetesClient.Aot/KubernetesJson.cs rename src/{KubernetesClient.Aot => KubernetesClient}/SourceGenerationContext.cs (83%) diff --git a/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj b/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj index 55305a1a1..ccce4f4fb 100644 --- a/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj +++ b/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj @@ -1,4 +1,4 @@ - + net8.0;net9.0 @@ -43,7 +43,8 @@ - + + diff --git a/src/KubernetesClient.Aot/KubernetesJson.cs b/src/KubernetesClient.Aot/KubernetesJson.cs deleted file mode 100644 index f6285ccc2..000000000 --- a/src/KubernetesClient.Aot/KubernetesJson.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System.Globalization; -using System.Text.RegularExpressions; -using System.Xml; - -namespace k8s -{ - internal static class KubernetesJson - { - internal sealed class Iso8601TimeSpanConverter : JsonConverter - { - public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var str = reader.GetString(); - return XmlConvert.ToTimeSpan(str); - } - - public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options) - { - var iso8601TimeSpanString = XmlConvert.ToString(value); // XmlConvert for TimeSpan uses ISO8601, so delegate serialization to it - writer.WriteStringValue(iso8601TimeSpanString); - } - } - - internal sealed class KubernetesDateTimeOffsetConverter : JsonConverter - { - private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffK"; - private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffK"; - private const string RFC3339Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK"; - - public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var str = reader.GetString(); - - if (DateTimeOffset.TryParseExact(str, new[] { RFC3339Format, RFC3339MicroFormat }, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result)) - { - return result; - } - - // try RFC3339NanoLenient by trimming 1-9 digits to 7 digits - var originalstr = str; - str = Regex.Replace(str, @"\.\d+", m => (m.Value + "000000000").Substring(0, 7 + 1)); // 7 digits + 1 for the dot - if (DateTimeOffset.TryParseExact(str, new[] { RFC3339NanoFormat }, CultureInfo.InvariantCulture, DateTimeStyles.None, out result)) - { - return result; - } - - throw new FormatException($"Unable to parse {originalstr} as RFC3339 RFC3339Micro or RFC3339Nano"); - } - - public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.ToString(RFC3339MicroFormat)); - } - } - - internal sealed class KubernetesDateTimeConverter : JsonConverter - { - private static readonly JsonConverter UtcConverter = new KubernetesDateTimeOffsetConverter(); - public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - return UtcConverter.Read(ref reader, typeToConvert, options).UtcDateTime; - } - - public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) - { - UtcConverter.Write(writer, value, options); - } - } - - /// - /// Configures for the . - /// To override existing converters, add them to the top of the list - /// e.g. as follows: options.Converters.Insert(index: 0, new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); - /// - /// An to configure the . - public static void AddJsonOptions(Action configure) - { - } - - public static TValue Deserialize(string json, JsonSerializerOptions jsonSerializerOptions = null) - { - var info = SourceGenerationContext.Default.GetTypeInfo(typeof(TValue)); - return (TValue)JsonSerializer.Deserialize(json, info); - } - - public static TValue Deserialize(Stream json, JsonSerializerOptions jsonSerializerOptions = null) - { - var info = SourceGenerationContext.Default.GetTypeInfo(typeof(TValue)); - return (TValue)JsonSerializer.Deserialize(json, info); - } - - public static string Serialize(object value, JsonSerializerOptions jsonSerializerOptions = null) - { - if (value is V1Patch { Content: string jsonValue }) - { - return jsonValue; - } - - var info = SourceGenerationContext.Default.GetTypeInfo(value.GetType()); - return JsonSerializer.Serialize(value, info); - } - } -} diff --git a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj index 81070f066..75b3d113b 100644 --- a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj +++ b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj @@ -1,4 +1,4 @@ - + netstandard2.0;net48 @@ -49,7 +49,7 @@ - + diff --git a/src/KubernetesClient/KubernetesJson.cs b/src/KubernetesClient/KubernetesJson.cs index b9f47774e..3c8b0c053 100644 --- a/src/KubernetesClient/KubernetesJson.cs +++ b/src/KubernetesClient/KubernetesJson.cs @@ -1,5 +1,6 @@ using System.Globalization; using System.Text.Json.Nodes; +using System.Text.Json.Serialization.Metadata; using System.Text.RegularExpressions; using System.Xml; @@ -7,9 +8,18 @@ namespace k8s { public static class KubernetesJson { - private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions(); - - private sealed class Iso8601TimeSpanConverter : JsonConverter + private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions + { +#if K8S_AOT + // Uses Source Generated IJsonTypeInfoResolver + TypeInfoResolver = SourceGenerationContext.Default, +#else + // Uses Source Generated IJsonTypeInfoResolver when available and falls back to reflection + TypeInfoResolver = JsonTypeInfoResolver.Combine(SourceGenerationContext.Default, new DefaultJsonTypeInfoResolver()), +#endif + }; + + internal sealed class Iso8601TimeSpanConverter : JsonConverter { public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { @@ -24,7 +34,7 @@ public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializer } } - private sealed class KubernetesDateTimeOffsetConverter : JsonConverter + internal sealed class KubernetesDateTimeOffsetConverter : JsonConverter { private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffK"; private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffK"; @@ -56,7 +66,7 @@ public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSeri } } - private sealed class KubernetesDateTimeConverter : JsonConverter + internal sealed class KubernetesDateTimeConverter : JsonConverter { private static readonly JsonConverter UtcConverter = new KubernetesDateTimeOffsetConverter(); public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) @@ -72,13 +82,8 @@ public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializer static KubernetesJson() { - JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; - JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; - JsonSerializerOptions.Converters.Add(new Iso8601TimeSpanConverter()); - JsonSerializerOptions.Converters.Add(new KubernetesDateTimeConverter()); - JsonSerializerOptions.Converters.Add(new KubernetesDateTimeOffsetConverter()); - JsonSerializerOptions.Converters.Add(new V1Status.V1StatusObjectViewConverter()); - JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); + //JsonSerializerOptions.Converters.Add(new V1Status.V1StatusObjectViewConverter()); + //JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); } /// @@ -99,47 +104,56 @@ public static void AddJsonOptions(Action configure) public static TValue Deserialize(string json, JsonSerializerOptions jsonSerializerOptions = null) { - return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); + var info = (JsonTypeInfo)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue)); + return JsonSerializer.Deserialize(json, info); } public static TValue Deserialize(Stream json, JsonSerializerOptions jsonSerializerOptions = null) { - return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); + var info = (JsonTypeInfo)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue)); + return JsonSerializer.Deserialize(json, info); } public static TValue Deserialize(JsonDocument json, JsonSerializerOptions jsonSerializerOptions = null) { - return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); + var info = (JsonTypeInfo)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue)); + return JsonSerializer.Deserialize(json, info); } public static TValue Deserialize(JsonElement json, JsonSerializerOptions jsonSerializerOptions = null) { - return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); + var info = (JsonTypeInfo)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue)); + return JsonSerializer.Deserialize(json, info); } public static TValue Deserialize(JsonNode json, JsonSerializerOptions jsonSerializerOptions = null) { - return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); + var info = (JsonTypeInfo)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue)); + return JsonSerializer.Deserialize(json, info); } public static string Serialize(object value, JsonSerializerOptions jsonSerializerOptions = null) { - return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions); + var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(value.GetType()); + return JsonSerializer.Serialize(value, info); } public static string Serialize(JsonDocument value, JsonSerializerOptions jsonSerializerOptions = null) { - return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions); + var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(JsonDocument)); + return JsonSerializer.Serialize(value, info); } public static string Serialize(JsonElement value, JsonSerializerOptions jsonSerializerOptions = null) { - return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions); + var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(JsonElement)); + return JsonSerializer.Serialize(value, info); } public static string Serialize(JsonNode value, JsonSerializerOptions jsonSerializerOptions = null) { - return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions); + var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(JsonNode)); + return JsonSerializer.Serialize(value, info); } } } diff --git a/src/KubernetesClient.Aot/SourceGenerationContext.cs b/src/KubernetesClient/SourceGenerationContext.cs similarity index 83% rename from src/KubernetesClient.Aot/SourceGenerationContext.cs rename to src/KubernetesClient/SourceGenerationContext.cs index decb9b5a9..150160c12 100644 --- a/src/KubernetesClient.Aot/SourceGenerationContext.cs +++ b/src/KubernetesClient/SourceGenerationContext.cs @@ -7,6 +7,6 @@ namespace k8s; PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter) }) ] -internal partial class SourceGenerationContext : JsonSerializerContext +public partial class SourceGenerationContext : JsonSerializerContext { } diff --git a/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template b/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template index 82942f915..f1fb9c66a 100644 --- a/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template +++ b/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template @@ -2,14 +2,12 @@ // Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator // Changes may cause incorrect behavior and will be lost if the code is regenerated. // -#if NET8_0_OR_GREATER namespace k8s { {{ for definition in definitions }} [JsonSerializable(typeof({{ GetClassName definition }}))] {{ end }} - internal partial class SourceGenerationContext : JsonSerializerContext + public partial class SourceGenerationContext : JsonSerializerContext { } } -#endif \ No newline at end of file From 352e51815a2fc2e546332ae70e1e6c7eafd864b7 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Tue, 21 Oct 2025 20:24:17 -0700 Subject: [PATCH 02/21] wip --- src/KubernetesClient.Aot/KubernetesClient.Aot.csproj | 1 + src/KubernetesClient/KubernetesJson.cs | 7 ++++--- src/KubernetesClient/Models/V1Status.ObjectView.cs | 6 ++++-- src/KubernetesClient/SourceGenerationContext.cs | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj b/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj index ccce4f4fb..09155cda7 100644 --- a/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj +++ b/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj @@ -45,6 +45,7 @@ + diff --git a/src/KubernetesClient/KubernetesJson.cs b/src/KubernetesClient/KubernetesJson.cs index 3c8b0c053..9348063da 100644 --- a/src/KubernetesClient/KubernetesJson.cs +++ b/src/KubernetesClient/KubernetesJson.cs @@ -8,7 +8,7 @@ namespace k8s { public static class KubernetesJson { - private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions + internal static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions { #if K8S_AOT // Uses Source Generated IJsonTypeInfoResolver @@ -82,8 +82,9 @@ public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializer static KubernetesJson() { - //JsonSerializerOptions.Converters.Add(new V1Status.V1StatusObjectViewConverter()); - //JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); +#if !K8S_AOT + JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); +#endif } /// diff --git a/src/KubernetesClient/Models/V1Status.ObjectView.cs b/src/KubernetesClient/Models/V1Status.ObjectView.cs index aab834a95..87a1a28d2 100644 --- a/src/KubernetesClient/Models/V1Status.ObjectView.cs +++ b/src/KubernetesClient/Models/V1Status.ObjectView.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization.Metadata; + namespace k8s.Models { public partial record V1Status @@ -10,7 +12,7 @@ public override V1Status Read(ref Utf8JsonReader reader, Type typeToConvert, Jso try { - return obj.Deserialize(); + return obj.Deserialize((JsonTypeInfo)options.GetTypeInfo(typeof(V1Status))); } catch (JsonException) { @@ -32,7 +34,7 @@ public override void Write(Utf8JsonWriter writer, V1Status value, JsonSerializer public T ObjectView() { - return _original.Deserialize(); + return _original.Deserialize((JsonTypeInfo)KubernetesJson.JsonSerializerOptions.GetTypeInfo(typeof(T))); } } } diff --git a/src/KubernetesClient/SourceGenerationContext.cs b/src/KubernetesClient/SourceGenerationContext.cs index 150160c12..c2af3e7bb 100644 --- a/src/KubernetesClient/SourceGenerationContext.cs +++ b/src/KubernetesClient/SourceGenerationContext.cs @@ -5,7 +5,7 @@ namespace k8s; [JsonSourceGenerationOptions( DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, - Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter) }) + Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter), typeof(V1Status.V1StatusObjectViewConverter) }) ] public partial class SourceGenerationContext : JsonSerializerContext { From 999d975236de6a89a15a2325153795bf700096ca Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Tue, 21 Oct 2025 20:28:50 -0700 Subject: [PATCH 03/21] chore: readd default settings --- src/KubernetesClient/KubernetesJson.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/KubernetesClient/KubernetesJson.cs b/src/KubernetesClient/KubernetesJson.cs index 9348063da..5a9f79646 100644 --- a/src/KubernetesClient/KubernetesJson.cs +++ b/src/KubernetesClient/KubernetesJson.cs @@ -83,6 +83,12 @@ public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializer static KubernetesJson() { #if !K8S_AOT + JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; + JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + JsonSerializerOptions.Converters.Add(new Iso8601TimeSpanConverter()); + JsonSerializerOptions.Converters.Add(new KubernetesDateTimeConverter()); + JsonSerializerOptions.Converters.Add(new KubernetesDateTimeOffsetConverter()); + JsonSerializerOptions.Converters.Add(new V1Status.V1StatusObjectViewConverter()); JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); #endif } From 2aeb7f7fbfde36615350127995739e3d16ecb73c Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Tue, 21 Oct 2025 20:36:54 -0700 Subject: [PATCH 04/21] chore: cleanup --- src/KubernetesClient/KubernetesJson.cs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/KubernetesClient/KubernetesJson.cs b/src/KubernetesClient/KubernetesJson.cs index 5a9f79646..73e788b2a 100644 --- a/src/KubernetesClient/KubernetesJson.cs +++ b/src/KubernetesClient/KubernetesJson.cs @@ -8,16 +8,7 @@ namespace k8s { public static class KubernetesJson { - internal static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions - { -#if K8S_AOT - // Uses Source Generated IJsonTypeInfoResolver - TypeInfoResolver = SourceGenerationContext.Default, -#else - // Uses Source Generated IJsonTypeInfoResolver when available and falls back to reflection - TypeInfoResolver = JsonTypeInfoResolver.Combine(SourceGenerationContext.Default, new DefaultJsonTypeInfoResolver()), -#endif - }; + internal static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions(); internal sealed class Iso8601TimeSpanConverter : JsonConverter { @@ -82,7 +73,12 @@ public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializer static KubernetesJson() { -#if !K8S_AOT +#if K8S_AOT + // Uses Source Generated IJsonTypeInfoResolver + JsonSerializerOptions.TypeInfoResolver = SourceGenerationContext.Default; +#else + // Uses Source Generated IJsonTypeInfoResolver when available and falls back to reflection + JsonSerializerOptions.TypeInfoResolver = JsonTypeInfoResolver.Combine(SourceGenerationContext.Default, new DefaultJsonTypeInfoResolver()); JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; JsonSerializerOptions.Converters.Add(new Iso8601TimeSpanConverter()); From a0ab96e00b8f7d0fbb25fe7ce7698bc2bb3c622c Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Tue, 21 Oct 2025 21:11:31 -0700 Subject: [PATCH 05/21] chore: remove source gen from classic as it depends on C#9 support --- .../KubernetesClient.Classic.csproj | 2 +- src/KubernetesClient/KubernetesJson.cs | 49 +++++++++++++++++-- .../SourceGenerationContext.cs.template | 2 + 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj index 75b3d113b..91cd69263 100644 --- a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj +++ b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj @@ -49,7 +49,7 @@ - + diff --git a/src/KubernetesClient/KubernetesJson.cs b/src/KubernetesClient/KubernetesJson.cs index 73e788b2a..e55cf09a8 100644 --- a/src/KubernetesClient/KubernetesJson.cs +++ b/src/KubernetesClient/KubernetesJson.cs @@ -1,9 +1,12 @@ using System.Globalization; using System.Text.Json.Nodes; -using System.Text.Json.Serialization.Metadata; using System.Text.RegularExpressions; using System.Xml; +#if NET8_0_OR_GREATER +using System.Text.Json.Serialization.Metadata; +#endif + namespace k8s { public static class KubernetesJson @@ -77,8 +80,10 @@ static KubernetesJson() // Uses Source Generated IJsonTypeInfoResolver JsonSerializerOptions.TypeInfoResolver = SourceGenerationContext.Default; #else +#if NET8_0_OR_GREATER // Uses Source Generated IJsonTypeInfoResolver when available and falls back to reflection JsonSerializerOptions.TypeInfoResolver = JsonTypeInfoResolver.Combine(SourceGenerationContext.Default, new DefaultJsonTypeInfoResolver()); +#endif JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; JsonSerializerOptions.Converters.Add(new Iso8601TimeSpanConverter()); @@ -107,56 +112,92 @@ public static void AddJsonOptions(Action configure) public static TValue Deserialize(string json, JsonSerializerOptions jsonSerializerOptions = null) { +#if NET8_0_OR_GREATER var info = (JsonTypeInfo)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue)); return JsonSerializer.Deserialize(json, info); +#else + return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); +#endif } public static TValue Deserialize(Stream json, JsonSerializerOptions jsonSerializerOptions = null) { +#if NET8_0_OR_GREATER var info = (JsonTypeInfo)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue)); return JsonSerializer.Deserialize(json, info); +#else + return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); +#endif } public static TValue Deserialize(JsonDocument json, JsonSerializerOptions jsonSerializerOptions = null) { +#if NET8_0_OR_GREATER var info = (JsonTypeInfo)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue)); return JsonSerializer.Deserialize(json, info); +#else + return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); +#endif } public static TValue Deserialize(JsonElement json, JsonSerializerOptions jsonSerializerOptions = null) { +#if NET8_0_OR_GREATER var info = (JsonTypeInfo)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue)); return JsonSerializer.Deserialize(json, info); +#else + return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); +#endif } public static TValue Deserialize(JsonNode json, JsonSerializerOptions jsonSerializerOptions = null) { +#if NET8_0_OR_GREATER var info = (JsonTypeInfo)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue)); return JsonSerializer.Deserialize(json, info); +#else + return JsonSerializer.Deserialize(json, jsonSerializerOptions ?? JsonSerializerOptions); +#endif } public static string Serialize(object value, JsonSerializerOptions jsonSerializerOptions = null) { +#if NET8_0_OR_GREATER var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(value.GetType()); return JsonSerializer.Serialize(value, info); +#else + return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions); +#endif } public static string Serialize(JsonDocument value, JsonSerializerOptions jsonSerializerOptions = null) { - var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(JsonDocument)); +#if NET8_0_OR_GREATER + var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(value.GetType()); return JsonSerializer.Serialize(value, info); +#else + return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions); +#endif } public static string Serialize(JsonElement value, JsonSerializerOptions jsonSerializerOptions = null) { - var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(JsonElement)); +#if NET8_0_OR_GREATER + var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(value.GetType()); return JsonSerializer.Serialize(value, info); +#else + return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions); +#endif } public static string Serialize(JsonNode value, JsonSerializerOptions jsonSerializerOptions = null) { - var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(JsonNode)); +#if NET8_0_OR_GREATER + var info = (jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(value.GetType()); return JsonSerializer.Serialize(value, info); +#else + return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions); +#endif } } } diff --git a/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template b/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template index f1fb9c66a..0a5113981 100644 --- a/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template +++ b/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template @@ -2,6 +2,7 @@ // Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator // Changes may cause incorrect behavior and will be lost if the code is regenerated. // +#if NET8_0_OR_GREATER namespace k8s { {{ for definition in definitions }} @@ -11,3 +12,4 @@ namespace k8s { } } +#endif From f9980d1f7993f0df20d5f1a70004f5947561d2e5 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Tue, 21 Oct 2025 21:12:35 -0700 Subject: [PATCH 06/21] Update KubernetesClient.Classic.csproj From 7d29c48929eda39d1bc083ce968b920b6755fbcd Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Tue, 21 Oct 2025 21:16:37 -0700 Subject: [PATCH 07/21] wip --- src/KubernetesClient/Models/V1Status.ObjectView.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/KubernetesClient/Models/V1Status.ObjectView.cs b/src/KubernetesClient/Models/V1Status.ObjectView.cs index 87a1a28d2..761d76616 100644 --- a/src/KubernetesClient/Models/V1Status.ObjectView.cs +++ b/src/KubernetesClient/Models/V1Status.ObjectView.cs @@ -1,4 +1,6 @@ +#if NET8_0_OR_GREATER using System.Text.Json.Serialization.Metadata; +#endif namespace k8s.Models { @@ -12,7 +14,11 @@ public override V1Status Read(ref Utf8JsonReader reader, Type typeToConvert, Jso try { +#if NET8_0_OR_GREATER return obj.Deserialize((JsonTypeInfo)options.GetTypeInfo(typeof(V1Status))); +#else + return obj.Deserialize(); +#endif } catch (JsonException) { @@ -34,7 +40,11 @@ public override void Write(Utf8JsonWriter writer, V1Status value, JsonSerializer public T ObjectView() { +#if NET8_0_OR_GREATER return _original.Deserialize((JsonTypeInfo)KubernetesJson.JsonSerializerOptions.GetTypeInfo(typeof(T))); +#else + return _original.Deserialize(); +#endif } } } From 66d51e33a9147f180669473ce9c9b36eb67fcf52 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Tue, 21 Oct 2025 21:50:56 -0700 Subject: [PATCH 08/21] enable UseStringEnumConverter --- src/KubernetesClient/SourceGenerationContext.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/KubernetesClient/SourceGenerationContext.cs b/src/KubernetesClient/SourceGenerationContext.cs index c2af3e7bb..c4dbb64f4 100644 --- a/src/KubernetesClient/SourceGenerationContext.cs +++ b/src/KubernetesClient/SourceGenerationContext.cs @@ -5,6 +5,7 @@ namespace k8s; [JsonSourceGenerationOptions( DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, + UseStringEnumConverter = true, Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter), typeof(V1Status.V1StatusObjectViewConverter) }) ] public partial class SourceGenerationContext : JsonSerializerContext From 9c142e1af4d2db2866adbd75c63149bb74c5f999 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Tue, 21 Oct 2025 22:13:49 -0700 Subject: [PATCH 09/21] chore: make converters public so we can use them in our libraries --- src/KubernetesClient/KubernetesJson.cs | 6 +++--- src/KubernetesClient/Models/V1Status.ObjectView.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/KubernetesClient/KubernetesJson.cs b/src/KubernetesClient/KubernetesJson.cs index e55cf09a8..5fb127ab2 100644 --- a/src/KubernetesClient/KubernetesJson.cs +++ b/src/KubernetesClient/KubernetesJson.cs @@ -13,7 +13,7 @@ public static class KubernetesJson { internal static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions(); - internal sealed class Iso8601TimeSpanConverter : JsonConverter + public sealed class Iso8601TimeSpanConverter : JsonConverter { public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { @@ -28,7 +28,7 @@ public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializer } } - internal sealed class KubernetesDateTimeOffsetConverter : JsonConverter + public sealed class KubernetesDateTimeOffsetConverter : JsonConverter { private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffK"; private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffK"; @@ -60,7 +60,7 @@ public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSeri } } - internal sealed class KubernetesDateTimeConverter : JsonConverter + public sealed class KubernetesDateTimeConverter : JsonConverter { private static readonly JsonConverter UtcConverter = new KubernetesDateTimeOffsetConverter(); public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) diff --git a/src/KubernetesClient/Models/V1Status.ObjectView.cs b/src/KubernetesClient/Models/V1Status.ObjectView.cs index 761d76616..61c23b925 100644 --- a/src/KubernetesClient/Models/V1Status.ObjectView.cs +++ b/src/KubernetesClient/Models/V1Status.ObjectView.cs @@ -6,7 +6,7 @@ namespace k8s.Models { public partial record V1Status { - internal sealed class V1StatusObjectViewConverter : JsonConverter + public sealed class V1StatusObjectViewConverter : JsonConverter { public override V1Status Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { From cf19b573f56337d5d5517e80f2ef77eb7c7d2bcd Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Tue, 21 Oct 2025 23:44:56 -0700 Subject: [PATCH 10/21] fix: recursion and remove converter from source gen --- .../Models/V1Status.ObjectView.cs | 28 +++++++++++++------ .../SourceGenerationContext.cs | 2 +- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/KubernetesClient/Models/V1Status.ObjectView.cs b/src/KubernetesClient/Models/V1Status.ObjectView.cs index 61c23b925..c3896a486 100644 --- a/src/KubernetesClient/Models/V1Status.ObjectView.cs +++ b/src/KubernetesClient/Models/V1Status.ObjectView.cs @@ -1,21 +1,33 @@ -#if NET8_0_OR_GREATER -using System.Text.Json.Serialization.Metadata; -#endif - namespace k8s.Models { public partial record V1Status { public sealed class V1StatusObjectViewConverter : JsonConverter { + private static JsonSerializerOptions WithoutThis(JsonSerializerOptions options) + { + var clone = new JsonSerializerOptions(options); + + for (var i = clone.Converters.Count - 1; i >= 0; i--) + { + if (clone.Converters[i] is V1StatusObjectViewConverter) + { + clone.Converters.RemoveAt(i); + } + } + + return clone; + } + public override V1Status Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - var obj = JsonElement.ParseValue(ref reader); + using var doc = JsonDocument.ParseValue(ref reader); + var ele = doc.RootElement.Clone(); try { #if NET8_0_OR_GREATER - return obj.Deserialize((JsonTypeInfo)options.GetTypeInfo(typeof(V1Status))); + return KubernetesJson.Deserialize(ele, WithoutThis(options)); #else return obj.Deserialize(); #endif @@ -25,7 +37,7 @@ public override V1Status Read(ref Utf8JsonReader reader, Type typeToConvert, Jso // should be an object } - return new V1Status { _original = obj, HasObject = true }; + return new V1Status { _original = ele, HasObject = true }; } public override void Write(Utf8JsonWriter writer, V1Status value, JsonSerializerOptions options) @@ -41,7 +53,7 @@ public override void Write(Utf8JsonWriter writer, V1Status value, JsonSerializer public T ObjectView() { #if NET8_0_OR_GREATER - return _original.Deserialize((JsonTypeInfo)KubernetesJson.JsonSerializerOptions.GetTypeInfo(typeof(T))); + return KubernetesJson.Deserialize(_original); #else return _original.Deserialize(); #endif diff --git a/src/KubernetesClient/SourceGenerationContext.cs b/src/KubernetesClient/SourceGenerationContext.cs index c4dbb64f4..23ef2d32b 100644 --- a/src/KubernetesClient/SourceGenerationContext.cs +++ b/src/KubernetesClient/SourceGenerationContext.cs @@ -6,7 +6,7 @@ namespace k8s; DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, UseStringEnumConverter = true, - Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter), typeof(V1Status.V1StatusObjectViewConverter) }) + Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter) }) ] public partial class SourceGenerationContext : JsonSerializerContext { From 22736609766c66af58406c8b3d8a1eaf372dc40e Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Wed, 22 Oct 2025 13:45:15 -0700 Subject: [PATCH 11/21] fix: V1StatusObjectViewConverter --- .../Models/V1Status.ObjectView.cs | 19 ++----------------- src/KubernetesClient/Models/V1Status.cs | 1 + .../SourceGenerationContext.cs | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/KubernetesClient/Models/V1Status.ObjectView.cs b/src/KubernetesClient/Models/V1Status.ObjectView.cs index c3896a486..710657847 100644 --- a/src/KubernetesClient/Models/V1Status.ObjectView.cs +++ b/src/KubernetesClient/Models/V1Status.ObjectView.cs @@ -4,21 +4,6 @@ public partial record V1Status { public sealed class V1StatusObjectViewConverter : JsonConverter { - private static JsonSerializerOptions WithoutThis(JsonSerializerOptions options) - { - var clone = new JsonSerializerOptions(options); - - for (var i = clone.Converters.Count - 1; i >= 0; i--) - { - if (clone.Converters[i] is V1StatusObjectViewConverter) - { - clone.Converters.RemoveAt(i); - } - } - - return clone; - } - public override V1Status Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { using var doc = JsonDocument.ParseValue(ref reader); @@ -27,9 +12,9 @@ public override V1Status Read(ref Utf8JsonReader reader, Type typeToConvert, Jso try { #if NET8_0_OR_GREATER - return KubernetesJson.Deserialize(ele, WithoutThis(options)); + return JsonSerializer.Deserialize(ele, StatusSourceGenerationContext.Default.V1Status); #else - return obj.Deserialize(); + return ele.Deserialize(); #endif } catch (JsonException) diff --git a/src/KubernetesClient/Models/V1Status.cs b/src/KubernetesClient/Models/V1Status.cs index d69116d2e..b7d754425 100644 --- a/src/KubernetesClient/Models/V1Status.cs +++ b/src/KubernetesClient/Models/V1Status.cs @@ -2,6 +2,7 @@ namespace k8s.Models { + [JsonConverter(typeof(V1StatusObjectViewConverter))] public partial record V1Status { /// Converts a object into a short description of the status. diff --git a/src/KubernetesClient/SourceGenerationContext.cs b/src/KubernetesClient/SourceGenerationContext.cs index 23ef2d32b..22cc9fa00 100644 --- a/src/KubernetesClient/SourceGenerationContext.cs +++ b/src/KubernetesClient/SourceGenerationContext.cs @@ -1,4 +1,5 @@ using static k8s.KubernetesJson; +using static k8s.Models.V1Status; namespace k8s; @@ -6,8 +7,22 @@ namespace k8s; DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, UseStringEnumConverter = true, - Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter) }) + Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter), typeof(V1StatusObjectViewConverter) }) ] public partial class SourceGenerationContext : JsonSerializerContext { } + +/// +/// Used by V1Status in order to avoid the recursive loop as SourceGenerationContext contains V1StatusObjectViewConverter +/// +[JsonSerializable(typeof(V1Status))] +[JsonSourceGenerationOptions( + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, + UseStringEnumConverter = true, + Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter) }) + ] +public partial class StatusSourceGenerationContext : JsonSerializerContext +{ +} From 296639be80665d1bd790e134741412ee037712e0 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Wed, 22 Oct 2025 14:14:38 -0700 Subject: [PATCH 12/21] wip --- .github/workflows/buildtest.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/buildtest.yaml b/.github/workflows/buildtest.yaml index b97747c7b..051e89857 100644 --- a/.github/workflows/buildtest.yaml +++ b/.github/workflows/buildtest.yaml @@ -1,4 +1,9 @@ name: Build and Test +on: + workflow_dispatch: + push: + branches: + - 'json-source-gen' # temp for testing jobs: build: From 50ee62c8951461c99ea112961af68234db63cfb0 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Wed, 22 Oct 2025 14:16:01 -0700 Subject: [PATCH 13/21] wip --- .github/workflows/buildtest.yaml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/buildtest.yaml b/.github/workflows/buildtest.yaml index 051e89857..0d05b4f9b 100644 --- a/.github/workflows/buildtest.yaml +++ b/.github/workflows/buildtest.yaml @@ -1,9 +1,4 @@ name: Build and Test -on: - workflow_dispatch: - push: - branches: - - 'json-source-gen' # temp for testing jobs: build: @@ -93,3 +88,6 @@ jobs: on: pull_request: types: [assigned, opened, synchronize, reopened] + push: + branches: + - 'json-source-gen' # temp for testing \ No newline at end of file From b526a1ee01266b9086408fb23738fea75ae62064 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Wed, 22 Oct 2025 14:18:59 -0700 Subject: [PATCH 14/21] wip --- src/KubernetesClient/Models/V1Status.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/KubernetesClient/Models/V1Status.cs b/src/KubernetesClient/Models/V1Status.cs index b7d754425..d69116d2e 100644 --- a/src/KubernetesClient/Models/V1Status.cs +++ b/src/KubernetesClient/Models/V1Status.cs @@ -2,7 +2,6 @@ namespace k8s.Models { - [JsonConverter(typeof(V1StatusObjectViewConverter))] public partial record V1Status { /// Converts a object into a short description of the status. From 36b2f6c7af7502d46ffa85864b02117826a90b49 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Wed, 22 Oct 2025 22:39:54 -0700 Subject: [PATCH 15/21] fix: rfc3339 json serialization and yaml de/serialization --- src/KubernetesClient/KubernetesJson.cs | 17 ++- src/KubernetesClient/KubernetesYaml.cs | 4 + .../KubernetesDateTimeOffsetYamlConverter.cs | 62 ++++++++ .../Models/KubernetesDateTimeYamlConverter.cs | 21 +++ .../KubernetesJsonTests.cs | 26 ++++ .../KubernetesYamlTests.cs | 143 ++++++++++++++++++ 6 files changed, 269 insertions(+), 4 deletions(-) create mode 100644 src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs create mode 100644 src/KubernetesClient/Models/KubernetesDateTimeYamlConverter.cs diff --git a/src/KubernetesClient/KubernetesJson.cs b/src/KubernetesClient/KubernetesJson.cs index 5fb127ab2..15e779e65 100644 --- a/src/KubernetesClient/KubernetesJson.cs +++ b/src/KubernetesClient/KubernetesJson.cs @@ -30,9 +30,9 @@ public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializer public sealed class KubernetesDateTimeOffsetConverter : JsonConverter { - private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffK"; - private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffK"; - private const string RFC3339Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK"; + private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffZ"; + private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffZ"; + private const string RFC3339Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"; public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { @@ -54,9 +54,18 @@ public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConver throw new FormatException($"Unable to parse {originalstr} as RFC3339 RFC3339Micro or RFC3339Nano"); } + public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) { - writer.WriteStringValue(value.ToString(RFC3339MicroFormat)); + // Output as RFC3339Nano + var date = value.ToUniversalTime(); + + var basePart = date.ToString("yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture); + var frac = date.ToString(".fffffff", CultureInfo.InvariantCulture) + .TrimEnd('0') + .TrimEnd('.'); + + writer.WriteStringValue(basePart + frac + "Z"); } } diff --git a/src/KubernetesClient/KubernetesYaml.cs b/src/KubernetesClient/KubernetesYaml.cs index 84feb3584..ef5384c72 100644 --- a/src/KubernetesClient/KubernetesYaml.cs +++ b/src/KubernetesClient/KubernetesYaml.cs @@ -21,6 +21,8 @@ public static class KubernetesYaml .WithTypeConverter(new IntOrStringYamlConverter()) .WithTypeConverter(new ByteArrayStringYamlConverter()) .WithTypeConverter(new ResourceQuantityYamlConverter()) + .WithTypeConverter(new KubernetesDateTimeYamlConverter()) + .WithTypeConverter(new KubernetesDateTimeOffsetYamlConverter()) .WithAttemptingUnquotedStringTypeDeserialization() .WithOverridesFromJsonPropertyAttributes(); @@ -41,6 +43,8 @@ public static class KubernetesYaml .WithTypeConverter(new IntOrStringYamlConverter()) .WithTypeConverter(new ByteArrayStringYamlConverter()) .WithTypeConverter(new ResourceQuantityYamlConverter()) + .WithTypeConverter(new KubernetesDateTimeYamlConverter()) + .WithTypeConverter(new KubernetesDateTimeOffsetYamlConverter()) .WithEventEmitter(e => new StringQuotingEmitter(e)) .WithEventEmitter(e => new FloatEmitter(e)) .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull) diff --git a/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs b/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs new file mode 100644 index 000000000..1cc85e00e --- /dev/null +++ b/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs @@ -0,0 +1,62 @@ +using System.Globalization; +using System.Text.RegularExpressions; +using YamlDotNet.Core; +using YamlDotNet.Core.Events; +using YamlDotNet.Serialization; + +public sealed class KubernetesDateTimeOffsetYamlConverter : IYamlTypeConverter +{ + private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffff'Z'"; + private const string RFC3339NanoFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffff'Z'"; + private const string RFC3339Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"; + + public bool Accepts(Type type) => type == typeof(DateTimeOffset); + + public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) + { + if (parser?.Current is Scalar scalar) + { + try + { + if (string.IsNullOrEmpty(scalar.Value)) + { + return null; + } + + var str = scalar.Value; + + if (DateTimeOffset.TryParseExact(str, new[] { RFC3339Format, RFC3339MicroFormat }, CultureInfo.InvariantCulture, DateTimeStyles.None, out var result)) + { + return result; + } + + // try RFC3339NanoLenient by trimming 1-9 digits to 7 digits + var originalstr = str; + str = Regex.Replace(str, @"\.\d+", m => (m.Value + "000000000").Substring(0, 7 + 1)); // 7 digits + 1 for the dot + if (DateTimeOffset.TryParseExact(str, new[] { RFC3339NanoFormat }, CultureInfo.InvariantCulture, DateTimeStyles.None, out result)) + { + return result; + } + } + finally + { + parser.MoveNext(); + } + } + + throw new InvalidOperationException($"Unable to parse '{parser.Current?.ToString()}' as RFC3339, RFC3339Micro, or RFC3339Nano"); + } + + public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer) + { + // Output as RFC3339Nano + var date = ((DateTimeOffset)value).ToUniversalTime(); + + var basePart = date.ToString("yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture); + var frac = date.ToString(".fffffff", CultureInfo.InvariantCulture) + .TrimEnd('0') + .TrimEnd('.'); + + emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, basePart + frac + "Z", ScalarStyle.DoubleQuoted, true, false)); + } +} diff --git a/src/KubernetesClient/Models/KubernetesDateTimeYamlConverter.cs b/src/KubernetesClient/Models/KubernetesDateTimeYamlConverter.cs new file mode 100644 index 000000000..42b6183eb --- /dev/null +++ b/src/KubernetesClient/Models/KubernetesDateTimeYamlConverter.cs @@ -0,0 +1,21 @@ +using YamlDotNet.Core; +using YamlDotNet.Serialization; + +public sealed class KubernetesDateTimeYamlConverter : IYamlTypeConverter +{ + private static readonly KubernetesDateTimeOffsetYamlConverter OffsetConverter = new(); + + public bool Accepts(Type type) => type == typeof(DateTime); + + public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer) + { + var dto = (DateTimeOffset)OffsetConverter.ReadYaml(parser, typeof(DateTimeOffset), rootDeserializer); + return dto.DateTime; + } + + public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer) + { + var date = new DateTimeOffset((DateTime)value); + OffsetConverter.WriteYaml(emitter, date, typeof(DateTimeOffset), serializer); + } +} diff --git a/tests/KubernetesClient.Tests/KubernetesJsonTests.cs b/tests/KubernetesClient.Tests/KubernetesJsonTests.cs index f923fd083..785e38fea 100644 --- a/tests/KubernetesClient.Tests/KubernetesJsonTests.cs +++ b/tests/KubernetesClient.Tests/KubernetesJsonTests.cs @@ -1,5 +1,7 @@ using System; +using System.Text.Json; using Xunit; +using k8s.Models; namespace k8s.Tests; @@ -116,4 +118,28 @@ type Time struct { #endif } + + [Fact] + public void ReadWriteDatesJson() + { + var kManifest = """ + { + "apiVersion": "v1", + "kind": "Secret", + "metadata": { + "creationTimestamp": "2025-09-03T05:15:53Z", + "name": "test-secret" + }, + "type": "Opaque" + } + """; + + var objFromJson = KubernetesJson.Deserialize(kManifest); + var jsonFromObj = KubernetesJson.Serialize(objFromJson); + + // Format Json + var jsonFromObj2 = JsonSerializer.Serialize(JsonSerializer.Deserialize(jsonFromObj), new JsonSerializerOptions() { WriteIndented = true }); + + Assert.Equal(kManifest, jsonFromObj2); + } } diff --git a/tests/KubernetesClient.Tests/KubernetesYamlTests.cs b/tests/KubernetesClient.Tests/KubernetesYamlTests.cs index 1b18f5493..95440e9e2 100644 --- a/tests/KubernetesClient.Tests/KubernetesYamlTests.cs +++ b/tests/KubernetesClient.Tests/KubernetesYamlTests.cs @@ -1170,5 +1170,148 @@ public void NoGlobalization() CultureInfo.CurrentCulture = old; } } + + [Fact] + public void ReadWriteDatesYaml() + { + var kManifest = """ + apiVersion: v1 + kind: Secret + metadata: + creationTimestamp: "2025-09-03T05:15:53Z" + name: test-secret + type: Opaque + """; + + var objFromYaml = KubernetesYaml.Deserialize(kManifest, true); + var yamlFromObj = KubernetesYaml.Serialize(objFromYaml); + + Assert.Equal(kManifest, yamlFromObj); + } + + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Build", "CA1812:'KubernetesYamlTests.RfcTime' is an internal class that is apparently never instantiated. If so, remove the code from the assembly. If this class is intended to contain only static members, make it 'static' (Module in Visual Basic). (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1812)", Justification = "json type")] + private class RfcTime + { + public DateTime Rfc3339 { get; set; } + public DateTime Rfc3339micro { get; set; } + public DateTime Rfc3339nano { get; set; } + public DateTime Rfc3339nanolenient1 { get; set; } + public DateTime Rfc3339nanolenient2 { get; set; } + public DateTime Rfc3339nanolenient3 { get; set; } + public DateTime Rfc3339nanolenient4 { get; set; } + public DateTime Rfc3339nanolenient5 { get; set; } + public DateTime Rfc3339nanolenient6 { get; set; } + public DateTime Rfc3339nanolenient7 { get; set; } + public DateTime Rfc3339nanolenient8 { get; set; } + public DateTime Rfc3339nanolenient9 { get; set; } + } + + [Fact] + public void RFC3339() + { + /* go code to generate the json https://go.dev/play/p/VL95pugm6o8 + + const RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00" + const RFC3339Nano = "2006-01-02T15:04:05.000000000Z07:00" + + func main() { + t := time.Now() + type Time struct { + RFC3339 string `json:"rfc3339"` + RFC3339Micro string `json:"rfc3339micro"` + RFC3339Nano string `json:"rfc3339nano"` + + RFC3339NanoLenient1 string `json:"rfc3339nanolenient1"` + RFC3339NanoLenient2 string `json:"rfc3339nanolenient2"` + RFC3339NanoLenient3 string `json:"rfc3339nanolenient3"` + RFC3339NanoLenient4 string `json:"rfc3339nanolenient4"` + RFC3339NanoLenient5 string `json:"rfc3339nanolenient5"` + RFC3339NanoLenient6 string `json:"rfc3339nanolenient6"` + RFC3339NanoLenient7 string `json:"rfc3339nanolenient7"` + RFC3339NanoLenient8 string `json:"rfc3339nanolenient8"` + RFC3339NanoLenient9 string `json:"rfc3339nanolenient9"` + } + t1 := Time{ + RFC3339: t.Add(45 * time.Minute).Add(12 * time.Second).Add(123456789 * time.Nanosecond).Format(time.RFC3339), + RFC3339Micro: t.Add(45 * time.Minute).Add(12 * time.Second).Add(123456789 * time.Nanosecond).Format(RFC3339Micro), + RFC3339Nano: t.Add(24 * time.Minute).Add(56 * time.Second).Add(123456789 * time.Nanosecond).Format(RFC3339Nano), + + RFC3339NanoLenient1: t.Add(100000000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient2: t.Add(120000000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient3: t.Add(123000000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient4: t.Add(123400000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient5: t.Add(123450000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient6: t.Add(123456000 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient7: t.Add(123456700 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient8: t.Add(123456780 * time.Nanosecond).Format(time.RFC3339Nano), + RFC3339NanoLenient9: t.Add(123456789 * time.Nanosecond).Format(time.RFC3339Nano), + } + b, err := json.Marshal(t1) + if err != nil { + fmt.Println("error:", err) + } + fmt.Println(string(b)) + } + */ + + var yaml = """ + rfc3339: '2009-11-10T23:45:12Z' + rfc3339micro: '2009-11-10T23:45:12.123456Z' + rfc3339nano: '2009-11-10T23:24:56.123456789Z' + rfc3339nanolenient1: '2009-11-10T23:00:00.1Z' + rfc3339nanolenient2: '2009-11-10T23:00:00.12Z' + rfc3339nanolenient3: '2009-11-10T23:00:00.123Z' + rfc3339nanolenient4: '2009-11-10T23:00:00.1234Z' + rfc3339nanolenient5: '2009-11-10T23:00:00.12345Z' + rfc3339nanolenient6: '2009-11-10T23:00:00.123456Z' + rfc3339nanolenient7: '2009-11-10T23:00:00.1234567Z' + rfc3339nanolenient8: '2009-11-10T23:00:00.12345678Z' + rfc3339nanolenient9: '2009-11-10T23:00:00.123456789Z' + """; + + var t = KubernetesYaml.Deserialize(yaml); + + Assert.Equal(new DateTime(2009, 11, 10, 23, 45, 12, DateTimeKind.Utc), t.Rfc3339); + + Assert.Equal(2009, t.Rfc3339micro.Year); + Assert.Equal(11, t.Rfc3339micro.Month); + Assert.Equal(10, t.Rfc3339micro.Day); + Assert.Equal(23, t.Rfc3339micro.Hour); + Assert.Equal(45, t.Rfc3339micro.Minute); + Assert.Equal(12, t.Rfc3339micro.Second); + Assert.Equal(123, t.Rfc3339micro.Millisecond); + + Assert.Equal(2009, t.Rfc3339nano.Year); + Assert.Equal(11, t.Rfc3339nano.Month); + Assert.Equal(10, t.Rfc3339nano.Day); + Assert.Equal(23, t.Rfc3339nano.Hour); + Assert.Equal(24, t.Rfc3339nano.Minute); + Assert.Equal(56, t.Rfc3339nano.Second); + Assert.Equal(123, t.Rfc3339nano.Millisecond); + +#if NET7_0_OR_GREATER + Assert.Equal(456, t.Rfc3339micro.Microsecond); + Assert.Equal(456, t.Rfc3339nano.Microsecond); + Assert.Equal(700, t.Rfc3339nano.Nanosecond); + + Assert.Equal(100, t.Rfc3339nanolenient1.Millisecond); + Assert.Equal(120, t.Rfc3339nanolenient2.Millisecond); + Assert.Equal(123, t.Rfc3339nanolenient3.Millisecond); + + Assert.Equal(400, t.Rfc3339nanolenient4.Microsecond); + Assert.Equal(450, t.Rfc3339nanolenient5.Microsecond); + Assert.Equal(456, t.Rfc3339nanolenient6.Microsecond); + + Assert.Equal(456, t.Rfc3339nanolenient7.Microsecond); + Assert.Equal(456, t.Rfc3339nanolenient8.Microsecond); + Assert.Equal(456, t.Rfc3339nanolenient9.Microsecond); + + Assert.Equal(700, t.Rfc3339nanolenient7.Nanosecond); + Assert.Equal(700, t.Rfc3339nanolenient8.Nanosecond); + Assert.Equal(700, t.Rfc3339nanolenient9.Nanosecond); +#endif + + } } } From 2dd853f190c9d75babb7f32270f4b377f9dfbfd1 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Wed, 22 Oct 2025 22:42:50 -0700 Subject: [PATCH 16/21] chore: add namespace --- .../Models/KubernetesDateTimeOffsetYamlConverter.cs | 2 ++ src/KubernetesClient/Models/KubernetesDateTimeYamlConverter.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs b/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs index 1cc85e00e..ebf1758db 100644 --- a/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs +++ b/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs @@ -4,6 +4,8 @@ using YamlDotNet.Core.Events; using YamlDotNet.Serialization; +namespace k8s.Models; + public sealed class KubernetesDateTimeOffsetYamlConverter : IYamlTypeConverter { private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffff'Z'"; diff --git a/src/KubernetesClient/Models/KubernetesDateTimeYamlConverter.cs b/src/KubernetesClient/Models/KubernetesDateTimeYamlConverter.cs index 42b6183eb..d6095983d 100644 --- a/src/KubernetesClient/Models/KubernetesDateTimeYamlConverter.cs +++ b/src/KubernetesClient/Models/KubernetesDateTimeYamlConverter.cs @@ -1,6 +1,8 @@ using YamlDotNet.Core; using YamlDotNet.Serialization; +namespace k8s.Models; + public sealed class KubernetesDateTimeYamlConverter : IYamlTypeConverter { private static readonly KubernetesDateTimeOffsetYamlConverter OffsetConverter = new(); From 8fe927caf18df31e82fb6540d75fb3ba75ff4789 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Wed, 22 Oct 2025 22:55:44 -0700 Subject: [PATCH 17/21] fix: imports --- src/KubernetesClient.Aot/KubernetesClient.Aot.csproj | 2 ++ src/KubernetesClient.Classic/KubernetesClient.Classic.csproj | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj b/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj index 09155cda7..5c7cf8fed 100644 --- a/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj +++ b/src/KubernetesClient.Aot/KubernetesClient.Aot.csproj @@ -46,6 +46,8 @@ + + diff --git a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj index 91cd69263..902dc41dd 100644 --- a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj +++ b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj @@ -49,6 +49,9 @@ + + + From 8a5fe8bc95c58720c46dc19d5a808aa0611962a2 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Wed, 22 Oct 2025 23:23:12 -0700 Subject: [PATCH 18/21] fix: switch output to RFC3339Micro to fit Time and MicroTime --- src/KubernetesClient/KubernetesJson.cs | 4 ++-- .../Models/KubernetesDateTimeOffsetYamlConverter.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/KubernetesClient/KubernetesJson.cs b/src/KubernetesClient/KubernetesJson.cs index 15e779e65..bb4b34742 100644 --- a/src/KubernetesClient/KubernetesJson.cs +++ b/src/KubernetesClient/KubernetesJson.cs @@ -57,11 +57,11 @@ public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConver public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) { - // Output as RFC3339Nano + // Output as RFC3339Micro var date = value.ToUniversalTime(); var basePart = date.ToString("yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture); - var frac = date.ToString(".fffffff", CultureInfo.InvariantCulture) + var frac = date.ToString(".ffffff", CultureInfo.InvariantCulture) .TrimEnd('0') .TrimEnd('.'); diff --git a/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs b/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs index ebf1758db..5eb4153eb 100644 --- a/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs +++ b/src/KubernetesClient/Models/KubernetesDateTimeOffsetYamlConverter.cs @@ -51,11 +51,11 @@ public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeseria public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer) { - // Output as RFC3339Nano + // Output as RFC3339Micro var date = ((DateTimeOffset)value).ToUniversalTime(); var basePart = date.ToString("yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture); - var frac = date.ToString(".fffffff", CultureInfo.InvariantCulture) + var frac = date.ToString(".ffffff", CultureInfo.InvariantCulture) .TrimEnd('0') .TrimEnd('.'); From 2b1ef1f67ff8553bed052fa9198521bb7035585a Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Thu, 23 Oct 2025 00:09:43 -0700 Subject: [PATCH 19/21] chore: update AOT to match KubernetesYaml --- src/KubernetesClient.Aot/KubernetesYaml.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/KubernetesClient.Aot/KubernetesYaml.cs b/src/KubernetesClient.Aot/KubernetesYaml.cs index 069b1c12e..5530a2e02 100644 --- a/src/KubernetesClient.Aot/KubernetesYaml.cs +++ b/src/KubernetesClient.Aot/KubernetesYaml.cs @@ -17,6 +17,8 @@ internal static class KubernetesYaml .WithTypeConverter(new IntOrStringYamlConverter()) .WithTypeConverter(new ByteArrayStringYamlConverter()) .WithTypeConverter(new ResourceQuantityYamlConverter()) + .WithTypeConverter(new KubernetesDateTimeYamlConverter()) + .WithTypeConverter(new KubernetesDateTimeOffsetYamlConverter()) .WithAttemptingUnquotedStringTypeDeserialization() ; @@ -33,6 +35,8 @@ internal static class KubernetesYaml .WithTypeConverter(new IntOrStringYamlConverter()) .WithTypeConverter(new ByteArrayStringYamlConverter()) .WithTypeConverter(new ResourceQuantityYamlConverter()) + .WithTypeConverter(new KubernetesDateTimeYamlConverter()) + .WithTypeConverter(new KubernetesDateTimeOffsetYamlConverter()) .WithEventEmitter(e => new StringQuotingEmitter(e)) .WithEventEmitter(e => new FloatEmitter(e)) .ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull) @@ -56,7 +60,7 @@ public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeseria return null; } - return Encoding.UTF8.GetBytes(scalar.Value); + return Convert.FromBase64String(scalar.Value); } finally { @@ -69,8 +73,15 @@ public object ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeseria public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer) { + if (value == null) + { + emitter.Emit(new Scalar(string.Empty)); + return; + } + var obj = (byte[])value; - emitter?.Emit(new Scalar(Encoding.UTF8.GetString(obj))); + var encoded = Convert.ToBase64String(obj); + emitter.Emit(new Scalar(encoded)); } } From e7211adcdb961e104ef6e9ee72effdccfc90aa4d Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Thu, 23 Oct 2025 00:10:46 -0700 Subject: [PATCH 20/21] fix aot --- src/KubernetesClient/KubernetesJson.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/KubernetesClient/KubernetesJson.cs b/src/KubernetesClient/KubernetesJson.cs index bb4b34742..69ecdf43e 100644 --- a/src/KubernetesClient/KubernetesJson.cs +++ b/src/KubernetesClient/KubernetesJson.cs @@ -92,6 +92,8 @@ static KubernetesJson() #if NET8_0_OR_GREATER // Uses Source Generated IJsonTypeInfoResolver when available and falls back to reflection JsonSerializerOptions.TypeInfoResolver = JsonTypeInfoResolver.Combine(SourceGenerationContext.Default, new DefaultJsonTypeInfoResolver()); +#endif + JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); #endif JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; @@ -99,8 +101,6 @@ static KubernetesJson() JsonSerializerOptions.Converters.Add(new KubernetesDateTimeConverter()); JsonSerializerOptions.Converters.Add(new KubernetesDateTimeOffsetConverter()); JsonSerializerOptions.Converters.Add(new V1Status.V1StatusObjectViewConverter()); - JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); -#endif } /// From 889b84d3f3bc5f2985ab44bb52f923adf233d548 Mon Sep 17 00:00:00 2001 From: Ivan Josipovic <9521987+IvanJosipovic@users.noreply.github.com> Date: Thu, 23 Oct 2025 00:24:18 -0700 Subject: [PATCH 21/21] Update buildtest.yaml --- .github/workflows/buildtest.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/buildtest.yaml b/.github/workflows/buildtest.yaml index 0d05b4f9b..b97747c7b 100644 --- a/.github/workflows/buildtest.yaml +++ b/.github/workflows/buildtest.yaml @@ -88,6 +88,3 @@ jobs: on: pull_request: types: [assigned, opened, synchronize, reopened] - push: - branches: - - 'json-source-gen' # temp for testing \ No newline at end of file