1 /* 2 * Copyright (C) 2012 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.caliper.json; 18 19 import com.google.gson.ExclusionStrategy; 20 import com.google.gson.Gson; 21 import com.google.gson.GsonBuilder; 22 import com.google.gson.TypeAdapterFactory; 23 import com.google.gson.internal.bind.TypeAdapters; 24 import dagger.Module; 25 import dagger.Provides; 26 import dagger.multibindings.IntoSet; 27 import org.joda.time.Instant; 28 import java.util.Set; 29 30 /** 31 * Binds a {@link Gson} instance suitable for serializing and deserializing Caliper 32 * {@linkplain com.google.caliper.model model} objects. 33 */ 34 @Module 35 public final class GsonModule { 36 37 @Provides 38 @IntoSet provideImmutableListTypeAdapterFactory()39 static TypeAdapterFactory provideImmutableListTypeAdapterFactory() { 40 return new ImmutableListTypeAdatperFactory(); 41 } 42 43 @Provides 44 @IntoSet provideImmutableMapTypeAdapterFactory()45 static TypeAdapterFactory provideImmutableMapTypeAdapterFactory() { 46 return new ImmutableMapTypeAdapterFactory(); 47 } 48 49 @Provides 50 @IntoSet provideNaturallySortedMapTypeAdapterFactory()51 static TypeAdapterFactory provideNaturallySortedMapTypeAdapterFactory() { 52 return new NaturallySortedMapTypeAdapterFactory(); 53 } 54 55 @Provides 56 @IntoSet provideImmutableMultimapTypeAdapterFactory()57 static TypeAdapterFactory provideImmutableMultimapTypeAdapterFactory() { 58 return new ImmutableMultimapTypeAdapterFactory(); 59 } 60 61 @Provides provideAnnotationExclusionStrategy()62 static ExclusionStrategy provideAnnotationExclusionStrategy() { 63 return new AnnotationExclusionStrategy(); 64 } 65 66 @Provides 67 @IntoSet provideTypeAdapterFactoryForInstant( InstantTypeAdapter typeAdapter)68 static TypeAdapterFactory provideTypeAdapterFactoryForInstant( 69 InstantTypeAdapter typeAdapter) { 70 return TypeAdapters.newFactory(Instant.class, typeAdapter); 71 } 72 provideInstantTypeAdapter()73 @Provides static InstantTypeAdapter provideInstantTypeAdapter() { 74 return new InstantTypeAdapter(); 75 } 76 provideGson(Set<TypeAdapterFactory> typeAdapterFactories, ExclusionStrategy exclusionStrategy)77 @Provides static Gson provideGson(Set<TypeAdapterFactory> typeAdapterFactories, 78 ExclusionStrategy exclusionStrategy) { 79 GsonBuilder gsonBuilder = new GsonBuilder().setExclusionStrategies(exclusionStrategy); 80 for (TypeAdapterFactory typeAdapterFactory : typeAdapterFactories) { 81 gsonBuilder.registerTypeAdapterFactory(typeAdapterFactory); 82 } 83 return gsonBuilder.create(); 84 } 85 } 86