diff options
author | Tyler Brock <tyler.brock@gmail.com> | 2014-06-19 14:38:00 -0400 |
---|---|---|
committer | Tyler Brock <tyler.brock@gmail.com> | 2014-06-25 15:01:59 -0400 |
commit | cdd1172ad392b8c2321e4e140b020edf8c97b7c9 (patch) | |
tree | f4588ef7f130a7ca28161154b1aeaae7d0539628 /src/mongo/db/json.h | |
parent | 11ea31ab1ac1109af219f7a36fb21dcf7dbbe5da (diff) | |
download | mongo-cdd1172ad392b8c2321e4e140b020edf8c97b7c9.tar.gz |
SERVER-14357: Add support for top level JSON Array
Diffstat (limited to 'src/mongo/db/json.h')
-rw-r--r-- | src/mongo/db/json.h | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/src/mongo/db/json.h b/src/mongo/db/json.h index 4df19f756d5..0d3d92c126d 100644 --- a/src/mongo/db/json.h +++ b/src/mongo/db/json.h @@ -55,13 +55,55 @@ namespace mongo { MONGO_CLIENT_API BSONObj fromjson(const char* str, int* len=NULL); /** + * Tests whether the JSON string is an Array. + * + * Useful for assigning the result of fromjson to the right object type. Either: + * BSONObj + * BSONArray + * + * @example Using the method to select the proper type. + * If this method returns true, the user could store the result of fromjson + * inside a BSONArray, rather than a BSONObj, in order to have it print as an + * array when passed to tojson. + * + * @param obj The JSON string to test. + */ + MONGO_CLIENT_API bool isArray(const StringData& str); + + /** + * Convert a BSONArray to a JSON string. + * + * @param arr The BSON Array. + * @param format The JSON format (JS, TenGen, Strict). + * @param pretty Enables pretty output. + */ + MONGO_CLIENT_API std::string tojson( + const BSONArray& arr, + JsonStringFormat format = Strict, + bool pretty = false + ); + + /** + * Convert a BSONObj to a JSON string. + * + * @param obj The BSON Object. + * @param format The JSON format (JS, TenGen, Strict). + * @param pretty Enables pretty output. + */ + MONGO_CLIENT_API std::string tojson( + const BSONObj& obj, + JsonStringFormat format = Strict, + bool pretty = false + ); + + /** * Parser class. A BSONObj is constructed incrementally by passing a * BSONObjBuilder to the recursive parsing methods. The grammar for the * element parsed is described before each function. */ class JParse { public: - explicit JParse(const char*); + explicit JParse(const StringData& str); /* * Notation: All-uppercase symbols denote non-terminals; all other @@ -123,6 +165,8 @@ namespace mongo { */ public: Status object(const StringData& fieldName, BSONObjBuilder&, bool subObj=true); + Status parse(BSONObjBuilder& builder); + bool isArray(); private: /* The following functions are called with the '{' and the first @@ -196,7 +240,7 @@ namespace mongo { * VALUE * | VALUE , ELEMENTS */ - Status array(const StringData& fieldName, BSONObjBuilder&); + Status array(const StringData& fieldName, BSONObjBuilder&, bool subObj=true); /* * NOTE: Currently only Date can be preceded by the "new" keyword |