/*
 * Daytona
 * Daytona AI platform API Docs
 *
 * The version of the OpenAPI document: 1.0
 * Contact: support@daytona.com
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */


package io.daytona.api.client;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
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.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;
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;
    }

    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 io.daytona.api.client.model.AccountProvider.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.AdminCreateRunner.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.AdminGetWebhookStatus200Response.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Announcement.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ApiKeyList.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ApiKeyResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.AuditLog.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.BuildInfo.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Command.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CompletionContext.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CompletionItem.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CompletionList.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CompressedScreenshotResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ComputerUseStartResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ComputerUseStatusResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ComputerUseStopResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateApiKey.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateBuildInfo.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateDockerRegistry.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateLinkedAccount.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateOrganization.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateOrganizationInvitation.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateOrganizationQuota.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateOrganizationRole.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateRegion.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateRegionResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateRunner.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateRunnerResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateSandbox.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateSandboxSnapshot.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateSessionRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateSnapshot.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateUser.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateVolume.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.CreateWorkspace.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.DaytonaConfiguration.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.DisplayInfoResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.DockerRegistry.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.DownloadFiles.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ExecuteRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ExecuteResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.FileInfo.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.FileStatus.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ForkSandbox.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.GitAddRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.GitBranchRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.GitCheckoutRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.GitCloneRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.GitCommitInfo.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.GitCommitRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.GitCommitResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.GitDeleteBranchRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.GitRepoRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.GitStatus.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.HealthControllerCheck200Response.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.HealthControllerCheck200ResponseInfoValue.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.HealthControllerCheck503Response.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Job.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.KeyboardHotkeyRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.KeyboardPressRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.KeyboardTypeRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ListBranchResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.LogEntry.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.LspCompletionParams.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.LspDocumentRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.LspLocation.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.LspServerRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.LspSymbol.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Match.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MetricDataPoint.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MetricSeries.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MetricsResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MouseClickRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MouseClickResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MouseDragRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MouseDragResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MouseMoveRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MouseMoveResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MousePosition.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MouseScrollRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.MouseScrollResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.OidcConfig.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Organization.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.OrganizationInvitation.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.OrganizationRole.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.OrganizationSandboxDefaultLimitedNetworkEgress.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.OrganizationSuspension.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.OrganizationUsageOverview.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.OrganizationUser.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.OtelConfig.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PaginatedAuditLogs.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PaginatedJobs.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PaginatedLogs.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PaginatedSandboxes.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PaginatedSnapshots.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PaginatedTraces.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PollJobsResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PortPreviewUrl.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Position.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PosthogConfig.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ProcessErrorsResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ProcessLogsResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ProcessRestartResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ProcessStatusResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ProjectDirResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PtyCreateRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PtyCreateResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PtyListResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PtyResizeRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.PtySessionInfo.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Range.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RateLimitConfig.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RateLimitEntry.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RegenerateApiKeyResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Region.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RegionQuota.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RegionScreenshotResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RegionUsageOverview.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RegistryPushAccessDto.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ReplaceRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ReplaceResult.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ResizeSandbox.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Runner.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RunnerFull.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RunnerHealthMetrics.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RunnerHealthcheck.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RunnerServiceHealth.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.RunnerSnapshotDto.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Sandbox.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SandboxInfo.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SandboxLabels.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SandboxVolume.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ScreenshotResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SearchFilesResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SendWebhookDto.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Session.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SessionExecuteRequest.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SessionExecuteResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SetSnapshotGeneralStatusDto.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SignedPortPreviewUrl.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SnapshotDto.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SnapshotManagerCredentials.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SshAccessDto.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.SshAccessValidationDto.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.StorageAccessDto.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.ToolboxProxyUrl.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.TraceSpan.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.TraceSummary.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateDockerRegistry.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateJobStatus.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateOrganizationDefaultRegion.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateOrganizationInvitation.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateOrganizationMemberAccess.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateOrganizationQuota.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateOrganizationRegionQuota.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateOrganizationRole.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateRegion.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateSandboxNetworkSettings.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UpdateSandboxStateDto.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Url.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.User.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UserHomeDirResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.UserPublicKey.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.VolumeDto.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.WebhookAppPortalAccess.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.WebhookInitializationStatus.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.WindowsResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.WorkDirResponse.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.Workspace.CustomTypeAdapterFactory());
        gsonBuilder.registerTypeAdapterFactory(new io.daytona.api.client.model.WorkspacePortPreviewUrl.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 <T>        Type
     * @param body       The JSON string
     * @param returnType The type to deserialize into
     * @return The deserialized Java object
     */
    @SuppressWarnings("unchecked")
    public static <T> 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);
            }
        }
    }

    /**
    * 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
     */
    public static class ByteArrayAdapter extends TypeAdapter<byte[]> {

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

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

    /**
     * Gson TypeAdapter for JSR310 OffsetDateTime type
     */
    public static class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> {

        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<LocalDate> {

        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<java.sql.Date> {

        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<Date> {

        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);
    }
}
