| 236 | |
| 237 | |
| 238 | function str(key, holder) { |
| 239 | |
| 240 | // Produce a string from holder[key]. |
| 241 | |
| 242 | var i, // The loop counter. |
| 243 | k, // The member key. |
| 244 | v, // The member value. |
| 245 | length, |
| 246 | mind = gap, |
| 247 | partial, |
| 248 | value = holder[key]; |
| 249 | |
| 250 | // If the value has a toJSON method, call it to obtain a replacement value. |
| 251 | |
| 252 | if (value && typeof value === 'object' && |
| 253 | typeof value.toJSON === 'function') { |
| 254 | value = value.toJSON(key); |
| 255 | } |
| 256 | |
| 257 | // If we were called with a replacer function, then call the replacer to |
| 258 | // obtain a replacement value. |
| 259 | |
| 260 | if (typeof rep === 'function') { |
| 261 | value = rep.call(holder, key, value); |
| 262 | } |
| 263 | |
| 264 | // What happens next depends on the value's type. |
| 265 | |
| 266 | switch (typeof value) { |
| 267 | case 'string': |
| 268 | return quote(value); |
| 269 | |
| 270 | case 'number': |
| 271 | |
| 272 | // JSON numbers must be finite. Encode non-finite numbers as null. |
| 273 | |
| 274 | return isFinite(value) ? String(value) : 'null'; |
| 275 | |
| 276 | case 'boolean': |
| 277 | case 'null': |
| 278 | |
| 279 | // If the value is a boolean or null, convert it to a string. Note: |
| 280 | // typeof null does not produce 'null'. The case is included here in |
| 281 | // the remote chance that this gets fixed someday. |
| 282 | |
| 283 | return String(value); |
| 284 | |
| 285 | // If the type is 'object', we might be dealing with an object or an array or |
| 286 | // null. |
| 287 | |
| 288 | case 'object': |
| 289 | |
| 290 | // Due to a specification blunder in ECMAScript, typeof null is 'object', |
| 291 | // so watch out for that case. |
| 292 | |
| 293 | if (!value) { |
| 294 | return 'null'; |
| 295 | } |