JS+JSON字符转义(escape)的几种方案
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
·
一、JSON.js
首先取自DouglasCrockford的方案,应该较多人使用。json.js和json2.js都差不多的。
// https://raw.github.com/douglascrockford/JSON-js/master/json.js
var meta = { // table of character substitutions
'\b' : '\\b',
'\t' : '\\t',
'\n' : '\\n',
'\f' : '\\f',
'\r' : '\\r',
'"' : '\\"',
'\\' : '\\\\'
};
function quote(string) {
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.
escapable.lastIndex = 0;
return escapable.test(string) ? '"'
+ string.replace(escapable, function(a) {
var c = meta[a];
return typeof c === 'string' ? c : '\\u'
+ ('0000' + a.charCodeAt(0).toString(16))
.slice(-4);
}) + '"' : '"' + string + '"';
}
二、依然取自D.C大大,在json_parse.js里。
// https://raw.github.com/douglascrockford/JSON-js/master/json_parse.js
var escapee = {
'"' : '"',
'\\' : '\\',
'/' : '/',
b : '\b',
f : '\f',
n : '\n',
r : '\r',
t : '\t'
};
string = function() {
// Parse a string value.
var hex, i, string = ', uffff;
// When parsing for string values, we must look for " and \ characters.
if (ch === '"') {
while (next()) {
if (ch === '"') {
next();
return string;
} else if (ch === '\\') {
next();
if (ch === 'u') {
uffff = 0;
for (i = 0; i < 4; i += 1) {
hex = parseInt(next(), 16);
if (!isFinite(hex)) {
break;
}
uffff = uffff * 16 + hex;
}
string += String.fromCharCode(uffff);
} else if (typeof escapee[ch] === 'string') {
string += escapee[ch];
} else {
break;
}
} else {
string += ch;
}
}
}
error("Bad string");
}
三、仍为D.C大大的,在json_parse_state.js里。
// https://raw.github.com/douglascrockford/JSON-js/master/json_parse_state.js
var escapes = { // Escapement translation table
'\\' : '\\',
'"' : '"',
'/' : '/',
't' : '\t',
'n' : '\n',
'r' : '\r',
'f' : '\f',
'b' : '\b'
};
function debackslashify(text) {
// Remove and replace any backslash escapement.
return text.replace(/\\(?:u(.{4})|([^u]))/g, function(a, b, c) {
return b ? String.fromCharCode(parseInt(b, 16)) : escapes[c];
});
}
四、旧版的json.js,moontools也是这种方案的?。这里/[\x00-\x1f\\"]/g代表什么意思呢?
// JSON用特殊字符转义
var special = {
'\b' : '\\b',
'\t' : '\\t',
'\n' : '\\n',
'\f' : '\\f',
'\r' : '\\r',
'"' : '\\"',
'\\' : '\\\\'
};
var escape = function(chr) {
return special[chr] || '\\u'
+ ('0000' + chr.charCodeAt(0).toString(16)).slice(-4)
};
function _escape(obj){
return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"'
}
五、我当前的方案,感觉比较简单明了,但会不会有未知的问题呢?待继续明了。
function StringAs(string) {
return '"' + string.replace(/(\\|\"|\n|\r|\t)/g, "\\$1") + '"';
}
六、最后发现这里也有http://relucent.iteye.com/blog/646016
源地址:
http://blog.csdn.net/zhangxin09/article/details/6582173
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 7 天前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 7 天前
更多推荐




所有评论(0)