[JAVA-698] bson can't serialize class java.math.BigInteger Created: 20/Nov/12 Updated: 19/Oct/16 Resolved: 21/Nov/12 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | None |
| Affects Version/s: | 2.9.1 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Blocker - P1 |
| Reporter: | Gina Chen | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | bson | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Red Hat Enterprise Linux Client release 5.8 (Tikanga) |
||
| Description |
|
While doing some load test, I got this exception from mongodb.
Our script generated the same data with different "_id", "id" and "created" and most of them were inserted into database fine. This exception only happened sporadically. |
| Comments |
| Comment by Jochen Kemnade [ 26/Jun/15 ] | ||||||||
|
Oh, right, now I see the problem. Of course, there is a problem if you use a BigInteger in a map-like structure. In my case, I have something like a POJO codec (based on org.apache.tapestry:beanmodel:5.4-beta-26) and I have type information. That's why I thought it would be easy. But I thought of | ||||||||
| Comment by Jeffrey Yemin [ 26/Jun/15 ] | ||||||||
|
Hi Jochen, You can write a BigIntegerCodec, which will allow big integers to be encoded as part of documents, e.g.
The problem is how to turn that BSON string back into a BigInteger on the way out. Presumably not all of your BSON strings will be big integers, so you can't decode all BSON strings to BigInteger. So when you read that document back out, you'll get a Document that looks like this:
Where a BigIntegerCodec becomes more useful is when it's combine with a POJO Codec of some sort. If you have a class like:
Now we have type information for each field, and can choose the appropriate Codec when decoding, based on the nominal type of each field. We plan to expose a generic POJO Codec in a future release of the driver, but for now you would have to roll your own. | ||||||||
| Comment by Jochen Kemnade [ 26/Jun/15 ] | ||||||||
|
I'm currently facing the same issue with the 3.0 (Cocec-based) API. If I am not mistaken, it shouldn't be a problem to provide a Codec that serializes to a BSON string. | ||||||||
| Comment by Jeffrey Yemin [ 21/Nov/12 ] | ||||||||
|
Gina, This is expected behavior. Serializing BigInteger is problematic because there is no way to round-trip it. Because they can be of arbitrary size, the driver can't encode them as numbers, so they would have to be encoded as strings. But then when the driver decodes them, it wouldn't know to turn them back into BigInteger. Because of problems like this, in general the driver only supports serialization of a fixed set of types that match what's specified in BSON. |