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

org.minijax.json.MinijaxJsonWriter Maven / Gradle / Ivy

There is a newer version: 0.5.10
Show newest version
package org.minijax.json;

import static javax.ws.rs.core.MediaType.*;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;

import com.fasterxml.jackson.databind.ObjectMapper;

@Singleton
@Produces(APPLICATION_JSON)
public class MinijaxJsonWriter implements MessageBodyWriter {

    @Inject
    private ObjectMapper objectMapper;


    @Override
    public boolean isWriteable(
            final Class type,
            final Type genericType,
            final Annotation[] annotations,
            final MediaType mediaType) {

        return mediaType != null && mediaType.isCompatible(APPLICATION_JSON_TYPE);
    }


    @Override
    public void writeTo(
            final Object t,
            final Class type,
            final Type genericType,
            final Annotation[] annotations,
            final MediaType mediaType,
            final MultivaluedMap httpHeaders,
            final OutputStream entityStream)
                    throws IOException {

        objectMapper.writeValue(entityStream, t);
    }
}