...

Source file src/github.com/launchdarkly/go-jsonstream/v3/jwriter/writer_array.go

Documentation: github.com/launchdarkly/go-jsonstream/v3/jwriter

     1  package jwriter
     2  
     3  import "encoding/json"
     4  
     5  // ArrayState is a decorator that manages the state of a JSON array that is in the process of being
     6  // written.
     7  //
     8  // Calling Writer.Array() or ObjectState.Array() creates an ArrayState. Until ArrayState.End() is
     9  // called, writing any value to either the ArrayState or the Writer will cause commas to be added
    10  // between values as needed.
    11  type ArrayState struct {
    12  	w             *Writer
    13  	previousState writerState
    14  }
    15  
    16  // Null is equivalent to writer.Null().
    17  func (arr *ArrayState) Null() {
    18  	if arr.w != nil {
    19  		arr.w.Null()
    20  	}
    21  }
    22  
    23  // Bool is equivalent to writer.Bool(value).
    24  func (arr *ArrayState) Bool(value bool) {
    25  	if arr.w != nil {
    26  		arr.w.Bool(value)
    27  	}
    28  }
    29  
    30  // Int is equivalent to writer.Int(value).
    31  func (arr *ArrayState) Int(value int) {
    32  	if arr.w != nil {
    33  		arr.w.Int(value)
    34  	}
    35  }
    36  
    37  // Float64 is equivalent to writer.Float64(value).
    38  func (arr *ArrayState) Float64(value float64) {
    39  	if arr.w != nil {
    40  		arr.w.Float64(value)
    41  	}
    42  }
    43  
    44  // String is equivalent to writer.String(value).
    45  func (arr *ArrayState) String(value string) {
    46  	if arr.w != nil {
    47  		arr.w.String(value)
    48  	}
    49  }
    50  
    51  // Array is equivalent to calling writer.Array(), to create a nested array.
    52  func (arr *ArrayState) Array() ArrayState {
    53  	if arr.w != nil {
    54  		return arr.w.Array()
    55  	}
    56  	return ArrayState{}
    57  }
    58  
    59  // Object is equivalent to calling writer.Object(), to create a nested object.
    60  func (arr *ArrayState) Object() ObjectState {
    61  	if arr.w != nil {
    62  		return arr.w.Object()
    63  	}
    64  	return ObjectState{}
    65  }
    66  
    67  // Raw is equivalent to calling writer.Raw().
    68  func (arr *ArrayState) Raw(value json.RawMessage) {
    69  	if arr.w != nil {
    70  		arr.w.Raw(value)
    71  	}
    72  }
    73  
    74  // End writes the closing delimiter of the array.
    75  func (arr *ArrayState) End() {
    76  	if arr.w == nil || arr.w.err != nil {
    77  		return
    78  	}
    79  	arr.w.AddError(arr.w.tw.Delimiter(']'))
    80  	arr.w.state = arr.previousState
    81  	arr.w = nil
    82  }
    83  

View as plain text