Documentation
¶
Index ¶
- func IsJSONBuffer(buf []byte) bool
- func NewDocumentDecoder(r io.ReadCloser) io.ReadCloser
- func ToJSON(data []byte) ([]byte, error)
- func Unmarshal(data []byte, v interface{}) error
- func UnmarshalStrict(data []byte, v interface{}) error
- type JSONSyntaxError
- type LineReader
- type Reader
- type StreamReader
- func (r *StreamReader) Consume(n int)
- func (r *StreamReader) Consumed() int
- func (r *StreamReader) Peek(n int) ([]byte, error)
- func (r *StreamReader) Read(p []byte) (n int, err error)
- func (r *StreamReader) ReadN(want int) ([]byte, error)
- func (r *StreamReader) Rewind()
- func (r *StreamReader) RewindN(n int)
- type YAMLDecoder
- type YAMLOrJSONDecoder
- type YAMLReader
- type YAMLSyntaxError
- type YAMLToJSONDecoder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsJSONBuffer ¶ added in v0.21.0
func IsJSONBuffer(buf []byte) bool
IsJSONBuffer scans the provided buffer, looking for an open brace indicating this is JSON.
func NewDocumentDecoder ¶
func NewDocumentDecoder(r io.ReadCloser) io.ReadCloser
NewDocumentDecoder decodes YAML documents from the provided stream in chunks by converting each document (as defined by the YAML spec) into its own chunk. io.ErrShortBuffer will be returned if the entire buffer could not be read to assist the caller in framing the chunk.
func ToJSON ¶
func ToJSON(data []byte) ([]byte, error)
ToJSON converts a single YAML document into a JSON document or returns an error. If the document appears to be JSON the YAML decoding path is not used (so that error messages are JSON specific).
func Unmarshal ¶ added in v0.18.18
func Unmarshal(data []byte, v interface{}) error
Unmarshal unmarshals the given data If v is a *map[string]interface{}, *[]interface{}, or *interface{} numbers are converted to int64 or float64
func UnmarshalStrict ¶ added in v0.23.0
func UnmarshalStrict(data []byte, v interface{}) error
UnmarshalStrict unmarshals the given data strictly (erroring when there are duplicate fields).
Types ¶
type JSONSyntaxError ¶
type JSONSyntaxError struct {
Offset int64
Err error
}
type LineReader ¶
type LineReader struct {
// contains filtered or unexported fields
}
type StreamReader ¶ added in v0.33.0
type StreamReader struct {
// contains filtered or unexported fields
}
StreamReader is a reader designed for consuming streams of variable-length messages. It buffers data until it is explicitly consumed, and can be rewound to re-read previous data.
func GuessJSONStream ¶
func GuessJSONStream(r io.Reader, size int) (*StreamReader, []byte, bool)
GuessJSONStream scans the provided reader up to size, looking for an open brace indicating this is JSON. It will return the bufio.Reader it creates for the consumer.
func NewStreamReader ¶ added in v0.33.0
func NewStreamReader(r io.Reader, size int) *StreamReader
NewStreamReader creates a new StreamReader wrapping the provided io.Reader.
func (*StreamReader) Consume ¶ added in v0.33.0
func (r *StreamReader) Consume(n int)
Consume discards up to n bytes of previously read data from the beginning of the buffer. Once consumed, that data is no longer available for rewinding. If n is greater than the current buffer, the buffer is cleared. Consume never consume data from the underlying reader.
func (*StreamReader) Consumed ¶ added in v0.33.0
func (r *StreamReader) Consumed() int
Consumed returns the number of bytes consumed from the input reader.
func (*StreamReader) Peek ¶ added in v0.33.0
func (r *StreamReader) Peek(n int) ([]byte, error)
Peek returns the next n bytes without advancing the reader. The returned bytes are valid until the next call to Consume.
func (*StreamReader) Read ¶ added in v0.33.0
func (r *StreamReader) Read(p []byte) (n int, err error)
Read implements io.Reader. It first returns any buffered data after the current offset, and if that's exhausted, reads from the underlying reader and buffers the data. The returned data is not considered consumed until the Consume method is called.
func (*StreamReader) ReadN ¶ added in v0.33.0
func (r *StreamReader) ReadN(want int) ([]byte, error)
ReadN reads exactly n bytes from the reader, blocking until all bytes are read or an error occurs. If an error occurs, the number of bytes read is returned along with the error. If EOF is hit before n bytes are read, this will return the bytes read so far, along with io.EOF. The returned data is not considered consumed until the Consume method is called.
type YAMLDecoder ¶
type YAMLDecoder struct {
// contains filtered or unexported fields
}
YAMLDecoder reads chunks of objects and returns ErrShortBuffer if the data is not sufficient.
type YAMLOrJSONDecoder ¶
type YAMLOrJSONDecoder struct {
// contains filtered or unexported fields
}
YAMLOrJSONDecoder attempts to decode a stream of JSON or YAML documents. While JSON is YAML, the way Go's JSON decode defines a multi-document stream is a series of JSON objects (e.g. {}{}), but YAML defines a multi-document stream as a series of documents separated by "---".
This decoder will attempt to decode the stream as JSON first, and if that fails, it will switch to YAML. Once it determines the stream is JSON (by finding a non-YAML-delimited series of objects), it will not switch to YAML. Once it switches to YAML it will not switch back to JSON.
func NewYAMLOrJSONDecoder ¶
func NewYAMLOrJSONDecoder(r io.Reader, bufferSize int) *YAMLOrJSONDecoder
NewYAMLOrJSONDecoder returns a decoder that will process YAML documents or JSON documents from the given reader as a stream. bufferSize determines how far into the stream the decoder will look to figure out whether this is a JSON stream (has whitespace followed by an open brace).
type YAMLReader ¶
type YAMLReader struct {
// contains filtered or unexported fields
}
func NewYAMLReader ¶
func NewYAMLReader(r *bufio.Reader) *YAMLReader
type YAMLSyntaxError ¶
type YAMLSyntaxError struct {
// contains filtered or unexported fields
}
type YAMLToJSONDecoder ¶
type YAMLToJSONDecoder struct {
// contains filtered or unexported fields
}
YAMLToJSONDecoder decodes YAML documents from an io.Reader by separating individual documents. It first converts the YAML body to JSON, then unmarshals the JSON.
func NewYAMLToJSONDecoder ¶
func NewYAMLToJSONDecoder(r io.Reader) *YAMLToJSONDecoder
NewYAMLToJSONDecoder decodes YAML documents from the provided stream in chunks by converting each document (as defined by the YAML spec) into its own chunk, converting it to JSON via yaml.YAMLToJSON, and then passing it to json.Decoder.
func (*YAMLToJSONDecoder) Decode ¶
func (d *YAMLToJSONDecoder) Decode(into interface{}) error
Decode reads a YAML document as JSON from the stream or returns an error. The decoding rules match json.Unmarshal, not yaml.Unmarshal.
func (*YAMLToJSONDecoder) InputOffset ¶ added in v0.33.0
func (d *YAMLToJSONDecoder) InputOffset() int