All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.swagger.client.JSON Maven / Gradle / Ivy

Go to download

The Clever-Java project is a Java wrapper for the Clever.com API for keeping educational applications rostered and up-to-date.

The newest version!
/*
 * Data API
 * Serves the Clever Data API
 *
 * OpenAPI spec version: 2.1.0
 * 
 *
 * NOTE: This class is auto generated by the swagger code generator program.
 * https://github.com/swagger-api/swagger-codegen.git
 * Do not edit the class manually.
 */

package io.swagger.client;

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.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.threeten.bp.LocalDate;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
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 io.gsonfire.GsonFireBuilder;
import io.gsonfire.PostProcessor;
import io.gsonfire.TypeSelector;
import io.swagger.client.model.ContactsCreated;
import io.swagger.client.model.ContactsDeleted;
import io.swagger.client.model.ContactsUpdated;
import io.swagger.client.model.CoursesCreated;
import io.swagger.client.model.CoursesDeleted;
import io.swagger.client.model.CoursesUpdated;
import io.swagger.client.model.DistrictadminsCreated;
import io.swagger.client.model.DistrictadminsDeleted;
import io.swagger.client.model.DistrictadminsUpdated;
import io.swagger.client.model.DistrictsCreated;
import io.swagger.client.model.DistrictsDeleted;
import io.swagger.client.model.DistrictsUpdated;
import io.swagger.client.model.Event;
import io.swagger.client.model.SchooladminsCreated;
import io.swagger.client.model.SchooladminsDeleted;
import io.swagger.client.model.SchooladminsUpdated;
import io.swagger.client.model.SchoolsCreated;
import io.swagger.client.model.SchoolsDeleted;
import io.swagger.client.model.SchoolsUpdated;
import io.swagger.client.model.SectionsCreated;
import io.swagger.client.model.SectionsDeleted;
import io.swagger.client.model.SectionsUpdated;
import io.swagger.client.model.StudentsCreated;
import io.swagger.client.model.StudentsDeleted;
import io.swagger.client.model.StudentsUpdated;
import io.swagger.client.model.TeachersCreated;
import io.swagger.client.model.TeachersDeleted;
import io.swagger.client.model.TeachersUpdated;
import io.swagger.client.model.TermsCreated;
import io.swagger.client.model.TermsDeleted;
import io.swagger.client.model.TermsUpdated;

public class JSON {
    private Gson gson;
    private boolean isLenientOnJson = false;
    private DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
    private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
    private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
    private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();

