kaleidos / grails-postgresql-extensions

Grails plugin to use postgresql native elements such as arrays, hstores,...
Apache License 2.0
78 stars 63 forks source link

Why int value in jsonb mapped to float ? #113

Open abcfy2 opened 6 years ago

abcfy2 commented 6 years ago

I use 5.2.0 in grails 3.3.3.

Here is my jsonb velue in database:

{
    "orderId": 890
}

But when I get from domain, I find it has been converted to float, why ?

Domain domain = Domain.first()
domain.params.orderId  // print 890.0
ilopmar commented 6 years ago

Is the field orderId defined as int or Integer in the domain class?

abcfy2 commented 6 years ago

No, the params is a Map which type is JsonbMapType

class Domain {
    Map params

    static mapping = {
         params type: JsonbMapType
    }
}

And seems that save() works very well.

Domain domain = new Domain()
domain.params = [orderId: 809]
domain.save()

And in postgresql this column is :

{"orderId": 890}

But when I read it from domain ,the int value is float:

Domain doman = Domain.first()
domain.params.orderId  // it's 890.0
abcfy2 commented 6 years ago

New discovery, I find sometimes int value saved to jsonb will convert to float.

Here is my SQL:

> SELECT operation_params ->> 'orderId' FROM domain;
 314
 354
 7.0
 247.0
 250
 251
 252.0
 244
 227.0
 248.0
 316
 249.0
 254
 319
 357
 291.0
 317.0
(361 rows)

But in fact all id should be int.

domain.operationParams = [orderId: id]  // id is int
domain.save()
dfrt82 commented 2 years ago

I second that. Very annoying, took me a while to track that down...

krishnact commented 1 year ago

It looks like a problem with GSON. Please see https://github.com/google/gson/pull/1290 The class net.kaleidos.hibernate.usertype.JsonMapType needs to be changed on line 24 from: private final Gson gson = new GsonBuilder().serializeNulls().create() to Gson gson = new GsonBuilder().serializeNulls().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();