Skip to content

Commit

Permalink
Add non-Strict mode for Dbref, use base64 encoding for bindata
Browse files Browse the repository at this point in the history
  • Loading branch information
astaple committed Dec 31, 2008
1 parent 9778190 commit 6a4de72
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
28 changes: 25 additions & 3 deletions db/jsobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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:
Expand All @@ -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' );
Expand Down
28 changes: 21 additions & 7 deletions dbtests/jsobjtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 ) );
}
};

Expand Down
2 changes: 2 additions & 0 deletions stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6a4de72

Please sign in to comment.