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 org.jclouds.json.internal; |
20 | |
21 | import java.lang.reflect.Method; |
22 | import java.lang.reflect.Type; |
23 | import java.util.concurrent.ExecutionException; |
24 | |
25 | import com.google.common.cache.Cache; |
26 | import com.google.common.cache.CacheBuilder; |
27 | import com.google.common.cache.CacheLoader; |
28 | import com.google.gson.JsonDeserializationContext; |
29 | import com.google.gson.JsonDeserializer; |
30 | import com.google.gson.JsonElement; |
31 | import com.google.gson.JsonParseException; |
32 | import com.google.gson.JsonPrimitive; |
33 | import com.google.gson.JsonSerializationContext; |
34 | import com.google.gson.JsonSerializer; |
35 | |
36 | /** |
37 | * @author Adrian Cole |
38 | */ |
39 | @SuppressWarnings("unchecked") |
40 | public class EnumTypeAdapterThatReturnsFromValue<T extends Enum<T>> implements JsonSerializer<T>, JsonDeserializer<T> { |
41 | public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) { |
42 | return new JsonPrimitive(src.name()); |
43 | } |
44 | |
45 | @SuppressWarnings("cast") |
46 | public T deserialize(JsonElement json, Type classOfT, JsonDeserializationContext context) throws JsonParseException { |
47 | try { |
48 | return (T) Enum.valueOf((Class<T>) classOfT, json.getAsString()); |
49 | } catch (IllegalArgumentException e) { |
50 | try { |
51 | Method converter = classToConvert.get((Class<?>) classOfT); |
52 | return (T) converter.invoke(null, json.getAsString()); |
53 | } catch (Exception e1) { |
54 | throw e; |
55 | } |
56 | } |
57 | } |
58 | |
59 | private final static Cache<Class<?>, Method> classToConvert = CacheBuilder.newBuilder() |
60 | .build(new CacheLoader<Class<?>, Method>() { |
61 | |
62 | @Override |
63 | public Method load(Class<?> from) throws ExecutionException { |
64 | try { |
65 | Method method = from.getMethod("fromValue", String.class); |
66 | method.setAccessible(true); |
67 | return method; |
68 | } catch (Exception e) { |
69 | throw new ExecutionException(e); |
70 | } |
71 | } |
72 | |
73 | }); |
74 | |
75 | @Override |
76 | public String toString() { |
77 | return EnumTypeAdapterThatReturnsFromValue.class.getSimpleName(); |
78 | } |
79 | } |