Skip to content

Commit e86ea83

Browse files
authored
feat: add KubernetesJson wrapper that allows more deserialize options (#907)
Signed-off-by: Christoph Bühler <[email protected]>
1 parent a85e110 commit e86ea83

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

docs/docs/operator/building-blocks/entities.mdx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,43 @@ public class V1DemoEntity : CustomKubernetesEntity<V1DemoEntity.V1DemoEntitySpec
154154
}
155155
}
156156
```
157+
158+
## Serialization Helper: KubernetesJsonSerializer
159+
160+
When working with custom entities, you may need to serialize or deserialize them to and from JSON, especially when interacting with the Kubernetes API or for testing purposes. KubeOps provides a helper class, `KubernetesJsonSerializer`, to make this process straightforward and consistent with Kubernetes conventions.
161+
162+
### Example Usage
163+
164+
#### Get the Kubernetes specific JsonSerializerOptions
165+
166+
```csharp
167+
var options = KubernetesJsonSerializer.SerializerOptions;
168+
```
169+
170+
#### Serialize an Entity
171+
172+
```csharp
173+
var entity = new V1DemoEntity { /* ... initialize ... */ };
174+
string json = KubernetesJsonSerializer.Serialize(entity);
175+
```
176+
177+
#### Deserialize an Entity
178+
179+
```csharp
180+
string json = /* JSON string from Kubernetes */;
181+
var entity = KubernetesJsonSerializer.Deserialize<V1DemoEntity>(json);
182+
```
183+
184+
#### With Custom JsonSerializerOptions
185+
186+
```csharp
187+
var options = new JsonSerializerOptions { WriteIndented = true };
188+
string json = KubernetesJsonSerializer.Serialize(entity, options);
189+
```
190+
191+
### API Overview
192+
193+
- `Serialize(object value, JsonSerializerOptions? options = null)`: Serializes an object to a JSON string.
194+
- `Deserialize<T>(...)`: Deserializes JSON (from string, stream, `JsonDocument`, `JsonElement`, or `JsonNode`) to a strongly-typed object.
195+
196+
This helper ensures your custom entities are always serialized and deserialized in a way that's compatible with Kubernetes expectations.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Nodes;
3+
4+
using k8s;
5+
6+
namespace KubeOps.Operator.Serialization;
7+
8+
/// <summary>
9+
/// This is a wrapper around <see cref="KubernetesJson"/> to allow for
10+
/// more flexible configuration and serialization/deserialization of Kubernetes objects.
11+
/// </summary>
12+
public static class KubernetesJsonSerializer
13+
{
14+
private static readonly Lazy<JsonSerializerOptions> Options = new(() =>
15+
{
16+
JsonSerializerOptions options = null!;
17+
KubernetesJson.AddJsonOptions(c => options = c);
18+
return options;
19+
});
20+
21+
public static JsonSerializerOptions SerializerOptions => Options.Value;
22+
23+
public static TValue Deserialize<TValue>(string json, JsonSerializerOptions? options = null) =>
24+
KubernetesJson.Deserialize<TValue>(json, options ?? Options.Value);
25+
26+
public static TValue Deserialize<TValue>(Stream json, JsonSerializerOptions? options = null) =>
27+
KubernetesJson.Deserialize<TValue>(json, options ?? Options.Value);
28+
29+
public static TValue Deserialize<TValue>(JsonDocument json, JsonSerializerOptions? options = null) =>
30+
json.Deserialize<TValue>(options ?? Options.Value) ??
31+
throw new JsonException("Deserialization returned null.");
32+
33+
public static TValue Deserialize<TValue>(JsonElement json, JsonSerializerOptions? options = null) =>
34+
json.Deserialize<TValue>(options ?? Options.Value) ??
35+
throw new JsonException("Deserialization returned null.");
36+
37+
public static TValue Deserialize<TValue>(JsonNode json, JsonSerializerOptions? options = null) =>
38+
json.Deserialize<TValue>(options ?? Options.Value) ??
39+
throw new JsonException("Deserialization returned null.");
40+
41+
public static string Serialize(object value, JsonSerializerOptions? options = null) =>
42+
KubernetesJson.Serialize(value, options ?? Options.Value);
43+
}

0 commit comments

Comments
 (0)