Why Jackson allows to Byte value more than MAX_BYTE

7 views
Skip to first unread message

Vladislav Bochenin

unread,
May 5, 2020, 7:10:42 PM5/5/20
to jackson-user
I'm receiving invalid value for `Byte` field during deserialization.

Json:

    {"byteField" : 128 }

Java:

    class Dto {
         private Byte byteField;
         ... getter/setter
    }

After deserialization I'm receiving `Dto.byteField` = -128

I found next code in Jackon-core library v.2.11.0 in `com.fasterxml.jackson.core.JsonParser#getByteValue`
    
    public byte getByteValue() throws IOException {
        int value = getIntValue();
        // So far so good: but does it fit?
        // [JACKSON-804]: Let's actually allow range of [-128, 255], as those are uniquely mapped
        //  (instead of just signed range of [-128, 127])
        if (value < MIN_BYTE_I || value > MAX_BYTE_I) {
            throw new InputCoercionException(this,
                    String.format("Numeric value (%s) out of range of Java byte", getText()),
                    JsonToken.VALUE_NUMBER_INT, Byte.TYPE);
        }
        return (byte) value;
    }


but not able to find issue JACKSON-804 as well as any discussion about byte range.

Question:

- Why Jackson allows to byte be more than 127? 

Tatu Saloranta

unread,
May 5, 2020, 7:12:49 PM5/5/20
to jackson-user
This is because byte values are often sent as unsigned (but Java does
not have matching concepts).
So upper limit is 255, instead of 127. Changed was to for better
interoperability with other systems, although
discussion under [JACKSON-804] is unfortunately no longer available --
this referred to old Codehaus issue tracker,
for Jackson 1.x.

-+ Tatu +-
> --
> 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 jackson-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/3f4b00d6-8c96-488e-8958-9fd1b18b57a7%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages