Invalid conversion from BigDecimal Map to JSON. #111

Open
fansgit opened this Issue Nov 8, 2012 · 5 comments

Projects

None yet

3 participants

fansgit commented Nov 8, 2012

Code snippet:

Map<BigDecimal, BigDecimal> map = new HashMap<BigDecimal, BigDecimal>();
map.put(new BigDecimal(1), new BigDecimal(101));
myService.doJob(map, myCallback);

Output:

[INFO] Sending http request: GET http://127.0.0.1:8888/rest/maptest/ ,timeout:0
{"\"1\"":"101"}

Expected encoding:

{"1":101}
Owner

thanx for reporting - number of test cases is growing . . .

@mkristian mkristian closed this Nov 18, 2012
fansgit commented Nov 20, 2012

Thanks for the bug-fix. Retest is done with the master branch below. It seems to be partially fixed - BigDecimal key's problem is gone but the value's remains.

$ git show --summary

commit a76475a080b42c84e12c0e534d7c5959b42b86ba
Author: Kristian Meier <m.kristian@web.de>
Date:   Sun Nov 18 16:18:01 2012 +0530

    fix encoding of BigDecimal keys of maps (#111)

Output:

[INFO] Sending http request: GET http://127.0.0.1:8888/rest/maptest/ ,timeout:0
{"1":"101"}

Expected encoding:

{"1":101}
fansgit commented Nov 20, 2012

BTW, this issue is in status of closed, how to re-open it?

@mkristian mkristian reopened this Nov 20, 2012
Owner

yes, that part slipt since it is the same encoding we use for BigDecimal anywhere else.

if I remove the quotes for BigDecimal then the conversion looks like this

        public JSONValue encode(BigDecimal value) throws EncodingException {
            return (value == null) ? getNullType() : new JSONNumber(value.toDoubleValue());
        }

this can loose precision - and I think that is the reason we have the "string" encoding. maybe some has an idea how to produce a JSONNumber with preserving BigDecimal ?

mdopp commented Jan 6, 2015

Current state is even worse, as it will encode always as JSONString, but decode only from JSONumber. This is a inconsequent behaviour, as RestyGWT would never be able to read its own JSON data.
So either the system must encode always to number, or at least try to parse a string value to number.

@Override
public BigDecimal decode(JSONValue value) throws DecodingException {
    if (value == null || value.isNull() != null) {
        return null;
    }
    return toBigDecimal(value);
}
static public BigDecimal toBigDecimal(JSONValue value) {
        JSONNumber number = value.isNumber();
        if (number == null) {
            throw new DecodingException("Expected a json number, but was given: " + value);
        }
        return new BigDecimal(value.toString());
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment