updated glesys to 2.1 style type adapters

This commit is contained in:
Adrian Cole 2012-01-02 10:42:47 -08:00
parent 23ce04fc91
commit fad1034427
4 changed files with 68 additions and 60 deletions

View File

@ -18,19 +18,20 @@
*/
package org.jclouds.glesys.config;
import java.lang.reflect.Type;
import java.util.Map;
import javax.inject.Singleton;
import org.jclouds.glesys.domain.ServerState;
import org.jclouds.glesys.domain.ServerUptime;
import org.jclouds.glesys.functions.internal.GleSYSTypeAdapters;
import org.jclouds.glesys.functions.internal.GlesysDateAdapter;
import org.jclouds.json.config.GsonModule.DateAdapter;
import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import org.jclouds.glesys.domain.ServerState;
import org.jclouds.glesys.domain.ServerUptime;
import org.jclouds.glesys.functions.internal.CustomDeserializers;
import org.jclouds.glesys.functions.internal.GlesysDateAdapter;
import org.jclouds.json.config.GsonModule;
import org.jclouds.json.config.GsonModule.DateAdapter;
import javax.inject.Singleton;
import java.lang.reflect.Type;
import java.util.Map;
/**
* @author Adrian Cole
@ -40,10 +41,8 @@ public class GleSYSParserModule extends AbstractModule {
@Provides
@Singleton
public Map<Type, Object> provideCustomAdapterBindings() {
return ImmutableMap.<Type, Object>of(
ServerState.class, new CustomDeserializers.ServerStateAdapter(),
ServerUptime.class, new CustomDeserializers.ServerUptimeAdaptor()
);
return ImmutableMap.<Type, Object> of(ServerState.class, new GleSYSTypeAdapters.ServerStateAdapter(),
ServerUptime.class, new GleSYSTypeAdapters.ServerUptimeAdapter());
}
@Override

View File

@ -1,34 +0,0 @@
package org.jclouds.glesys.functions.internal;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import org.jclouds.glesys.domain.ServerState;
import org.jclouds.glesys.domain.ServerUptime;
import java.lang.reflect.Type;
/**
* @author Adam Lowe
*/
public class CustomDeserializers {
public static class ServerStateAdapter implements JsonDeserializer<ServerState> {
@Override
public ServerState deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
throws JsonParseException {
String toParse = jsonElement.getAsJsonPrimitive().getAsString();
return ServerState.fromValue(toParse);
}
}
public static class ServerUptimeAdaptor implements JsonDeserializer<ServerUptime> {
@Override
public ServerUptime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context)
throws JsonParseException {
String toParse = jsonElement.getAsJsonPrimitive().getAsString();
return ServerUptime.fromValue(toParse);
}
}
}

View File

@ -0,0 +1,40 @@
package org.jclouds.glesys.functions.internal;
import java.io.IOException;
import org.jclouds.glesys.domain.ServerState;
import org.jclouds.glesys.domain.ServerUptime;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
/**
* @author Adam Lowe
*/
public class GleSYSTypeAdapters {
public static class ServerStateAdapter extends TypeAdapter<ServerState> {
@Override
public void write(JsonWriter writer, ServerState value) throws IOException {
writer.value(value.value());
}
@Override
public ServerState read(JsonReader reader) throws IOException {
return ServerState.fromValue(reader.nextString());
}
}
public static class ServerUptimeAdapter extends TypeAdapter<ServerUptime> {
@Override
public void write(JsonWriter writer, ServerUptime value) throws IOException {
writer.value(value.toString());
}
@Override
public ServerUptime read(JsonReader reader) throws IOException {
return ServerUptime.fromValue(reader.nextString());
}
}
}

View File

@ -1,39 +1,42 @@
package org.jclouds.glesys.functions.internal;
import com.google.gson.*;
import org.jclouds.json.config.GsonModule;
import javax.inject.Singleton;
import java.lang.reflect.Type;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.inject.Singleton;
import org.jclouds.json.config.GsonModule;
import com.google.common.base.Throwables;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
/**
* Parser for Glesys Date formats
*
* @author Adam Lowe
*/
@Singleton
public class GlesysDateAdapter implements GsonModule.DateAdapter {
public class GlesysDateAdapter extends GsonModule.DateAdapter {
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
public void write(JsonWriter writer, Date value) throws IOException {
synchronized (dateFormat) {
return new JsonPrimitive(dateFormat.format(src));
writer.value(dateFormat.format(value));
}
}
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
String toParse = json.getAsJsonPrimitive().getAsString();
public Date read(JsonReader reader) throws IOException {
String toParse = reader.nextString();
try {
synchronized (dateFormat) {
return dateFormat.parse(toParse);
}
} catch (ParseException e) {
throw new RuntimeException(e);
throw Throwables.propagate(e);
}
}
}