Skip to content

Commit

Permalink
Fix invalid encoding of control characters. #11
Browse files Browse the repository at this point in the history
  • Loading branch information
aw committed Mar 20, 2018
1 parent 67da9e9 commit d0b70b7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 3.5.0 (2018-03-20)

* Fix issue #11 - Invalid encoding of special control characters (^J, ^M, ^I)
* Ensure ^H (\b) and ^L (\f) are also encoded correctly
* Add regression tests for this issue

## 3.4.0 (2018-03-19)

* Fix issue #10 - Invalid parsing of strings with caret (^)
Expand Down
15 changes: 14 additions & 1 deletion json.l
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,20 @@
(when (num? Value) Value) ]

[de make-json-string (Value)
(when (str? Value) (pack "\"" (filter '((N) (if (= "\\\^" N) "\^" N)) (chop Value)) "\"") ]
(when (str? Value)
(pack
"\""
(extract '((N)
(case N
("^H" "\\b")
("^L" "\\f")
("^J" "\\n")
("^M" "\\r")
("^I" "\\t")
("\\\^" "\^")
(T N) ) )
(chop Value) )
"\"" ]

[de make-json-array (Value)
(when (=T (car Value)) (make-array (cdr Value))) ]
Expand Down
2 changes: 1 addition & 1 deletion module.l
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[de MODULE_INFO
("name" "json")
("version" "3.4.0")
("version" "3.5.0")
("summary" "JSON encoder/decoder for PicoLisp")
("source" "https://github.com/aw/picolisp-json.git")
("author" "Alexander Williams")
Expand Down
12 changes: 11 additions & 1 deletion test/test_regressions.l
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,20 @@
(encode '(("test" . "x\^2\\u1234")))
"Regression test GH issue #10 - Invalid parsing of caret (\^) characters (4)" ]

# Invalid encoding of special control (^J^M^I) characters - https://github.com/aw/picolisp-json/issues/11
[de test-gh-issue-11 ()
(assert-equal "{\"test\":\"\\n\\r\\t\\b\\f\"}"
(encode (decode "../test5.json" T))
"Regression test GH issue #11 - Invalid encoding of special control (\^J\^M\^I) characters (1)" )
(assert-equal "{\"test\":\"Hello\\n\\r\\t\\b\\fWorld\"}"
(encode (list (cons "test" "Hello^J^M^I^H^LWorld")))
"Regression test GH issue #11 - Invalid encoding of special control (\^J\^M\^I) characters (2)" ) ]

[execute
'(test-gh-issue-4)
'(test-gh-issue-5)
'(test-gh-issue-6)
'(test-gh-issue-8)
(when symbols '(test-gh-issue-9))
'(test-gh-issue-10) ]
'(test-gh-issue-10)
'(test-gh-issue-11) ]
1 change: 1 addition & 0 deletions test5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"test":"\n\r\t\b\f"}

0 comments on commit d0b70b7

Please sign in to comment.