001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     slacoin
011 */
012package org.nuxeo.ecm.automation.jaxrs.io;
013
014import java.io.IOException;
015import java.io.OutputStream;
016import java.lang.annotation.Annotation;
017import java.lang.reflect.Type;
018
019import javax.ws.rs.Produces;
020import javax.ws.rs.WebApplicationException;
021import javax.ws.rs.core.MediaType;
022import javax.ws.rs.core.MultivaluedMap;
023import javax.ws.rs.ext.MessageBodyWriter;
024
025@Produces({ "application/json+nxentity", "application/json" })
026public class JsonPrimitiveWriter implements MessageBodyWriter<Object> {
027
028    @Override
029    public long getSize(Object arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
030        return -1;
031    }
032
033    @Override
034    public boolean isWriteable(Class<?> typeClass, Type arg1, Annotation[] arg2, MediaType arg3) {
035        if (typeClass == String.class) {
036            return true;
037        }
038        if (typeClass == Boolean.class) {
039            return true;
040        }
041        if (Number.class.isAssignableFrom(typeClass)) {
042            return true;
043        }
044        return false;
045    }
046
047    @Override
048    public void writeTo(Object value, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4,
049            MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
050        JsonWriter.writePrimitive(out, value);
051    }
052
053}