Skip to content

Commit 7617ed7

Browse files
committed
Fix issue 51: Guard against object with own property "hasOwnProperty"
* Previously caused an exception to be thrown in such case * Added test case to confirm * Rebuild the *.min.js * Rename 2.3-edge to 2.4-edge. This is the edge / development version that will become 2.4. 2.3 has already been released.
1 parent 9c99454 commit 7617ed7

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/jquery.json.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* jQuery JSON Plugin v2.3-edge (2011-09-18)
2+
* jQuery JSON Plugin v2.3-edge (2011-09-25)
33
*
44
* @author Brantley Harris, 2009-2011
55
* @author Timo Tijhof, 2011
@@ -23,7 +23,8 @@
2323
'\r': '\\r',
2424
'"' : '\\"',
2525
'\\': '\\\\'
26-
};
26+
},
27+
hasOwn = Object.prototype.hasOwnProperty;
2728

2829
/**
2930
* jQuery.toJSON
@@ -106,7 +107,7 @@
106107
for ( var k in o ) {
107108
// Only include own properties,
108109
// Filter out inherited prototypes
109-
if ( !o.hasOwnProperty( k ) ) {
110+
if ( !hasOwn.call( o, k ) ) {
110111
continue;
111112
}
112113

src/jquery.json.min.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
(function($){var escapeable=/["\\\x00-\x1f\x7f-\x9f]/g,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};$.toJSON=typeof JSON==='object'&&JSON.stringify?JSON.stringify:function(o){if(o===null){return'null';}
2+
(function($){var escapeable=/["\\\x00-\x1f\x7f-\x9f]/g,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},hasOwn=Object.prototype.hasOwnProperty;$.toJSON=typeof JSON==='object'&&JSON.stringify?JSON.stringify:function(o){if(o===null){return'null';}
33
var type=typeof o;if(type==='undefined'){return undefined;}
44
if(type==='number'||type==='boolean'){return''+o;}
55
if(type==='string'){return $.quoteString(o);}
@@ -15,7 +15,7 @@ return'"'+year+'-'+month+'-'+day+'T'+
1515
hours+':'+minutes+':'+seconds+'.'+milli+'Z"';}
1616
if(o.constructor===Array){var ret=[];for(var i=0;i<o.length;i++){ret.push($.toJSON(o[i])||'null');}
1717
return'['+ret.join(',')+']';}
18-
var name,val,pairs=[];for(var k in o){if(!o.hasOwnProperty(k)){continue;}
18+
var name,val,pairs=[];for(var k in o){if(!hasOwn.call(o,k)){continue;}
1919
type=typeof k;if(type==='number'){name='"'+k+'"';}else if(type==='string'){name=$.quoteString(k);}else{continue;}
2020
type=typeof o[k];if(type==='function'||type==='undefined'){continue;}
2121
val=$.toJSON(o[k]);pairs.push(name+':'+val);}

test/jquery.json.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ test( 'Undefined and null', function(){
168168

169169

170170
});
171+
171172
test( 'Prototype inheritance', function(){
172173

173174
Object.prototype.AWESOME = 7;
@@ -189,4 +190,15 @@ test( 'Prototype inheritance', function(){
189190
delete Object.prototype.AWESOME;
190191
} catch(e){}
191192

192-
})
193+
});
194+
195+
test( '"hasOwnProperty" mixup', function(){
196+
197+
testToJSON(
198+
{ isAwesome: true, hasOwnProperty: false },
199+
'{"isAwesome":true,"hasOwnProperty":false}',
200+
'Guard against inherited prototypes should not depend on prototype inheritance itself'
201+
);
202+
203+
204+
});

0 commit comments

Comments
 (0)