com.bandwidth.sdk.JSON Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bandwidth-sdk Show documentation
Show all versions of bandwidth-sdk Show documentation
The official client SDK for Bandwidth's Voice, Messaging, MFA, and WebRTC APIs
The newest version!
/*
* Bandwidth
* Bandwidth's Communication APIs
*
* The version of the OpenAPI document: 1.0.0
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package com.bandwidth.sdk;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
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.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.TimeZone;
/*
* 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();
private static final StdDateFormat sdf = new StdDateFormat()
.withTimeZone(TimeZone.getTimeZone(ZoneId.systemDefault()))
.withColonInTimeZone(true);
private static final DateTimeFormatter dtf = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
@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;
}
static {
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);
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.AccountStatistics.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.AnswerCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.BridgeCompleteCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.BridgeTargetCompleteCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.CallRecordingMetadata.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.CallState.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.CallTranscription.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.CallTranscriptionMetadata.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.CallTranscriptionResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.CodeRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.Conference.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.ConferenceCompletedCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.ConferenceCreatedCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.ConferenceMember.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.ConferenceMemberExitCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.ConferenceMemberJoinCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.ConferenceRecordingAvailableCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.ConferenceRecordingMetadata.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.ConferenceRedirectCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.CreateCall.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.CreateCallResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.CreateLookupResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.CreateMessageRequestError.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.DeferredResult.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.DisconnectCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.Diversion.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.DtmfCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.FieldError.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.GatherCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.InboundMessageCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.InboundMessageCallbackMessage.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.InitiateCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.ListMessageItem.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.LookupRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.LookupResult.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.LookupStatus.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MachineDetectionCompleteCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MachineDetectionConfiguration.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MachineDetectionResult.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.Media.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.Message.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MessageDeliveredCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MessageDeliveredCallbackMessage.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MessageFailedCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MessageFailedCallbackMessage.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MessageRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MessageSendingCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MessageSendingCallbackMessage.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MessagesList.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MessagingCodeResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MessagingRequestError.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MfaForbiddenRequestError.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MfaRequestError.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.MfaUnauthorizedRequestError.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.PageInfo.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.RecordingAvailableCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.RecordingCompleteCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.RecordingTranscriptionMetadata.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.RecordingTranscriptions.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.RedirectCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.StirShaken.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.Tag.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.TnLookupRequestError.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.TranscribeRecording.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.Transcription.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.TranscriptionAvailableCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.TransferAnswerCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.TransferCompleteCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.TransferDisconnectCallback.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.UpdateCall.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.UpdateCallRecording.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.UpdateConference.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.UpdateConferenceMember.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.VerifyCodeRequest.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.VerifyCodeResponse.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.VoiceApiError.CustomTypeAdapterFactory());
gsonBuilder.registerTypeAdapterFactory(new com.bandwidth.sdk.model.VoiceCodeResponse.CustomTypeAdapterFactory());
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(sdf.parse(date).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
}
}
/**
* Gson TypeAdapter for java.util.Date type
* If the dateFormat is null, DateTimeFormatter 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 = date.toInstant().atOffset(ZoneOffset.UTC).format(dtf);
}
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 sdf.parse(date);
} 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);
}
}