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

Why int value in jsonb mapped to float ? #113

Open
abcfy2 opened this issue May 9, 2018 · 5 comments
Open

Why int value in jsonb mapped to float ? #113

abcfy2 opened this issue May 9, 2018 · 5 comments

Comments

@abcfy2
Copy link

abcfy2 commented May 9, 2018

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
@abcfy2 abcfy2 changed the title Why int value in jsonb mapped to double ? Why int value in jsonb mapped to float ? May 9, 2018
@ilopmar
Copy link
Collaborator

ilopmar commented May 9, 2018

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

@abcfy2
Copy link
Author

abcfy2 commented May 9, 2018

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
Copy link
Author

abcfy2 commented May 12, 2018

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
Copy link

dfrt82 commented Mar 30, 2022

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

@krishnact
Copy link

It looks like a problem with GSON. Please see google/gson#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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants