-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix single \ chars and fruity quote chars in JSON
- Loading branch information
Showing
3 changed files
with
51 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"use strict"; | ||
|
||
function replace_all(s, search, replace) { | ||
if (!s.includes(search)) { // Seems to improve speed overall. | ||
return s; | ||
} | ||
return s.split(search).join(replace); | ||
} | ||
|
||
function debork_json(s) { | ||
|
||
// Convert totally blank files into {} | ||
|
||
if (s.length < 50 && s.trim() === "") { | ||
s = "{}"; | ||
} | ||
|
||
// Replace fruity quote characters. Note that these could exist in legit JSON, | ||
// which is why we only call this function if the first parse fails... | ||
|
||
s = replace_all(s, '“', '"'); | ||
s = replace_all(s, '”', '"'); | ||
|
||
// Replace single \ characters | ||
|
||
s = replace_all(s, "\\\\", "correct_zbcyg278gfdakjadjk"); | ||
s = replace_all(s, "\\", "\\\\"); | ||
s = replace_all(s, "correct_zbcyg278gfdakjadjk", "\\\\"); | ||
|
||
return s; | ||
} | ||
|
||
module.exports = debork_json; |
This file contains 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