feat(KubernetesJson): Use of SourceGenerationContext outside AOT (#1677)
* feat: initial source gen for json * wip * chore: readd default settings * chore: cleanup * chore: remove source gen from classic as it depends on C#9 support * Update KubernetesClient.Classic.csproj * wip * enable UseStringEnumConverter * chore: make converters public so we can use them in our libraries * fix: recursion and remove converter from source gen * fix: V1StatusObjectViewConverter * wip * wip * wip * fix: rfc3339 json serialization and yaml de/serialization * chore: add namespace * fix: imports * fix: switch output to RFC3339Micro to fit Time and MicroTime * chore: update AOT to match KubernetesYaml * fix aot * Update buildtest.yaml
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
|
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
|
||||||
@@ -43,7 +43,11 @@
|
|||||||
<Compile Include="..\KubernetesClient\Models\V1Patch.cs" />
|
<Compile Include="..\KubernetesClient\Models\V1Patch.cs" />
|
||||||
<Compile Include="..\KubernetesClient\Models\V1PodTemplateSpec.cs" />
|
<Compile Include="..\KubernetesClient\Models\V1PodTemplateSpec.cs" />
|
||||||
<Compile Include="..\KubernetesClient\Models\V1Status.cs" />
|
<Compile Include="..\KubernetesClient\Models\V1Status.cs" />
|
||||||
|
<Compile Include="..\KubernetesClient\KubernetesJson.cs" />
|
||||||
|
<Compile Include="..\KubernetesClient\SourceGenerationContext.cs" />
|
||||||
|
<Compile Include="..\KubernetesClient\Models\V1Status.ObjectView.cs" />
|
||||||
|
<Compile Include="..\KubernetesClient\Models\KubernetesDateTimeOffsetYamlConverter.cs" />
|
||||||
|
<Compile Include="..\KubernetesClient\Models\KubernetesDateTimeYamlConverter.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="..\KubernetesClient\ClientSets\ClientSet.cs" />
|
<Compile Include="..\KubernetesClient\ClientSets\ClientSet.cs" />
|
||||||
|
|||||||
@@ -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<TimeSpan>
|
|
||||||
{
|
|
||||||
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<DateTimeOffset>
|
|
||||||
{
|
|
||||||
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<DateTime>
|
|
||||||
{
|
|
||||||
private static readonly JsonConverter<DateTimeOffset> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Configures <see cref="JsonSerializerOptions"/> for the <see cref="JsonSerializer"/>.
|
|
||||||
/// To override existing converters, add them to the top of the <see cref="JsonSerializerOptions.Converters"/> list
|
|
||||||
/// e.g. as follows: <code>options.Converters.Insert(index: 0, new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));</code>
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="configure">An <see cref="Action"/> to configure the <see cref="JsonSerializerOptions"/>.</param>
|
|
||||||
public static void AddJsonOptions(Action<JsonSerializerOptions> configure)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TValue Deserialize<TValue>(string json, JsonSerializerOptions jsonSerializerOptions = null)
|
|
||||||
{
|
|
||||||
var info = SourceGenerationContext.Default.GetTypeInfo(typeof(TValue));
|
|
||||||
return (TValue)JsonSerializer.Deserialize(json, info);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TValue Deserialize<TValue>(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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -17,6 +17,8 @@ namespace k8s
|
|||||||
.WithTypeConverter(new IntOrStringYamlConverter())
|
.WithTypeConverter(new IntOrStringYamlConverter())
|
||||||
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
||||||
.WithTypeConverter(new ResourceQuantityYamlConverter())
|
.WithTypeConverter(new ResourceQuantityYamlConverter())
|
||||||
|
.WithTypeConverter(new KubernetesDateTimeYamlConverter())
|
||||||
|
.WithTypeConverter(new KubernetesDateTimeOffsetYamlConverter())
|
||||||
.WithAttemptingUnquotedStringTypeDeserialization()
|
.WithAttemptingUnquotedStringTypeDeserialization()
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -33,6 +35,8 @@ namespace k8s
|
|||||||
.WithTypeConverter(new IntOrStringYamlConverter())
|
.WithTypeConverter(new IntOrStringYamlConverter())
|
||||||
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
||||||
.WithTypeConverter(new ResourceQuantityYamlConverter())
|
.WithTypeConverter(new ResourceQuantityYamlConverter())
|
||||||
|
.WithTypeConverter(new KubernetesDateTimeYamlConverter())
|
||||||
|
.WithTypeConverter(new KubernetesDateTimeOffsetYamlConverter())
|
||||||
.WithEventEmitter(e => new StringQuotingEmitter(e))
|
.WithEventEmitter(e => new StringQuotingEmitter(e))
|
||||||
.WithEventEmitter(e => new FloatEmitter(e))
|
.WithEventEmitter(e => new FloatEmitter(e))
|
||||||
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
|
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
|
||||||
@@ -56,7 +60,7 @@ namespace k8s
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Encoding.UTF8.GetBytes(scalar.Value);
|
return Convert.FromBase64String(scalar.Value);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -69,8 +73,15 @@ namespace k8s
|
|||||||
|
|
||||||
public void WriteYaml(IEmitter emitter, object value, Type type, ObjectSerializer serializer)
|
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;
|
var obj = (byte[])value;
|
||||||
emitter?.Emit(new Scalar(Encoding.UTF8.GetString(obj)));
|
var encoded = Convert.ToBase64String(obj);
|
||||||
|
emitter.Emit(new Scalar(encoded));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
using static k8s.KubernetesJson;
|
|
||||||
|
|
||||||
namespace k8s;
|
|
||||||
|
|
||||||
[JsonSourceGenerationOptions(
|
|
||||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
|
||||||
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
|
|
||||||
Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter) })
|
|
||||||
]
|
|
||||||
internal partial class SourceGenerationContext : JsonSerializerContext
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netstandard2.0;net48</TargetFrameworks>
|
<TargetFrameworks>netstandard2.0;net48</TargetFrameworks>
|
||||||
@@ -49,6 +49,9 @@
|
|||||||
<Compile Include="..\KubernetesClient\Models\V1PodTemplateSpec.cs" />
|
<Compile Include="..\KubernetesClient\Models\V1PodTemplateSpec.cs" />
|
||||||
<Compile Include="..\KubernetesClient\Models\V1Status.cs" />
|
<Compile Include="..\KubernetesClient\Models\V1Status.cs" />
|
||||||
<Compile Include="..\KubernetesClient\Models\V1Status.ObjectView.cs" />
|
<Compile Include="..\KubernetesClient\Models\V1Status.ObjectView.cs" />
|
||||||
|
<Compile Include="..\KubernetesClient\Models\KubernetesDateTimeOffsetYamlConverter.cs" />
|
||||||
|
<Compile Include="..\KubernetesClient\Models\KubernetesDateTimeYamlConverter.cs" />
|
||||||
|
|
||||||
|
|
||||||
<Compile Include="..\KubernetesClient\KubeConfigModels\ClusterEndpoint.cs" />
|
<Compile Include="..\KubernetesClient\KubeConfigModels\ClusterEndpoint.cs" />
|
||||||
<Compile Include="..\KubernetesClient\KubeConfigModels\Context.cs" />
|
<Compile Include="..\KubernetesClient\KubeConfigModels\Context.cs" />
|
||||||
|
|||||||
@@ -3,13 +3,17 @@ using System.Text.Json.Nodes;
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
|
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
using System.Text.Json.Serialization.Metadata;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace k8s
|
namespace k8s
|
||||||
{
|
{
|
||||||
public static class KubernetesJson
|
public static class KubernetesJson
|
||||||
{
|
{
|
||||||
private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions();
|
internal static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions();
|
||||||
|
|
||||||
private sealed class Iso8601TimeSpanConverter : JsonConverter<TimeSpan>
|
public sealed class Iso8601TimeSpanConverter : JsonConverter<TimeSpan>
|
||||||
{
|
{
|
||||||
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
@@ -24,11 +28,11 @@ namespace k8s
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class KubernetesDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
|
public sealed class KubernetesDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
|
||||||
{
|
{
|
||||||
private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffK";
|
private const string RFC3339MicroFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffZ";
|
||||||
private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffK";
|
private const string RFC3339NanoFormat = "yyyy-MM-dd'T'HH':'mm':'ss.fffffffZ";
|
||||||
private const string RFC3339Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK";
|
private const string RFC3339Format = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ";
|
||||||
|
|
||||||
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
@@ -50,13 +54,22 @@ namespace k8s
|
|||||||
throw new FormatException($"Unable to parse {originalstr} as RFC3339 RFC3339Micro or RFC3339Nano");
|
throw new FormatException($"Unable to parse {originalstr} as RFC3339 RFC3339Micro or RFC3339Nano");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
|
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
|
||||||
{
|
{
|
||||||
writer.WriteStringValue(value.ToString(RFC3339MicroFormat));
|
// Output as RFC3339Micro
|
||||||
|
var date = value.ToUniversalTime();
|
||||||
|
|
||||||
|
var basePart = date.ToString("yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture);
|
||||||
|
var frac = date.ToString(".ffffff", CultureInfo.InvariantCulture)
|
||||||
|
.TrimEnd('0')
|
||||||
|
.TrimEnd('.');
|
||||||
|
|
||||||
|
writer.WriteStringValue(basePart + frac + "Z");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed class KubernetesDateTimeConverter : JsonConverter<DateTime>
|
public sealed class KubernetesDateTimeConverter : JsonConverter<DateTime>
|
||||||
{
|
{
|
||||||
private static readonly JsonConverter<DateTimeOffset> UtcConverter = new KubernetesDateTimeOffsetConverter();
|
private static readonly JsonConverter<DateTimeOffset> UtcConverter = new KubernetesDateTimeOffsetConverter();
|
||||||
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
@@ -72,13 +85,22 @@ namespace k8s
|
|||||||
|
|
||||||
static KubernetesJson()
|
static KubernetesJson()
|
||||||
{
|
{
|
||||||
|
#if K8S_AOT
|
||||||
|
// 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.Converters.Add(new JsonStringEnumConverter());
|
||||||
|
#endif
|
||||||
JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
||||||
JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
||||||
JsonSerializerOptions.Converters.Add(new Iso8601TimeSpanConverter());
|
JsonSerializerOptions.Converters.Add(new Iso8601TimeSpanConverter());
|
||||||
JsonSerializerOptions.Converters.Add(new KubernetesDateTimeConverter());
|
JsonSerializerOptions.Converters.Add(new KubernetesDateTimeConverter());
|
||||||
JsonSerializerOptions.Converters.Add(new KubernetesDateTimeOffsetConverter());
|
JsonSerializerOptions.Converters.Add(new KubernetesDateTimeOffsetConverter());
|
||||||
JsonSerializerOptions.Converters.Add(new V1Status.V1StatusObjectViewConverter());
|
JsonSerializerOptions.Converters.Add(new V1Status.V1StatusObjectViewConverter());
|
||||||
JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -99,47 +121,92 @@ namespace k8s
|
|||||||
|
|
||||||
public static TValue Deserialize<TValue>(string json, JsonSerializerOptions jsonSerializerOptions = null)
|
public static TValue Deserialize<TValue>(string json, JsonSerializerOptions jsonSerializerOptions = null)
|
||||||
{
|
{
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
var info = (JsonTypeInfo<TValue>)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue));
|
||||||
|
return JsonSerializer.Deserialize(json, info);
|
||||||
|
#else
|
||||||
return JsonSerializer.Deserialize<TValue>(json, jsonSerializerOptions ?? JsonSerializerOptions);
|
return JsonSerializer.Deserialize<TValue>(json, jsonSerializerOptions ?? JsonSerializerOptions);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TValue Deserialize<TValue>(Stream json, JsonSerializerOptions jsonSerializerOptions = null)
|
public static TValue Deserialize<TValue>(Stream json, JsonSerializerOptions jsonSerializerOptions = null)
|
||||||
{
|
{
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
var info = (JsonTypeInfo<TValue>)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue));
|
||||||
|
return JsonSerializer.Deserialize(json, info);
|
||||||
|
#else
|
||||||
return JsonSerializer.Deserialize<TValue>(json, jsonSerializerOptions ?? JsonSerializerOptions);
|
return JsonSerializer.Deserialize<TValue>(json, jsonSerializerOptions ?? JsonSerializerOptions);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TValue Deserialize<TValue>(JsonDocument json, JsonSerializerOptions jsonSerializerOptions = null)
|
public static TValue Deserialize<TValue>(JsonDocument json, JsonSerializerOptions jsonSerializerOptions = null)
|
||||||
{
|
{
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
var info = (JsonTypeInfo<TValue>)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue));
|
||||||
|
return JsonSerializer.Deserialize(json, info);
|
||||||
|
#else
|
||||||
return JsonSerializer.Deserialize<TValue>(json, jsonSerializerOptions ?? JsonSerializerOptions);
|
return JsonSerializer.Deserialize<TValue>(json, jsonSerializerOptions ?? JsonSerializerOptions);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TValue Deserialize<TValue>(JsonElement json, JsonSerializerOptions jsonSerializerOptions = null)
|
public static TValue Deserialize<TValue>(JsonElement json, JsonSerializerOptions jsonSerializerOptions = null)
|
||||||
{
|
{
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
var info = (JsonTypeInfo<TValue>)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue));
|
||||||
|
return JsonSerializer.Deserialize(json, info);
|
||||||
|
#else
|
||||||
return JsonSerializer.Deserialize<TValue>(json, jsonSerializerOptions ?? JsonSerializerOptions);
|
return JsonSerializer.Deserialize<TValue>(json, jsonSerializerOptions ?? JsonSerializerOptions);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TValue Deserialize<TValue>(JsonNode json, JsonSerializerOptions jsonSerializerOptions = null)
|
public static TValue Deserialize<TValue>(JsonNode json, JsonSerializerOptions jsonSerializerOptions = null)
|
||||||
{
|
{
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
var info = (JsonTypeInfo<TValue>)(jsonSerializerOptions ?? JsonSerializerOptions).GetTypeInfo(typeof(TValue));
|
||||||
|
return JsonSerializer.Deserialize(json, info);
|
||||||
|
#else
|
||||||
return JsonSerializer.Deserialize<TValue>(json, jsonSerializerOptions ?? JsonSerializerOptions);
|
return JsonSerializer.Deserialize<TValue>(json, jsonSerializerOptions ?? JsonSerializerOptions);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Serialize(object value, JsonSerializerOptions jsonSerializerOptions = null)
|
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);
|
return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Serialize(JsonDocument value, JsonSerializerOptions jsonSerializerOptions = null)
|
public static string Serialize(JsonDocument 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);
|
return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Serialize(JsonElement value, JsonSerializerOptions jsonSerializerOptions = null)
|
public static string Serialize(JsonElement 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);
|
return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Serialize(JsonNode value, JsonSerializerOptions jsonSerializerOptions = null)
|
public static string Serialize(JsonNode 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);
|
return JsonSerializer.Serialize(value, jsonSerializerOptions ?? JsonSerializerOptions);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ namespace k8s
|
|||||||
.WithTypeConverter(new IntOrStringYamlConverter())
|
.WithTypeConverter(new IntOrStringYamlConverter())
|
||||||
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
||||||
.WithTypeConverter(new ResourceQuantityYamlConverter())
|
.WithTypeConverter(new ResourceQuantityYamlConverter())
|
||||||
|
.WithTypeConverter(new KubernetesDateTimeYamlConverter())
|
||||||
|
.WithTypeConverter(new KubernetesDateTimeOffsetYamlConverter())
|
||||||
.WithAttemptingUnquotedStringTypeDeserialization()
|
.WithAttemptingUnquotedStringTypeDeserialization()
|
||||||
.WithOverridesFromJsonPropertyAttributes();
|
.WithOverridesFromJsonPropertyAttributes();
|
||||||
|
|
||||||
@@ -41,6 +43,8 @@ namespace k8s
|
|||||||
.WithTypeConverter(new IntOrStringYamlConverter())
|
.WithTypeConverter(new IntOrStringYamlConverter())
|
||||||
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
||||||
.WithTypeConverter(new ResourceQuantityYamlConverter())
|
.WithTypeConverter(new ResourceQuantityYamlConverter())
|
||||||
|
.WithTypeConverter(new KubernetesDateTimeYamlConverter())
|
||||||
|
.WithTypeConverter(new KubernetesDateTimeOffsetYamlConverter())
|
||||||
.WithEventEmitter(e => new StringQuotingEmitter(e))
|
.WithEventEmitter(e => new StringQuotingEmitter(e))
|
||||||
.WithEventEmitter(e => new FloatEmitter(e))
|
.WithEventEmitter(e => new FloatEmitter(e))
|
||||||
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
|
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using YamlDotNet.Core;
|
||||||
|
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'";
|
||||||
|
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 RFC3339Micro
|
||||||
|
var date = ((DateTimeOffset)value).ToUniversalTime();
|
||||||
|
|
||||||
|
var basePart = date.ToString("yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture);
|
||||||
|
var frac = date.ToString(".ffffff", CultureInfo.InvariantCulture)
|
||||||
|
.TrimEnd('0')
|
||||||
|
.TrimEnd('.');
|
||||||
|
|
||||||
|
emitter.Emit(new Scalar(AnchorName.Empty, TagName.Empty, basePart + frac + "Z", ScalarStyle.DoubleQuoted, true, false));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
using YamlDotNet.Core;
|
||||||
|
using YamlDotNet.Serialization;
|
||||||
|
|
||||||
|
namespace k8s.Models;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,22 +2,27 @@ namespace k8s.Models
|
|||||||
{
|
{
|
||||||
public partial record V1Status
|
public partial record V1Status
|
||||||
{
|
{
|
||||||
internal sealed class V1StatusObjectViewConverter : JsonConverter<V1Status>
|
public sealed class V1StatusObjectViewConverter : JsonConverter<V1Status>
|
||||||
{
|
{
|
||||||
public override V1Status Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
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
|
try
|
||||||
{
|
{
|
||||||
return obj.Deserialize<V1Status>();
|
#if NET8_0_OR_GREATER
|
||||||
|
return JsonSerializer.Deserialize(ele, StatusSourceGenerationContext.Default.V1Status);
|
||||||
|
#else
|
||||||
|
return ele.Deserialize<V1Status>();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
catch (JsonException)
|
catch (JsonException)
|
||||||
{
|
{
|
||||||
// should be an object
|
// 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)
|
public override void Write(Utf8JsonWriter writer, V1Status value, JsonSerializerOptions options)
|
||||||
@@ -32,7 +37,11 @@ namespace k8s.Models
|
|||||||
|
|
||||||
public T ObjectView<T>()
|
public T ObjectView<T>()
|
||||||
{
|
{
|
||||||
|
#if NET8_0_OR_GREATER
|
||||||
|
return KubernetesJson.Deserialize<T>(_original);
|
||||||
|
#else
|
||||||
return _original.Deserialize<T>();
|
return _original.Deserialize<T>();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
28
src/KubernetesClient/SourceGenerationContext.cs
Normal file
28
src/KubernetesClient/SourceGenerationContext.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using static k8s.KubernetesJson;
|
||||||
|
using static k8s.Models.V1Status;
|
||||||
|
|
||||||
|
namespace k8s;
|
||||||
|
|
||||||
|
[JsonSourceGenerationOptions(
|
||||||
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||||
|
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
|
||||||
|
UseStringEnumConverter = true,
|
||||||
|
Converters = new[] { typeof(Iso8601TimeSpanConverter), typeof(KubernetesDateTimeConverter), typeof(KubernetesDateTimeOffsetConverter), typeof(V1StatusObjectViewConverter) })
|
||||||
|
]
|
||||||
|
public partial class SourceGenerationContext : JsonSerializerContext
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used by V1Status in order to avoid the recursive loop as SourceGenerationContext contains V1StatusObjectViewConverter
|
||||||
|
/// </summary>
|
||||||
|
[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
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -8,8 +8,8 @@ namespace k8s
|
|||||||
{{ for definition in definitions }}
|
{{ for definition in definitions }}
|
||||||
[JsonSerializable(typeof({{ GetClassName definition }}))]
|
[JsonSerializable(typeof({{ GetClassName definition }}))]
|
||||||
{{ end }}
|
{{ end }}
|
||||||
internal partial class SourceGenerationContext : JsonSerializerContext
|
public partial class SourceGenerationContext : JsonSerializerContext
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using k8s.Models;
|
||||||
|
|
||||||
namespace k8s.Tests;
|
namespace k8s.Tests;
|
||||||
|
|
||||||
@@ -116,4 +118,28 @@ public class KubernetesJsonTests
|
|||||||
#endif
|
#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<V1Secret>(kManifest);
|
||||||
|
var jsonFromObj = KubernetesJson.Serialize(objFromJson);
|
||||||
|
|
||||||
|
// Format Json
|
||||||
|
var jsonFromObj2 = JsonSerializer.Serialize(JsonSerializer.Deserialize<JsonElement>(jsonFromObj), new JsonSerializerOptions() { WriteIndented = true });
|
||||||
|
|
||||||
|
Assert.Equal(kManifest, jsonFromObj2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1170,5 +1170,148 @@ spec:
|
|||||||
CultureInfo.CurrentCulture = old;
|
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<V1Secret>(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<RfcTime>(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
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user