1 | /** |
2 | * |
3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
4 | * |
5 | * ==================================================================== |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * ==================================================================== |
18 | */ |
19 | package org.jclouds.json.internal; |
20 | |
21 | import java.lang.reflect.Method; |
22 | import java.lang.reflect.Type; |
23 | import java.util.Map; |
24 | |
25 | import com.google.common.base.Function; |
26 | import com.google.common.collect.MapMaker; |
27 | import com.google.gson.JsonDeserializationContext; |
28 | import com.google.gson.JsonDeserializer; |
29 | import com.google.gson.JsonElement; |
30 | import com.google.gson.JsonParseException; |
31 | import com.google.gson.JsonPrimitive; |
32 | import com.google.gson.JsonSerializationContext; |
33 | import com.google.gson.JsonSerializer; |
34 | |
35 | /** |
36 | * @author Adrian Cole |
37 | */ |
38 | @SuppressWarnings("unchecked") |
39 | public class EnumTypeAdapterThatReturnsFromValue<T extends Enum<T>> implements JsonSerializer<T>, JsonDeserializer<T> { |
40 | public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) { |
41 | return new JsonPrimitive(src.name()); |
42 | } |
43 | |
44 | @SuppressWarnings("cast") |
45 | public T deserialize(JsonElement json, Type classOfT, JsonDeserializationContext context) throws JsonParseException { |
46 | try { |
47 | return (T) Enum.valueOf((Class<T>) classOfT, json.getAsString()); |
48 | } catch (IllegalArgumentException e) { |
49 | Method converter = classToConvert.get(classOfT); |
50 | if (converter != null) |
51 | try { |
52 | return (T) converter.invoke(null, json.getAsString()); |
53 | } catch (Exception e1) { |
54 | throw e; |
55 | } |
56 | else |
57 | throw e; |
58 | } |
59 | } |
60 | |
61 | private final static Map<Class<?>, Method> classToConvert = new MapMaker() |
62 | .makeComputingMap(new Function<Class<?>, Method>() { |
63 | |
64 | @Override |
65 | public Method apply(Class<?> from) { |
66 | try { |
67 | Method method = from.getMethod("fromValue", String.class); |
68 | method.setAccessible(true); |
69 | return method; |
70 | } catch (Exception e) { |
71 | return null; |
72 | } |
73 | } |
74 | |
75 | }); |
76 | |
77 | @Override |
78 | public String toString() { |
79 | return EnumTypeAdapterThatReturnsFromValue.class.getSimpleName(); |
80 | } |
81 | } |