[CXX-955] a escape sequence BUG Created: 24/Jun/16  Updated: 26/Jun/16  Resolved: 25/Jun/16

Status: Closed
Project: C++ Driver
Component/s: Release
Affects Version/s: 3.0.1
Fix Version/s: None

Type: Bug Priority: Critical - P2
Reporter: qi bao Assignee: Andrew Morrow (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CXX-941 When converting to JSON, strings are ... Closed

 Description   

Recently,I found a strange phenomenon that most data can be read correctly,but there are a piece of data cannot work normally,it looks like this:

{
    "_id" : 146,
 
    omit
 
    "introduce" : "Servers need a way to know who a user is. \"Once a server knows who the user is, it can decide which transactions and resources the user can access. \" ",
 
    omit
 
}

Did you notice that there are a escape sequence in the vaue of key introduce?
if there are a escape sequence in the values,c++ driver json parse function will give a error :
json parse(): has error, <unspecified file>(23): expected '}' or ','

Please fix this bug,thanks!



 Comments   
Comment by qi bao [ 25/Jun/16 ]

thanks!

Comment by Andrew Morrow (Inactive) [ 25/Jun/16 ]

This is a duplicate of CXX-941

Comment by Andrew Morrow (Inactive) [ 25/Jun/16 ]

Thank you for posting the reproduction. I think you are encountering the known bug CXX-941. It wasn't obvious to me at first, because you said the problem was with parsing JSON, rather than generating JSON. But the Boost parser is doing the right thing and rejecting the improper JSON that the BSON library is generating.

The underlying issue is that the bsoncxx::to_json method does not properly escape the quote (and other) characters per the JSON specification. We plan to address this in the upcoming 3.0.2 C++11 driver release.

Please follow CXX-941. I will close this issue as a duplicate.

Comment by qi bao [ 25/Jun/16 ]

about environment:
Ubuntu 16.04,amd x86_64,
gcc (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413 (install from apt-get)
cmake version 3.5.1 (install from apt-get)
libbson 1.3.5 ,mongo-c-driver 1.3.5 ,mongo-cxx-driver 3.0.1 , all build from source of release.
boost Version 1.61.0

Comment by qi bao [ 25/Jun/16 ]

The SSCCE:
file name: mongosscce.cpp

#include <iostream>
#include <stdexcept>
 
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
 
#include <mongocxx/client.hpp>
#include <mongocxx/options/find.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
 
#include <boost/property_tree/json_parser.hpp>
//#include <boost/property_tree/ptree.hpp>
 
bool json_parse(const std::string& strin, boost::property_tree::ptree& ptout)
{
    if (strin.length() <= 0)
        return false;
    try
    {
        std::istringstream isstream(strin);
        boost::property_tree::json_parser::read_json(isstream, ptout);
    }
    catch (std::exception& err)
    {
        std::cout << "json parse(): has error, " << err.what() << std::endl;
        return false;
    }
    catch (...)
    {
        std::cout << "json parse(): has unknown error!\n" << std::endl;
        return false;
    }
    return true;
}
 
 
int main(int argc, char **argv)
{
    mongocxx::instance inst{};
    mongocxx::client conn{ mongocxx::uri{} };
 
    auto db = conn["test"];
    auto cursor = db["abc"].find( {} );
 
    boost::property_tree::ptree pt_callback;
 
    for ( auto && doc : cursor )
    {
        json_parse(bsoncxx::to_json(doc), pt_callback);
    }
 
    return 0;
}

about compile:

> g++ -o mongosscce -std=c++11 $(pkg-config --cflags --libs libmongocxx) -I ~/boost   mongosscce.cpp

(ps: the boost source code is in my home directory )

about execute:
> ./mongosscce
json parse(): has error, <unspecified file>(3): expected '}' or ','

=========================== Mongodb ================================

db: test

collections: abc

documents:
/* 1 */

{ "_id" : 145, "introduce" : "Some control sequences are special characters that always have the same meaning." }

/* 2 */

{ "_id" : 146, "introduce" : "Servers need a way to know who a user is. \"Once a server knows who the user is, it can decide which transactions and resources the user can access. \" " }

====================== Mongodb ======== End ========================

Comment by Andrew Morrow (Inactive) [ 24/Jun/16 ]

Thank you for the bug report.

Can you please provide an SSCCE that demonstrates this issue? In other words, a standalone file that can be directly compiled without modification against the driver that demonstrates the problem?

Generated at Wed Feb 07 22:00:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.