• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Provides.Type;
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(type = Type.SET)
provideImmutableListTypeAdapterFactory()38   static TypeAdapterFactory provideImmutableListTypeAdapterFactory() {
39     return new ImmutableListTypeAdatperFactory();
40   }
41 
42   @Provides(type = Type.SET)
provideImmutableMapTypeAdapterFactory()43   static TypeAdapterFactory provideImmutableMapTypeAdapterFactory() {
44     return new ImmutableMapTypeAdapterFactory();
45   }
46 
47   @Provides(type = Type.SET)
provideNaturallySortedMapTypeAdapterFactory()48   static TypeAdapterFactory provideNaturallySortedMapTypeAdapterFactory() {
49     return new NaturallySortedMapTypeAdapterFactory();
50   }
51 
52   @Provides(type = Type.SET)
provideImmutableMultimapTypeAdapterFactory()53   static TypeAdapterFactory provideImmutableMultimapTypeAdapterFactory() {
54     return new ImmutableMultimapTypeAdapterFactory();
55   }
56 
57   @Provides
provideAnnotationExclusionStrategy()58   static ExclusionStrategy provideAnnotationExclusionStrategy() {
59     return new AnnotationExclusionStrategy();
60   }
61 
62   @Provides(type = Type.SET)
provideTypeAdapterFactoryForInstant( InstantTypeAdapter typeAdapter)63   static TypeAdapterFactory provideTypeAdapterFactoryForInstant(
64       InstantTypeAdapter typeAdapter) {
65     return TypeAdapters.newFactory(Instant.class, typeAdapter);
66   }
67 
provideInstantTypeAdapter()68   @Provides static InstantTypeAdapter provideInstantTypeAdapter() {
69     return new InstantTypeAdapter();
70   }
71 
provideGson(Set<TypeAdapterFactory> typeAdapterFactories, ExclusionStrategy exclusionStrategy)72   @Provides static Gson provideGson(Set<TypeAdapterFactory> typeAdapterFactories,
73       ExclusionStrategy exclusionStrategy) {
74     GsonBuilder gsonBuilder = new GsonBuilder().setExclusionStrategies(exclusionStrategy);
75     for (TypeAdapterFactory typeAdapterFactory : typeAdapterFactories) {
76       gsonBuilder.registerTypeAdapterFactory(typeAdapterFactory);
77     }
78     return gsonBuilder.create();
79   }
80 }
81