Created
December 18, 2012 00:46
-
-
Save johan/4323895 to your computer and use it in GitHub Desktop.
KissMetrics' idea of "json" isn't very canon. This hack mends json-illegal octal escapes.
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
#! /usr/bin/env pike | |
inherit Tools.Standalone.process_files; | |
string version = "1.0"; | |
string description = "Replaces \\oct escapes with \\u00XX escapes."; | |
string usage = #"[options] <files> | |
jsonish-to-json is specifically suited to mending KissMetrics \"json\" dumps. | |
Available options: | |
" + default_flag_docs; | |
string encode(int c) { | |
switch (c) { | |
case '\b': case '\t': case '\n': case '\f': case '\r': | |
case 32..127: return (string)({ c }); | |
default: return sprintf("\\u00%2x", c); | |
} | |
} | |
string process(string jsonish) { | |
mapping changes = ([]); | |
for (int i = 255; i >= 0; i--) { | |
string c = (string)({ i }), e = encode(i); | |
if (c == e || c == "\\") continue; // no need to change encoding of these | |
changes[c] = e; | |
changes[sprintf("\\00%o", i)] = e; | |
changes[sprintf("\\0%o", i)] = e; | |
changes[sprintf("\\%o", i)] = e; | |
} | |
return replace(jsonish, changes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment