Permalink
Browse files

Support JSON values

  • Loading branch information...
justinsb committed Dec 17, 2013
1 parent ef2f703 commit 929a33064c214b0d25da161e27896ef4b5e58468
@@ -18,6 +18,12 @@
<artifactId>cloudata-shared</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -0,0 +1,115 @@
package com.cloudata.keyvalue;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
@Provider
@Singleton
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public final class GsonObjectMessageBodyHandler implements MessageBodyWriter<Object>, MessageBodyReader<Object> {
private static final String UTF_8 = "UTF-8";
private Gson gson;
private Gson getGson() {
if (gson == null) {
GsonBuilder gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();
}
return gson;
}
@Override
public boolean isReadable(Class<?> type, Type genericType, java.lang.annotation.Annotation[] annotations,
MediaType mediaType) {
if (JsonElement.class.isAssignableFrom(type)) {
return true;
}
return false;
}
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
InputStreamReader streamReader = new InputStreamReader(entityStream, UTF_8);
try {
Type jsonType;
if (type.equals(genericType)) {
jsonType = type;
} else {
jsonType = genericType;
}
if (JsonElement.class.isAssignableFrom(type)) {
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(streamReader);
return jsonElement;
} else {
throw new IllegalArgumentException();
// Gson gson = getGson();
// return gson.fromJson(streamReader, jsonType);
}
} finally {
streamReader.close();
}
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
if (JsonElement.class.isAssignableFrom(type)) {
return true;
}
return false;
}
@Override
public long getSize(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
@Override
public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException,
WebApplicationException {
OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8);
try {
Type jsonType;
if (type.equals(genericType)) {
jsonType = type;
} else {
jsonType = genericType;
}
Gson gson = getGson();
if (JsonElement.class.isAssignableFrom(type)) {
gson.toJson((JsonElement) object, writer);
} else {
throw new IllegalArgumentException();
// gson.toJson(object, jsonType, writer);
}
} finally {
writer.close();
}
}
}
Oops, something went wrong.

0 comments on commit 929a330

Please sign in to comment.