INT32 JSONRecordParser::parseRecord(const CHAR* data, INT32 length, bson& obj) { INT32 rc = SDB_OK; BOOLEAN result = TRUE; SDB_ASSERT(NULL != data, "data can't be NULL"); SDB_ASSERT(length > 0, "length must be greater than 0"); bson_init(&obj); result = json2bson( data, _pMachine, CJSON_RIGOROUS_PARSE, FALSE, &obj ) ; if (!result) { rc = SDB_INVALIDARG; goto error; } if (bson_size(&obj) > IMP_MAX_BSON_SIZE) { rc = SDB_DRIVER_BSON_ERROR; PD_LOG(PDERROR, "the bson obj is beyond " "the max size %d, actual size %d, rc=%d", IMP_MAX_BSON_SIZE, bson_size(&obj), rc); goto error; } done: return rc; error: bson_destroy(&obj); goto done; }
int test_json2bson() { const char* tst = " {" "\"result\": {" "\"entries\": [{" "\"type\": \"track\"," "\"_id\": ObjectId(\"53e8d553f7f8d8548a000001\")," "\"image number\": 123456789" "}]" "}," "\"status\": {" "\"error\": \"ok\"," "\"errorMessage\": \"\"" "}" "}"; bson_document_ref d = json2bson(tst, strlen(tst)); bson_document_ref d2 = create_test_doc(); if(bson_document_size(d) != bson_document_size(d2)) { return 1; } if(memcmp(d->data, d2->data, bson_document_size(d))) { return 1; } return 0; }
INT32 expCLExporter::_query( sdbCollectionHandle &hCL, sdbCursorHandle &hCusor ) { INT32 rc = SDB_OK ; string clFullName ; bson select ; bson condition ; bson sort ; bson_init ( &select ) ; bson_init ( &condition ) ; bson_init ( &sort ) ; clFullName = _cl.fullName() ; if ( _cl.select.empty() ) { bson_empty( &select ) ; } else { if( !json2bson( _cl.select.c_str(), NULL, CJSON_RIGOROUS_PARSE, FALSE, &select ) ) { rc = SDB_INVALIDARG ; PD_LOG( PDERROR, "Invalid format of select : %s", _cl.select.c_str() ) ; goto error ; } } if ( _cl.filter.empty() ) { bson_empty( &condition ) ; } else { if( !json2bson( _cl.filter.c_str(), NULL, CJSON_RIGOROUS_PARSE, FALSE, &condition ) ) { rc = SDB_INVALIDARG ; PD_LOG( PDERROR, "Invalid format of filter : %s", _cl.filter.c_str() ) ; goto error ; } } if ( _cl.sort.empty() ) { bson_empty( &sort ) ; } else { if( !json2bson( _cl.sort.c_str(), NULL, CJSON_RIGOROUS_PARSE, FALSE, &sort ) ) { rc = SDB_INVALIDARG ; PD_LOG( PDERROR, "Invalid format of sort : %s", _cl.sort.c_str() ) ; goto error ; } } rc = sdbGetCollection( _hConn, clFullName.c_str(), &hCL ) ; if ( SDB_DMS_NOTEXIST == rc ) { PD_LOG( PDERROR, "Collection %s does not exist", clFullName.c_str() ) ; goto error ; } if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to get collection %s, rc = %d", clFullName.c_str(), rc ) ; goto error ; } rc = sdbQuery ( hCL, &condition, &select, &sort, NULL, _cl.skip, _cl.limit, &hCusor ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "Failed to query the first record of %s, rc = %d", clFullName.c_str(), rc ) ; goto error ; } done : bson_destroy( &select ) ; bson_destroy( &condition ) ; bson_destroy( &sort ) ; return rc ; error : goto done ; }
std::shared_ptr<bson> collection::convert_to_bson(nlohmann::json const& value) { return std::shared_ptr<bson>(json2bson(value.dump().c_str()), bson_del); }