Created
December 20, 2012 02:06
-
-
Save quackingduck/4342416 to your computer and use it in GitHub Desktop.
Parse a json encoded hashmap or return an empty one and print a warning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Parse a json encoded hashmap or return an empty one and print a warning | |
parseMsg = (jsonStr) -> | |
try json = JSON.parse jsonStr | |
catch err then handleParseErr err, jsonStr | |
json ? {} | |
handleParseErr = (err, jsonStr) -> | |
console.error '[WARN]','JSON parser:', err, 'input json was:', jsonStr | |
if process.argv[1] is __filename | |
assert = require 'assert' | |
assert.deepEqual (parseMsg '{'), {} | |
assert.deepEqual (parseMsg 'daskh das'), {} | |
assert.deepEqual (parseMsg '{ "foo": 1 }'), { foo: 1 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment