Adding gson adaptor for HostResourceUsage

This commit is contained in:
Adam Lowe 2012-04-19 10:31:23 +01:00
parent 1d09fc3400
commit 43d258837d
1 changed files with 34 additions and 1 deletions

View File

@ -25,8 +25,15 @@ import javax.inject.Singleton;
import org.jclouds.json.config.GsonModule; import org.jclouds.json.config.GsonModule;
import org.jclouds.json.config.GsonModule.DateAdapter; import org.jclouds.json.config.GsonModule.DateAdapter;
import org.jclouds.openstack.nova.v1_1.domain.HostResourceUsage;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Provides; import com.google.inject.Provides;
@ -38,7 +45,7 @@ public class NovaParserModule extends AbstractModule {
@Provides @Provides
@Singleton @Singleton
public Map<Type, Object> provideCustomAdapterBindings() { public Map<Type, Object> provideCustomAdapterBindings() {
return ImmutableMap.<Type, Object> of(); return ImmutableMap.<Type, Object> of(HostResourceUsage.class, new HostResourceUsageAdapter());
} }
@Override @Override
@ -46,4 +53,30 @@ public class NovaParserModule extends AbstractModule {
bind(DateAdapter.class).to(GsonModule.Iso8601DateAdapter.class); bind(DateAdapter.class).to(GsonModule.Iso8601DateAdapter.class);
} }
@Singleton
public static class HostResourceUsageAdapter implements JsonSerializer<HostResourceUsage>, JsonDeserializer<HostResourceUsage> {
public HostResourceUsage apply(HostResourceUsageWrapper in) {
return in.resource.toBuilder().build();
}
@Override
public HostResourceUsage deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
return apply((HostResourceUsageWrapper) context.deserialize(jsonElement, HostResourceUsageWrapper.class));
}
@Override
public JsonElement serialize(HostResourceUsage hostResourceUsage, Type type, JsonSerializationContext context) {
return context.serialize(hostResourceUsage);
}
private static class HostResourceUsageWrapper {
protected HostResourceUsageInternal resource;
}
private static class HostResourceUsageInternal extends HostResourceUsage {
protected HostResourceUsageInternal(Builder<?> builder) {
super(builder);
}
}
}
} }