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