func MarshalJSONWithWriter(writable Writable) ([]byte, error)
MarshalJSONWithWriter is a convenience method for implementing json.Marshaler to marshal to a byte slice with the default TokenWriter implementation.
ArrayState is a decorator that manages the state of a JSON array that is in the process of being written.
Calling Writer.Array() or ObjectState.Array() creates an ArrayState. Until ArrayState.End() is called, writing any value to either the ArrayState or the Writer will cause commas to be added between values as needed.
type ArrayState struct {
// contains filtered or unexported fields
}
func (arr *ArrayState) Array() ArrayState
Array is equivalent to calling writer.Array(), to create a nested array.
▹ Example
func (arr *ArrayState) Bool(value bool)
Bool is equivalent to writer.Bool(value).
▹ Example
func (arr *ArrayState) End()
End writes the closing delimiter of the array.
func (arr *ArrayState) Float64(value float64)
Float64 is equivalent to writer.Float64(value).
▹ Example
func (arr *ArrayState) Int(value int)
Int is equivalent to writer.Int(value).
▹ Example
func (arr *ArrayState) Null()
Null is equivalent to writer.Null().
▹ Example
func (arr *ArrayState) Object() ObjectState
Object is equivalent to calling writer.Object(), to create a nested object.
▹ Example
func (arr *ArrayState) Raw(value json.RawMessage)
Raw is equivalent to calling writer.Raw().
▹ Example
func (arr *ArrayState) String(value string)
String is equivalent to writer.String(value).
▹ Example
ObjectState is a decorator that writes values to an underlying Writer within the context of a JSON object, adding property names and commas between values as appropriate.
type ObjectState struct {
// contains filtered or unexported fields
}
func (obj *ObjectState) End()
End writes the closing delimiter of the object.
func (obj *ObjectState) Maybe(name string, shouldWrite bool) *Writer
Maybe writes an object property name conditionally depending on a boolean parameter. If shouldWrite is true, this behaves the same as Property(name). However, if shouldWrite is false, it does not write a property name and instead of returning the underlying Writer, it returns a stub Writer that does not produce any output. This allows you to chain method calls without having to use an if statement.
obj.Maybe(shouldWeIncludeTheProperty, "myBooleanProperty").Bool(true)
▹ Example
func (obj *ObjectState) Name(name string) *Writer
Name writes an object property name and a colon. You can then use Writer methods to write the property value. The return value is the same as the underlying Writer, so you can chain method calls:
obj.Name("myBooleanProperty").Bool(true)
▹ Example
Writable is an interface for types that can write their data to a Writer.
type Writable interface { // WriteToJSONWriter writes JSON content to the Writer. // // This method does not need to return an error value. If the Writer encounters an error during output // generation, it will remember its own error state, which can be detected with Writer.Error(). WriteToJSONWriter(*Writer) }
Writer is a high-level API for writing JSON data sequentially.
It is designed to make writing custom marshallers for application types as convenient as possible. The general usage pattern is as follows:
- There is one method for each JSON data type.
- For writing array or object structures, the Array and Object methods return a struct that keeps track of additional writer state while that structure is being written.
- If any method encounters an error (for instance, if an underlying io.Writer returns an error when using NewStreamingWriter), or if an error is explicitly raised with AddError, the Writer permanently enters a failed state and remembers that error; all subsequent method calls for producing output will be ignored.
type Writer struct {
// contains filtered or unexported fields
}
func NewStreamingWriter(target io.Writer, bufferSize int) Writer
NewStreamingWriter creates a Writer that will buffer a limited amount of its output in memory and dump the output to the specified io.Writer whenever the buffer is full. You should also call Flush at the end of your output to ensure that any remaining buffered output is flushed.
If the Writer returns an error at any point, it enters a failed state and will not try to write any more data to the target.
This function returns the struct by value (Writer, not *Writer). This avoids the overhead of a heap allocation since, in typical usage, the Writer will not escape the scope in which it was declared and can remain on the stack.
▹ Example
func NewWriter() Writer
NewWriter creates a Writer that will buffer its entire output in memory.
This function returns the struct by value (Writer, not *Writer). This avoids the overhead of a heap allocation since, in typical usage, the Writer will not escape the scope in which it was declared and can remain on the stack.
▹ Example
func (w *Writer) AddError(err error)
AddError sets the error state if an error has not already been recorded.
▹ Example
func (w *Writer) Array() ArrayState
Array begins writing a JSON array to the output. It returns an ArrayState that provides the array formatting; you must call ArrayState.End() when finished.
▹ Example
func (w *Writer) Bool(value bool)
Bool writes a JSON boolean value to the output.
▹ Example
func (w *Writer) BoolOrNull(isDefined bool, value bool)
BoolOrNull is a shortcut for calling Bool(value) if isDefined is true, or else Null().
▹ Example
func (w *Writer) Bytes() []byte
Bytes returns the full contents of the output buffer.
func (w *Writer) Error() error
Error returns the first error, if any, that occurred during output generation. If there have been no errors, it returns nil.
As soon as any operation fails at any level, either in the JSON encoding or in writing to an underlying io.Writer, the Writer remembers the error and will generate no further output.
func (w *Writer) Float64(value float64)
Float64 writes a JSON numeric value to the output.
▹ Example
func (w *Writer) Float64OrNull(isDefined bool, value float64)
Float64OrNull is a shortcut for calling Float64(value) if isDefined is true, or else Null().
▹ Example
func (w *Writer) Flush() error
Flush writes any remaining in-memory output to the underlying io.Writer, if this is a streaming writer created with NewStreamingWriter. It has no effect otherwise.
func (w *Writer) Int(value int)
Int writes a JSON numeric value to the output.
▹ Example
func (w *Writer) IntOrNull(isDefined bool, value int)
IntOrNull is a shortcut for calling Int(value) if isDefined is true, or else Null().
▹ Example
func (w *Writer) Null()
Null writes a JSON null value to the output.
▹ Example
func (w *Writer) Object() ObjectState
Object begins writing a JSON object to the output. It returns an ObjectState that provides the object formatting; you must call ObjectState.End() when finished.
▹ Example
func (w *Writer) Raw(value json.RawMessage)
Raw writes a pre-encoded JSON value to the output as-is. Its format is assumed to be correct; this operation will not fail unless it is not permitted to write a value at this point.
▹ Example
func (w *Writer) String(value string)
String writes a JSON string value to the output, adding quotes and performing any necessary escaping.
▹ Example
func (w *Writer) StringOrNull(isDefined bool, value string)
StringOrNull is a shortcut for calling String(value) if isDefined is true, or else Null().
▹ Example