Skip to content

Commit

Permalink
Fixed #64
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 5, 2017
1 parent 5325ba0 commit 6ab11a8
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, Typ
* Method called to map a JSON Object into a Java value.
*/
// Would we just be better off deferring to the Map<Object,Object> deserializer?
@Override
protected Object mapObject(JsonParser p, DeserializationContext ctxt) throws IOException {
Object key1;
JsonToken t = p.getCurrentToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public Schema getSchema() {
}
}

@SuppressWarnings("unchecked")
public T read(Object reuse, Decoder in) throws IOException {
try {
return (T) READ.invoke(encoding, reuse, in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ protected Scalar(AvroReadContext parent,
_scalarDecoder = sd;
}

@Override
public long getRemainingElements() {
return _count - _index;
}
Expand Down Expand Up @@ -185,6 +186,7 @@ public NonScalar(AvroReadContext parent,
_structureReader = reader;
}

@Override
public long getRemainingElements() {
return _count - _index;
}
Expand All @@ -193,6 +195,7 @@ public long getRemainingElements() {
public MapReader newReader(AvroReadContext parent, AvroParserImpl parser) {
return new NonScalar(parent, parser, _structureReader, _typeId, _keyTypeId);
}

@Override
public JsonToken nextToken() throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public abstract class AvroSchemaHelper
*
* @since 2.8.8
*/
public static final String AVRO_SCHEMA_PROP_ELEMENT_CLASS = SpecificData.ELEMENT_PROP;
public static final String AVRO_SCHEMA_PROP_ELEMENT_CLASS = SpecificData.ELEMENT_PROP;
/**
* Default stringable classes
*
Expand All @@ -61,7 +61,8 @@ public abstract class AvroSchemaHelper
));

/**
* Checks if a given type is "Stringable", that is one of the default {@link #STRINGABLE_CLASSES}, is an {@code Enum},
* Checks if a given type is "Stringable", that is one of the default
* {@link #STRINGABLE_CLASSES}, is an {@code Enum},
* or is annotated with
* {@link Stringable @Stringable} and has a constructor that takes a single string argument capable of deserializing the output of its
* {@code toString()} method.
Expand Down Expand Up @@ -89,7 +90,7 @@ public static boolean isStringable(AnnotatedClass type) {
protected static String getNamespace(JavaType type) {
Class<?> cls = type.getRawClass();
// 16-Feb-2017, tatu: Fixed as suggested by `baharclerode@github`;
// NOTE: was revert in 2.8.8, but is enabled for Jackson 2.9.
// NOTE: was reverted in 2.8.8, but is enabled for Jackson 2.9.
Class<?> enclosing = cls.getEnclosingClass();
if (enclosing != null) {
return enclosing.getName() + "$";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public CustomEncodingDatum(CustomEncodingWrapper<T> encoding, T datum) {
this._datum = datum;
}

@Override
public void write(Encoder encoder) throws IOException {
_encoding.write(_datum, encoder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1596,9 +1596,8 @@ public Number getNumberValue() throws IOException
return _numberBigDecimal;
}

/* And then floating point types. But here optimal type
* needs to be big decimal, to avoid losing any data?
*/
// And then floating point types. But here optimal type
// needs to be big decimal, to avoid losing any data?
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
return _numberBigDecimal;
}
Expand Down Expand Up @@ -1897,14 +1896,6 @@ protected void convertNumberToBigDecimal() throws IOException
_numTypesValid |= NR_BIGDECIMAL;
}

protected void reportOverflowInt() throws IOException {
_reportError("Numeric value ("+getText()+") out of range of int ("+Integer.MIN_VALUE+" - "+Integer.MAX_VALUE+")");
}

protected void reportOverflowLong() throws IOException {
_reportError("Numeric value ("+getText()+") out of range of long ("+Long.MIN_VALUE+" - "+Long.MAX_VALUE+")");
}

/*
/**********************************************************
/* Internal methods, secondary parsing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ public class ProtobufParser extends ParserMinimalBase

// And then floating point types

final protected static int NR_DOUBLE = 0x008;
final protected static int NR_BIGDECIMAL = 0x0010;
final protected static int NR_FLOAT = 0x008;
final protected static int NR_DOUBLE = 0x010;
final protected static int NR_BIGDECIMAL = 0x0020;

// Also, we need some numeric constants

Expand All @@ -298,7 +299,7 @@ public class ProtobufParser extends ParserMinimalBase

final static double MIN_INT_D = (double) Integer.MIN_VALUE;
final static double MAX_INT_D = (double) Integer.MAX_VALUE;

/**
* Bitfield that indicates which numeric representations
* have been calculated for the current type
Expand All @@ -308,6 +309,8 @@ public class ProtobufParser extends ParserMinimalBase
// First primitives

protected int _numberInt;
protected float _numberFloat;

protected long _numberLong;
protected double _numberDouble;

Expand Down Expand Up @@ -802,8 +805,8 @@ private JsonToken _readNextValue(FieldType t, int nextState) throws IOException
type = JsonToken.VALUE_NUMBER_FLOAT;
break;
case FLOAT:
_numberDouble = (double) Float.intBitsToFloat(_decode32Bits());
_numTypesValid = NR_DOUBLE;
_numberFloat = Float.intBitsToFloat(_decode32Bits());
_numTypesValid = NR_FLOAT;
type = JsonToken.VALUE_NUMBER_FLOAT;
break;
case VINT32_Z:
Expand Down Expand Up @@ -1478,7 +1481,23 @@ public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IO
/* Numeric accessors of public API
/**********************************************************
*/


@Override // since 2.9
public boolean isNaN() {
if (_currToken == JsonToken.VALUE_NUMBER_FLOAT) {
if ((_numTypesValid & NR_DOUBLE) != 0) {
// 10-Mar-2017, tatu: Alas, `Double.isFinite(d)` only added in JDK 8
double d = _numberDouble;
return Double.isNaN(d) || Double.isInfinite(d);
}
if ((_numTypesValid & NR_FLOAT) != 0) {
float f = _numberFloat;
return Float.isNaN(f) || Float.isInfinite(f);
}
}
return false;
}

@Override
public Number getNumberValue() throws IOException
{
Expand All @@ -1499,17 +1518,19 @@ public Number getNumberValue() throws IOException
// Shouldn't get this far but if we do
return _numberBigDecimal;
}

/* And then floating point types. But here optimal type
* needs to be big decimal, to avoid losing any data?
*/

// And then floating point types. But here optimal type
// needs to be big decimal, to avoid losing any data?
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
return _numberBigDecimal;
}
if ((_numTypesValid & NR_DOUBLE) == 0) { // sanity check
if ((_numTypesValid & NR_DOUBLE) != 0) {
return _numberDouble;
}
if ((_numTypesValid & NR_FLOAT) == 0) { // sanity check
_throwInternal();
}
return _numberDouble;
return _numberFloat;
}

@Override
Expand Down Expand Up @@ -1537,9 +1558,12 @@ public NumberType getNumberType() throws IOException
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
return NumberType.BIG_DECIMAL;
}
return NumberType.DOUBLE;
if ((_numTypesValid & NR_DOUBLE) != 0) {
return NumberType.DOUBLE;
}
return NumberType.FLOAT;
}

