Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:13
Show Gist options
  • Save st98/ad1a638d1ae7e180b702 to your computer and use it in GitHub Desktop.
Save st98/ad1a638d1ae7e180b702 to your computer and use it in GitHub Desktop.
不思議な JSON.stringify の第 2 引数。
# 参考: ネイティブ JSON を使う
# (https://developer.mozilla.org/ja/docs/Using_native_JSON#replacer_.E3.83.91.E3.83.A9.E3.83.A1.E3.83.BC.E3.82.BF)
obj =
a: 12
b: 'hoge'
c: true
d: undefined
e: 'fuga'
f: [12, 'piyo', true]
f = (k, v) ->
if k is 'e'
return undefined
switch typeof v
when 'number' then v * 2
when 'string' then v.repeat 2
when 'boolean' then !v
when 'undefined' then null
else v
JSON.stringify obj, f, ' '
###
{
"a": 24,
"b": "hogehoge",
"c": false,
"d": null,
"f": [
24,
"piyopiyo",
false
]
}
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment