Skip to content

Commit 9c99454

Browse files
committed
Add fix issue 50 (Object for loop should filter with obj.hasOwnProperty(key))
* Follows-up r37, r36 * Re-order some comments * Rebuild .min.js * Bump version to 2.3-edge
1 parent c342c5f commit 9c99454

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/jquery.json.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* jQuery JSON Plugin v2.3 (2011-09-17)
2+
* jQuery JSON Plugin v2.3-edge (2011-09-18)
33
*
44
* @author Brantley Harris, 2009-2011
55
* @author Timo Tijhof, 2011
@@ -104,21 +104,27 @@
104104
val,
105105
pairs = [];
106106
for ( var k in o ) {
107+
// Only include own properties,
108+
// Filter out inherited prototypes
109+
if ( !o.hasOwnProperty( k ) ) {
110+
continue;
111+
}
112+
113+
// Keys must be numerical or string. Skip others
107114
type = typeof k;
108115
if ( type === 'number' ) {
109116
name = '"' + k + '"';
110117
} else if (type === 'string') {
111118
name = $.quoteString(k);
112119
} else {
113-
// Keys must be numerical or string. Skip others
114120
continue;
115121
}
116122
type = typeof o[k];
117123

124+
// Invalid values like these return undefined
125+
// from toJSON, however those object members
126+
// shouldn't be included in the JSON string at all.
118127
if ( type === 'function' || type === 'undefined' ) {
119-
// Invalid values like these return undefined
120-
// from toJSON, however those object members
121-
// shouldn't be included in the JSON string at all.
122128
continue;
123129
}
124130
val = $.toJSON( o[k] );

src/jquery.json.min.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ 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){type=typeof k;if(type==='number'){name='"'+k+'"';}else if(type==='string'){name=$.quoteString(k);}else{continue;}
18+
var name,val,pairs=[];for(var k in o){if(!o.hasOwnProperty(k)){continue;}
19+
type=typeof k;if(type==='number'){name='"'+k+'"';}else if(type==='string'){name=$.quoteString(k);}else{continue;}
1920
type=typeof o[k];if(type==='function'||type==='undefined'){continue;}
2021
val=$.toJSON(o[k]);pairs.push(name+':'+val);}
2122
return'{'+pairs.join(',')+'}';}};$.evalJSON=typeof JSON==='object'&&JSON.parse?JSON.parse:function(src){return eval('('+src+')');};$.secureEvalJSON=typeof JSON==='object'&&JSON.parse?JSON.parse:function(src){var filtered=src.replace(/\\["\\\/bfnrtu]/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered)){return eval('('+src+')');}else{throw new SyntaxError('Error parsing JSON, source is not valid.');}};$.quoteString=function(string){if(string.match(escapeable)){return'"'+string.replace(escapeable,function(a){var c=meta[a];if(typeof c==='string'){return c;}

0 commit comments

Comments
 (0)