@Override
public int getIntValue() throws IOException
{
Expand Down Expand Up @@ -1585,16 +1609,21 @@ public BigInteger getBigIntegerValue() throws IOException
@Override
public float getFloatValue() throws IOException
{
double value = getDoubleValue();
/* 22-Jan-2009, tatu: Bounds/range checks would be tricky
* here, so let's not bother even trying...
*/
if ((_numTypesValid & NR_FLOAT) == 0) {
if (_numTypesValid == NR_UNKNOWN) {
_checkNumericValue(NR_FLOAT);
}
if ((_numTypesValid & NR_FLOAT) == 0) {
convertNumberToFloat();
}
}
// Bounds/range checks would be tricky here, so let's not bother even trying...
/*
if (value < -Float.MAX_VALUE || value > MAX_FLOAT_D) {
_reportError("Numeric value ("+getText()+") out of range of Java float");
}
*/
return (float) value;
return _numberFloat;
}

@Override
Expand Down Expand Up @@ -1640,18 +1669,6 @@ protected void _checkNumericValue(int expType) throws IOException
_reportError("Current token ("+_currToken+") not numeric, can not use numeric value accessors");
}

@Override // since 2.9
public boolean isNaN() {
if (_currToken == JsonToken.VALUE_NUMBER_FLOAT) {
if ((_numTypesValid & NR_DOUBLE) != 0) {
// 10-Mar-2017, tatu: Alas, `Double.isFinite(d)` only added in JDK 8
double d = _numberDouble;
return Double.isNaN(d) || Double.isInfinite(d);
}
}
return false;
}

