com.konfigthis.carbonai.client.JSON Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of carbonai-java-sdk Show documentation
Show all versions of carbonai-java-sdk Show documentation
Connect external data to LLMs, no matter the source.
This library was generated by https://konfigthis.com
/*
* Carbon
* Connect external data to LLMs, no matter the source.
*
* The version of the OpenAPI document: 1.0.0
*
*
* NOTE: This class is auto generated by Konfig (https://konfigthis.com).
* Do not edit the class manually.
*/
package com.konfigthis.carbonai.client;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.bind.util.ISO8601Utils;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.google.gson.JsonElement;
import io.gsonfire.GsonFireBuilder;
import io.gsonfire.TypeSelector;
import okio.ByteString;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
/*
* A JSON utility class
*
* NOTE: in the future, this class may be converted to static, which may break
* backward-compatibility
*/
public class JSON {
private static Gson gson;
private static boolean isLenientOnJson = false;
private static DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
private static SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
private static OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
private static LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
private static ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
@SuppressWarnings("unchecked")
public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
;
GsonBuilder builder = fireBuilder.createGsonBuilder();
return builder;
}
private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) {
JsonElement element = readElement.getAsJsonObject().get(discriminatorField);
if (null == element) {
throw new IllegalArgumentException("missing discriminator field: <" + discriminatorField + ">");
}
return element.getAsString();
}
/**
* Returns the Java class that implements the OpenAPI schema for the specified discriminator value.
*
* @param classByDiscriminatorValue The map of discriminator values to Java classes.
* @param discriminatorValue The value of the OpenAPI discriminator in the input data.
* @return The Java class that implements the OpenAPI schema
*/
private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) {
Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue);
if (null == clazz) {
throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
}
return clazz;
}
{
GsonBuilder gsonBuilder = createGson();
gsonBuilder.registerTypeAdapter(Date.class, dateTypeAdapter);
gsonBuilder.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter);
gsonBuilder.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter);
gsonBuilder.registerTypeAdapter(LocalDate.class, localDateTypeAdapter);
gsonBuilder.registerTypeAdapter(byte[].class, byteArrayAdapter);
/**
* For the "type: number" schema we accept both Double and Integer
* In the case that we pass "1.0" or "1" we serialize the JsonPrimitive
* as the "1" literal. This is useful when the server expects an integer.
*/
gsonBuilder.registerTypeAdapter(Double.class, new JsonSerializer() {
@Override
public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
if (src == src.longValue())
return new JsonPrimitive(src.longValue());
return new JsonPrimitive(src);
}
});
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.AddWebhookProps.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.BodyCreateUploadFileUploadfilePost.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ChunkProperties.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ChunkPropertiesNullable.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ChunksAndEmbeddings.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ChunksAndEmbeddingsUploadInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ColdStorageProps.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ConfluenceAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ConnectDataSourceInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ConnectDataSourceResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.DataSourceConfiguration.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.DeleteFilesQueryInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.DeleteFilesV2QueryInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.DeleteUsersInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.DirectoryItem.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.DocumentResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.DocumentResponseList.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.EmbeddingAndChunk.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.EmbeddingProperties.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.EmbeddingsAndChunksFilters.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.EmbeddingsAndChunksQueryInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.EmbeddingsAndChunksQueryInputV2.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.EmbeddingsAndChunksResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ExternalSourceItem.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.FetchURLsRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.FetchURLsResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.FileStatistics.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.FileStatisticsNullable.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.FileSyncConfig.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.FileSyncConfigNullable.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.FreshDeskConnectRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.FreskdeskAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GenericSuccessResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GetEmbeddingDocumentsBody.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GitbookAuthetication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GitbookConnectRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GitbookSyncRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GithubAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GithubConnectRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GithubFetchReposRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GmailSyncInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GongAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GuruAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.GuruConnectRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.HybridSearchTuningParams.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.HybridSearchTuningParamsNullable.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ListDataSourceItemsRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ListDataSourceItemsResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ListItemsFilters.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ListItemsFiltersNullable.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ListRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ListResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ListUserResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ListUsersFilters.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ListUsersRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ModifyColdStorageParametersQueryInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ModifyUserConfigurationInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.MoveToHotStorageQueryInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.NotionAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OANSCZGF.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OAuthAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OAuthURLRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OrganizationResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OrganizationUserDataSourceAPI.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OrganizationUserDataSourceFilters.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OrganizationUserDataSourceQueryInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OrganizationUserDataSourceResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OrganizationUserFileTagCreate.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OrganizationUserFileTagsRemove.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OrganizationUserFilesToSyncFilters.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OrganizationUserFilesToSyncQueryInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OuthURLResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.OutlookSyncInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.Pagination.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.PresignedURLResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.RSSFeedInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.RawTextInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.RerankParams.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.RerankParamsNullable.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ResyncFileQueryInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.RevokeAccessTokenInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.S3AuthRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.S3Authentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.S3FileSyncInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.S3GetFileInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SalesforceAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SentWebhookPayload.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SentWebhookPayloadObject.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SentWebhookRequestBody.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ServiceNowAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ServiceNowCredentials.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ServiceNowCredentialsNullable.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SharepointAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SingleChunksAndEmbeddingsUploadInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SitemapScrapeRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SlackFilters.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SlackSyncRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SyncDirectoryRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SyncFilesIds.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SyncFilesRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.SyncOptions.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.TokenResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UpdateOrganizationInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UpdateUsersInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UploadFileFromUrlInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UserConfiguration.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UserConfigurationNullable.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UserFile.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UserFilesV2.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UserListResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UserRequestContent.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UserResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UserWebPagesFilters.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.UserWebPagesRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ValidationError.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.Webhook.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.WebhookFilters.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.WebhookNoKey.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.WebhookQueryInput.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.WebhookQueryResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.WebscrapeRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.WhiteLabelingResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.YoutubeTranscriptResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ZendeskAuthentication.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.konfigthis.carbonai.client.model.ZoteroAuthentication.CustomTypeAdapterFactory());
gsonBuilder.disableHtmlEscaping();
gson = gsonBuilder.create();
}
/**
* Get Gson.
*
* @return Gson
*/
public static Gson getGson() {
return gson;
}
/**
* Set Gson.
*
* @param gson Gson
*/
public static void setGson(Gson gson) {
JSON.gson = gson;
}
public static void setLenientOnJson(boolean lenientOnJson) {
isLenientOnJson = lenientOnJson;
}
/**
* Serialize the given Java object into JSON string.
*
* @param obj Object
* @return String representation of the JSON
*/
public static String serialize(Object obj) {
return gson.toJson(obj);
}
/**
* Deserialize the given JSON string to Java object.
*
* @param Type
* @param body The JSON string
* @param returnType The type to deserialize into
* @return The deserialized Java object
*/
@SuppressWarnings("unchecked")
public static T deserialize(String body, Type returnType) {
try {
if (isLenientOnJson) {
JsonReader jsonReader = new JsonReader(new StringReader(body));
// see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
jsonReader.setLenient(true);
return gson.fromJson(jsonReader, returnType);
} else {
return gson.fromJson(body, returnType);
}
} catch (JsonParseException e) {
// Fallback processing when failed to parse JSON form response body:
// return the response body string directly for the String return type;
if (returnType.equals(String.class)) {
return (T) body;
} else {
throw (e);
}
}
}
/**
* Gson TypeAdapter for Byte Array type
*/
public static class ByteArrayAdapter extends TypeAdapter {
@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();
}
}
}
/**
* Gson TypeAdapter for JSR310 OffsetDateTime type
*/
public static class OffsetDateTimeTypeAdapter extends TypeAdapter {
private DateTimeFormatter formatter;
public OffsetDateTimeTypeAdapter() {
this(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}
public OffsetDateTimeTypeAdapter(DateTimeFormatter formatter) {
this.formatter = formatter;
}
public void setFormat(DateTimeFormatter dateFormat) {
this.formatter = dateFormat;
}
@Override
public void write(JsonWriter out, OffsetDateTime date) throws IOException {
if (date == null) {
out.nullValue();
} else {
out.value(formatter.format(date));
}
}
@Override
public OffsetDateTime read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
if (date.endsWith("+0000")) {
date = date.substring(0, date.length()-5) + "Z";
}
return OffsetDateTime.parse(date, formatter);
}
}
}
/**
* Gson TypeAdapter for JSR310 LocalDate type
*/
public static class LocalDateTypeAdapter extends TypeAdapter {
private DateTimeFormatter formatter;
public LocalDateTypeAdapter() {
this(DateTimeFormatter.ISO_LOCAL_DATE);
}
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
this.formatter = formatter;
}
public void setFormat(DateTimeFormatter dateFormat) {
this.formatter = dateFormat;
}
@Override
public void write(JsonWriter out, LocalDate date) throws IOException {
if (date == null) {
out.nullValue();
} else {
out.value(formatter.format(date));
}
}
@Override
public LocalDate read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
return LocalDate.parse(date, formatter);
}
}
}
public static void setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
offsetDateTimeTypeAdapter.setFormat(dateFormat);
}
public static void setLocalDateFormat(DateTimeFormatter dateFormat) {
localDateTypeAdapter.setFormat(dateFormat);
}
/**
* Gson TypeAdapter for java.sql.Date type
* If the dateFormat is null, a simple "yyyy-MM-dd" format will be used
* (more efficient than SimpleDateFormat).
*/
public static class SqlDateTypeAdapter extends TypeAdapter {
private DateFormat dateFormat;
public SqlDateTypeAdapter() {}
public SqlDateTypeAdapter(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}
public void setFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}
@Override
public void write(JsonWriter out, java.sql.Date date) throws IOException {
if (date == null) {
out.nullValue();
} else {
String value;
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = date.toString();
}
out.value(value);
}
}
@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);
}
}
}
}
/**
* Gson TypeAdapter for java.util.Date type
* If the dateFormat is null, ISO8601Utils will be used.
*/
public static class DateTypeAdapter extends TypeAdapter {
private DateFormat dateFormat;
public DateTypeAdapter() {}
public DateTypeAdapter(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}
public void setFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}
@Override
public void write(JsonWriter out, Date date) throws IOException {
if (date == null) {
out.nullValue();
} else {
String value;
if (dateFormat != null) {
value = dateFormat.format(date);
} else {
value = ISO8601Utils.format(date, true);
}
out.value(value);
}
}
@Override
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);
}
}
} catch (IllegalArgumentException e) {
throw new JsonParseException(e);
}
}
}
public static void setDateFormat(DateFormat dateFormat) {
dateTypeAdapter.setFormat(dateFormat);
}
public static void setSqlDateFormat(DateFormat dateFormat) {
sqlDateTypeAdapter.setFormat(dateFormat);
}
}