Skip to content

Instantly share code, notes, and snippets.

@johan
Created December 18, 2012 00:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johan/4323895 to your computer and use it in GitHub Desktop.
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.
#! /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