EMMA Coverage Report (generated Wed Oct 26 13:47:17 EDT 2011)
[all classes][org.jclouds.json.config]

COVERAGE SUMMARY FOR SOURCE FILE [GsonModule.java]

nameclass, %method, %block, %line, %
GsonModule.java82%  (9/11)54%  (15/28)59%  (163/278)58%  (35.8/62)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class GsonModule$Iso8601DateAdapter0%   (0/1)0%   (0/3)0%   (0/29)0%   (0/8)
GsonModule$Iso8601DateAdapter (DateService): void 0%   (0/1)0%   (0/6)0%   (0/3)
deserialize (JsonElement, Type, JsonDeserializationContext): Date 0%   (0/1)0%   (0/15)0%   (0/4)
serialize (Date, Type, JsonSerializationContext): JsonElement 0%   (0/1)0%   (0/8)0%   (0/1)
     
class GsonModule$LongDateAdapter0%   (0/1)0%   (0/3)0%   (0/27)0%   (0/7)
GsonModule$LongDateAdapter (): void 0%   (0/1)0%   (0/3)0%   (0/1)
deserialize (JsonElement, Type, JsonDeserializationContext): Date 0%   (0/1)0%   (0/17)0%   (0/5)
serialize (Date, Type, JsonSerializationContext): JsonElement 0%   (0/1)0%   (0/7)0%   (0/1)
     
class GsonModule$HexByteListAdapter100% (1/1)33%  (1/3)20%  (3/15)33%  (1/3)
deserialize (JsonElement, Type, JsonDeserializationContext): List 0%   (0/1)0%   (0/5)0%   (0/1)
serialize (List, Type, JsonSerializationContext): JsonElement 0%   (0/1)0%   (0/7)0%   (0/1)
GsonModule$HexByteListAdapter (): void 100% (1/1)100% (3/3)100% (1/1)
     
class GsonModule$HexByteArrayAdapter100% (1/1)33%  (1/3)23%  (3/13)33%  (1/3)
deserialize (JsonElement, Type, JsonDeserializationContext): byte [] 0%   (0/1)0%   (0/4)0%   (0/1)
serialize (byte [], Type, JsonSerializationContext): JsonElement 0%   (0/1)0%   (0/6)0%   (0/1)
GsonModule$HexByteArrayAdapter (): void 100% (1/1)100% (3/3)100% (1/1)
     
class GsonModule$CDateAdapter100% (1/1)33%  (1/3)24%  (6/25)43%  (3/7)
deserialize (JsonElement, Type, JsonDeserializationContext): Date 0%   (0/1)0%   (0/11)0%   (0/3)
serialize (Date, Type, JsonSerializationContext): JsonElement 0%   (0/1)0%   (0/8)0%   (0/1)
GsonModule$CDateAdapter (DateService): void 100% (1/1)100% (6/6)100% (3/3)
     
class GsonModule$JsonAdapterBindings100% (1/1)67%  (2/3)64%  (9/14)60%  (3/5)
setBindings (Map): void 0%   (0/1)0%   (0/5)0%   (0/2)
GsonModule$JsonAdapterBindings (): void 100% (1/1)100% (6/6)100% (2/2)
getBindings (): Map 100% (1/1)100% (3/3)100% (1/1)
     
