| 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 | |
| 17 | package com.google.gson; |
| 18 | |
| 19 | import java.lang.reflect.ParameterizedType; |
| 20 | import java.lang.reflect.Type; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import org.jclouds.json.internal.ParseObjectFromElement; |
| 25 | |
| 26 | import 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") |
| 37 | public 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 | } |