Skip to content

Commit b5458f5

Browse files
author
oweiler
committed
added byte[] handling from kubernetes-client/java#131
1 parent d6b83e2 commit b5458f5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,55 @@
11
package de.predic8.kubernetesclient.client;
22

3+
import com.google.gson.GsonBuilder;
4+
import com.google.gson.JsonDeserializationContext;
5+
import com.google.gson.JsonDeserializer;
6+
import com.google.gson.JsonElement;
7+
import com.google.gson.JsonParseException;
8+
import com.google.gson.JsonPrimitive;
9+
import com.google.gson.JsonSerializationContext;
10+
import com.google.gson.JsonSerializer;
311
import io.kubernetes.client.ApiClient;
12+
import io.kubernetes.client.JSON;
13+
import org.joda.time.DateTime;
14+
import org.joda.time.LocalDate;
15+
16+
import java.lang.reflect.Type;
17+
import java.util.Base64;
18+
import java.util.Date;
419

520
public abstract class NamespacedApiClient extends ApiClient {
621

22+
23+
public NamespacedApiClient() {
24+
setJSON(new ByteArrayHandlingJSON());
25+
}
26+
727
/**
828
* @return "default" (overrideable using the "kubernetes.client.namespace" Spring property) if running outside of the cluster,
929
* the pod's own namespace, if running on the inside.
1030
*/
1131
public abstract String getMyNamespace();
1232

33+
private static class ByteArrayHandlingJSON extends JSON {
34+
public ByteArrayHandlingJSON() {
35+
setGson(new GsonBuilder()
36+
.registerTypeAdapter(Date.class, new DateTypeAdapter())
37+
.registerTypeAdapter(java.sql.Date.class, new SqlDateTypeAdapter())
38+
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
39+
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
40+
.registerTypeAdapter(byte[].class, new ByteArrayBase64StringTypeAdapter())
41+
.create());
42+
43+
}
44+
}
45+
46+
public static class ByteArrayBase64StringTypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
47+
48+
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
49+
return Base64.getDecoder().decode(json.getAsString());
50+
}
51+
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
52+
return new JsonPrimitive(Base64.getEncoder().encodeToString(src));
53+
}
54+
}
1355
}

0 commit comments

Comments
 (0)