Skip to content

Commit 21c7553

Browse files
Merge pull request #219 from brendandburns/byte-array
Fix up byte array handling.
2 parents 83a1031 + 7452c4a commit 21c7553

File tree

1 file changed

+73
-41
lines changed
  • kubernetes/src/main/java/io/kubernetes/client

1 file changed

+73
-41
lines changed

kubernetes/src/main/java/io/kubernetes/client/JSON.java

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* Do not edit the class manually.
1111
*/
1212

13-
1413
package io.kubernetes.client;
1514

1615
import com.google.gson.Gson;
@@ -20,6 +19,9 @@
2019
import com.google.gson.internal.bind.util.ISO8601Utils;
2120
import com.google.gson.stream.JsonReader;
2221
import com.google.gson.stream.JsonWriter;
22+
23+
import okio.ByteString;
24+
2325
import org.joda.time.DateTime;
2426
import org.joda.time.LocalDate;
2527
import org.joda.time.format.DateTimeFormatter;
@@ -41,13 +43,15 @@ public class JSON {
4143
private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
4244
private DateTimeTypeAdapter dateTimeTypeAdapter = new DateTimeTypeAdapter();
4345
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
46+
private ByteArrayAdapter byteArrayTypeAdapter = new ByteArrayAdapter();
4447

4548
public JSON() {
4649
gson = new GsonBuilder()
4750
.registerTypeAdapter(Date.class, dateTypeAdapter)
4851
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
4952
.registerTypeAdapter(DateTime.class, dateTimeTypeAdapter)
5053
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
54+
.registerTypeAdapter(byte[].class, byteArrayTypeAdapter)
5155
.create();
5256
}
5357

@@ -110,7 +114,36 @@ public <T> T deserialize(String body, Type returnType) {
110114
// return the response body string directly for the String return type;
111115
if (returnType.equals(String.class))
112116
return (T) body;
113-
else throw (e);
117+
else
118+
throw (e);
119+
}
120+
}
121+
122+
/**
123+
+ * Gson TypeAdapter for Byte Array type
124+
+ */
125+
public class ByteArrayAdapter extends TypeAdapter<byte[]> {
126+
127+
@Override
128+
public void write(JsonWriter out, byte[] value) throws IOException {
129+
if (value == null) {
130+
out.nullValue();
131+
} else {
132+
out.value(ByteString.of(value).base64());
133+
}
134+
}
135+
136+
@Override
137+
public byte[] read(JsonReader in) throws IOException {
138+
switch (in.peek()) {
139+
case NULL:
140+
in.nextNull();
141+
return null;
142+
default:
143+
String bytesAsBase64 = in.nextString();
144+
ByteString byteString = ByteString.decodeBase64(bytesAsBase64);
145+
return byteString.toByteArray();
146+
}
114147
}
115148
}
116149

@@ -122,9 +155,8 @@ public static class DateTimeTypeAdapter extends TypeAdapter<DateTime> {
122155
private DateTimeFormatter formatter;
123156

124157
public DateTimeTypeAdapter() {
125-
this(new DateTimeFormatterBuilder()
126-
.append(ISODateTimeFormat.dateTime().getPrinter(), ISODateTimeFormat.dateOptionalTimeParser().getParser())
127-
.toFormatter());
158+
this(new DateTimeFormatterBuilder().append(ISODateTimeFormat.dateTime().getPrinter(),
159+
ISODateTimeFormat.dateOptionalTimeParser().getParser()).toFormatter());
128160
}
129161

130162
public DateTimeTypeAdapter(DateTimeFormatter formatter) {
@@ -147,12 +179,12 @@ public void write(JsonWriter out, DateTime date) throws IOException {
147179
@Override
148180
public DateTime read(JsonReader in) throws IOException {
149181
switch (in.peek()) {
150-
case NULL:
151-
in.nextNull();
152-
return null;
153-
default:
154-
String date = in.nextString();
155-
return formatter.parseDateTime(date);
182+
case NULL:
183+
in.nextNull();
184+
return null;
185+
default:
186+
String date = in.nextString();
187+
return formatter.parseDateTime(date);
156188
}
157189
}
158190
}
@@ -188,12 +220,12 @@ public void write(JsonWriter out, LocalDate date) throws IOException {
188220
@Override
189221
public LocalDate read(JsonReader in) throws IOException {
190222
switch (in.peek()) {
191-
case NULL:
192-
in.nextNull();
193-
return null;
194-
default:
195-
String date = in.nextString();
196-
return formatter.parseLocalDate(date);
223+
case NULL:
224+
in.nextNull();
225+
return null;
226+
default:
227+
String date = in.nextString();
228+
return formatter.parseLocalDate(date);
197229
}
198230
}
199231
}
@@ -246,19 +278,19 @@ public void write(JsonWriter out, java.sql.Date date) throws IOException {
246278
@Override
247279
public java.sql.Date read(JsonReader in) throws IOException {
248280
switch (in.peek()) {
249-
case NULL:
250-
in.nextNull();
251-
return null;
252-
default:
253-
String date = in.nextString();
254-
try {
255-
if (dateFormat != null) {
256-
return new java.sql.Date(dateFormat.parse(date).getTime());
257-
}
258-
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
259-
} catch (ParseException e) {
260-
throw new JsonParseException(e);
281+
case NULL:
282+
in.nextNull();
283+
return null;
284+
default:
285+
String date = in.nextString();
286+
try {
287+
if (dateFormat != null) {
288+
return new java.sql.Date(dateFormat.parse(date).getTime());
261289
}
290+
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
291+
} catch (ParseException e) {
292+
throw new JsonParseException(e);
293+
}
262294
}
263295
}
264296
}
@@ -301,19 +333,19 @@ public void write(JsonWriter out, Date date) throws IOException {
301333
public Date read(JsonReader in) throws IOException {
302334
try {
303335
switch (in.peek()) {
304-
case NULL:
305-
in.nextNull();
306-
return null;
307-
default:
308-
String date = in.nextString();
309-
try {
310-
if (dateFormat != null) {
311-
return dateFormat.parse(date);
312-
}
313-
return ISO8601Utils.parse(date, new ParsePosition(0));
314-
} catch (ParseException e) {
315-
throw new JsonParseException(e);
336+
case NULL:
337+
in.nextNull();
338+
return null;
339+
default:
340+
String date = in.nextString();
341+
try {
342+
if (dateFormat != null) {
343+
return dateFormat.parse(date);
316344
}
345+
return ISO8601Utils.parse(date, new ParsePosition(0));
346+
} catch (ParseException e) {
347+
throw new JsonParseException(e);
348+
}
317349
}
318350
} catch (IllegalArgumentException e) {
319351
throw new JsonParseException(e);

0 commit comments

Comments
 (0)