Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

error message now contains line and column

  • Loading branch information...
commit c0c6035d7d90acef92fd89d9402ca66c54e3bda9 1 parent 45ff1fd
Ladislav Marek authored
16  Json/Json.st
... ...
@@ -1,5 +1,5 @@
1 1
 Object subclass: Json [
2  
-	| stream |
  2
+	| stream line linePosition |
3 3
 
4 4
 	<category: 'json'>
5 5
 	<comment: 'This class reads and writes JSON format data - strings, numbers, boolean, nil, arrays and dictionaries. See http://json.org.'>
@@ -64,6 +64,8 @@ Object subclass: Json [
64 64
 		<category: 'initialising'>
65 65
 
66 66
 		stream := anObject.
  67
+		line := 1.
  68
+		linePosition := 0.
67 69
 	]
68 70
 
69 71
 	stream [
@@ -147,8 +149,10 @@ Object subclass: Json [
147 149
 
148 150
 		| char |
149 151
 
150  
-		[(char := stream peek) notNil and: [char isSeparator]]
151  
-			whileTrue: [stream next].
  152
+		[(char := stream peek) notNil and: [char isSeparator]] whileTrue: [
  153
+			stream next = Character nl ifTrue: [
  154
+				line := line + 1.
  155
+				linePosition := stream position]].
152 156
 	]
153 157
 
154 158
 	nextDictionary [
@@ -271,6 +275,10 @@ Object subclass: Json [
271 275
 		<category: 'error raising'>
272 276
 
273 277
 		JsonSyntaxError signal:
274  
-			'Expected %1 but %2 found' % {aString. currrentString ifNil: ['end of input']}
  278
+			'Expected %1 but %2 found on line %3 column %4' %
  279
+				{aString.
  280
+				currrentString ifNil: ['end of input'].
  281
+				line.
  282
+				stream position - linePosition}
275 283
 	]
276 284
 ]
22  tests/JsonTest.st
@@ -116,17 +116,17 @@ TestCase subclass: JsonTest [
116 116
 
117 117
 	testErrorMessages [
118 118
 		self
119  
-			parseJson: '[' error: 'Expected string, object, array, true, false, null or digit but end of input found';
120  
-			parseJson: '{' error: 'Expected string but end of input found';
121  
-			parseJson: '{""' error: 'Expected : but end of input found';
122  
-			parseJson: 't' error: 'Expected true but t found';
123  
-			parseJson: 'f' error: 'Expected false but f found';
124  
-			parseJson: 'n' error: 'Expected null but n found';
125  
-			parseJson: '"s' error: 'Expected character but end of input found';
126  
-			parseJson: '"', Character nl asString, '"' error: 'Expected printable character but control character found';
127  
-			parseJson: '"\uA' error: 'Expected 4 hex digits but A found';
128  
-			parseJson: '"\x' error: 'Expected ", \, /, b, n, f, r, t, u but x found';
129  
-			parseJson: '0 1' error: 'Expected end of input but 1 found'.
  119
+			parseJson: '[' error: 'Expected string, object, array, true, false, null or digit but end of input found on line 1 column 1';
  120
+			parseJson: '{' error: 'Expected string but end of input found on line 1 column 1';
  121
+			parseJson: '{""' error: 'Expected : but end of input found on line 1 column 3';
  122
+			parseJson: 't' error: 'Expected true but t found on line 1 column 1';
  123
+			parseJson: 'f' error: 'Expected false but f found on line 1 column 1';
  124
+			parseJson: 'n' error: 'Expected null but n found on line 1 column 1';
  125
+			parseJson: '"s' error: 'Expected character but end of input found on line 1 column 2';
  126
+			parseJson: '"', Character nl asString, '"' error: 'Expected printable character but control character found on line 1 column 2';
  127
+			parseJson: '"\uA' error: 'Expected 4 hex digits but A found on line 1 column 4';
  128
+			parseJson: '"\x' error: 'Expected ", \, /, b, n, f, r, t, u but x found on line 1 column 3';
  129
+			parseJson: '0 1' error: 'Expected end of input but 1 found on line 1 column 2'.
130 130
 	]
131 131
 
132 132
 	parseJson: aString error: aMessageString [

0 notes on commit c0c6035

Please sign in to comment.
Something went wrong with that request. Please try again.