This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository observe. See http://git.codelutin.com/observe.git commit 3c3e86a84444588627505a1bdf0d357a81dc2d99 Author: Tony CHEMIT <chemit@codelutin.com> Date: Thu Aug 20 18:53:55 2015 +0200 Ajout du GsonSupplier --- observe-services-model/pom.xml | 4 + .../ird/observe/services/dto/gson/DateAdapter.java | 94 ++++++++++++++++++++++ .../observe/services/dto/gson/IntegerAdapter.java | 36 +++++++++ .../services/dto/gson/ObserveDtoGsonSupplier.java | 52 ++++++++++++ 4 files changed, 186 insertions(+) diff --git a/observe-services-model/pom.xml b/observe-services-model/pom.xml index a35de0f..7c25fff 100644 --- a/observe-services-model/pom.xml +++ b/observe-services-model/pom.xml @@ -62,6 +62,10 @@ <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + </dependency> </dependencies> diff --git a/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/DateAdapter.java b/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/DateAdapter.java new file mode 100644 index 0000000..a5f99c9 --- /dev/null +++ b/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/DateAdapter.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2008 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package fr.ird.observe.services.dto.gson; + + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import com.google.gson.JsonSyntaxException; + +import java.lang.reflect.Type; +import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +/** + * This type adapter supports three subclasses of date: Date, Timestamp, and + * java.sql.Date. + * + * @author Inderjeet Singh + * @author Joel Leitch + * @author Kevin Morin : morin@codelutin.com + */ +public class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> { + + private final DateFormat iso8601Format; + + public DateAdapter() { + this.iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + this.iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); + } + + // These methods need to be synchronized since JDK DateFormat classes are not thread-safe + public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { + synchronized (iso8601Format) { + String dateFormatAsString = iso8601Format.format(src); + return new JsonPrimitive(dateFormatAsString); + } + } + + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + if (!(json instanceof JsonPrimitive)) { + throw new JsonParseException("The date should be a string value"); + } + Date date = deserializeToDate(json); + if (typeOfT == Date.class) { + return date; + } else if (typeOfT == Timestamp.class) { + return new Timestamp(date.getTime()); + } else if (typeOfT == java.sql.Date.class) { + return new java.sql.Date(date.getTime()); + } else { + throw new IllegalArgumentException(getClass() + " cannot deserialize to " + typeOfT); + } + } + + private Date deserializeToDate(JsonElement json) { + String stringDate = json.getAsString(); + if (stringDate.matches("^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))([T\\s]((([01]\\d|2[0-3])((:?)[0-5]\\d)?|24\\:?00)([\\.,]\\d+(?!:))?)?(\\17[0-5]\\d([\\.,]\\d+)?)?([zZ]|([\\+-])([01]\\d|2[0-3]):?([0-5]\\d)?)?)?)?$")) { + synchronized (iso8601Format) { + try { + return iso8601Format.parse(json.getAsString()); + } catch (ParseException e) { + throw new JsonSyntaxException(json.getAsString(), e); + } + } + } else { + return null; + } + + } + +} diff --git a/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/IntegerAdapter.java b/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/IntegerAdapter.java new file mode 100644 index 0000000..f833db4 --- /dev/null +++ b/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/IntegerAdapter.java @@ -0,0 +1,36 @@ +package fr.ird.observe.services.dto.gson; + +import com.google.gson.JsonSyntaxException; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; + +/** + * @author Arnaud Thimel : thimel@codelutin.com + */ +public class IntegerAdapter extends TypeAdapter<Integer> { + @Override + public void write(JsonWriter out, Integer value) throws IOException { + out.value(value); + } + + @Override + public Integer read(JsonReader in) throws IOException { + if (in.peek() == JsonToken.NULL) { + in.nextNull(); + return null; + } + try { + String result = in.nextString(); + if ("".equals(result)) { + return null; + } + return Integer.valueOf(result); + } catch (NumberFormatException e) { + throw new JsonSyntaxException(e); + } + } +} diff --git a/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/ObserveDtoGsonSupplier.java b/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/ObserveDtoGsonSupplier.java new file mode 100644 index 0000000..453f6db --- /dev/null +++ b/observe-services-model/src/main/java/fr/ird/observe/services/dto/gson/ObserveDtoGsonSupplier.java @@ -0,0 +1,52 @@ +package fr.ird.observe.services.dto.gson; + +import com.google.common.base.Supplier; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.sql.Timestamp; +import java.util.Date; + +/** + * @author Arnaud Thimel - thimel@codelutin.com + * @author Tony Chemit - chemit@codelutin.com + */ +public class ObserveDtoGsonSupplier implements Supplier<Gson> { + + public static final Supplier<GsonBuilder> GSON_BUILDER_SUPPLIER = new Supplier<GsonBuilder>() { + + @Override + public GsonBuilder get() { + GsonBuilder result = new GsonBuilder(); + + // Type adapters : base types + result.registerTypeAdapter(Integer.class, new IntegerAdapter()); + result.registerTypeAdapter(Date.class, new DateAdapter()); + result.registerTypeAdapter(Timestamp.class, new DateAdapter()); + result.registerTypeAdapter(java.sql.Date.class, new DateAdapter()); + + return result; + } + }; + + protected static GsonBuilder gsonBuilder; + + protected Gson gson; + + protected static GsonBuilder getGsonBuilder() { + if (gsonBuilder == null) { + // Assign only when initialization is finished to be thread-safe + gsonBuilder = GSON_BUILDER_SUPPLIER.get(); + } + return gsonBuilder; + } + + @Override + public Gson get() { + if (gson == null) { + gson = getGsonBuilder().create(); + } + return gson; + } + +} -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@list.forge.codelutin.com>.