All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eclipsesource.jaxrs.provider.gson.GsonProvider Maven / Gradle / Ivy

The newest version!
package com.eclipsesource.jaxrs.provider.gson;
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.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 implements MessageBodyReader, MessageBodyWriter {

  private Gson gson;

  public GsonProvider() {
    gson = new Gson();
  }
  
  public Gson getGson() {
    return gson;
  }
  
  public void setGson( Gson gson ) {
    validateGson( gson );
    this.gson = gson;
  }

  private void validateGson( Gson gson ) {
    if( gson == null ) {
      throw new IllegalArgumentException( "gson must not be null" );
    }
  }

  @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 httpHeaders,
                       OutputStream entityStream ) throws IOException, WebApplicationException
  {
    try( OutputStream stream = entityStream ) {
      entityStream.write( gson.toJson( object ).getBytes( "utf-8" ) );
      entityStream.flush();
    }
  }

  @Override
  public boolean isReadable( Class type, Type genericType, Annotation[] annotations, MediaType mediaType ) {
    return true;
  }

  @Override
  public T readFrom( Class type,
                     Type gnericType,
                     Annotation[] annotations,
                     MediaType mediaType,
                     MultivaluedMap httpHeaders,
                     InputStream entityStream ) throws IOException, WebApplicationException
  {
    try( InputStreamReader reader = new InputStreamReader( entityStream, "UTF-8" ) ) {
      return gson.fromJson( reader, type );
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy