mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
Add non-Strict mode for Dbref, use base64 encoding for bindata
This commit is contained in:
parent
9778190a26
commit
6a4de72765
28
db/jsobj.cpp
28
db/jsobj.cpp
@ -155,6 +155,11 @@ string escape( string s ) {
|
||||
return ret.str();
|
||||
}
|
||||
|
||||
typedef boost::archive::iterators::base64_from_binary
|
||||
< boost::archive::iterators::transform_width
|
||||
< string::const_iterator, 6, 8>
|
||||
> base64_t;
|
||||
|
||||
string BSONElement::jsonString( JsonStringFormat format, bool includeFieldNames ) const {
|
||||
stringstream s;
|
||||
if ( includeFieldNames )
|
||||
@ -205,8 +210,18 @@ string BSONElement::jsonString( JsonStringFormat format, bool includeFieldNames
|
||||
}
|
||||
case DBRef: {
|
||||
OID *x = (OID *) (valuestr() + valuestrsize());
|
||||
s << "{ \"$ns\" : \"" << valuestr() << "\", \"$id\" : \"";
|
||||
s << *x << "\" }";
|
||||
if ( format == Strict )
|
||||
s << "{ \"$ns\" : ";
|
||||
else
|
||||
s << "Dbref( ";
|
||||
s << '"' << valuestr() << "\", ";
|
||||
if ( format == Strict )
|
||||
s << "\"$id\" : ";
|
||||
s << '"' << *x << "\" ";
|
||||
if ( format == Strict )
|
||||
s << '}';
|
||||
else
|
||||
s << ')';
|
||||
break;
|
||||
}
|
||||
case jstOID:
|
||||
@ -219,7 +234,14 @@ string BSONElement::jsonString( JsonStringFormat format, bool includeFieldNames
|
||||
case BinData: {
|
||||
int len = *(int *)( value() );
|
||||
BinDataType type = BinDataType( *(char *)( (int *)( value() ) + 1 ) );
|
||||
s << "{ \"$binary\" : \"" << escape( string( (char *)( value() ) + sizeof( int ) + 1, len ) );
|
||||
s << "{ \"$binary\" : \"";
|
||||
char *start = ( char * )( value() ) + sizeof( int ) + 1;
|
||||
char *end = start + len;
|
||||
string base64 = string( base64_t( start ), base64_t( end ) );
|
||||
s << base64;
|
||||
int padding = ( 4 - ( base64.length() % 4 ) ) % 4;
|
||||
for( int i = 0; i < padding; ++i )
|
||||
s << '=';
|
||||
s << "\", \"$type\" : \"" << hex;
|
||||
s.width( 2 );
|
||||
s.fill( '0' );
|
||||
|
@ -271,7 +271,11 @@ namespace JsonStringTests {
|
||||
b.appendDBRef( "a", "namespace", oid );
|
||||
ASSERT_EQUALS( "{ \"a\" : { \"$ns\" : \"namespace\", \"$id\" : \"ffffffffffffffffffffffff\" } }",
|
||||
b.done().jsonString( Strict ) );
|
||||
}
|
||||
ASSERT_EQUALS( "{ \"a\" : Dbref( \"namespace\", \"ffffffffffffffffffffffff\" ) }",
|
||||
b.done().jsonString( TenGen ) );
|
||||
ASSERT_EQUALS( "{ \"a\" : Dbref( \"namespace\", \"ffffffffffffffffffffffff\" ) }",
|
||||
b.done().jsonString( JS ) );
|
||||
}
|
||||
};
|
||||
|
||||
class ObjectId {
|
||||
@ -291,14 +295,24 @@ namespace JsonStringTests {
|
||||
class BinData {
|
||||
public:
|
||||
void run() {
|
||||
char d[ 3 ];
|
||||
d[ 0 ] = 'a';
|
||||
d[ 1 ] = '\0';
|
||||
d[ 2 ] = 'b';
|
||||
char z[ 3 ];
|
||||
z[ 0 ] = 'a';
|
||||
z[ 1 ] = 'b';
|
||||
z[ 2 ] = 'c';
|
||||
BSONObjBuilder b;
|
||||
b.appendBinData( "a", 3, ByteArray, d );
|
||||
ASSERT_EQUALS( "{ \"a\" : { \"$binary\" : \"a\\u0000b\", \"$type\" : \"02\" } }",
|
||||
b.appendBinData( "a", 3, ByteArray, z );
|
||||
ASSERT_EQUALS( "{ \"a\" : { \"$binary\" : \"YWJj\", \"$type\" : \"02\" } }",
|
||||
b.done().jsonString( Strict ) );
|
||||
|
||||
BSONObjBuilder c;
|
||||
c.appendBinData( "a", 2, ByteArray, z );
|
||||
ASSERT_EQUALS( "{ \"a\" : { \"$binary\" : \"YWI=\", \"$type\" : \"02\" } }",
|
||||
c.done().jsonString( Strict ) );
|
||||
|
||||
BSONObjBuilder d;
|
||||
d.appendBinData( "a", 1, ByteArray, z );
|
||||
ASSERT_EQUALS( "{ \"a\" : { \"$binary\" : \"YQ==\", \"$type\" : \"02\" } }",
|
||||
d.done().jsonString( Strict ) );
|
||||
}
|
||||
};
|
||||
|
||||
|
2
stdafx.h
2
stdafx.h
@ -270,6 +270,8 @@ inline void our_debug_free(void *p) {
|
||||
#undef yassert
|
||||
#include <boost/filesystem/convenience.hpp>
|
||||
#include <boost/filesystem/operations.hpp>
|
||||
#include <boost/archive/iterators/base64_from_binary.hpp>
|
||||
#include <boost/archive/iterators/transform_width.hpp>
|
||||
#undef assert
|
||||
#define assert xassert
|
||||
#define yassert 1
|
||||
|
Loading…
Reference in New Issue
Block a user