com.github.nmorel.gwtjackson.client.deser.BaseNumberJsonDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gwt-jackson Show documentation
Show all versions of gwt-jackson Show documentation
gwt-jackson is a GWT JSON serializer/deserializer mechanism based on Jackson annotations
package com.github.nmorel.gwtjackson.client.deser;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import com.github.nmorel.gwtjackson.client.JsonDeserializationContext;
import com.github.nmorel.gwtjackson.client.JsonDeserializer;
import com.github.nmorel.gwtjackson.client.stream.JsonReader;
import com.github.nmorel.gwtjackson.client.stream.JsonToken;
/**
* Base implementation of {@link JsonDeserializer} for {@link Number}.
*
* @author Nicolas Morel
*/
public abstract class BaseNumberJsonDeserializer extends JsonDeserializer {
/**
* Default implementation of {@link BaseNumberJsonDeserializer} for {@link BigDecimal}
*/
public static final class BigDecimalJsonDeserializer extends BaseNumberJsonDeserializer {
private static final BigDecimalJsonDeserializer INSTANCE = new BigDecimalJsonDeserializer();
/**
* @return an instance of {@link BigDecimalJsonDeserializer}
*/
public static BigDecimalJsonDeserializer getInstance() {
return INSTANCE;
}
private BigDecimalJsonDeserializer() { }
@Override
protected BigDecimal doDeserialize( JsonReader reader, JsonDeserializationContext ctx ) throws IOException {
return new BigDecimal( reader.nextString() );
}
}
/**
* Default implementation of {@link BaseNumberJsonDeserializer} for {@link BigInteger}
*/
public static final class BigIntegerJsonDeserializer extends BaseNumberJsonDeserializer {
private static final BigIntegerJsonDeserializer INSTANCE = new BigIntegerJsonDeserializer();
/**
* @return an instance of {@link BigIntegerJsonDeserializer}
*/
public static BigIntegerJsonDeserializer getInstance() {
return INSTANCE;
}
private BigIntegerJsonDeserializer() { }
@Override
protected BigInteger doDeserialize( JsonReader reader, JsonDeserializationContext ctx ) throws IOException {
return new BigInteger( reader.nextString() );
}
}
/**
* Default implementation of {@link BaseNumberJsonDeserializer} for {@link Byte}
*/
public static final class ByteJsonDeserializer extends BaseNumberJsonDeserializer {
private static final ByteJsonDeserializer INSTANCE = new ByteJsonDeserializer();
/**
* @return an instance of {@link ByteJsonDeserializer}
*/
public static ByteJsonDeserializer getInstance() {
return INSTANCE;
}
private ByteJsonDeserializer() { }
@Override
protected Byte doDeserialize( JsonReader reader, JsonDeserializationContext ctx ) throws IOException {
return (byte) reader.nextInt();
}
}
/**
* Default implementation of {@link BaseNumberJsonDeserializer} for {@link Double}
*/
public static final class DoubleJsonDeserializer extends BaseNumberJsonDeserializer {
private static final DoubleJsonDeserializer INSTANCE = new DoubleJsonDeserializer();
/**
* @return an instance of {@link DoubleJsonDeserializer}
*/
public static DoubleJsonDeserializer getInstance() {
return INSTANCE;
}
private DoubleJsonDeserializer() { }
@Override
protected Double doDeserialize( JsonReader reader, JsonDeserializationContext ctx ) throws IOException {
return reader.nextDouble();
}
}
/**
* Default implementation of {@link BaseNumberJsonDeserializer} for {@link Float}
*/
public static final class FloatJsonDeserializer extends BaseNumberJsonDeserializer {
private static final FloatJsonDeserializer INSTANCE = new FloatJsonDeserializer();
/**
* @return an instance of {@link FloatJsonDeserializer}
*/
public static FloatJsonDeserializer getInstance() {
return INSTANCE;
}
private FloatJsonDeserializer() { }
@Override
protected Float doDeserialize( JsonReader reader, JsonDeserializationContext ctx ) throws IOException {
return Float.parseFloat( reader.nextString() );
}
}
/**
* Default implementation of {@link BaseNumberJsonDeserializer} for {@link Integer}
*/
public static final class IntegerJsonDeserializer extends BaseNumberJsonDeserializer {
private static final IntegerJsonDeserializer INSTANCE = new IntegerJsonDeserializer();
/**
* @return an instance of {@link IntegerJsonDeserializer}
*/
public static IntegerJsonDeserializer getInstance() {
return INSTANCE;
}
private IntegerJsonDeserializer() { }
@Override
protected Integer doDeserialize( JsonReader reader, JsonDeserializationContext ctx ) throws IOException {
if ( JsonToken.NUMBER.equals( reader.peek() ) ) {
return reader.nextInt();
} else {
return Integer.parseInt( reader.nextString() );
}
}
}
/**
* Default implementation of {@link BaseNumberJsonDeserializer} for {@link Long}
*/
public static final class LongJsonDeserializer extends BaseNumberJsonDeserializer {
private static final LongJsonDeserializer INSTANCE = new LongJsonDeserializer();
/**
* @return an instance of {@link LongJsonDeserializer}
*/
public static LongJsonDeserializer getInstance() {
return INSTANCE;
}
private LongJsonDeserializer() { }
@Override
protected Long doDeserialize( JsonReader reader, JsonDeserializationContext ctx ) throws IOException {
return reader.nextLong();
}
}
/**
* Default implementation of {@link BaseNumberJsonDeserializer} for {@link Short}
*/
public static final class ShortJsonDeserializer extends BaseNumberJsonDeserializer {
private static final ShortJsonDeserializer INSTANCE = new ShortJsonDeserializer();
/**
* @return an instance of {@link ShortJsonDeserializer}
*/
public static ShortJsonDeserializer getInstance() {
return INSTANCE;
}
private ShortJsonDeserializer() { }
@Override
protected Short doDeserialize( JsonReader reader, JsonDeserializationContext ctx ) throws IOException {
if ( JsonToken.NUMBER.equals( reader.peek() ) ) {
return (short) reader.nextInt();
} else {
return Short.parseShort( reader.nextString() );
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy