0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

Support getting size of JSON object

This commit is contained in:
Mathias Stearn 2010-06-14 08:38:17 -04:00
parent 97a024a47f
commit 10e83e8220
2 changed files with 12 additions and 10 deletions

View File

@ -557,15 +557,20 @@ public:
ObjectBuilder &b; ObjectBuilder &b;
}; };
BSONObj fromjson( const char *str ) { BSONObj fromjson( const char *str , int* len) {
if ( str[0] == '\0' ) if ( str[0] == '\0' ){
if (len) *len = 0;
return BSONObj(); return BSONObj();
}
ObjectBuilder b; ObjectBuilder b;
JsonGrammar parser( b ); JsonGrammar parser( b );
parse_info<> result = parse( str, parser, space_p ); parse_info<> result = parse( str, parser, space_p );
if ( !result.full ) { if (len) {
int len = strnlen(result.stop , 10); *len = result.stop - str;
massert( 10340 , "Failure parsing JSON string near: " + string( result.stop, len ) , false ); } 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(); BSONObj ret = b.pop();
assert( b.empty() ); assert( b.empty() );
@ -576,8 +581,4 @@ public:
return fromjson( str.c_str() ); return fromjson( str.c_str() );
} }
BSONObj fromjson( istream &str ) {
return BSONObj();
}
} // namespace mongo } // namespace mongo

View File

@ -35,6 +35,7 @@ namespace mongo {
*/ */
BSONObj fromjson(const string &str); 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 } // namespace mongo