[Java] ApiClient: support deserializing from InputStream instead of String to bypass 2GB Java String limit (#21115)

* ApiClient: support deserializing JSON from InputStream instead of String to bypass 2GB Java String limit

* Update test_file_list.yaml
This commit is contained in:
Kevin Lin 2025-04-22 23:59:02 -07:00 committed by GitHub
parent 338f7f2a09
commit b844d8d4cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
34 changed files with 701 additions and 307 deletions

View File

@ -10,7 +10,7 @@
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/ClientTest.java"
sha256: 325fdd5d7e2c97790c0fb44f712ab7b2ba022d7e1a5b0056f47b07f342682b6d
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/JSONTest.java"
sha256: e673d9928c8eb848262d0116fe0d28db832e128671a810a7c966d06d90cb9b63
sha256: 67941355a0a27ed9ff9318b1caa103e78b81b9aff61b594b18be5cd2bb9f6591
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/api/PetApiTest.java"
sha256: 8b1b8f2a2ad00ccb090873a94a5f73e328b98317d2ec715f53bd7a1accb2a023
- filename: "samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/model/PetTest.java"

View File

@ -1166,17 +1166,8 @@ public class ApiClient {
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;
}
@ -1185,17 +1176,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -23,8 +23,11 @@ import org.joda.time.format.ISODateTimeFormat;
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.text.ParsePosition;
@ -198,6 +201,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1006,17 +1006,8 @@ public class ApiClient {
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;
}
@ -1025,17 +1016,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -164,6 +167,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -936,17 +936,8 @@ public class ApiClient {
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;
}
@ -955,17 +946,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -168,6 +171,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -910,17 +910,8 @@ public class ApiClient {
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;
}
@ -929,17 +920,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -160,6 +163,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -910,17 +910,8 @@ public class ApiClient {
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;
}
@ -929,17 +920,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -159,6 +162,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -910,17 +910,8 @@ public class ApiClient {
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;
}
@ -929,17 +920,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -160,6 +163,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1006,17 +1006,8 @@ public class ApiClient {
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;
}
@ -1025,17 +1016,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -158,6 +161,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1006,17 +1006,8 @@ public class ApiClient {
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;
}
@ -1025,17 +1016,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -211,6 +214,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1026,17 +1026,8 @@ public class ApiClient {
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;
}
@ -1045,17 +1036,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -164,6 +167,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1011,17 +1011,8 @@ public class ApiClient {
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;
}
@ -1030,17 +1021,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -239,6 +242,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1006,17 +1006,8 @@ public class ApiClient {
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;
}
@ -1025,17 +1016,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -164,6 +167,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1009,17 +1009,8 @@ public class ApiClient {
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;
}
@ -1028,17 +1019,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -166,6 +169,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1012,17 +1012,8 @@ public class ApiClient {
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;
}
@ -1031,17 +1022,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -239,6 +242,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1006,17 +1006,8 @@ public class ApiClient {
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;
}
@ -1025,17 +1016,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -164,6 +167,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1006,17 +1006,8 @@ public class ApiClient {
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;
}
@ -1025,17 +1016,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -164,6 +167,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -1080,17 +1080,8 @@ public class ApiClient {
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;
}
@ -1099,17 +1090,25 @@ public class ApiClient {
// 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(
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(),
respBody);
response.body().string());
}
} catch (IOException e) {
throw new ApiException(e);
}
}

View File

@ -27,8 +27,11 @@ import io.gsonfire.TypeSelector;
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.text.ParsePosition;
@ -402,6 +405,28 @@ public class JSON {
}
}
/**
* 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
*/

View File

@ -7,7 +7,9 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
@ -274,13 +276,13 @@ public class JSONTest {
assertEquals(t2.getName(), "tag test 1");
assertEquals(t2.getId(), null);
// with all required fields
// with all required fields
String json3 = "{\"id\": 5847, \"name\":\"pet test 1\", \"photoUrls\": [\"https://a.com\", \"https://b.com\"]}";
Pet t3 = gson.fromJson(json3, Pet.class);
assertEquals(t3.getName(), "pet test 1");
assertEquals(t3.getId(), Long.valueOf(5847));
// with all required fields and tags (optional)
// with all required fields and tags (optional)
String json4 = "{\"id\": 5847, \"name\":\"pet test 1\", \"photoUrls\": [\"https://a.com\", \"https://b.com\"],\"tags\":[{\"id\":\"tag 123\"}]}";
Pet t4 = gson.fromJson(json3, Pet.class);
assertEquals(t4.getName(), "pet test 1");
@ -671,4 +673,12 @@ public class JSONTest {
assertTrue(exception.getMessage().contains("java.io.IOException: The JSON string is invalid for"));
}
}
@Test
public void testDeserializeInputStream() throws Exception {
final String str = "\"2016-09-09\"";
final InputStream inputStream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
final LocalDate date = LocalDate.of(2016, 9, 9);
assertEquals(date, json.deserialize(inputStream, LocalDate.class));
}
}