mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
json support for undefined
This commit is contained in:
parent
4e1187f46b
commit
08b057a79d
17
db/jsobj.cpp
17
db/jsobj.cpp
@ -98,7 +98,7 @@ namespace mongo {
|
||||
string BSONElement::jsonString( JsonStringFormat format, bool includeFieldNames, int pretty ) const {
|
||||
BSONType t = type();
|
||||
if ( t == Undefined )
|
||||
return "";
|
||||
return "undefined";
|
||||
|
||||
stringstream s;
|
||||
if ( includeFieldNames )
|
||||
@ -142,19 +142,28 @@ namespace mongo {
|
||||
s << "[ ";
|
||||
BSONObjIterator i( embeddedObject() );
|
||||
BSONElement e = i.next();
|
||||
if ( !e.eoo() )
|
||||
if ( !e.eoo() ) {
|
||||
int count = 0;
|
||||
while ( 1 ) {
|
||||
if( pretty ) {
|
||||
s << '\n';
|
||||
for( int x = 0; x < pretty; x++ )
|
||||
s << " ";
|
||||
}
|
||||
s << e.jsonString( format, false, pretty?pretty+1:0 );
|
||||
e = i.next();
|
||||
|
||||
if (strtol(e.fieldName(), 0, 10) > count) {
|
||||
s << "undefined";
|
||||
}
|
||||
else {
|
||||
s << e.jsonString( format, false, pretty?pretty+1:0 );
|
||||
e = i.next();
|
||||
}
|
||||
count++;
|
||||
if ( e.eoo() )
|
||||
break;
|
||||
s << ", ";
|
||||
}
|
||||
}
|
||||
s << " ]";
|
||||
break;
|
||||
}
|
||||
|
@ -315,6 +315,14 @@ namespace mongo {
|
||||
ObjectBuilder &b;
|
||||
};
|
||||
|
||||
struct undefinedValue {
|
||||
undefinedValue( ObjectBuilder &_b ) : b( _b ) {}
|
||||
void operator() ( const char *start, const char *end ) const {
|
||||
b.back()->appendUndefined( b.fieldName() );
|
||||
}
|
||||
ObjectBuilder &b;
|
||||
};
|
||||
|
||||
struct dbrefNS {
|
||||
dbrefNS( ObjectBuilder &_b ) : b( _b ) {}
|
||||
void operator() ( const char *start, const char *end ) const {
|
||||
@ -460,6 +468,7 @@ namespace mongo {
|
||||
lexeme_d[ str_p( "true" ) ][ trueValue( self.b ) ] |
|
||||
lexeme_d[ str_p( "false" ) ][ falseValue( self.b ) ] |
|
||||
lexeme_d[ str_p( "null" ) ][ nullValue( self.b ) ] |
|
||||
lexeme_d[ str_p( "undefined" ) ][ undefinedValue( self.b ) ] |
|
||||
singleQuoteStr[ stringEnd( self.b ) ] |
|
||||
date[ dateEnd( self.b ) ] |
|
||||
oid[ oidEnd( self.b ) ] |
|
||||
|
@ -4,7 +4,8 @@ t = new ToolTest( "exportimport1" );
|
||||
|
||||
c = t.startDB( "foo" );
|
||||
assert.eq( 0 , c.count() , "setup1" );
|
||||
c.save( { a : 22 } );
|
||||
var arr = ["x", undefined, "y", undefined];
|
||||
c.save( { a : 22 , b : arr} );
|
||||
assert.eq( 1 , c.count() , "setup2" );
|
||||
|
||||
t.runTool( "export" , "--out" , t.extFile , "-d" , t.baseName , "-c" , "foo" );
|
||||
@ -15,8 +16,11 @@ assert.eq( 0 , c.count() , "after drop" , "-d" , t.baseName , "-c" , "foo" );;
|
||||
t.runTool( "import" , "--file" , t.extFile , "-d" , t.baseName , "-c" , "foo" );
|
||||
assert.soon( "c.findOne()" , "no data after sleep" );
|
||||
assert.eq( 1 , c.count() , "after restore 2" );
|
||||
assert.eq( 22 , c.findOne().a , "after restore 2" );
|
||||
|
||||
var doc = c.findOne();
|
||||
assert.eq( 22 , doc.a , "after restore 2" );
|
||||
for (var i=0; i<arr.length; i++) {
|
||||
assert.eq( arr[i], doc.b[i] , "after restore array: "+i );
|
||||
}
|
||||
|
||||
// now with --jsonArray
|
||||
|
||||
@ -30,4 +34,23 @@ assert.soon( "c.findOne()" , "no data after sleep" );
|
||||
assert.eq( 1 , c.count() , "after restore 2" );
|
||||
assert.eq( 22 , c.findOne().a , "after restore 2" );
|
||||
|
||||
c.drop();
|
||||
assert.eq( 0 , c.count() , "after drop" , "-d" , t.baseName , "-c" , "foo" );
|
||||
|
||||
arr = ["a", undefined, "c"];
|
||||
c.save({a : arr});
|
||||
assert.eq( 1 , c.count() , "setup2" );
|
||||
t.runTool( "export" , "--out" , t.extFile , "-d" , t.baseName , "-c" , "foo" );
|
||||
c.drop();
|
||||
assert.eq( 0 , c.count() , "after drop" , "-d" , t.baseName , "-c" , "foo" );;
|
||||
|
||||
t.runTool( "import" , "--file" , t.extFile , "-d" , t.baseName , "-c" , "foo" );
|
||||
assert.soon( "c.findOne()" , "no data after sleep" );
|
||||
assert.eq( 1 , c.count() , "after restore 2" );
|
||||
var doc = c.findOne();
|
||||
for (var i=0; i<arr.length; i++) {
|
||||
assert.eq( arr[i], doc.a[i] , "after restore array: "+i );
|
||||
}
|
||||
|
||||
|
||||
t.stop();
|
||||
|
Loading…
Reference in New Issue
Block a user