    public static GsonBuilder createGson() {
        GsonFireBuilder fireBuilder = new GsonFireBuilder()
          .registerTypeSelector(Event.class, new TypeSelector() {
            @Override
            public Class getClassForElement(JsonElement readElement) {
                Map> classByDiscriminatorValue = new HashMap<>();
                    classByDiscriminatorValue.put("contacts.created".toUpperCase(), ContactsCreated.class);
                    classByDiscriminatorValue.put("contacts.deleted".toUpperCase(), ContactsDeleted.class);
                    classByDiscriminatorValue.put("contacts.updated".toUpperCase(), ContactsUpdated.class);
                    classByDiscriminatorValue.put("courses.created".toUpperCase(), CoursesCreated.class);
                    classByDiscriminatorValue.put("courses.deleted".toUpperCase(), CoursesDeleted.class);
                    classByDiscriminatorValue.put("courses.updated".toUpperCase(), CoursesUpdated.class);
                    classByDiscriminatorValue.put("districtadmins.created".toUpperCase(), DistrictadminsCreated.class);
                    classByDiscriminatorValue.put("districtadmins.deleted".toUpperCase(), DistrictadminsDeleted.class);
                    classByDiscriminatorValue.put("districtadmins.updated".toUpperCase(), DistrictadminsUpdated.class);
                    classByDiscriminatorValue.put("districts.created".toUpperCase(), DistrictsCreated.class);
                    classByDiscriminatorValue.put("districts.deleted".toUpperCase(), DistrictsDeleted.class);
                    classByDiscriminatorValue.put("districts.updated".toUpperCase(), DistrictsUpdated.class);
                    classByDiscriminatorValue.put("schooladmins.created".toUpperCase(), SchooladminsCreated.class);
                    classByDiscriminatorValue.put("schooladmins.deleted".toUpperCase(), SchooladminsDeleted.class);
                    classByDiscriminatorValue.put("schooladmins.updated".toUpperCase(), SchooladminsUpdated.class);
                    classByDiscriminatorValue.put("schools.created".toUpperCase(), SchoolsCreated.class);
                    classByDiscriminatorValue.put("schools.deleted".toUpperCase(), SchoolsDeleted.class);
                    classByDiscriminatorValue.put("schools.updated".toUpperCase(), SchoolsUpdated.class);
                    classByDiscriminatorValue.put("sections.created".toUpperCase(), SectionsCreated.class);
                    classByDiscriminatorValue.put("sections.deleted".toUpperCase(), SectionsDeleted.class);
                    classByDiscriminatorValue.put("sections.updated".toUpperCase(), SectionsUpdated.class);
                    classByDiscriminatorValue.put("students.created".toUpperCase(), StudentsCreated.class);
                    classByDiscriminatorValue.put("students.deleted".toUpperCase(), StudentsDeleted.class);
                    classByDiscriminatorValue.put("students.updated".toUpperCase(), StudentsUpdated.class);
                    classByDiscriminatorValue.put("teachers.created".toUpperCase(), TeachersCreated.class);
                    classByDiscriminatorValue.put("teachers.deleted".toUpperCase(), TeachersDeleted.class);
                    classByDiscriminatorValue.put("teachers.updated".toUpperCase(), TeachersUpdated.class);
                    classByDiscriminatorValue.put("terms.created".toUpperCase(), TermsCreated.class);
                    classByDiscriminatorValue.put("terms.deleted".toUpperCase(), TermsDeleted.class);
                    classByDiscriminatorValue.put("terms.updated".toUpperCase(), TermsUpdated.class);
                    classByDiscriminatorValue.put("Event".toUpperCase(), Event.class);
                return getClassByDiscriminator(
                            classByDiscriminatorValue,
                            getDiscriminatorValue(readElement, "type"));
            }
          })
          .registerPostProcessor(Event.class, new PostProcessor() {
              @Override
              public void postDeserialize(Event result, JsonElement src, Gson gson) {

              }

              @Override
              public void postSerialize(JsonElement result, Event src, Gson gson) {
                  Map, String> discriminatorValueByClass = new HashMap<>();
                      discriminatorValueByClass.put(ContactsCreated.class, "contacts.created");
                      discriminatorValueByClass.put(ContactsDeleted.class, "contacts.deleted");
                      discriminatorValueByClass.put(ContactsUpdated.class, "contacts.updated");
                      discriminatorValueByClass.put(CoursesCreated.class, "courses.created");
                      discriminatorValueByClass.put(CoursesDeleted.class, "courses.deleted");
                      discriminatorValueByClass.put(CoursesUpdated.class, "courses.updated");
                      discriminatorValueByClass.put(DistrictadminsCreated.class, "districtadmins.created");
                      discriminatorValueByClass.put(DistrictadminsDeleted.class, "districtadmins.deleted");
                      discriminatorValueByClass.put(DistrictadminsUpdated.class, "districtadmins.updated");
                      discriminatorValueByClass.put(DistrictsCreated.class, "districts.created");
                      discriminatorValueByClass.put(DistrictsDeleted.class, "districts.deleted");
                      discriminatorValueByClass.put(DistrictsUpdated.class, "districts.updated");
                      discriminatorValueByClass.put(SchooladminsCreated.class, "schooladmins.created");
                      discriminatorValueByClass.put(SchooladminsDeleted.class, "schooladmins.deleted");
                      discriminatorValueByClass.put(SchooladminsUpdated.class, "schooladmins.updated");
                      discriminatorValueByClass.put(SchoolsCreated.class, "schools.created");
                      discriminatorValueByClass.put(SchoolsDeleted.class, "schools.deleted");
                      discriminatorValueByClass.put(SchoolsUpdated.class, "schools.updated");
                      discriminatorValueByClass.put(SectionsCreated.class, "sections.created");
                      discriminatorValueByClass.put(SectionsDeleted.class, "sections.deleted");
                      discriminatorValueByClass.put(SectionsUpdated.class, "sections.updated");
                      discriminatorValueByClass.put(StudentsCreated.class, "students.created");
                      discriminatorValueByClass.put(StudentsDeleted.class, "students.deleted");
                      discriminatorValueByClass.put(StudentsUpdated.class, "students.updated");
                      discriminatorValueByClass.put(TeachersCreated.class, "teachers.created");
                      discriminatorValueByClass.put(TeachersDeleted.class, "teachers.deleted");
                      discriminatorValueByClass.put(TeachersUpdated.class, "teachers.updated");
                      discriminatorValueByClass.put(TermsCreated.class, "terms.created");
                      discriminatorValueByClass.put(TermsDeleted.class, "terms.deleted");
                      discriminatorValueByClass.put(TermsUpdated.class, "terms.updated");
                      discriminatorValueByClass.put(Event.class, "Event");
                  if(result instanceof JsonObject)
                  {
                      if(!((JsonObject) result).has("type"))
                      {
                          ((JsonObject) result).addProperty("type", discriminatorValueByClass.get(src.getClass()));
                      }
                  }
              }
          })
        ;
        return fireBuilder.createGsonBuilder();
    }

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

    private static  Class getClassByDiscriminator(Map> classByDiscriminatorValue, String discriminatorValue) {
        Class clazz = classByDiscriminatorValue.get(discriminatorValue.toUpperCase());
        if(null == clazz) {
            throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">");
        }
        return clazz;
    }

    public JSON() {
        gson = createGson()
            .registerTypeAdapter(Date.class, dateTypeAdapter)
            .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
            .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
            .registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
            .create();
    }

    /**
     * Get Gson.
     *
     * @return Gson
     */
    public Gson getGson() {
        return gson;
    }

    /**
     * Set Gson.
     *
     * @param gson Gson
     * @return JSON
     */
    public JSON setGson(Gson gson) {
        this.gson = gson;
        return this;
    }

    public JSON setLenientOnJson(boolean lenientOnJson) {
        isLenientOnJson = lenientOnJson;
        return this;
    }

    /**
     * Serialize the given Java object into JSON string.
     *
     * @param obj Object
     * @return String representation of the JSON
     */
    public 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  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 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 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 JSON setOffsetDateTimeFormat(DateTimeFormatter dateFormat) {
        offsetDateTimeTypeAdapter.setFormat(dateFormat);
        return this;
    }

    public JSON setLocalDateFormat(DateTimeFormatter dateFormat) {
        localDateTypeAdapter.setFormat(dateFormat);
        return this;
    }

    /**
     * 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 JSON setDateFormat(DateFormat dateFormat) {
        dateTypeAdapter.setFormat(dateFormat);
        return this;
    }

    public JSON setSqlDateFormat(DateFormat dateFormat) {
        sqlDateTypeAdapter.setFormat(dateFormat);
        return this;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy