| 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.gogrid.functions.internal; |
| 20 | |
| 21 | import java.lang.reflect.Type; |
| 22 | |
| 23 | import org.jclouds.gogrid.domain.IpState; |
| 24 | import org.jclouds.gogrid.domain.JobState; |
| 25 | import org.jclouds.gogrid.domain.LoadBalancerOs; |
| 26 | import org.jclouds.gogrid.domain.LoadBalancerPersistenceType; |
| 27 | import org.jclouds.gogrid.domain.LoadBalancerState; |
| 28 | import org.jclouds.gogrid.domain.LoadBalancerType; |
| 29 | import org.jclouds.gogrid.domain.ObjectType; |
| 30 | import org.jclouds.gogrid.domain.ServerImageState; |
| 31 | import org.jclouds.gogrid.domain.ServerImageType; |
| 32 | import org.jclouds.gogrid.domain.ServerState; |
| 33 | |
| 34 | import com.google.gson.JsonDeserializationContext; |
| 35 | import com.google.gson.JsonDeserializer; |
| 36 | import com.google.gson.JsonElement; |
| 37 | import com.google.gson.JsonObject; |
| 38 | import com.google.gson.JsonParseException; |
| 39 | |
| 40 | /** |
| 41 | * @author Oleksiy Yarmula |
| 42 | */ |
| 43 | public class CustomDeserializers { |
| 44 | |
| 45 | public static class ServerStateAdapter implements JsonDeserializer<ServerState> { |
| 46 | @Override |
| 47 | public ServerState deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) |
| 48 | throws JsonParseException { |
| 49 | String name = ((JsonObject) jsonElement).get("name").getAsString(); |
| 50 | return ServerState.fromValue(name); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public static class ObjectTypeAdapter implements JsonDeserializer<ObjectType> { |
| 55 | @Override |
| 56 | public ObjectType deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) |
| 57 | throws JsonParseException { |
| 58 | String name = ((JsonObject) jsonElement).get("name").getAsString(); |
| 59 | return ObjectType.fromValue(name); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public static class LoadBalancerOsAdapter implements JsonDeserializer<LoadBalancerOs> { |
| 64 | @Override |
| 65 | public LoadBalancerOs deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) |
| 66 | throws JsonParseException { |
| 67 | String name = ((JsonObject) jsonElement).get("name").getAsString(); |
| 68 | return LoadBalancerOs.fromValue(name); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public static class LoadBalancerStateAdapter implements JsonDeserializer<LoadBalancerState> { |
| 73 | @Override |
| 74 | public LoadBalancerState deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) |
| 75 | throws JsonParseException { |
| 76 | String name = ((JsonObject) jsonElement).get("name").getAsString(); |
| 77 | return LoadBalancerState.fromValue(name); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public static class LoadBalancerPersistenceTypeAdapter implements JsonDeserializer<LoadBalancerPersistenceType> { |
| 82 | @Override |
| 83 | public LoadBalancerPersistenceType deserialize(JsonElement jsonElement, Type type, |
| 84 | JsonDeserializationContext context) throws JsonParseException { |
| 85 | String name = ((JsonObject) jsonElement).get("name").getAsString(); |
| 86 | return LoadBalancerPersistenceType.fromValue(name); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public static class LoadBalancerTypeAdapter implements JsonDeserializer<LoadBalancerType> { |
| 91 | @Override |
| 92 | public LoadBalancerType deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) |
| 93 | throws JsonParseException { |
| 94 | String name = ((JsonObject) jsonElement).get("name").getAsString(); |
| 95 | return LoadBalancerType.fromValue(name); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public static class IpStateAdapter implements JsonDeserializer<IpState> { |
| 100 | @Override |
| 101 | public IpState deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) |
| 102 | throws JsonParseException { |
| 103 | String name = ((JsonObject) jsonElement).get("name").getAsString(); |
| 104 | return IpState.fromValue(name); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | public static class JobStateAdapter implements JsonDeserializer<JobState> { |
| 109 | @Override |
| 110 | public JobState deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) |
| 111 | throws JsonParseException { |
| 112 | String name = ((JsonObject) jsonElement).get("name").getAsString(); |
| 113 | return JobState.fromValue(name); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public static class ServerImageStateAdapter implements JsonDeserializer<ServerImageState> { |
| 118 | @Override |
| 119 | public ServerImageState deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) |
| 120 | throws JsonParseException { |
| 121 | String name = ((JsonObject) jsonElement).get("name").getAsString(); |
| 122 | return ServerImageState.fromValue(name); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | public static class ServerImageTypeAdapter implements JsonDeserializer<ServerImageType> { |
| 127 | @Override |
| 128 | public ServerImageType deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) |
| 129 | throws JsonParseException { |
| 130 | String name = ((JsonObject) jsonElement).get("name").getAsString(); |
| 131 | return ServerImageType.fromValue(name); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } |