Example usage for com.google.gson FieldNamingStrategy FieldNamingStrategy

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

Introduction

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

Prototype

FieldNamingStrategy

Source Link

Usage

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//www. j a va2  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.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  . j a va 2 s  .co  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();
                }
            }).create();
}