@@ -57,12 +57,12 @@ describe('Extended JSON', function() {
57
57
var json =
58
58
'{"_id":{"$numberInt":"100"},"gh":{"$numberInt":"1"},"binary":{"$binary":{"base64":"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==","subType":"00"}},"date":{"$date":{"$numberLong":"1488372056737"}},"code":{"$code":"function() {}","$scope":{"a":{"$numberInt":"1"}}},"dbRef":{"$ref":"tests","$id":{"$numberInt":"1"},"$db":"test"},"decimal":{"$numberDecimal":"100"},"double":{"$numberDouble":"10.1"},"int32":{"$numberInt":"10"},"long":{"$numberLong":"200"},"maxKey":{"$maxKey":1},"minKey":{"$minKey":1},"objectId":{"$oid":"111111111111111111111111"},"regexp":{"$regularExpression":{"pattern":"hello world","options":"i"}},"symbol":{"$symbol":"symbol"},"timestamp":{"$timestamp":{"t":0,"i":1000}},"int32Number":{"$numberInt":"300"},"doubleNumber":{"$numberDouble":"200.2"},"longNumberIntFit":{"$numberLong":"7036874417766400"},"doubleNumberIntFit":{"$numberLong":"19007199250000000"}}' ;
59
59
60
- assert . equal ( json , EJSON . stringify ( doc , null , 0 ) ) ;
60
+ assert . equal ( json , EJSON . stringify ( doc , null , 0 , { relaxed : false } ) ) ;
61
61
} ) ;
62
62
63
- it ( 'should correctly deserialize using strict, and non-strict mode' , function ( ) {
63
+ it ( 'should correctly deserialize using the default relaxed mode' , function ( ) {
64
64
// Deserialize the document using non strict mode
65
- var doc1 = EJSON . parse ( EJSON . stringify ( doc , null , 0 ) , { strict : false } ) ;
65
+ var doc1 = EJSON . parse ( EJSON . stringify ( doc , null , 0 ) ) ;
66
66
67
67
// Validate the values
68
68
assert . equal ( 300 , doc1 . int32Number ) ;
@@ -71,7 +71,7 @@ describe('Extended JSON', function() {
71
71
assert . equal ( 19007199250000000.12 , doc1 . doubleNumberIntFit ) ;
72
72
73
73
// Deserialize the document using strict mode
74
- doc1 = EJSON . parse ( EJSON . stringify ( doc , null , 0 ) , { strict : true } ) ;
74
+ doc1 = EJSON . parse ( EJSON . stringify ( doc , null , 0 ) , { relaxed : false } ) ;
75
75
76
76
// Validate the values
77
77
expect ( doc1 . int32Number . _bsontype ) . to . equal ( 'Int32' ) ;
@@ -89,28 +89,31 @@ describe('Extended JSON', function() {
89
89
} ;
90
90
91
91
// Serialize the document
92
- var text = EJSON . stringify ( doc1 , null , 0 ) ;
92
+ var text = EJSON . stringify ( doc1 , null , 0 , { relaxed : false } ) ;
93
93
expect ( text ) . to . equal ( '{"int32":{"$numberInt":"10"}}' ) ;
94
94
95
95
// Deserialize the json in strict and non strict mode
96
- var doc2 = EJSON . parse ( text , { strict : true } ) ;
96
+ var doc2 = EJSON . parse ( text , { relaxed : false } ) ;
97
97
expect ( doc2 . int32 . _bsontype ) . to . equal ( 'Int32' ) ;
98
- doc2 = EJSON . parse ( text , { strict : false } ) ;
98
+ doc2 = EJSON . parse ( text ) ;
99
99
expect ( doc2 . int32 ) . to . equal ( 10 ) ;
100
100
} ) ;
101
101
102
102
it ( 'should correctly serialize bson types when they are values' , function ( ) {
103
103
var Int32 = EJSON . BSON . Int32 ;
104
- var serialized = EJSON . stringify ( new ObjectID ( '591801a468f9e7024b6235ea' ) ) ;
104
+ var serialized = EJSON . stringify ( new ObjectID ( '591801a468f9e7024b6235ea' ) , { relaxed : false } ) ;
105
105
expect ( serialized ) . to . equal ( '{"$oid":"591801a468f9e7024b6235ea"}' ) ;
106
- serialized = EJSON . stringify ( new Int32 ( 42 ) ) ;
106
+ serialized = EJSON . stringify ( new Int32 ( 42 ) , { relaxed : false } ) ;
107
107
expect ( serialized ) . to . equal ( '{"$numberInt":"42"}' ) ;
108
- serialized = EJSON . stringify ( {
109
- _id : { $nin : [ new ObjectID ( '591801a468f9e7024b6235ea' ) ] }
110
- } ) ;
108
+ serialized = EJSON . stringify (
109
+ {
110
+ _id : { $nin : [ new ObjectID ( '591801a468f9e7024b6235ea' ) ] }
111
+ } ,
112
+ { relaxed : false }
113
+ ) ;
111
114
expect ( serialized ) . to . equal ( '{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}' ) ;
112
115
113
- serialized = EJSON . stringify ( new Binary ( new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] ) ) ) ;
116
+ serialized = EJSON . stringify ( new Binary ( new Uint8Array ( [ 1 , 2 , 3 , 4 , 5 ] ) ) , { relaxed : false } ) ;
114
117
expect ( serialized ) . to . equal ( '{"$binary":{"base64":"AQIDBAU=","subType":"00"}}' ) ;
115
118
} ) ;
116
119
@@ -119,7 +122,7 @@ describe('Extended JSON', function() {
119
122
expect ( EJSON . parse ( '[null]' ) [ 0 ] ) . to . be . null ;
120
123
121
124
var input = '{"result":[{"_id":{"$oid":"591801a468f9e7024b623939"},"emptyField":null}]}' ;
122
- var parsed = EJSON . parse ( input , { strict : false } ) ;
125
+ var parsed = EJSON . parse ( input ) ;
123
126
124
127
expect ( parsed ) . to . deep . equal ( {
125
128
result : [ { _id : new ObjectID ( '591801a468f9e7024b623939' ) , emptyField : null } ]
@@ -128,11 +131,11 @@ describe('Extended JSON', function() {
128
131
129
132
it ( 'should correctly throw when passed a non-string to parse' , function ( ) {
130
133
expect ( ( ) => {
131
- EJSON . parse ( { } , { strict : true } ) ;
134
+ EJSON . parse ( { } ) ;
132
135
} ) . to . throw ;
133
136
} ) ;
134
137
135
- it ( 'should allow relaxed parsing' , function ( ) {
138
+ it ( 'should allow relaxed parsing by default ' , function ( ) {
136
139
const dt = new Date ( 1452124800000 ) ;
137
140
const inputObject = {
138
141
int : { $numberInt : '500' } ,
@@ -141,7 +144,7 @@ describe('Extended JSON', function() {
141
144
date : { $date : { $numberLong : '1452124800000' } }
142
145
} ;
143
146
144
- const parsed = EJSON . parse ( JSON . stringify ( inputObject ) , { relaxed : true } ) ;
147
+ const parsed = EJSON . parse ( JSON . stringify ( inputObject ) ) ;
145
148
expect ( parsed ) . to . eql ( {
146
149
int : 500 ,
147
150
long : 42 ,
@@ -151,7 +154,7 @@ describe('Extended JSON', function() {
151
154
} ) ;
152
155
153
156
it ( 'should allow regexp' , function ( ) {
154
- const parsedRegExp = EJSON . stringify ( { test : / s o m e - r e g e x / i } , { relaxed : true } ) ;
157
+ const parsedRegExp = EJSON . stringify ( { test : / s o m e - r e g e x / i } ) ;
155
158
const parsedBSONRegExp = EJSON . stringify (
156
159
{ test : new BSONRegExp ( 'some-regex' , 'i' ) } ,
157
160
{ relaxed : true }
@@ -176,7 +179,7 @@ describe('Extended JSON', function() {
176
179
timestamp : new Timestamp ( )
177
180
} ;
178
181
179
- const result = EJSON . serialize ( doc ) ;
182
+ const result = EJSON . serialize ( doc , { relaxed : false } ) ;
180
183
expect ( result ) . to . deep . equal ( {
181
184
binary : { $binary : { base64 : '' , subType : '00' } } ,
182
185
code : { $code : 'function() {}' } ,
@@ -211,7 +214,7 @@ describe('Extended JSON', function() {
211
214
timestamp : { $timestamp : { t : 0 , i : 0 } }
212
215
} ;
213
216
214
- const result = EJSON . deserialize ( doc ) ;
217
+ const result = EJSON . deserialize ( doc , { relaxed : false } ) ;
215
218
216
219
// binary
217
220
expect ( result . binary ) . to . be . an . instanceOf ( BSON . Binary ) ;
0 commit comments