EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][com.google.gson]

COVERAGE SUMMARY FOR SOURCE FILE [ObjectMapTypeAdapter.java]

nameclass, %method, %block, %line, %
ObjectMapTypeAdapter.java100% (1/1)75%  (3/4)88%  (113/129)85%  (23.9/28)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ObjectMapTypeAdapter100% (1/1)75%  (3/4)88%  (113/129)85%  (23.9/28)
toString (): String 0%   (0/1)0%   (0/3)0%   (0/1)
serialize (Map, Type, JsonSerializationContext): JsonElement 100% (1/1)78%  (45/58)78%  (10.9/14)
ObjectMapTypeAdapter (): void 100% (1/1)100% (3/3)100% (1/1)
deserialize (JsonElement, Type, JsonDeserializationContext): Map 100% (1/1)100% (65/65)100% (12/12)

1/*
2 * Copyright (C) 2011 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 
17package com.google.gson;
18 
19import java.lang.reflect.ParameterizedType;
20import java.lang.reflect.Type;
21import java.util.Map;
22import java.util.Set;
23 
24import org.jclouds.json.internal.ParseObjectFromElement;
25 
26import com.google.gson.internal.$Gson$Types;
27 
28/**
29 * Default serialization and deserialization of a map type. This implementation really only works
30 * well with simple primitive types as the map key. If the key is not a simple primitive then the
31 * object is {@code toString}ed and that value is used as its key.
32 *   <p/>
33 *   Patched depending on <a href="http://code.google.com/p/google-gson/issues/detail?id=325">this</a>
34 * @author Joel Leitch
35 */
36@SuppressWarnings("unchecked")
37public final class ObjectMapTypeAdapter extends BaseMapTypeAdapter {
38 
39  public JsonElement serialize(Map src, Type typeOfSrc, JsonSerializationContext context) {
40    JsonObject map = new JsonObject();
41    Type childGenericType = null;
42    if (typeOfSrc instanceof ParameterizedType) {
43      Class<?> rawTypeOfSrc = $Gson$Types.getRawType(typeOfSrc);
44      childGenericType = $Gson$Types.getMapKeyAndValueTypes(typeOfSrc, rawTypeOfSrc)[1];
45    }
46 
47    for (Map.Entry entry : (Set<Map.Entry>) src.entrySet()) {
48      Object value = entry.getValue();
49 
50      JsonElement valueElement;
51      if (value == null) {
52        valueElement = JsonNull.createJsonNull();
53      } else {
54        Type childType = (childGenericType == null)
55            ? value.getClass() : childGenericType;
56        valueElement = serialize(context, value, childType);
57      }
58      map.add(String.valueOf(entry.getKey()), valueElement);
59    }
60    return map;
61  }
62 
63  public Map deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
64      throws JsonParseException {
65    // Use ObjectConstructor to create instance instead of hard-coding a specific type.
66    // This handles cases where users are using their own subclass of Map.
67    Map<Object, Object> map = constructMapType(typeOfT, context);
68    Type[] keyAndValueTypes = $Gson$Types.getMapKeyAndValueTypes(typeOfT, $Gson$Types.getRawType(typeOfT));
69    for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
70      Object key = context.deserialize(new JsonPrimitive(entry.getKey()), keyAndValueTypes[0]);
71      // START JCLOUDS PATCH
72      // http://code.google.com/p/google-gson/issues/detail?id=325
73      Object value = null;
74      if (keyAndValueTypes[1] == Object.class) {
75         value = ParseObjectFromElement.SINGLETON.apply(entry.getValue());
76      }
77      if (value == null) {
78         value = context.deserialize(entry.getValue(), keyAndValueTypes[1]);
79      }
80      // END JCLOUDS PATCH
81      map.put(key, value);
82    }
83    return map;
84  }
85 
86  @Override
87  public String toString() {
88    return MapTypeAdapter.class.getSimpleName();
89  }
90}

[all classes][com.google.gson]
EMMA 2.0.5312 (C) Vladimir Roubtsov