Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,18 @@
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.kubernetes.client.openapi.auth.Authentication;
import io.kubernetes.client.openapi.auth.HttpBasicAuth;
import io.kubernetes.client.openapi.auth.HttpBearerAuth;
import io.kubernetes.client.openapi.auth.ApiKeyAuth;

/**
Expand Down Expand Up @@ -886,17 +883,8 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
return (T) downloadFileFromResponse(response);
}

String respBody;
try {
if (response.body() != null)
respBody = response.body().string();
else
respBody = null;
} catch (IOException e) {
throw new ApiException(e);
}

if (respBody == null || "".equals(respBody)) {
ResponseBody respBody = response.body();
if (respBody == null) {
return null;
}

Expand All @@ -905,17 +893,25 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
// ensuring a default content type
contentType = "application/json";
}
if (isJsonMime(contentType)) {
return JSON.deserialize(respBody, returnType);
} else if (returnType.equals(String.class)) {
// Expecting string, return the raw response body.
return (T) respBody;
} else {
throw new ApiException(
"Content type \"" + contentType + "\" is not supported for type: " + returnType,
response.code(),
response.headers().toMultimap(),
respBody);
try {
if (isJsonMime(contentType)) {
return JSON.deserialize(respBody.byteStream(), returnType);
} else if (returnType.equals(String.class)) {
String respBodyString = respBody.string();
if (respBodyString.isEmpty()) {
return null;
}
// Expecting string, return the raw response body.
return (T) respBodyString;
} else {
throw new ApiException(
"Content type \"" + contentType + "\" is not supported for type: " + returnType,
response.code(),
response.headers().toMultimap(),
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

Expand Down
25 changes: 25 additions & 0 deletions kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
import okio.ByteString;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.time.LocalDate;
Expand Down Expand Up @@ -809,6 +812,28 @@ public static <T> T deserialize(String body, Type returnType) {
}
}

/**
* Deserialize the given JSON InputStream to a Java object.
*
* @param <T> Type
* @param inputStream The JSON InputStream
* @param returnType The type to deserialize into
* @return The deserialized Java object
*/
@SuppressWarnings("unchecked")
public static <T> T deserialize(InputStream inputStream, Type returnType) throws IOException {
try (InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
if (isLenientOnJson) {
// see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
JsonReader jsonReader = new JsonReader(reader);
jsonReader.setLenient(true);
return gson.fromJson(jsonReader, returnType);
} else {
return gson.fromJson(reader, returnType);
}
}
}

/**
* Gson TypeAdapter for Byte Array type
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;

import com.google.gson.Gson;
Expand Down Expand Up @@ -108,4 +111,13 @@ void v1ListMetaTypeValidationDisabled() throws IOException {
JsonReader jsonReader = new JsonReader(new StringReader("{\"foo\":\"bar\"}"));
new V1ListMeta.CustomTypeAdapterFactory().create(gson, TypeToken.get(V1ListMeta.class)).read(jsonReader);
}

@Test
void inputStream() throws IOException {
String timeStr = "\"2018-04-03T11:32:26.123Z\"";
OffsetDateTime dateTime = JSON.deserialize(new ByteArrayInputStream(timeStr.getBytes(StandardCharsets.UTF_8)), OffsetDateTime.class);
String serializedStr = JSON.serialize(dateTime);
String expectedStr = "\"2018-04-03T11:32:26.123000Z\"";
assertThat(serializedStr).isEqualTo(expectedStr);
}
}