void BSONOutputStream::writeBSON(const BSONObj& bson) { Logger* log = getLogger(NULL); if (log->isDebug()) log->debug("BSONOutputStream::writeBSON bson elements: %d", bson.length()); _outputStream->writeLong(bson.length()); for (std::map<t_keytype, BSONContent* >::const_iterator i = bson.begin(); i != bson.end(); i++) { t_keytype key = i->first; if (log->isDebug()) log->debug("BSONOutputStream::writeBSON name: %s", key.c_str()); _outputStream->writeString(key); BSONContent* cont = i->second; // If the type is PTRCHAR_TYPE change it to string_type, to remove this type in future _outputStream->writeLong(cont->type() != PTRCHAR_TYPE? cont->type(): STRING_TYPE); char* text; BSONObj* inner; switch (cont->type()) { case BSON_TYPE: inner = (BSONObj*)cont->_element; writeBSON(*inner); break; case INT_TYPE: _outputStream->writeInt(*((int*)cont->_element)); break; case LONG_TYPE: _outputStream->writeLong(*((long*)cont->_element)); break; case DOUBLE_TYPE: _outputStream->writeDoubleIEEE(*((double*)cont->_element)); break; case PTRCHAR_TYPE: text = (char*)cont->_element; _outputStream->writeString(std::string(text)); break; case STRING_TYPE: { string* str = (string*)cont->_element; _outputStream->writeString(*str); break; } case BSONARRAY_TYPE: { BSONArrayObj* array = (BSONArrayObj*)cont->_element; writeBSONArray(array); break; } } } delete log; }
void testParserSimple() { cout << "testParserSimple" << endl; BSONObj* testEmpty = BSONParser::parse("{}"); TEST_ASSERT(testEmpty->length() == 0); BSONObj* obj = BSONParser::parse("{age: 1, name: 'John:test\\'test2\\'', salary: 3500.25, lnumber: 100000000000}"); TEST_ASSERT(obj->has("age")); TEST_ASSERT(obj->getInt("age") == 1); TEST_ASSERT(strcmp(obj->getString("name"), "John:test\\'test2\\'") == 0); TEST_ASSERT(obj->has("salary")); TEST_ASSERT(obj->getDouble("salary") == 3500.25); TEST_ASSERT(obj->has("lnumber")); TEST_ASSERT(obj->getLong("lnumber") == 100000000000); delete obj; delete testEmpty; }
bool BSONObj::operator !=(const BSONObj& obj) const { if (this->has("_id") && obj.has("_id")) { BSONContent* idThis = this->getContent("_id"); BSONContent* idOther = obj.getContent("_id"); return (*idThis != *idOther); } // Element count if (this->length() != obj.length()) { return true; } for (BSONObj::const_iterator it = this->begin(); it != this->end(); it++) { std::string key = it->first; BSONContent* content = it->second; BSONContent* other = obj.getContent(key); if (other == NULL) { return true; } if (content->type() != other->type()) { return true; } bool result; switch (content->type()) { case BOOL_TYPE: { result = *((BSONContentBoolean*)content) != *((BSONContentBoolean*)other); break; } case INT_TYPE: { result = *((BSONContentInt*)content) != *((BSONContentInt*)other); break; } case DOUBLE_TYPE: { result = *((BSONContentDouble*)content) != *((BSONContentDouble*)other); break; } case LONG64_TYPE: case LONG_TYPE: { result = *((BSONContentLong*)content) != *((BSONContentLong*)other); break; } case PTRCHAR_TYPE: case STRING_TYPE: { result = *((BSONContentString*)content) != *((BSONContentString*)other); break; } case BSON_TYPE: { result = *((BSONContentBSON*)content) != *((BSONContentBSON*)other); break; } case BSONARRAY_TYPE: { result = *((BSONContentBSONArray*)content) != *((BSONContentBSONArray*)other); break; } case NULL_TYPE: { result = true; break; } case UNKNOWN_TYPE: default: { result = *((BSONContentInt*)content) != *((BSONContentInt*)other); break; } } if (result) { return result; } } return false; }