Search in sources :

Example 1 with JsonDeserializationContext

use of com.google.gson.JsonDeserializationContext in project ninja by ninjaframework.

the class ApiControllerDocTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 2 with JsonDeserializationContext

use of com.google.gson.JsonDeserializationContext in project ninja by ninjaframework.

the class ApiControllerDocTesterTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 3 with JsonDeserializationContext

use of com.google.gson.JsonDeserializationContext in project ninja by ninjaframework.

the class ApiControllerTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 4 with JsonDeserializationContext

use of com.google.gson.JsonDeserializationContext in project intellij-plugins by StepicOrg.

the class ReplyDeserializer method deserialize.

@Override
public Reply deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (json == null || !(json instanceof JsonObject)) {
        return null;
    }
    Reply reply = new Reply();
    JsonObject object = json.getAsJsonObject();
    String language = getString(object, "language");
    reply.setLanguage(language);
    String code = getString(object, "code");
    reply.setCode(code);
    String formula = getString(object, "formula");
    reply.setFormula(formula);
    String text = getString(object, "text");
    reply.setText(text);
    String number = getString(object, "number");
    reply.setNumber(number);
    JsonArray ordering = getJsonArray(object, "ordering");
    if (ordering != null) {
        List<Integer> intOrdering = new ArrayList<>();
        ordering.forEach(item -> intOrdering.add(item.getAsInt()));
        reply.setOrdering(intOrdering);
    }
    reply.setAttachments(getList(object, "attachments", (jsonElement) -> context.deserialize(jsonElement, Attachment.class)));
    reply.setFiles(getStringList(object, "files"));
    JsonArray choices = getJsonArray(object, "choices");
    if (choices != null && choices.size() > 0) {
        if (choices.get(0).isJsonPrimitive()) {
            List<Boolean> list = new ArrayList<>();
            choices.forEach(item -> list.add(item.getAsBoolean()));
            reply.setChoices(list);
        } else {
            List<Choice> list = new ArrayList<>();
            choices.forEach(item -> list.add(context.deserialize(item, Choice.class)));
            reply.setChoices(list);
        }
    }
    reply.setBlanks(getStringList(object, "blanks"));
    return reply;
}
Also used : Utils.getJsonArray(org.stepik.api.Utils.getJsonArray) JsonArray(com.google.gson.JsonArray) JsonParseException(com.google.gson.JsonParseException) JsonObject(com.google.gson.JsonObject) Utils.getList(org.stepik.api.Utils.getList) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) ArrayList(java.util.ArrayList) Utils.getJsonArray(org.stepik.api.Utils.getJsonArray) JsonElement(com.google.gson.JsonElement) JsonArray(com.google.gson.JsonArray) List(java.util.List) Type(java.lang.reflect.Type) Utils.getString(org.stepik.api.Utils.getString) Utils.getStringList(org.stepik.api.Utils.getStringList) JsonDeserializer(com.google.gson.JsonDeserializer) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Utils.getString(org.stepik.api.Utils.getString)

Example 5 with JsonDeserializationContext

use of com.google.gson.JsonDeserializationContext in project AngularBeans by bessemHmidi.

the class ByteArrayJsonAdapter method initJsonSerialiser.

