[CXX-224] BSONArray jsonString() always prints as object Created: 11/May/14  Updated: 08/Jul/14  Resolved: 08/Jul/14

Status: Closed
Project: C++ Driver
Component/s: BSON
Affects Version/s: legacy-0.0-26compat-2.6.0, legacy-0.8.0
Fix Version/s: legacy-0.11.0

Type: Improvement Priority: Major - P3
Reporter: Itay Neeman Assignee: Tyler Brock
Resolution: Done Votes: 0
Labels: legacy-cxx
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on CXX-264 Merge upstream changes for SERVER-14357 Closed

 Description   

The BSONArray type does not re-implement the jsonString() function, so it defaults to the BSONObj one. This means that arrays are always printed as:

{ "0": 1, "1": "abc"}

rather than using square brackets, etc. We've implemented a custom function, which is here below:

static std::string arrayJSONString(const mongo::BSONArray& array, mongo::JsonStringFormat format = mongo::Strict, int pretty = 0 ) {
 
    if ( array.isEmpty() ) return "[]";
 
    mongo::StringBuilder s;
    s << "[ ";
    mongo::BSONObjIterator i(array);
    mongo::BSONElement e = i.next();
    if ( !e.eoo() )
        while ( 1 ) {
            s << e.jsonString( format, false, pretty?pretty+1:0 );
            e = i.next();
            if ( e.eoo() )
                break;
            s << ",";
            if ( pretty ) {
                s << '\n';
                for( int x = 0; x < pretty; x++ )
                    s << "  ";
            }
            else {
                s << " ";
            }
        }
    s << " ]";
 
    return s.str();
}

It's basically identical with a couple of very small changes. I'm happy to submit a PR to get this fixed if it is agreeable.



 Comments   
Comment by Tyler Brock [ 26/Jun/14 ]

Resolving for now, let me know if you have any issues and we can reopen.

Comment by Tyler Brock [ 25/Jun/14 ]

Ok, on the legacy branch we just merged upstream changes that will make this work correctly.

Can you confirm that you can now print arrays using the mongo::tojson(<BSONArray>) method?

Comment by Tyler Brock [ 19/Jun/14 ]

Moving back to "In Progress" as we are working on an upstream solution.

Comment by Tyler Brock [ 18/Jun/14 ]

https://github.com/mongodb/mongo-cxx-driver/pull/104

Comment by Itay Neeman [ 18/Jun/14 ]

Yes, exactly. There is no way to format an outermost array as a proper array - it will always output as an object with string indices.

Comment by Tyler Brock [ 18/Jun/14 ]

Hi Itay,

It looks like this is only an issue when the top level is an array. Can you confirm that is the case?

Code

        BSONArrayBuilder builder;
        builder.append(1);
        builder.append("abc");
 
        BSONObj obj = BSON("a" << builder.arr());
        std::cout << obj.jsonString() << std::endl;
 
        c.save(TEST_NS, obj);
 
        std::cout << c.findOne(TEST_NS, Query("{}")).jsonString() << std::endl;

Output

{ "a" : [ 1, "abc" ] }
{ "_id" : { "$oid" : "53a1dd6c9be4a2d5e343712f" }, "a" : [ 1, "abc" ] }

Generated at Wed Feb 07 21:58:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.