Commit 422f3aa9f270f07d258b0b7e8d107303343f180e

Authored by totoro
1 parent 9b0c86d0

add escaping CR LF and TAB characters

allow to have simple quote in string
add write_to_file function to save Json on disk
Showing 1 changed file with 15 additions and 4 deletions   Show diff stats
web/CXM_json.anubis
@@ -100,10 +100,13 @@ define List(Word8) @@ -100,10 +100,13 @@ define List(Word8)
100 { 100 {
101 [] then new_string, 101 [] then new_string,
102 [h . t] then 102 [h . t] then
103 - if (h >=+ 32 & h +=< 33) | (h >=+ 35 & h +=< 38) | (h >=+ 40 & h +=< 91) | h >=+ 93 then // Allowed characters for JSON see RFC4627 + " ' " 103 + if (h >=+ 32 & h +=< 33) | (h >=+ 35 & h +=< 91) | h >=+ 93 then // Allowed characters for JSON see RFC4627 + " ' " Why ' ???
104 _format_json_esc(t, [h . new_string]) 104 _format_json_esc(t, [h . new_string])
105 - else  
106 - _format_json_esc(t, [h, '\\' . new_string]) 105 + else if h = '\n' then _format_json_esc(t, ['n', '\\' . new_string])
  106 + else if h = '\r' then _format_json_esc(t, ['r', '\\' . new_string])
  107 + else if h = '\t' then _format_json_esc(t, ['t', '\\' . new_string])
  108 + else if h = 0x08 then _format_json_esc(t, ['b', '\\' . new_string]) //Backspace
  109 + else _format_json_esc(t, [h, '\\' . new_string])
107 }. 110 }.
108 111
109 public define String 112 public define String
@@ -166,4 +169,12 @@ public define Printable_tree @@ -166,4 +169,12 @@ public define Printable_tree
166 json_bool(b) then str_pt(if b then "true" else "false", []), 169 json_bool(b) then str_pt(if b then "true" else "false", []),
167 json_null then str_pt("null", []) 170 json_null then str_pt("null", [])
168 }. 171 }.
169 - 172 +
  173 +public define WriteFileResult
  174 + write_to_file
  175 + (
  176 + String filename, //filename where the JSON will be dump
  177 + JsonValue json //JSON to dump
  178 + )=
  179 + write_to_file(filename, format_json(json)).
  180 +