public void initJsonSerialiser() {
    GsonBuilder builder = new GsonBuilder().serializeNulls();
    builder.setExclusionStrategies(NGConfiguration.getGsonExclusionStrategy());
    builder.registerTypeAdapter(LobWrapper.class, new LobWrapperJsonAdapter(cache));
    builder.registerTypeAdapter(byte[].class, new ByteArrayJsonAdapter(cache, contextPath));
    builder.registerTypeAdapter(LobWrapper.class, new JsonDeserializer<LobWrapper>() {

        @Override
        public LobWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
            return null;
        }
    });
    if (NGConfiguration.getProperty("DATE_PATTERN") != null) {
        final SimpleDateFormat dateFormat = new SimpleDateFormat(NGConfiguration.getProperty("DATE_PATTERN"));
        if (dateFormat != null && NGConfiguration.getProperty("TIME_ZONE") != null) {
            dateFormat.setTimeZone(TimeZone.getTimeZone(NGConfiguration.getProperty("TIME_ZONE")));
        }
        builder.registerTypeAdapter(java.sql.Date.class, new JsonSerializer<java.sql.Date>() {

            @Override
            public JsonElement serialize(java.sql.Date src, Type typeOfSrc, JsonSerializationContext context) {
                if (src != null) {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(src);
                    cal.set(Calendar.HOUR_OF_DAY, 0);
                    cal.set(Calendar.MINUTE, 0);
                    cal.set(Calendar.SECOND, 0);
                    cal.set(Calendar.MILLISECOND, 0);
                    return new JsonPrimitive(dateFormat.format(cal.getTime()));
                }
                return null;
            }
        });
        builder.registerTypeAdapter(java.sql.Date.class, new JsonDeserializer<java.sql.Date>() {

            @Override
            public java.sql.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
                try {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(dateFormat.parse(json.getAsString()));
                    cal.set(Calendar.HOUR_OF_DAY, 0);
                    cal.set(Calendar.MINUTE, 0);
                    cal.set(Calendar.SECOND, 0);
                    cal.set(Calendar.MILLISECOND, 0);
                    return new java.sql.Date(cal.getTime().getTime());
                } catch (Exception e) {
                }
                return null;
            }
        });
        builder.registerTypeAdapter(java.sql.Time.class, new JsonSerializer<java.sql.Time>() {

            @Override
            public JsonElement serialize(java.sql.Time src, Type typeOfSrc, JsonSerializationContext context) {
                if (src != null) {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(src);
                    return new JsonPrimitive(dateFormat.format(cal.getTime()));
                }
                return null;
            }
        });
        builder.registerTypeAdapter(java.sql.Time.class, new JsonDeserializer<java.sql.Time>() {

            @Override
            public java.sql.Time deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
                try {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(dateFormat.parse(json.getAsString()));
                    return new java.sql.Time(cal.getTime().getTime());
                } catch (Exception e) {
                }
                return null;
            }
        });
        builder.registerTypeAdapter(java.sql.Timestamp.class, new JsonSerializer<java.sql.Timestamp>() {

            @Override
            public JsonElement serialize(java.sql.Timestamp src, Type typeOfSrc, JsonSerializationContext context) {
                if (src != null) {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(src);
                    return new JsonPrimitive(dateFormat.format(cal.getTime()));
                }
                return null;
            }
        });
        builder.registerTypeAdapter(java.sql.Timestamp.class, new JsonDeserializer<java.sql.Timestamp>() {

            @Override
            public java.sql.Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
                try {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(dateFormat.parse(json.getAsString()));
                    return new java.sql.Timestamp(cal.getTime().getTime());
                } catch (Exception e) {
                }
                return null;
            }
        });
        builder.registerTypeAdapter(java.util.Date.class, new JsonSerializer<java.util.Date>() {

            @Override
            public JsonElement serialize(java.util.Date src, Type typeOfSrc, JsonSerializationContext context) {
                return src == null ? null : new JsonPrimitive(dateFormat.format(src));
            }
        });
        builder.registerTypeAdapter(java.util.Date.class, new JsonDeserializer<java.util.Date>() {

            @Override
            public java.util.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
                try {
                    return dateFormat.parse(json.getAsString());
                } catch (Exception e) {
                }
                return null;
            }
        });
    }
    mainSerializer = builder.create();
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) LobWrapper(angularBeans.io.LobWrapper) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) Calendar(java.util.Calendar) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) JsonSerializationContext(com.google.gson.JsonSerializationContext) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

JsonDeserializationContext (com.google.gson.JsonDeserializationContext)5 JsonElement (com.google.gson.JsonElement)5 Type (java.lang.reflect.Type)5 GsonBuilder (com.google.gson.GsonBuilder)4 JsonParseException (com.google.gson.JsonParseException)4 Gson (com.google.gson.Gson)3 Date (java.util.Date)3 LobWrapper (angularBeans.io.LobWrapper)1 JsonArray (com.google.gson.JsonArray)1 JsonDeserializer (com.google.gson.JsonDeserializer)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 JsonSerializationContext (com.google.gson.JsonSerializationContext)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 List (java.util.List)1 Utils.getJsonArray (org.stepik.api.Utils.getJsonArray)1 Utils.getList (org.stepik.api.Utils.getList)1 Utils.getString (org.stepik.api.Utils.getString)1