| 1 |
export default ( jsonString, forceJson = true ) => { |
| 2 |
try { |
| 3 |
var o = JSON.parse( jsonString ); |
| 4 |
|
| 5 |
// Handle non-exception-throwing cases: |
| 6 |
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking, |
| 7 |
// but... JSON.parse(null) returns null, and typeof null === "object", |
| 8 |
// so we must check for that, too. Thankfully, null is falsey, so this suffices: |
| 9 |
if (o && typeof o === "object") { |
| 10 |
return o; |
| 11 |
} |
| 12 |
} |
| 13 |
catch (e) { } |
| 14 |
if ( jsonString && typeof jsonString === "object" ) { |
| 15 |
return jsonString; |
| 16 |
} |
| 17 |
if ( forceJson ) { |
| 18 |
return {}; |
| 19 |
} |
| 20 |
return false; |
| 21 |
}; |