001/* 002 * Copyright (c) 2012 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 * Nuxeo 011 */ 012package org.nuxeo.ecm.automation.client.jaxrs.spi.marshallers; 013 014import java.io.IOException; 015 016import org.codehaus.jackson.JsonGenerator; 017import org.codehaus.jackson.JsonParser; 018import org.nuxeo.ecm.automation.client.jaxrs.spi.JsonMarshaller; 019 020/** 021 * Marshaller for the default ObjectCodec for the java Number abstract class instances. 022 * 023 * @author ogrisel 024 * @since 5.7 025 */ 026public class NumberMarshaller implements JsonMarshaller<Number> { 027 028 @Override 029 public String getType() { 030 return "number"; 031 } 032 033 @Override 034 public Class<Number> getJavaType() { 035 return Number.class; 036 } 037 038 @Override 039 public Number read(JsonParser jp) throws IOException { 040 jp.nextToken(); 041 jp.nextToken(); 042 return jp.readValueAs(Number.class); 043 } 044 045 @Override 046 public void write(JsonGenerator jg, Object value) throws IOException { 047 Number number = (Number) value; 048 if (number instanceof Double || number instanceof Float) { 049 jg.writeNumber(number.doubleValue()); 050 } else { 051 jg.writeNumber(number.longValue()); 052 } 053 } 054 055}