@@ -20,6 +20,7 @@ class JSONCharInputReader
20
20
private $ outputInterface ;
21
21
private $ state ;
22
22
private $ depth ;
23
+ private $ quoteCount ;
23
24
24
25
/**
25
26
* Create a JSONCharInputReader object
@@ -34,6 +35,7 @@ public function __construct(JSONChunkProcessor $outputInterface)
34
35
$ this ->state = self ::STATE_OUTSIDE ;
35
36
$ this ->outputInterface = $ outputInterface ;
36
37
$ this ->depth = 0 ;
38
+ $ this ->quoteCount = 0 ;
37
39
}
38
40
39
41
/**
@@ -62,27 +64,39 @@ public function readChar($char)
62
64
case self ::STATE_INCURLY :
63
65
case self ::STATE_INSQUARE :
64
66
67
+ if ($ char == '" ' && !$ this ->lastCharIs ('\\' ))
68
+ $ this ->quoteCount ++;
69
+
65
70
$ this ->buffer .= $ char ;
66
71
72
+ // if quote count is odd we're inside a string....
73
+ if ($ this ->quoteCount % 2 )
74
+ break ;
75
+
67
76
$ closing = $ this ->state == self ::STATE_INCURLY ? '} ' : '] ' ;
68
77
$ opening = $ this ->state == self ::STATE_INCURLY ? '{ ' : '[ ' ;
69
78
70
79
if ($ char == $ opening )
71
80
// if this is another opening brace/bracket character, increase the depth
72
81
$ this ->depth ++;
73
82
else if ($ char == $ closing && --$ this ->depth == 0 )
83
+ {
74
84
// if this is a closing character, decrease the depth and process the buffer if
75
85
// the bottom was reached
76
86
$ this ->processBuffer ();
87
+ $ this ->quoteCount = 0 ;
88
+ }
77
89
78
90
break ;
79
91
80
92
// Inside a string
81
93
case self ::STATE_INSTRING :
82
- $ this -> buffer .= $ char ;
83
- if ($ char == '" ' )
94
+
95
+ if ($ char == '" ' && ! $ this -> lastCharIs ( '\\' ) )
84
96
$ this ->state = self ::STATE_WAITING ;
85
97
98
+ $ this ->buffer .= $ char ;
99
+
86
100
break ;
87
101
88
102
// Waiting on any input within a JSON stream
@@ -136,6 +150,15 @@ public function readChar($char)
136
150
}
137
151
}
138
152
153
+ private function lastCharIs ($ char ) {
154
+ $ len = strlen ($ this ->buffer );
155
+ if ($ len == 0 )
156
+ return false ;
157
+
158
+ $ lastChar = $ this ->buffer [$ len - 1 ];
159
+ return $ lastChar === $ char ;
160
+ }
161
+
139
162
/**
140
163
* Process the JSON data stream's buffer and reset the state
141
164
*
0 commit comments