github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/iterator_read_array.go (about)

     1  package jzon
     2  
     3  func readArrayWithStack(it *Iterator, _ byte) (interface{}, error) {
     4  	c, err := it.nextToken()
     5  	if err != nil {
     6  		return nil, err
     7  	}
     8  	it.head++
     9  	topObj := make([]interface{}, 0)
    10  	if c == ']' {
    11  		return topObj, nil
    12  	}
    13  	for {
    14  		// We disabled the following switch to test benchmark
    15  		// comparing to using builtin stack
    16  		// the result is using our own stack will improve the
    17  		// performance by about 25%
    18  		switch c {
    19  		case '{':
    20  			s := stackPool.Get().(*stack).initArray()
    21  			ns := nodeStackPool.Get().(*nodeStack).
    22  				initArray(topObj).
    23  				pushObject("")
    24  			ret, err := readWithStack(it, stackElementObjectBegin, s, ns)
    25  			releaseNodeStack(ns)
    26  			stackPool.Put(s)
    27  			return ret, err
    28  		case '[':
    29  			s := stackPool.Get().(*stack).initArray()
    30  			ns := nodeStackPool.Get().(*nodeStack).
    31  				initArray(topObj).
    32  				pushArray("")
    33  			ret, err := readWithStack(it, stackElementArrayBegin, s, ns)
    34  			releaseNodeStack(ns)
    35  			stackPool.Put(s)
    36  			return ret, err
    37  		}
    38  		o, err := readFunctions[c](it, c)
    39  		if err != nil {
    40  			return nil, err
    41  		}
    42  		topObj = append(topObj, o)
    43  		c, err = it.nextToken()
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  		it.head++
    48  		if c == ']' {
    49  			return topObj, nil
    50  		}
    51  		if c != ',' {
    52  			return nil, UnexpectedByteError{got: c, exp: ',', exp2: ']'}
    53  		}
    54  		c, err = it.nextToken()
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  		it.head++
    59  	}
    60  }