Skip to content

Instantly share code, notes, and snippets.

@jnwelzel
Forked from hstaudacher/GsonProvider.java
Created March 10, 2014 14:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnwelzel/9466105 to your computer and use it in GitHub Desktop.
Save jnwelzel/9466105 to your computer and use it in GitHub Desktop.
Integrating Gson into a JAX-RS application to replace the other shitty default providers
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
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;
@Provider
@Produces( APPLICATION_JSON )
@Consumes( APPLICATION_JSON )
public class GsonProvider<T> implements MessageBodyReader<T>, MessageBodyWriter<T> {
private final Gson gson;
public GsonProvider() {
gson = new Gson();
}
@Override
public long getSize( T t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) {
return -1;
}
@Override
public boolean isWriteable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) {
return true;
}
@Override
public void writeTo( T object,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream ) throws IOException, WebApplicationException
{
PrintWriter printWriter = new PrintWriter( entityStream );
try {
String json = gson.toJson( object );
printWriter.write( json );
printWriter.flush();
} finally {
printWriter.close();
}
}
@Override
public boolean isReadable( Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType ) {
return true;
}
@Override
public T readFrom( Class<T> type,
Type gnericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream ) throws IOException, WebApplicationException
{
InputStreamReader reader = new InputStreamReader( entityStream, "UTF-8" );
try {
return gson.fromJson( reader, type );
} finally {
reader.close();
}
}
}
import static org.fest.assertions.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.ws.rs.WebApplicationException;
import org.junit.Test;
import com.google.gson.Gson;
public class GsonProviderTest {
@Test
public void testGetSize() {
GsonProvider<TestObject> testObjectProvider = new GsonProvider<TestObject>();
long size = testObjectProvider.getSize( mock( TestObject.class ), null, null, null, null );
assertThat( size ).isEqualTo( -1 );
}
@Test
public void testIsWritableWithTestObject() {
GsonProvider<TestObject> testObjectProvider = new GsonProvider<TestObject>();
boolean writeable = testObjectProvider.isWriteable( TestObject.class, null, null, null );
assertTrue( writeable );
}
@Test
public void testIsReadableWithTestObject() {
GsonProvider<TestObject> testObjectProvider = new GsonProvider<TestObject>();
boolean readable = testObjectProvider.isReadable( TestObject.class, null, null, null );
assertTrue( readable );
}
@Test
public void testWritesTestObject() throws WebApplicationException, IOException {
GsonProvider<TestObject> testObjectProvider = new GsonProvider<TestObject>();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
TestObject testObject = new TestObject( "foo" );
testObjectProvider.writeTo( testObject, TestObject.class, null, null, null, null, stream );
TestObject actualTestObject = new Gson().fromJson( convertToReader( stream ), TestObject.class );
assertThat( actualTestObject ).isEqualTo( testObject );
}
@Test
public void testWritesTestObjectClosesStream() throws WebApplicationException, IOException {
GsonProvider<TestObject> testObjectProvider = new GsonProvider<TestObject>();
OutputStream stream = mock( OutputStream.class );
TestObject testObject = new TestObject( "foo" );
testObjectProvider.writeTo( testObject, TestObject.class, null, null, null, null, stream );
verify( stream ).close();
}
@Test
public void testReadsTestObject() throws WebApplicationException, IOException {
GsonProvider<TestObject> testObjectProvider = new GsonProvider<TestObject>();
TestObject testObject = new TestObject( "foo" );
String json = new Gson().toJson( testObject );
ByteArrayInputStream inputStream = new ByteArrayInputStream( json.getBytes( "UTF-8" ) );
TestObject actualTestObject = testObjectProvider.readFrom( TestObject.class, null, null, null, null, inputStream );
assertThat( actualTestObject ).isEqualTo( testObject );
}
@Test
public void testReadTestObjectClosesStream() throws WebApplicationException, IOException {
GsonProvider<TestObject> testObjectProvider = new GsonProvider<TestObject>();
TestObject testObject = new TestObject( "foo" );
String json = new Gson().toJson( testObject );
ByteArrayInputStream inputStream = spy( new ByteArrayInputStream( json.getBytes( "UTF-8" ) ) );
testObjectProvider.readFrom( TestObject.class, null, null, null, null, inputStream );
verify( inputStream ).close();
}
private BufferedReader convertToReader( ByteArrayOutputStream stream ) {
InputStream input = new ByteArrayInputStream(stream.toByteArray());
return new BufferedReader( new InputStreamReader( input ) );
}
private static class TestObject {
private final String id;
public TestObject( String id ) {
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( ( id == null ) ? 0 : id.hashCode() );
return result;
}
@Override
public boolean equals( Object obj ) {
if( this == obj )
return true;
if( obj == null )
return false;
if( getClass() != obj.getClass() )
return false;
TestObject other = ( TestObject )obj;
if( id == null ) {
if( other.id != null )
return false;
} else if( !id.equals( other.id ) )
return false;
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment