package cactusServer.utils; import java.io.IOException; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import javax.inject.Singleton; 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.MessageBodyWriter; import javax.ws.rs.ext.Provider; import net.arnx.jsonic.JSON; @Provider @Produces(MediaType.APPLICATION_JSON) @Singleton public class JSONProvider implements MessageBodyWriter<Object> { @Override public long getSize(Object obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { try { return JSON.encode(obj).getBytes("UTF-8").length; } catch (Exception e) { throw new RuntimeException(e); } } @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return true; } @Override public void writeTo(Object obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { System.out.println("JSONProvider#writeTo()を開始"); JSON.encode(obj, entityStream); System.out.println("JSONProvider#writeTo()を終了"); } }