Ajax でクライアント/サーバ間を JSON でやり取りしたかったので、プレーンデータを JSON 文字列に変換する関数を書いてみた。 ちなみに prototype.js には Object の拡張として toJSON() が定義されている。jQuery はプラグインとして存在する。 今回は単体の関数として引数にデータを渡すように書いてみた。 function toJSON(data){ if (data === undefined) return; if (data === null) return "null"; var type = typeof data; if (type == 'number' || type == 'boolean') { return data.toString(); } else if (type == 'function' || type == 'un