Java tutorial
/* * Copyright (c) 2015 OpenSilk Productions LLC * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * */ package syncthing.api; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; 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 org.joda.time.DateTime; import java.lang.reflect.Type; import java.util.Date; import javax.inject.Singleton; import dagger.Module; import dagger.Provides; import syncthing.api.model.Versioning; import syncthing.api.model.VersioningTypeConverter; import syncthing.api.model.event.Event; import syncthing.api.model.event.EventDeserializer; /** * Created by drew on 3/8/15. */ @Module public class GsonModule { @Provides @Singleton public Gson provideGson() { return new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY) .registerTypeAdapter(DateTime.class, new DateTimeTypeConverter()) .registerTypeAdapter(Event.class, new EventDeserializer()) .registerTypeAdapter(Versioning.class, new VersioningTypeConverter()).create(); } private static class DateTimeTypeConverter implements JsonSerializer<DateTime>, JsonDeserializer<DateTime> { @Override public JsonElement serialize(DateTime src, Type srcType, JsonSerializationContext context) { return new JsonPrimitive(src.toString()); } @Override public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { try { return new DateTime(json.getAsString()); } catch (IllegalArgumentException e) { // May be it came in formatted as a java.util.Date, so try that Date date = context.deserialize(json, Date.class); return new DateTime(date); } } } }