[JAVA-1280] In JsonWriter, support writing BSON Int64 values as JSON numbers rather than $numberLong Created: 16/Jun/14  Updated: 09/Mar/17  Resolved: 09/Mar/17

Status: Closed
Project: Java Driver
Component/s: JSON
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Ross Lawley Assignee: Unassigned
Resolution: Won't Fix Votes: 3
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to JAVA-1772 Make it easier to change JsonWriter b... Closed
is related to SERVER-6812 Extended JSON syntax should support a... Closed

 Description   

MongoDB 2.6 introduced support for the NumberLong data type in MongoDB Extended JSON using $numberLong.

In strict mode, JsonWriter will write all Int64 values using $numberLong. But some applications will prefer to lose the type information and just writes them as JSON numbers.



 Comments   
Comment by Jeffrey Yemin [ 09/Mar/17 ]

Closing as won't fix, since JAVA-1772 allows applications to achieve the same effect.

Comment by Jeffrey Yemin [ 09/Mar/17 ]

In scope of JAVA-1772, you can add a custom converter for BSON Int64 values (or any other type). This code, for example:

        JsonWriterSettings settings = JsonWriterSettings.builder()
                                                 .int64Converter((value, writer) -> writer.writeNumber(value.toString()))
                                                 .build();
        String json = new Document("a", 12).append("b", Long.MAX_VALUE).toJson(settings);

will output:

{ "a" : 12, "b" : 9223372036854775807 }

Comment by Jeffrey Yemin [ 03/Jan/17 ]

We plan to address this, probably in scope of JAVA-1772.

Comment by guillaume dufour [ 03/Jan/17 ]

ok, so you can close it as unresolved ;o(

Comment by Michael Gagnon [ 31/Aug/16 ]

This current functionality (among other things) breaks the upgrade path in Scala when moving from Casbah driver - which unfortunately is a hard requirement if one intends to migrate beyond Mongo 3.0. Was using toJson on documents fetched as input to a JSON => case class deserializer, and this format introduces unconsidered expectations which breaks that. My only current recourse seems to be sanitizing the document first by converting bson longs to bson strings or to otherwise hand-roll deserialization logic for all document types in my system. It's a fairly large increase in complexity

Comment by guillaume dufour [ 31/Mar/16 ]

I try a PR on this
https://github.com/mongodb/mongo-java-driver/pull/346
Hope it can help.

Comment by Jeffrey Yemin [ 19/Jun/14 ]

I think that the driver should punt on this until SERVER-6812 is addressed, in particular the part about

mongoexport should have a flag for whether to represent integers in JSON syntax (lossy, but backward compatible and better for interoperability with other JSON tools), or in Extended JSON syntax.

and once those flag(s) are defined the driver can expose corresponding flags.

Generated at Thu Feb 08 08:54:13 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.