diff options
author | Andrew Morrow <acm@10gen.com> | 2013-05-21 10:14:27 -0400 |
---|---|---|
committer | Andrew Morrow <acm@10gen.com> | 2013-05-22 16:31:54 -0400 |
commit | 6a7413d9c28534b0f5e507787ed522c95914df9d (patch) | |
tree | bb7307e1d362940edf98b2b501caad8fc8d05e86 | |
parent | 98e9bc926749a7096b19c2e1fea636cd96380597 (diff) | |
download | mongo-6a7413d9c28534b0f5e507787ed522c95914df9d.tar.gz |
SERVER-9750 Fix BSONObj::toString handling of empty arrays
-rw-r--r-- | src/mongo/SConscript | 3 | ||||
-rw-r--r-- | src/mongo/bson/bson-inl.h | 4 | ||||
-rw-r--r-- | src/mongo/bson/bson_obj_test.cpp | 30 |
3 files changed, 35 insertions, 2 deletions
diff --git a/src/mongo/SConscript b/src/mongo/SConscript index 9a9f06a6929..e47f22f4007 100644 --- a/src/mongo/SConscript +++ b/src/mongo/SConscript @@ -102,6 +102,9 @@ env.CppUnitTest('string_map_test', ['util/string_map_test.cpp'], env.CppUnitTest('bson_field_test', ['bson/bson_field_test.cpp'], LIBDEPS=['bson']) +env.CppUnitTest('bson_obj_test', ['bson/bson_obj_test.cpp'], + LIBDEPS=['bson']) + env.CppUnitTest('bson_validate_test', ['bson/bson_validate_test.cpp'], LIBDEPS=['bson']) diff --git a/src/mongo/bson/bson-inl.h b/src/mongo/bson/bson-inl.h index 014d6e3369b..40491bea09f 100644 --- a/src/mongo/bson/bson-inl.h +++ b/src/mongo/bson/bson-inl.h @@ -452,14 +452,14 @@ dodouble: } inline std::string BSONObj::toString( bool isArray, bool full ) const { - if ( isEmpty() ) return "{}"; + if ( isEmpty() ) return (isArray ? "[]" : "{}"); StringBuilder s; toString(s, isArray, full); return s.str(); } inline void BSONObj::toString( StringBuilder& s, bool isArray, bool full, int depth ) const { if ( isEmpty() ) { - s << "{}"; + s << (isArray ? "[]" : "{}"); return; } diff --git a/src/mongo/bson/bson_obj_test.cpp b/src/mongo/bson/bson_obj_test.cpp new file mode 100644 index 00000000000..6e4d857ee44 --- /dev/null +++ b/src/mongo/bson/bson_obj_test.cpp @@ -0,0 +1,30 @@ +/* Copyright 2013 10gen Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mongo/db/jsobj.h" +#include "mongo/db/json.h" + +#include "mongo/unittest/unittest.h" + +namespace { + + TEST(ToString, EmptyArray) { + const char text[] = "{ x: [] }"; + mongo::BSONObj o1 = mongo::fromjson(text); + const std::string o1_str = o1.toString(); + ASSERT_EQUALS(text, o1_str); + } + +} // unnamed namespace |