From 10e83e822012f46431e11c117f31fa84b37518c8 Mon Sep 17 00:00:00 2001 From: Mathias Stearn Date: Mon, 14 Jun 2010 08:38:17 -0400 Subject: [PATCH] Support getting size of JSON object --- db/json.cpp | 19 ++++++++++--------- db/json.h | 3 ++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/db/json.cpp b/db/json.cpp index 77983c39c36..208d78f5c0f 100644 --- a/db/json.cpp +++ b/db/json.cpp @@ -557,15 +557,20 @@ public: ObjectBuilder &b; }; - BSONObj fromjson( const char *str ) { - if ( str[0] == '\0' ) + BSONObj fromjson( const char *str , int* len) { + if ( str[0] == '\0' ){ + if (len) *len = 0; return BSONObj(); + } + ObjectBuilder b; JsonGrammar parser( b ); parse_info<> result = parse( str, parser, space_p ); - if ( !result.full ) { - int len = strnlen(result.stop , 10); - massert( 10340 , "Failure parsing JSON string near: " + string( result.stop, len ) , false ); + if (len) { + *len = result.stop - str; + } else if ( !result.full ) { + int limit = strnlen(result.stop , 10); + msgasserted(10340, "Failure parsing JSON string near: " + string( result.stop, limit )); } BSONObj ret = b.pop(); assert( b.empty() ); @@ -576,8 +581,4 @@ public: return fromjson( str.c_str() ); } - BSONObj fromjson( istream &str ) { - return BSONObj(); - } - } // namespace mongo diff --git a/db/json.h b/db/json.h index bbed12c0801..68dae042574 100644 --- a/db/json.h +++ b/db/json.h @@ -35,6 +35,7 @@ namespace mongo { */ BSONObj fromjson(const string &str); - BSONObj fromjson(const char *str); + /** len will be size of JSON object in text chars. */ + BSONObj fromjson(const char *str, int* len=NULL); } // namespace mongo