Example usage for com.google.gson JsonSerializer JsonSerializer

List of usage examples for com.google.gson JsonSerializer JsonSerializer

Introduction

In this page you can find the example usage for com.google.gson JsonSerializer JsonSerializer.

Prototype

JsonSerializer

Source Link

Usage

From source file:org.unitime.timetable.export.rooms.RoomsExportJSON.java

License:Apache License

@Override
protected void print(ExportHelper helper, List<RoomDetailInterface> rooms, ExportContext context)
        throws IOException {
    helper.getSessionContext().checkPermission(Right.RoomsExportJson);

    helper.setup("application/json", reference(), false);

    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override//from ww w . ja  va2s  .c  o m
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).setFieldNamingStrategy(new FieldNamingStrategy() {
        Pattern iPattern = Pattern.compile("i([A-Z])(.*)");

        @Override
        public String translateName(Field f) {
            Matcher matcher = iPattern.matcher(f.getName());
            if (matcher.matches())
                return matcher.group(1).toLowerCase() + matcher.group(2);
            else
                return f.getName();
        }
    }).setPrettyPrinting().create();

    helper.getWriter().write(gson.toJson(rooms));
}

From source file:org.unitime.timetable.gwt.command.server.GwtRpcServlet.java

License:Apache License

@Override
public void init() throws ServletException {
    iSaver = new QueryLogFilter.Saver();
    iSaver.setName("GwtRpcLogSaver");
    iSaver.start();/*from w ww .ja  va 2 s. co  m*/
    iGson = new GsonBuilder()
            .registerTypeAdapter(java.sql.Timestamp.class, new JsonSerializer<java.sql.Timestamp>() {
                @Override
                public JsonElement serialize(java.sql.Timestamp src, Type typeOfSrc,
                        JsonSerializationContext context) {
                    return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
                }
            }).registerTypeAdapter(java.sql.Date.class, new JsonSerializer<java.sql.Date>() {
                @Override
                public JsonElement serialize(java.sql.Date src, Type typeOfSrc,
                        JsonSerializationContext context) {
                    return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd").format(src));
                }
            }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
                @Override
                public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
                    return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
                }
            }).setFieldNamingStrategy(new FieldNamingStrategy() {
                Pattern iPattern = Pattern.compile("i([A-Z])(.*)");

                @Override
                public String translateName(Field f) {
                    Matcher matcher = iPattern.matcher(f.getName());
                    if (matcher.matches())
                        return matcher.group(1).toLowerCase() + matcher.group(2);
                    else
                        return f.getName();
                }
            }).create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.DegreeWorksPlanScraper.java

License:Apache License

protected Gson getGson() {
    return new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//from w  ww  . j ava2s.  c o  m
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    }).setPrettyPrinting().create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.GsonRepresentation.java

License:Apache License

public GsonBuilder getBuilder() {
    if (iBuilder == null) {
        iBuilder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
            @Override/*from   ww w .j av a2 s . c o  m*/
            public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
            }
        }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
            @Override
            public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
            }
        }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
            @Override
            public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
                return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(src));
            }
        }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
            @Override
            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                try {
                    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                            .parse(json.getAsJsonPrimitive().getAsString());
                } catch (ParseException e) {
                    throw new JsonParseException(e.getMessage(), e);
                }
            }
        });
    }
    return iBuilder;
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.PurdueBatchSolverValidator.java

License:Apache License

protected Gson getGson() {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override// ww w .j  a  v  a 2s. c om
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    });
    return builder.create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.PurdueCourseRequestsValidationProvider.java

License:Apache License

protected Gson getGson(OnlineSectioningHelper helper) {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override/*w  w  w .ja v a2  s . c  o m*/
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    });
    if (helper.isDebugEnabled())
        builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.XEStudentEnrollment.java

License:Open Source License

protected Gson getGson(OnlineSectioningHelper helper) {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//  ww  w .  j a v  a 2 s .  c  o  m
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    });
    if (helper.isDebugEnabled())
        builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.unitime.timetable.server.script.TaskExecutionItem.java

License:Apache License

public static Gson createGson() {
    return new GsonBuilder()
            .registerTypeAdapter(java.sql.Timestamp.class, new JsonSerializer<java.sql.Timestamp>() {
                @Override//  w  w w.j  ava  2 s. c  o m
                public JsonElement serialize(java.sql.Timestamp src, Type typeOfSrc,
                        JsonSerializationContext context) {
                    return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
                }
            }).registerTypeAdapter(java.sql.Date.class, new JsonSerializer<java.sql.Date>() {
                @Override
                public JsonElement serialize(java.sql.Date src, Type typeOfSrc,
                        JsonSerializationContext context) {
                    return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd").format(src));
                }
            }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
                @Override
                public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
                    return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
                }
            }).setFieldNamingStrategy(new FieldNamingStrategy() {
                Pattern iPattern = Pattern.compile("i([A-Z])(.*)");

                @Override
                public String translateName(Field f) {
                    Matcher matcher = iPattern.matcher(f.getName());
                    if (matcher.matches())
                        return matcher.group(1).toLowerCase() + matcher.group(2);
                    else
                        return f.getName();
                }
            }).setPrettyPrinting().create();
}

From source file:org.unitime.timetable.server.sectioning.PublishedSectioningSolutionsBackend.java

License:Apache License

protected Gson getGson() {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//w w w .jav  a  2  s .c o  m
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    });
    builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.unitime.timetable.spring.gwt.GwtDispatcherServlet.java

License:Apache License

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    iGson = new GsonBuilder()
            .registerTypeAdapter(java.sql.Timestamp.class, new JsonSerializer<java.sql.Timestamp>() {
                @Override//from w  w w.  ja  v  a  2s .  c  om
                public JsonElement serialize(java.sql.Timestamp src, Type typeOfSrc,
                        JsonSerializationContext context) {
                    return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
                }
            }).registerTypeAdapter(java.sql.Date.class, new JsonSerializer<java.sql.Date>() {
                @Override
                public JsonElement serialize(java.sql.Date src, Type typeOfSrc,
                        JsonSerializationContext context) {
                    return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd").format(src));
                }
            }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
                @Override
                public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
                    return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
                }
            }).setFieldNamingStrategy(new FieldNamingStrategy() {
                Pattern iPattern = Pattern.compile("i([A-Z])(.*)");

                @Override
                public String translateName(Field f) {
                    Matcher matcher = iPattern.matcher(f.getName());
                    if (matcher.matches())
                        return matcher.group(1).toLowerCase() + matcher.group(2);
                    else
                        return f.getName();
                }
            }).create();
}