summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathias Stearn <mathias@10gen.com>2009-11-30 15:10:14 -0500
committerMathias Stearn <mathias@10gen.com>2009-11-30 17:05:31 -0500
commitce036039e6c23ef2c6fcb5344f1beb07aa251cb5 (patch)
treed5629083fdcb98f00e9a1903b8e3f089f7ccf1fa
parent8cbc9e1c1b7d23731796fea489f82a070b7372a0 (diff)
downloadmongo-ce036039e6c23ef2c6fcb5344f1beb07aa251cb5.tar.gz
JSON parser now creates ints and longs SERVER-309
-rw-r--r--db/json.cpp19
-rw-r--r--dbtests/jsontests.cpp52
-rw-r--r--dbtests/matchertests.cpp2
-rw-r--r--dbtests/namespacetests.cpp2
4 files changed, 71 insertions, 4 deletions
diff --git a/db/json.cpp b/db/json.cpp
index 4735f5250d6..ceb214e97b5 100644
--- a/db/json.cpp
+++ b/db/json.cpp
@@ -248,6 +248,17 @@ namespace mongo {
ObjectBuilder &b;
};
+ struct intValue {
+ intValue( ObjectBuilder &_b ) : b( _b ) {}
+ void operator() ( long long num ) const {
+ if (num >= numeric_limits<int>::min() && num <= numeric_limits<int>::max())
+ b.back()->append( b.fieldName(), (int)num );
+ else
+ b.back()->append( b.fieldName(), num );
+ }
+ ObjectBuilder &b;
+ };
+
struct subobjectEnd {
subobjectEnd( ObjectBuilder &_b ) : b( _b ) {}
void operator() ( const char *start, const char *end ) const {
@@ -435,6 +446,7 @@ public:
str[ stringEnd( self.b ) ] |
singleQuoteStr[ stringEnd( self.b ) ] |
number |
+ integer |
object[ subobjectEnd( self.b ) ] |
array[ arrayEnd( self.b ) ] |
lexeme_d[ str_p( "true" ) ][ trueValue( self.b ) ] |
@@ -470,7 +482,10 @@ public:
// real_p accepts numbers with nonsignificant zero prefixes, which
// aren't allowed in JSON. Oh well.
- number = real_p[ numberValue( self.b ) ];
+ number = strict_real_p[ numberValue( self.b ) ];
+
+ static int_parser<long long, 10, 1, numeric_limits<long long>::digits10 + 1> long_long_p;
+ integer = long_long_p[ intValue(self.b) ];
// We allow a subset of valid js identifier names here.
unquotedFieldName = lexeme_d[ ( alpha_p | ch_p( '$' ) | ch_p( '_' ) ) >> *( ( alnum_p | ch_p( '$' ) | ch_p( '_' )) ) ];
@@ -512,7 +527,7 @@ public:
( ~range_p( 0x00, 0x1f ) & ~ch_p( '/' ) & ( ~ch_p( '\\' ) )[ ch( self.b ) ] ) ) >> str_p( "/" )[ regexValue( self.b ) ]
>> ( *( ch_p( 'i' ) | ch_p( 'g' ) | ch_p( 'm' ) ) )[ regexOptions( self.b ) ] ];
}
- rule< ScannerT > object, members, array, elements, value, str, number,
+ rule< ScannerT > object, members, array, elements, value, str, number, integer,
dbref, dbrefS, dbrefT, oid, oidS, oidT, bindata, date, dateS, dateT,
regex, regexS, regexT, quotedOid, fieldName, unquotedFieldName, singleQuoteStr;
const rule< ScannerT > &start() const {
diff --git a/dbtests/jsontests.cpp b/dbtests/jsontests.cpp
index d0c21731254..b7f0a678180 100644
--- a/dbtests/jsontests.cpp
+++ b/dbtests/jsontests.cpp
@@ -900,6 +900,56 @@ namespace JsonTests {
}
};
+ class NumericTypes : public Base {
+ public:
+ void run(){
+ Base::run();
+
+ BSONObj o = fromjson(json());
+
+ ASSERT(o["int"].type() == NumberInt);
+ ASSERT(o["long"].type() == NumberLong);
+ ASSERT(o["double"].type() == NumberDouble);
+
+ ASSERT(o["long"].numberLong() == 9223372036854775807ll);
+ }
+
+ virtual BSONObj bson() const {
+ return BSON( "int" << 123
+ << "long" << 9223372036854775807ll // 2**63 - 1
+ << "double" << 3.14
+ );
+ }
+ virtual string json() const {
+ return "{ \"int\": 123, \"long\": 9223372036854775807, \"double\": 3.14 }";
+ }
+ };
+
+ class NegativeNumericTypes : public Base {
+ public:
+ void run(){
+ Base::run();
+
+ BSONObj o = fromjson(json());
+
+ ASSERT(o["int"].type() == NumberInt);
+ ASSERT(o["long"].type() == NumberLong);
+ ASSERT(o["double"].type() == NumberDouble);
+
+ ASSERT(o["long"].numberLong() == -9223372036854775807ll);
+ }
+
+ virtual BSONObj bson() const {
+ return BSON( "int" << -123
+ << "long" << -9223372036854775807ll // -1 * (2**63 - 1)
+ << "double" << -3.14
+ );
+ }
+ virtual string json() const {
+ return "{ \"int\": -123, \"long\": -9223372036854775807, \"double\": -3.14 }";
+ }
+ };
+
} // namespace FromJsonTests
class All : public Suite {
@@ -978,6 +1028,8 @@ namespace JsonTests {
add< FromJsonTests::SingleQuotes >();
add< FromJsonTests::ObjectId >();
add< FromJsonTests::ObjectId2 >();
+ add< FromJsonTests::NumericTypes >();
+ add< FromJsonTests::NegativeNumericTypes >();
}
} myall;
diff --git a/dbtests/matchertests.cpp b/dbtests/matchertests.cpp
index 08370889d6e..023cc9cda4f 100644
--- a/dbtests/matchertests.cpp
+++ b/dbtests/matchertests.cpp
@@ -70,7 +70,7 @@ namespace MatcherTests {
void run(){
BSONObj query = fromjson( "{ a : { $in : [4,6] } }" );
ASSERT_EQUALS( 4 , query["a"].embeddedObject()["$in"].embeddedObject()["0"].number() );
- ASSERT_EQUALS( NumberDouble , query["a"].embeddedObject()["$in"].embeddedObject()["0"].type() );
+ ASSERT_EQUALS( NumberInt , query["a"].embeddedObject()["$in"].embeddedObject()["0"].type() );
JSMatcher m( query );
diff --git a/dbtests/namespacetests.cpp b/dbtests/namespacetests.cpp
index aabaf8e0066..ff3aa879d06 100644
--- a/dbtests/namespacetests.cpp
+++ b/dbtests/namespacetests.cpp
@@ -553,7 +553,7 @@ namespace NamespaceTests {
id().getKeysFromObject( fromjson( "{a:1,b:[]}" ), keys );
checkSize(1, keys );
cout << "YO : " << *(keys.begin()) << endl;
- ASSERT_EQUALS( NumberDouble , keys.begin()->firstElement().type() );
+ ASSERT_EQUALS( NumberInt , keys.begin()->firstElement().type() );
keys.clear();
}