Skip to content

Commit 690dfe0

Browse files
esprehnIgorMinar
authored andcommitted
Serialize RegExp to string in JSON. Closes angular#119.
1 parent 91c835d commit 690dfe0

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

src/JSON.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ function toJsonArray(buf, obj, pretty, stack){
3333
var type = typeof obj;
3434
if (obj === _null) {
3535
buf.push($null);
36+
} else if (obj instanceof RegExp) {
37+
buf.push(angular['String']['quoteUnicode'](obj.toString()));
3638
} else if (type === $function) {
3739
return;
3840
} else if (type === $boolean) {

test/JsonSpec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ describe('json', function(){
3030
assertEquals('[1,"b"]', toJson([1,"b"]));
3131
});
3232

33+
it('should parse RegExp', function() {
34+
assertEquals('"/foo/"', toJson(/foo/));
35+
assertEquals('[1,"/foo/"]', toJson([1,new RegExp("foo")]));
36+
});
37+
3338
it('should parse IgnoreFunctions', function() {
3439
assertEquals('[null,1]', toJson([function(){},1]));
3540
assertEquals('{}', toJson({a:function(){}}));

0 commit comments

Comments
 (0)