Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with BigInteger handling #31

Merged
merged 3 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonParser.NumberType;
import com.fasterxml.jackson.core.JsonToken;

import com.fasterxml.jackson.databind.DeserializationContext;
Expand Down Expand Up @@ -115,11 +114,7 @@ protected JsonObject _deserializeObject(JsonParser p, DeserializationContext ctx
b.addNull(name);
break;
case VALUE_NUMBER_FLOAT:
if (p.getNumberType() == NumberType.BIG_DECIMAL) {
b.add(name, p.getDecimalValue());
} else {
b.add(name, p.getDoubleValue());
}
b.add(name, p.getDecimalValue());
break;
case VALUE_NUMBER_INT:
// very cumbersome... but has to be done
Expand Down Expand Up @@ -177,11 +172,7 @@ protected JsonArray _deserializeArray(JsonParser p, DeserializationContext ctxt)
b.addNull();
break;
case VALUE_NUMBER_FLOAT:
if (p.getNumberType() == NumberType.BIG_DECIMAL) {
b.add(p.getDecimalValue());
} else {
b.add(p.getDoubleValue());
}
b.add(p.getDecimalValue());
break;
case VALUE_NUMBER_INT:
// very cumbersome... but has to be done
Expand Down Expand Up @@ -224,13 +215,10 @@ protected JsonValue _deserializeScalar(JsonParser p, DeserializationContext ctxt
// very cumbersome... but has to be done
{
JsonArrayBuilder b = _builderFactory.createArrayBuilder();
if (p.getNumberType() == NumberType.BIG_DECIMAL) {
return b.add(p.getDecimalValue()).build().get(0);
}
return b.add(p.getDoubleValue()).build().get(0);
return b.add(p.getDecimalValue()).build().get(0);
}
case VALUE_NUMBER_INT:
// very cumbersome... but has to be done
case VALUE_NUMBER_INT:
// very cumbersome... but has to be done
{
JsonArrayBuilder b = _builderFactory.createArrayBuilder();
switch (p.getNumberType()) {
Expand All @@ -242,16 +230,16 @@ protected JsonValue _deserializeScalar(JsonParser p, DeserializationContext ctxt
return b.add(p.getBigIntegerValue()).build().get(0);
}
}
case VALUE_STRING:
return _builderFactory.createArrayBuilder().add(p.getText()).build().get(0);
default: // errors, should never get here
case VALUE_STRING:
return _builderFactory.createArrayBuilder().add(p.getText()).build().get(0);
default: // errors, should never get here
// case END_ARRAY:
// case END_OBJECT:
// case FIELD_NAME:
// case NOT_AVAILABLE:
// case START_ARRAY:
// case START_OBJECT:
return (JsonValue) ctxt.handleUnexpectedToken(getValueType(ctxt), p);
return (JsonValue) ctxt.handleUnexpectedToken(getValueType(ctxt), p);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.beans.ConstructorProperties;
import java.math.BigDecimal;

public class JsonValueDeserializationTest extends TestBase
{
Expand Down Expand Up @@ -134,4 +135,23 @@ public void testConstructorProperties() throws Exception
assertNull(ob2.obj1);
assertSame(JsonValue.NULL, ob2.obj2);
}

public void testBigInteger() throws Exception
{
final String JSON = "[2e308]";
JsonValue v = MAPPER.readValue(JSON, JsonValue.class);
assertTrue(v instanceof JsonArray);
JsonArray a = (JsonArray) v;
assertEquals(1, a.size());
assertTrue(a.get(0) instanceof JsonNumber);
assertEquals(new BigDecimal("2e308").toBigInteger(), ((JsonNumber) a.get(0)).bigIntegerValue());


// also, should work with explicit type
JsonArray array = MAPPER.readValue(JSON, JsonArray.class);
assertEquals(1, array.size());

// and round-tripping ought to be ok:
assertEquals("[2E+308]", serializeAsString(v));
}
}