class GsonModule100% (1/1)100% (3/3)85%  (72/85)93%  (14.8/16)
provideGson (GsonModule$JsonBallAdapter, GsonModule$DateAdapter, GsonModule$B... 100% (1/1)83%  (62/75)90%  (10.8/12)
GsonModule (): void 100% (1/1)100% (3/3)100% (2/2)
configure (): void 100% (1/1)100% (7/7)100% (2/2)
     
class GsonModule$1100% (1/1)100% (1/1)100% (6/6)100% (1/1)
GsonModule$1 (GsonModule): void 100% (1/1)100% (6/6)100% (1/1)
     
class GsonModule$JsonBallAdapterImpl100% (1/1)100% (3/3)100% (14/14)100% (3/3)
GsonModule$JsonBallAdapterImpl (): void 100% (1/1)100% (3/3)100% (1/1)
deserialize (JsonElement, Type, JsonDeserializationContext): JsonBall 100% (1/1)100% (6/6)100% (1/1)
serialize (JsonBall, Type, JsonSerializationContext): JsonElement 100% (1/1)100% (5/5)100% (1/1)
     
class GsonModule$SerializePropertiesDefaults100% (1/1)100% (2/2)100% (44/44)100% (10/10)
GsonModule$SerializePropertiesDefaults (Json): void 100% (1/1)100% (13/13)100% (4/4)
serialize (Properties, Type, JsonSerializationContext): JsonElement 100% (1/1)100% (31/31)100% (6/6)
     
class GsonModule$SerializePropertiesDefaults$1100% (1/1)100% (1/1)100% (6/6)100% (1/1)
GsonModule$SerializePropertiesDefaults$1 (GsonModule$SerializePropertiesDefau... 100% (1/1)100% (6/6)100% (1/1)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.json.config;
20 
21import java.lang.reflect.Type;
22import java.util.Date;
23import java.util.Enumeration;
24import java.util.List;
25import java.util.Map;
26import java.util.Properties;
27import java.util.logging.Level;
28import java.util.logging.Logger;
29 
30import javax.inject.Inject;
31import javax.inject.Singleton;
32 
33import org.jclouds.crypto.CryptoStreams;
34import org.jclouds.date.DateService;
35import org.jclouds.domain.JsonBall;
36import org.jclouds.json.Json;
37import org.jclouds.json.internal.EnumTypeAdapterThatReturnsFromValue;
38import org.jclouds.json.internal.GsonWrapper;
39 
40import com.google.common.collect.ImmutableMap;
41import com.google.common.collect.ImmutableMap.Builder;
42import com.google.common.collect.Maps;
43import com.google.common.primitives.Bytes;
44import com.google.gson.Gson;
45import com.google.gson.GsonBuilder;
46import com.google.gson.JsonDeserializationContext;
47import com.google.gson.JsonDeserializer;
48import com.google.gson.JsonElement;
49import com.google.gson.JsonLiteral;
50import com.google.gson.JsonParseException;
51import com.google.gson.JsonPrimitive;
52import com.google.gson.JsonSerializationContext;
53import com.google.gson.JsonSerializer;
54import com.google.gson.ObjectMapTypeAdapter;
55import com.google.gson.reflect.TypeToken;
56import com.google.inject.AbstractModule;
57import com.google.inject.ImplementedBy;
58import com.google.inject.Provides;
59import com.google.inject.TypeLiteral;
60 
61/**
62 * Contains logic for parsing objects from Strings.
63 * 
64 * @author Adrian Cole
65 */
66public class GsonModule extends AbstractModule {
67 
68   @SuppressWarnings("rawtypes")
69   @Provides
70   @Singleton
71   Gson provideGson(JsonBallAdapter jsonAdapter, DateAdapter adapter, ByteListAdapter byteListAdapter,
72            ByteArrayAdapter byteArrayAdapter, SerializePropertiesDefaults propertiesAdapter,
73            JsonAdapterBindings bindings) throws ClassNotFoundException, Exception {
74      GsonBuilder builder = new GsonBuilder();
75      Logger.getLogger("com.google.gson.ParameterizedTypeHandlerMap").setLevel(Level.OFF);
76      builder.registerTypeHierarchyAdapter(Enum.class, new EnumTypeAdapterThatReturnsFromValue());
77      builder.registerTypeHierarchyAdapter(Map.class, new ObjectMapTypeAdapter());
78      builder.registerTypeAdapter(JsonBall.class, jsonAdapter);
79      builder.registerTypeAdapter(Date.class, adapter);
80      builder.registerTypeAdapter(Properties.class, propertiesAdapter);
81      builder.registerTypeAdapter(new TypeToken<List<Byte>>() {
82      }.getType(), byteListAdapter);
83      builder.registerTypeAdapter(byte[].class, byteArrayAdapter);
84      for (Map.Entry<Type, Object> binding : bindings.getBindings().entrySet()) {
85         builder.registerTypeAdapter(binding.getKey(), binding.getValue());
86      }
87      return builder.create();
88   }
89 
90   // http://code.google.com/p/google-gson/issues/detail?id=326
91   @ImplementedBy(JsonBallAdapterImpl.class)
92   public static interface JsonBallAdapter extends JsonSerializer<JsonBall>, JsonDeserializer<JsonBall> {
93 
94   }
95 
96   @Singleton
97   public static class JsonBallAdapterImpl implements JsonBallAdapter {
98 
99      public JsonElement serialize(JsonBall src, Type typeOfSrc, JsonSerializationContext context) {
100         return new JsonLiteral(src);
101      }
102 
103      public JsonBall deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
104               throws JsonParseException {
105         return new JsonBall(json.toString());
106      }
107 
108   }
109 
110   @ImplementedBy(CDateAdapter.class)
111   public static interface DateAdapter extends JsonSerializer<Date>, JsonDeserializer<Date> {
112 
113   }
114 
115   @ImplementedBy(HexByteListAdapter.class)
116   public static interface ByteListAdapter extends JsonSerializer<List<Byte>>, JsonDeserializer<List<Byte>> {
117 
118   }
119 
120   @ImplementedBy(HexByteArrayAdapter.class)
121   public static interface ByteArrayAdapter extends JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
122 
123   }
124 
125   @Singleton
126   public static class HexByteListAdapter implements ByteListAdapter {
127 
128      @Override
129      public List<Byte> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
130               throws JsonParseException {
131         return Bytes.asList(CryptoStreams.hex(json.getAsString()));
132      }
133 
134      @Override
135      public JsonElement serialize(List<Byte> src, Type typeOfSrc, JsonSerializationContext context) {
136         return new JsonPrimitive(CryptoStreams.hex(Bytes.toArray(src)));
137      }
138 
139   }
140 
141   @Singleton
142   public static class HexByteArrayAdapter implements ByteArrayAdapter {
143 
144      @Override
145      public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
146               throws JsonParseException {
147         return CryptoStreams.hex(json.getAsString());
148      }
149 
150      @Override
151      public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
152         return new JsonPrimitive(CryptoStreams.hex(src));
153      }
154   }
155 
156   @Singleton
157   public static class Iso8601DateAdapter implements DateAdapter {
158      private final DateService dateService;
159 
160      @Inject
161      private Iso8601DateAdapter(DateService dateService) {
162         this.dateService = dateService;
163      }
164 
165      public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
166         return new JsonPrimitive(dateService.iso8601DateFormat(src));
167      }
168 
169      public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
170               throws JsonParseException {
171         String toParse = json.getAsJsonPrimitive().getAsString();
172         try {
173            return dateService.iso8601DateParse(toParse);
174         } catch (RuntimeException e) {
175            return dateService.iso8601SecondsDateParse(toParse);
176         }
177      }
178 
179   }
180 
181   @Singleton
182   public static class SerializePropertiesDefaults implements JsonSerializer<Properties> {
183      private final Json json;
184      private final Type mapType = new TypeLiteral<Map<String, String>>() {
185      }.getRawType();
186 
187      @Inject
188      public SerializePropertiesDefaults(Json json) {
189         this.json = json;
190      }
191 
192      public JsonElement serialize(Properties src, Type typeOfSrc, JsonSerializationContext context) {
193         Builder<String, String> srcMap = ImmutableMap.<String, String> builder();
194         for (Enumeration<?> propNames = src.propertyNames(); propNames.hasMoreElements();) {
195            String propName = (String) propNames.nextElement();
196            srcMap.put(propName, src.getProperty(propName));
197         }
198         return new JsonLiteral(json.toJson(srcMap.build(), mapType));
199      }
200 
201   }
202 
203   @Singleton
204   public static class CDateAdapter implements DateAdapter {
205      private final DateService dateService;
206 
207      @Inject
208      private CDateAdapter(DateService dateService) {
209         this.dateService = dateService;
210      }
211 
212      public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
213         return new JsonPrimitive(dateService.cDateFormat(src));
214      }
215 
216      public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
217               throws JsonParseException {
218         String toParse = json.getAsJsonPrimitive().getAsString();
219         Date toReturn = dateService.cDateParse(toParse);
220         return toReturn;
221      }
222 
223   }
224 
225   @Singleton
226   public static class LongDateAdapter implements DateAdapter {
227 
228      public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
229         return new JsonPrimitive(src.getTime());
230      }
231 
232      public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
233               throws JsonParseException {
234         long toParse = json.getAsJsonPrimitive().getAsLong();
235         if (toParse == -1)
236            return null;
237         Date toReturn = new Date(toParse);
238         return toReturn;
239      }
240   }
241 
242   @Singleton
243   public static class JsonAdapterBindings {
244      private final Map<Type, Object> bindings = Maps.newHashMap();
245 
246      @com.google.inject.Inject(optional = true)
247      public void setBindings(Map<Type, Object> bindings) {
248         this.bindings.putAll(bindings);
249      }
250 
251      public Map<Type, Object> getBindings() {
252         return bindings;
253      }
254   }
255 
256   @Override
257   protected void configure() {
258      bind(Json.class).to(GsonWrapper.class);
259   }
260}

[all classes][org.jclouds.json.config]
EMMA 2.0.5312 (C) Vladimir Roubtsov