integers deserialized as Doubles from JSON collection

34 views
Skip to first unread message

Val Waeselynck

unread,
Apr 20, 2014, 8:08:24 AM4/20/14
to clojure...@googlegroups.com
Hi all,

I'm working with a MongoDb database that has JSON collections.

One of the fields of the documents in these collections are 64-bits integers stored as JSON numbers. They're application-specific fields, not something generated.

When I query the DB from Mongo CLI, I get integers alright. However, when I query through Monger, these integers are deserialized as java.lang.Double objects.

I know I always have the option of coercing to a long or BigInt after deserialization, but still, this does not seem right to me.
Is there any way to correct this behavior? Should it be considered a bug?

Example where this field is "userId":

From Mongo CLI
> db.mycoll.find();
{ "_id" : ObjectId("53452c104900004b00d88287"),
 "userId" : 100001465868054 }

[...]

From Clojure console using Monger :
> (monger.collection/find-one "mycoll")
{"_id" #<ObjectId 53452c104900004b00d88287>, "userId" 1.00001465868054E14}
>(.get *1 "userId")
1.00001465868054E14
> (class *1)
java.lang.Double

Thanks in advance,

Valentin Waeselynck.
 

Michael Klishin

unread,
Apr 20, 2014, 8:21:06 AM4/20/14
to Monger, a Clojure MongoDB driver
2014-04-20 16:08 GMT+04:00 Val Waeselynck <val.v...@gmail.com>:
I'm working with a MongoDb database that has JSON collections.

One of the fields of the documents in these collections are 64-bits integers stored as JSON numbers. They're application-specific fields, not something generated.

Are you storing them from JavaScript? There is no such thing as 64 bit integers
in JavaScript, all numbers are 64 bit floating point (Double on the JVM).

Also, Monger doesn't serialize or deserialize numbers other than clojure.lang.Ratio, MongoDB Java
client does.
--
MK

http://github.com/michaelklishin
http://twitter.com/michaelklishin

Michael Klishin

unread,
Apr 20, 2014, 3:41:29 PM4/20/14
to Val Waeselynck, Monger, a Clojure MongoDB driver
2014-04-20 16:48 GMT+04:00 Val Waeselynck <val.v...@gmail.com>:
From your answer, I'm guessing what I saw is just 2 representations of the same JSON value. If that value does not change over several JSON serialization/deserialization cycles, I can rest assured.

I'd still check it from a few other languages (like JS and Python) :)

BSON spec suggests there is a 64 bit integer type but given that the original value comes from JSON
which *does not* have it (JS heritage I've mentioned above), I suspect a Double is used by the Scala
serializer to be on the safe side:

Reply all
Reply to author
Forward
0 new messages