]> Pileus Git - ~andy/rhawk/commitdiff
Handle special JSON characters
authorAndy Spencer <andy753421@gmail.com>
Thu, 30 Oct 2014 04:08:40 +0000 (04:08 +0000)
committerAndy Spencer <andy753421@gmail.com>
Thu, 30 Oct 2014 04:08:46 +0000 (04:08 +0000)
json.awk

index 90bd38f716e5547ebedce02689c802bcf0e7b994..70d68f4d0d0edc7d798d45bfa5a11250ea7c5c3a 100644 (file)
--- a/json.awk
+++ b/json.awk
@@ -117,7 +117,14 @@ function json_write_number(number)
 
 function json_write_string(string)
 {
 
 function json_write_string(string)
 {
-       # todo: special characters
+       gsub(/\\/, "\\\\", string)
+       gsub(/"/,  "\\\"", string)
+       gsub(/\b/, "\\b",  string)
+       gsub(/\f/, "\\f",  string)
+       gsub(/\n/, "\\n",  string)
+       gsub(/\r/, "\\r",  string)
+       gsub(/\t/, "\\t",  string)
+
        return "\"" string "\""
 }
 
        return "\"" string "\""
 }
 
@@ -126,7 +133,7 @@ function json_write_string(string)
 function json_tokenize(str, tokens,   i, line, items, table, type, found)
 {
        table["term"]  = "^[\\[\\]{}:,]"
 function json_tokenize(str, tokens,   i, line, items, table, type, found)
 {
        table["term"]  = "^[\\[\\]{}:,]"
-       table["str"]   = "^\"[^\"]*\""
+       table["str"]   = "^\"([^\\\\\"]|\\\\.)*\""
        table["num"]   = "^[+-]?[0-9]+(.[0-9]+)?"
        table["var"]   = "^(true|false|null)"
        table["space"] = "^[ \\t]+"
        table["num"]   = "^[+-]?[0-9]+(.[0-9]+)?"
        table["var"]   = "^(true|false|null)"
        table["space"] = "^[ \\t]+"
@@ -159,11 +166,12 @@ function json_tokenize(str, tokens,   i, line, items, table, type, found)
                        return 0
                }
        }
                        return 0
                }
        }
-       return i
 
        #for (i = 0; i < length(tokens); i++)
        #       printf "%-3s %-5s [%s]\n", i":",
        #               tokens[i]["type"], tokens[i]["text"]
 
        #for (i = 0; i < length(tokens); i++)
        #       printf "%-3s %-5s [%s]\n", i":",
        #               tokens[i]["type"], tokens[i]["text"]
+
+       return i
 }
 
 function json_parse_value(tokens, i, value, key,   line, type, text)
 }
 
 function json_parse_value(tokens, i, value, key,   line, type, text)
@@ -249,6 +257,15 @@ function json_parse_string(tokens, i, value, key,   text)
        text = tokens[i++]["text"]
        len  = length(text);
        text = len == 2 ? "" : substr(text, 2, len-2)
        text = tokens[i++]["text"]
        len  = length(text);
        text = len == 2 ? "" : substr(text, 2, len-2)
+
+       gsub(/\\\\/, "\\", text)
+       gsub(/\\"/,  "\"", text)
+       gsub(/\\b/,  "\b", text)
+       gsub(/\\f/,  "\f", text)
+       gsub(/\\n/,  "\n", text)
+       gsub(/\\r/,  "\r", text)
+       gsub(/\\t/,  "\t", text)
+
        json_copy(value, key, text)
        #print "parse_string: [" text "]"
        return i
        json_copy(value, key, text)
        #print "parse_string: [" text "]"
        return i
@@ -270,7 +287,7 @@ function json_parse_var(tokens, i, value, key,   text, null)
 function json_load(file, var,   line, text, tokens, data, key)
 {
        while ((getline line < file) > 0)
 function json_load(file, var,   line, text, tokens, data, key)
 {
        while ((getline line < file) > 0)
-               text = text line
+               text = text line "\n"
        close(file)
        if (!json_tokenize(text, tokens))
                return ""
        close(file)
        if (!json_tokenize(text, tokens))
                return ""
@@ -344,7 +361,8 @@ function json_test_read()
               "                \"str\":    \"hello, world\" }, \n" \
               "  \"second\": { \"arr\":    [ \"zero\",         \n" \
               "                              \"one\",          \n" \
               "                \"str\":    \"hello, world\" }, \n" \
               "  \"second\": { \"arr\":    [ \"zero\",         \n" \
               "                              \"one\",          \n" \
-              "                              \"two\" ],        \n" \
+              "                              \"two\",          \n" \
+              "                              \"\\\"\t\r\b\" ], \n" \
               "                \"number\": 42,                 \n" \
               "                \"obj\":    { \"A\": \"a!\",    \n" \
               "                              \"B\": \"b!\",    \n" \
               "                \"number\": 42,                 \n" \
               "                \"obj\":    { \"A\": \"a!\",    \n" \
               "                              \"B\": \"b!\",    \n" \