Skip to content

Instantly share code, notes, and snippets.

@ovrmrw
Created January 7, 2016 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ovrmrw/9f639e7ff2c61faede43 to your computer and use it in GitHub Desktop.
Save ovrmrw/9f639e7ff2c61faede43 to your computer and use it in GitHub Desktop.
(未完成) FalcorでフロントからサーバーにJSONを送るとき、送る前に色々と文字置換をして、送った後にそれを復元する必要がある。
'use strict';
const obj = {
a: 1,
b: "two'`%#",
c: true,
d: null,
e: [1, '`two', true, null],
percentReplacer: '@PERSONT@',
sharpReplacer: '@SHARP@'
};
const json = JSON.stringify(obj);
console.log(json);
let flag = true;
const separaterSeed = "`";
let separater = "'" + separaterSeed;
while (flag) {
if (json.indexOf(separater) > -1) {
separater = separater + separaterSeed;
} else {
flag = false;
}
}
const json2 = separater + json.replace(/"/g, separater);
console.log(json2);
let json3 = json2;
if (obj.percentReplacer) {
json3 = json3.replace(/%/g, obj.percentReplacer);
}
if (obj.sharpReplacer) {
json3 = json3.replace(/#/g, obj.sharpReplacer);
}
console.log(json3);
//////////////////////////////////////////////////////
const s = json3.match(/^.*?{/)[0].slice(0, -1);
console.log(s);
let p = json3.replace(new RegExp(s, 'g'), '"').slice(1);
console.log(JSON.parse(p));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment