Hello,
I'm new to Jackson and using it to parse some JSON using Jackson annotated
Java classes. The resultant tree is passed into another API that performs
conversion to Avro objects subject to an Avro schema. In the POJO class I
have a float field that I'm trying to map to and Avro Schema.Type.FLOAT.
However. I'm finding that the Avro conversion code is tripping up as it is
expecting the relevant node to return true from isFloat(). It turns out
that the node representing the field is of type DoubleNode, not FloatNode,
as I would expect. Consequently the call to node.isFloat() returns false.
Note that in this particular instance, the value of this field is well
within the range of a Java float. I believe I have traced this behaviour
back to
com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer._fromFloat(...)
:
protected final JsonNode _fromFloat(JsonParser p,
DeserializationContext ctxt,
final JsonNodeFactory nodeFactory) throws IOException
{
JsonParser.NumberType nt = p.getNumberType();
if (nt == JsonParser.NumberType.BIG_DECIMAL
||
ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
return nodeFactory.numberNode(p.getDecimalValue());
}
return nodeFactory.numberNode(p.getDoubleValue());
}
The last call to the nodeFactory returns a DoubleNode not a FloatNode. I
wonder if this should instead be implemented like so:
protected final JsonNode _fromFloat(JsonParser p,
DeserializationContext ctxt,
final JsonNodeFactory nodeFactory) throws IOException
{
JsonParser.NumberType nt = p.getNumberType();
if (nt == JsonParser.NumberType.BIG_DECIMAL
||
ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
return nodeFactory.numberNode(p.getDecimalValue());
}
if (nt == JsonParser.NumberType.DOUBLE) {
return nodeFactory.numberNode(p.getDoubleValue());
}
return nodeFactory.numberNode(p.getFloatValue());
}
Currently I am using this less than ideal work around in my Avro conversion
code:
if (node.isDouble() && node.doubleValue() >= -Float.MAX_VALUE &&
node.doubleValue() <= Float.MAX_VALUE || node.isFloat()) {
return datum.floatValue();
Can anyone offer some insight on on the current implementation? Is there
something I've overlooked or misunderstood regarding JSON/Jackson float
handling or interpretation?
Thanks,
Elliot.
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.