mirror of https://github.com/apache/jclouds.git
fixed to allow json binding as well parse dates that come in as -1
This commit is contained in:
parent
1ad4631266
commit
89ae22364c
|
@ -92,9 +92,10 @@ public class ParserModule extends AbstractModule {
|
|||
|
||||
@Provides
|
||||
@Singleton
|
||||
Gson provideGson(DateAdapter adapter, ByteListAdapter byteListAdapter,
|
||||
Gson provideGson(JsonObjectAdapter jsonObjectAdapter, DateAdapter adapter, ByteListAdapter byteListAdapter,
|
||||
ByteArrayAdapter byteArrayAdapter, GsonAdapterBindings bindings) {
|
||||
GsonBuilder gson = new GsonBuilder();
|
||||
gson.registerTypeAdapter(JsonElement.class, jsonObjectAdapter);
|
||||
gson.registerTypeAdapter(Date.class, adapter);
|
||||
gson.registerTypeAdapter(new TypeToken<List<Byte>>() {
|
||||
}.getType(), byteListAdapter);
|
||||
|
@ -106,20 +107,17 @@ public class ParserModule extends AbstractModule {
|
|||
}
|
||||
|
||||
@ImplementedBy(CDateAdapter.class)
|
||||
public static interface DateAdapter extends JsonSerializer<Date>,
|
||||
JsonDeserializer<Date> {
|
||||
public static interface DateAdapter extends JsonSerializer<Date>, JsonDeserializer<Date> {
|
||||
|
||||
}
|
||||
|
||||
@ImplementedBy(HexByteListAdapter.class)
|
||||
public static interface ByteListAdapter extends JsonSerializer<List<Byte>>,
|
||||
JsonDeserializer<List<Byte>> {
|
||||
public static interface ByteListAdapter extends JsonSerializer<List<Byte>>, JsonDeserializer<List<Byte>> {
|
||||
|
||||
}
|
||||
|
||||
@ImplementedBy(HexByteArrayAdapter.class)
|
||||
public static interface ByteArrayAdapter extends JsonSerializer<byte[]>,
|
||||
JsonDeserializer<byte[]> {
|
||||
public static interface ByteArrayAdapter extends JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
|
||||
|
||||
}
|
||||
|
||||
|
@ -133,14 +131,13 @@ public class ParserModule extends AbstractModule {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<Byte> deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
public List<Byte> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
return Bytes.asList(encryptionService.fromHex(json.getAsString()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(List<Byte> src, Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
public JsonElement serialize(List<Byte> src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(encryptionService.hex(Bytes.toArray(src)));
|
||||
}
|
||||
|
||||
|
@ -156,14 +153,13 @@ public class ParserModule extends AbstractModule {
|
|||
}
|
||||
|
||||
@Override
|
||||
public byte[] deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
public byte[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
return encryptionService.fromHex(json.getAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(byte[] src, Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(encryptionService.hex(src));
|
||||
}
|
||||
}
|
||||
|
@ -177,13 +173,12 @@ public class ParserModule extends AbstractModule {
|
|||
this.dateService = dateService;
|
||||
}
|
||||
|
||||
public JsonElement serialize(Date src, Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(dateService.iso8601DateFormat(src));
|
||||
}
|
||||
|
||||
public Date deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
String toParse = json.getAsJsonPrimitive().getAsString();
|
||||
try {
|
||||
return dateService.iso8601DateParse(toParse);
|
||||
|
@ -203,13 +198,12 @@ public class ParserModule extends AbstractModule {
|
|||
this.dateService = dateService;
|
||||
}
|
||||
|
||||
public JsonElement serialize(Date src, Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(dateService.cDateFormat(src));
|
||||
}
|
||||
|
||||
public Date deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
String toParse = json.getAsJsonPrimitive().getAsString();
|
||||
Date toReturn = dateService.cDateParse(toParse);
|
||||
return toReturn;
|
||||
|
@ -217,21 +211,40 @@ public class ParserModule extends AbstractModule {
|
|||
|
||||
}
|
||||
|
||||
@ImplementedBy(JsonObjectAdapterImpl.class)
|
||||
public static interface JsonObjectAdapter extends JsonSerializer<JsonElement>, JsonDeserializer<JsonElement> {
|
||||
|
||||
}
|
||||
|
||||
@Singleton
|
||||
public static class JsonObjectAdapterImpl implements JsonObjectAdapter {
|
||||
|
||||
public JsonElement serialize(JsonElement src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return src;
|
||||
}
|
||||
|
||||
public JsonElement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
return json;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Singleton
|
||||
public static class LongDateAdapter implements DateAdapter {
|
||||
|
||||
public JsonElement serialize(Date src, Type typeOfSrc,
|
||||
JsonSerializationContext context) {
|
||||
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(src.getTime());
|
||||
}
|
||||
|
||||
public Date deserialize(JsonElement json, Type typeOfT,
|
||||
JsonDeserializationContext context) throws JsonParseException {
|
||||
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
||||
throws JsonParseException {
|
||||
long toParse = json.getAsJsonPrimitive().getAsLong();
|
||||
if (toParse == -1)
|
||||
return null;
|
||||
Date toReturn = new Date(toParse);
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Singleton
|
||||
|
@ -239,8 +252,7 @@ public class ParserModule extends AbstractModule {
|
|||
private final Map<Type, Object> bindings = Maps.newHashMap();
|
||||
|
||||
@com.google.inject.Inject(optional = true)
|
||||
public void setBindings(
|
||||
@Named(Constants.PROPERTY_GSON_ADAPTERS) Map<Type, Object> bindings) {
|
||||
public void setBindings(@Named(Constants.PROPERTY_GSON_ADAPTERS) Map<Type, Object> bindings) {
|
||||
this.bindings.putAll(bindings);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue