Parse JSON requestBodies so Client can consume them correctly

This commit is contained in:
Kyle Shockey
2017-09-05 15:07:55 -07:00
parent 7a2c7d2cdc
commit d14ae62aa5
2 changed files with 27 additions and 1 deletions

View File

@@ -13,6 +13,25 @@ const DEFAULT_REPONSE_KEY = "default"
export const isImmutable = (maybe) => Im.Iterable.isIterable(maybe)
export function isJSONObject (str) {
try {
var o = JSON.parse(str)
// Handle non-exception-throwing cases:
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
// but... JSON.parse(null) returns null, and typeof null === "object",
// so we must check for that, too. Thankfully, null is falsey, so this suffices:
if (o && typeof o === "object") {
return o
}
}
catch (e) {
// do nothing
}
return false
}
export function objectify (thing) {
if(!isObject(thing))
return {}