Skip to content

Fix up byte array handling. #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 73 additions & 41 deletions kubernetes/src/main/java/io/kubernetes/client/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* Do not edit the class manually.
*/


package io.kubernetes.client;

import com.google.gson.Gson;
Expand All @@ -20,6 +19,9 @@
import com.google.gson.internal.bind.util.ISO8601Utils;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import okio.ByteString;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormatter;
Expand All @@ -41,13 +43,15 @@ public class JSON {
private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
private DateTimeTypeAdapter dateTimeTypeAdapter = new DateTimeTypeAdapter();
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
private ByteArrayAdapter byteArrayTypeAdapter = new ByteArrayAdapter();

public JSON() {
gson = new GsonBuilder()
.registerTypeAdapter(Date.class, dateTypeAdapter)
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
.registerTypeAdapter(DateTime.class, dateTimeTypeAdapter)
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
.registerTypeAdapter(byte[].class, byteArrayTypeAdapter)
.create();
}

Expand Down Expand Up @@ -110,7 +114,36 @@ public <T> T deserialize(String body, Type returnType) {
// return the response body string directly for the String return type;
if (returnType.equals(String.class))
return (T) body;
else throw (e);
else
throw (e);
}
}

/**
+ * Gson TypeAdapter for Byte Array type
+ */
public class ByteArrayAdapter extends TypeAdapter<byte[]> {

@Override
public void write(JsonWriter out, byte[] value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(ByteString.of(value).base64());
}
}

@Override
public byte[] read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String bytesAsBase64 = in.nextString();
ByteString byteString = ByteString.decodeBase64(bytesAsBase64);
return byteString.toByteArray();
}
}
}

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

public DateTimeTypeAdapter() {
this(new DateTimeFormatterBuilder()
.append(ISODateTimeFormat.dateTime().getPrinter(), ISODateTimeFormat.dateOptionalTimeParser().getParser())
.toFormatter());
this(new DateTimeFormatterBuilder().append(ISODateTimeFormat.dateTime().getPrinter(),
ISODateTimeFormat.dateOptionalTimeParser().getParser()).toFormatter());
}

public DateTimeTypeAdapter(DateTimeFormatter formatter) {
Expand All @@ -147,12 +179,12 @@ public void write(JsonWriter out, DateTime date) throws IOException {
@Override
public DateTime read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
return formatter.parseDateTime(date);
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
return formatter.parseDateTime(date);
}
}
}
Expand Down Expand Up @@ -188,12 +220,12 @@ public void write(JsonWriter out, LocalDate date) throws IOException {
@Override
public LocalDate read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
return formatter.parseLocalDate(date);
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
return formatter.parseLocalDate(date);
}
}
}
Expand Down Expand Up @@ -246,19 +278,19 @@ public void write(JsonWriter out, java.sql.Date date) throws IOException {
@Override
public java.sql.Date read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
try {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
try {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
}
}
Expand Down Expand Up @@ -301,19 +333,19 @@ public void write(JsonWriter out, Date date) throws IOException {
public Date read(JsonReader in) throws IOException {
try {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
try {
if (dateFormat != null) {
return dateFormat.parse(date);
}
return ISO8601Utils.parse(date, new ParsePosition(0));
} catch (ParseException e) {
throw new JsonParseException(e);
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
try {
if (dateFormat != null) {
return dateFormat.parse(date);
}
return ISO8601Utils.parse(date, new ParsePosition(0));
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
} catch (IllegalArgumentException e) {
throw new JsonParseException(e);
Expand Down