protected void convertNumberToInt() throws IOException
{
// First, converting from long ought to be easy
Expand All @@ -1674,6 +1691,11 @@ protected void convertNumberToInt() throws IOException
reportOverflowInt();
}
_numberInt = (int) _numberDouble;
} else if ((_numTypesValid & NR_FLOAT) != 0) {
if (_numberFloat < MIN_INT_D || _numberFloat > MAX_INT_D) {
reportOverflowInt();
}
_numberInt = (int) _numberFloat;
} else if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
if (BD_MIN_INT.compareTo(_numberBigDecimal) > 0
|| BD_MAX_INT.compareTo(_numberBigDecimal) < 0) {
Expand All @@ -1697,11 +1719,15 @@ protected void convertNumberToLong() throws IOException
}
_numberLong = _numberBigInt.longValue();
} else if ((_numTypesValid & NR_DOUBLE) != 0) {
// Need to check boundaries
if (_numberDouble < MIN_LONG_D || _numberDouble > MAX_LONG_D) {
reportOverflowLong();
}
_numberLong = (long) _numberDouble;
} else if ((_numTypesValid & NR_FLOAT) != 0) {
if (_numberFloat < MIN_LONG_D || _numberFloat > MAX_LONG_D) {
reportOverflowInt();
}
_numberLong = (long) _numberFloat;
} else if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
if (BD_MIN_LONG.compareTo(_numberBigDecimal) > 0
|| BD_MAX_LONG.compareTo(_numberBigDecimal) < 0) {
Expand All @@ -1725,18 +1751,42 @@ protected void convertNumberToBigInteger() throws IOException
_numberBigInt = BigInteger.valueOf(_numberInt);
} else if ((_numTypesValid & NR_DOUBLE) != 0) {
_numberBigInt = BigDecimal.valueOf(_numberDouble).toBigInteger();
} else if ((_numTypesValid & NR_FLOAT) != 0) {
_numberBigInt = BigDecimal.valueOf(_numberFloat).toBigInteger();
} else {
_throwInternal();
}
_numTypesValid |= NR_BIGINT;
}


protected void convertNumberToFloat() throws IOException
{
// Note: this MUST start with more accurate representations, since we don't know which
// value is the original one (others get generated when requested)
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
_numberFloat = _numberBigDecimal.floatValue();
} else if ((_numTypesValid & NR_BIGINT) != 0) {
_numberFloat = _numberBigInt.floatValue();
} else if ((_numTypesValid & NR_DOUBLE) != 0) {
_numberFloat = (float) _numberDouble;
} else if ((_numTypesValid & NR_LONG) != 0) {
_numberFloat = (float) _numberLong;
} else if ((_numTypesValid & NR_INT) != 0) {
_numberFloat = (float) _numberInt;
} else {
_throwInternal();
}
_numTypesValid |= NR_FLOAT;
}

protected void convertNumberToDouble() throws IOException
{
// Note: this MUST start with more accurate representations, since we don't know which
// value is the original one (others get generated when requested)
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
_numberDouble = _numberBigDecimal.doubleValue();
} else if ((_numTypesValid & NR_FLOAT) != 0) {
_numberDouble = (double) _numberFloat;
} else if ((_numTypesValid & NR_BIGINT) != 0) {
_numberDouble = _numberBigInt.doubleValue();
} else if ((_numTypesValid & NR_LONG) != 0) {
Expand All @@ -1753,7 +1803,7 @@ protected void convertNumberToBigDecimal() throws IOException
{
// Note: this MUST start with more accurate representations, since we don't know which
// value is the original one (others get generated when requested)
if ((_numTypesValid & NR_DOUBLE) != 0) {
if ((_numTypesValid & (NR_DOUBLE | NR_FLOAT)) != 0) {
// Let's parse from String representation, to avoid rounding errors that
//non-decimal floating operations would incur
_numberBigDecimal = NumberInput.parseBigDecimal(getText());
Expand Down Expand Up @@ -2453,16 +2503,6 @@ protected final static long _long(int i1, int i2)
/**********************************************************
*/

protected void reportOverflowInt() throws IOException {
_reportErrorF("Numeric value (%s) out of range of int (%d - %d)",
getText(), Integer.MIN_VALUE, Integer.MAX_VALUE);
}

protected void reportOverflowLong() throws IOException {
_reportErrorF("Numeric value (%s) out of range of long (%d - %d)",
getText(), Long.MIN_VALUE, Long.MAX_VALUE);
}

private void _reportErrorF(String format, Object... args) throws JsonParseException {
_reportError(String.format(format, args));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader;
import com.fasterxml.jackson.dataformat.protobuf.testutil.LimitingInputStream;

public class ReadComplexPojoTest extends ProtobufTestBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.dataformat.protobuf.schemagen;
package com.fasterxml.jackson.dataformat.protobuf.schema;

import static org.junit.Assert.assertArrayEquals;

Expand All @@ -14,6 +14,7 @@
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufMessage;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader;
import com.fasterxml.jackson.dataformat.protobuf.schemagen.ProtobufSchemaGenerator;

public class SchemaGenTest extends ProtobufTestBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.fasterxml.jackson.dataformat.protobuf;
package com.fasterxml.jackson.dataformat.protobuf.schema;

import java.util.List;

import com.fasterxml.jackson.dataformat.protobuf.ProtobufTestBase;
import com.fasterxml.jackson.dataformat.protobuf.schema.NativeProtobufSchema;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufField;
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.dataformat.protobuf.schemagen;
package com.fasterxml.jackson.dataformat.protobuf.schema;

import java.nio.ByteBuffer;
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.dataformat.protobuf;
package com.fasterxml.jackson.dataformat.protobuf.testutil;

import java.io.*;
import java.util.Random;
Expand Down
Loading

0 comments on commit 6ab11a8

Please sign in to comment.