@@ -50,6 +50,9 @@ func NewParser() Interface {
5050
5151func (p * jsonParser ) Init (buf []byte ) error {
5252 p .parser .Init (buf )
53+ p .action = nextAction
54+ p .state = state {}
55+ p .stack = p .stack [:0 ]
5356 p .pool .FreeAll ()
5457 return nil
5558}
@@ -58,9 +61,6 @@ func (p *jsonParser) nextAtStartState(action action) error {
5861 fmt .Printf ("nextAtStartState\n " )
5962 switch action {
6063 case nextAction :
61- // find value type
62- // if object Next over open
63- // if array: TODO
6464 parseKind , err := p .parser .Next ()
6565 if err != nil {
6666 return err
@@ -77,8 +77,16 @@ func (p *jsonParser) nextAtStartState(action action) error {
7777 }
7878 return nil
7979 case parse .ArrayOpenKind :
80- p .state .kind = inArrayStateKind
81- panic ("TODO" )
80+ p .state .kind = inArrayIndexStateKind
81+ parseKindNext , err := p .parser .Next ()
82+ if err != nil {
83+ return err
84+ }
85+ if parseKindNext == parse .ArrayCloseKind {
86+ return p .eof ()
87+ }
88+ p .state .arrayElemKind = parseKindNext
89+ return nil
8290 case parse .NullKind , parse .BoolKind , parse .NumberKind , parse .StringKind :
8391 p .state .kind = inLeafStateKind
8492 return nil
@@ -215,6 +223,108 @@ func (p *jsonParser) nextInObjectAtValueState(action action) error {
215223 panic ("unreachable" )
216224}
217225
226+ func (p * jsonParser ) nextInArrayIndexState (action action ) error {
227+ fmt .Printf ("nextInArrayIndexState\n " )
228+ // inArrayIndexState represents that we have scanned an element, if it was null, bool, number or string and the first key of an object or .
229+ switch action {
230+ case nextAction :
231+ p .state .arrayIndex += 1
232+ switch p .state .arrayElemKind {
233+ case parse .ObjectOpenKind , parse .ArrayOpenKind :
234+ if err := p .parser .Skip (); err != nil {
235+ return err
236+ }
237+ case parse .NullKind , parse .BoolKind , parse .NumberKind , parse .StringKind :
238+ default :
239+ panic ("unreachable" )
240+ }
241+ parseKind , err := p .parser .Next ()
242+ if err != nil {
243+ return err
244+ }
245+ if parseKind == parse .ArrayCloseKind {
246+ return p .eof ()
247+ }
248+ p .state .arrayElemKind = parseKind
249+ return nil
250+ case downAction :
251+ // We are at an array element that we are representing as an index.
252+ // We do not need parse another thing, simply update the state.
253+ p .state .kind = inArrayAfterIndexStateKind
254+ if err := p .push (); err != nil {
255+ return err
256+ }
257+ switch p .state .arrayElemKind {
258+ case parse .ObjectOpenKind :
259+ p .state .kind = inObjectAtKeyStateKind
260+ parseKindNext , err := p .parser .Next ()
261+ if err != nil {
262+ return err
263+ }
264+ if parseKindNext == parse .ObjectCloseKind {
265+ return p .eof ()
266+ }
267+ return nil
268+ case parse .ArrayOpenKind :
269+ p .state .kind = inArrayIndexStateKind
270+ parseKindNext , err := p .parser .Next ()
271+ if err != nil {
272+ return err
273+ }
274+ if parseKindNext == parse .ArrayCloseKind {
275+ return p .eof ()
276+ }
277+ p .state .arrayElemKind = parseKindNext
278+ return nil
279+ case parse .NullKind , parse .BoolKind , parse .NumberKind , parse .StringKind :
280+ p .state .kind = inLeafStateKind
281+ return nil
282+ }
283+ panic ("unreachable" )
284+ case upAction :
285+ // Skip the rest of the array
286+ if err := p .parser .Skip (); err != nil {
287+ return err
288+ }
289+ if err := p .pop (); err != nil {
290+ return err
291+ }
292+ return p .next ()
293+ }
294+ panic ("unreachable" )
295+ }
296+
297+ func (p * jsonParser ) nextInArrayAfterIndexState (action action ) error {
298+ fmt .Printf ("nextInArrayAfterIndexState\n " )
299+ // This is after Up was called on an element.
300+ switch action {
301+ case nextAction :
302+ p .state .arrayIndex += 1
303+ parseKind , err := p .parser .Next ()
304+ if err != nil {
305+ return err
306+ }
307+ if parseKind == parse .ArrayCloseKind {
308+ return p .eof ()
309+ }
310+ p .state .kind = inArrayIndexStateKind
311+ p .state .arrayElemKind = parseKind
312+ return nil
313+ case downAction :
314+ return errDown
315+ case upAction :
316+ // Skip the rest of the array
317+ if err := p .parser .Skip (); err != nil {
318+ return err
319+ }
320+ if err := p .pop (); err != nil {
321+ return err
322+ }
323+ return p .next ()
324+ }
325+ panic ("unreachable" )
326+ }
327+
218328func (p * jsonParser ) eof () error {
219329 // When EOF is returned also set the state to an EOF state.
220330 // This state allows us to call Up.
@@ -232,8 +342,10 @@ func (p *jsonParser) next() error {
232342 return p .nextAtStartState (action )
233343 case inLeafStateKind :
234344 return p .nextInLeafState (action )
235- case inArrayStateKind :
236- panic ("TODO" )
345+ case inArrayIndexStateKind :
346+ return p .nextInArrayIndexState (action )
347+ case inArrayAfterIndexStateKind :
348+ return p .nextInArrayAfterIndexState (action )
237349 case inObjectAtKeyStateKind :
238350 return p .nextInObjectAtKeyState (action )
239351 case inObjectAtValueStateKind :
@@ -264,6 +376,7 @@ func (p *jsonParser) push() error {
264376 // Append the current state to the stack.
265377 p .stack = append (p .stack , p .state )
266378 p .state .kind = atStartStateKind
379+ p .state .arrayIndex = 0
267380 return nil
268381}
269382
@@ -291,7 +404,7 @@ func (p *jsonParser) Bool() (bool, error) {
291404}
292405
293406func (p * jsonParser ) Int () (int64 , error ) {
294- if p .state .kind == inArrayStateKind {
407+ if p .state .kind == inArrayIndexStateKind {
295408 return p .state .arrayIndex , nil
296409 }
297410 return p .parser .Int ()
0 commit comments