Permalink
Browse files

more strict string parsing

  • Loading branch information...
1 parent 96631e0 commit 45ff1fd3a950a223f15359cd23bba1354d56053f @lm committed Feb 26, 2012
Showing with 14 additions and 1 deletion.
  1. +11 −1 Json/Json.st
  2. +3 −0 tests/JsonTest.st
View
@@ -100,6 +100,9 @@ Object subclass: Json [
(stream peekFor: $") ifFalse: [self errorExpected: 'string' was: stream peek].
[(char := stream next) isNil or: [char = $"]] whileFalse: [
+ char < LastEncodedCharacter ifTrue: [
+ self errorExpected: 'printable character' was: 'control character'].
+
char = $\
ifTrue: [writeStream nextPutAll: self nextEscapedCharacter]
ifFalse: [writeStream nextPut: char]
@@ -124,12 +127,19 @@ Object subclass: Json [
char == $r ifTrue: [^Character cr asString].
char == $t ifTrue: [^Character tab asString].
char == $u ifTrue: [^self nextUnicodeEscapedCharacter asString].
+
+ self errorExpected: '", \, /, b, n, f, r, t, u' was: char asString.
]
nextUnicodeEscapedCharacter [
<category: 'private'>
- ^Character codePoint: (Integer readFrom: (stream next: 4) readStream radix: 16)
+ | codePoint |
+
+ (codePoint := stream nextAvailable: 4) size < 4 ifTrue: [
+ self errorExpected: '4 hex digits' was: codePoint].
+
+ ^Character codePoint: (Integer readFrom: codePoint readStream radix: 16)
]
nextSeparator [
View
@@ -123,6 +123,9 @@ TestCase subclass: JsonTest [
parseJson: 'f' error: 'Expected false but f found';
parseJson: 'n' error: 'Expected null but n found';
parseJson: '"s' error: 'Expected character but end of input found';
+ parseJson: '"', Character nl asString, '"' error: 'Expected printable character but control character found';
+ parseJson: '"\uA' error: 'Expected 4 hex digits but A found';
+ parseJson: '"\x' error: 'Expected ", \, /, b, n, f, r, t, u but x found';
parseJson: '0 1' error: 'Expected end of input but 1 found'.
]

0 comments on commit 45ff1fd

Please sign in to comment.