Documentation ¶
Overview ¶
Package byteslice provides a thin abstraction over `[]byte` types. This package exist to support encoding/decoding `[]byte` slices in JSON serialization/deserialization.
Yes, `encoding/json` supports base64 encoding for `[]byte` fields, but there's no way to customize the way these fields get serialized/deserialized -- e.g. with padding or no padding, which characters to use for padding, etc.
By using byteslice.Buffer as the field instead of `[]byte` you can change the the way this base64 handling is performed.
Index ¶
- func SetGlobalB64Decoder(dec B64Decoder)
- func SetGlobalB64Encoder(enc B64Encoder)
- type B64Decoder
- type B64DecoderFunc
- type B64Encoder
- type B64EncoderFunc
- type Buffer
- func (b *Buffer) AcceptValue(in interface{}) error
- func (b *Buffer) B64Decoder() B64Decoder
- func (b *Buffer) B64Encoder() B64Encoder
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Len() int
- func (b Buffer) MarshalJSON() ([]byte, error)
- func (b *Buffer) SetB64Decoder(dec B64Decoder) *Buffer
- func (b *Buffer) SetBytes(data []byte)
- func (b *Buffer) SetEncoder(enc B64Encoder) *Buffer
- func (b *Buffer) UnmarshalJSON(data []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetGlobalB64Decoder ¶
func SetGlobalB64Decoder(dec B64Decoder)
SetGlobalB64Decoder sets the `B64Decoder` that should be used globally
func SetGlobalB64Encoder ¶
func SetGlobalB64Encoder(enc B64Encoder)
SetGlobalB64Encoder sets the `B64Encoder` that should be used globally
Types ¶
type B64Decoder ¶
B64Decoder is the interface for objects that can decode base64 encoded strings into `[]byte`
Any `*base64.Encoding` object satisfies this interface.
func GlobalB64Decoder ¶
func GlobalB64Decoder() B64Decoder
GlobalB64Decoder returns the `B64Decoder` that is to be used by default for all `byteslice.Buffer` types. Each instance can be configured to use its own decoder if set individually.
The default decoder uses heuristics to determine which of the `"encoding/base64".Encoding` objects should be used.
- If the incoming payload does NOT contain a '=' at the end of the string, it is considered to be a "raw" base64 encoding (i.e. no padding)
- If the incoming payload does NOT contain either '+' or '/', it is considered to be a "url" encoding
By combining these two, we decide which of `base64.URLEncoding`, `base64.StdEncoding`, `base64.RawURLEncoding`, or `base64.RawStdEncoding` we should be using to decode the JSON string
type B64DecoderFunc ¶
B64DecoderFunc is an instance of B64Decoder that is based on a function.
func (B64DecoderFunc) DecodeString ¶
func (f B64DecoderFunc) DecodeString(s string) ([]byte, error)
DecodeString implements the B64Decoder interface
type B64Encoder ¶
B64Encoder is the interface for objects that can encode `[]byte` into base64 encoded string
Any `*base64.Encoding` object satisfies this interface.
func GlobalB64Encoder ¶
func GlobalB64Encoder() B64Encoder
GlobalB64Encoder returns the `B64Encoder` that is to be used by default for all `byteslice.Buffer` types. Each instance can be configured to use its own encoder if set individually.
The default encoder uses the same encoder as the standard library's "encoding/json", which is the `base64.StdEncoding`
type B64EncoderFunc ¶
B64EncoderFunc is an instance of B64Encoder that is based on a function.
func (B64EncoderFunc) EncodeToString ¶
func (f B64EncoderFunc) EncodeToString(data []byte) string
EncodeString implements the B64Encoder interface
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer represents a byte slice. Its only purpose is to act as a thing layer on top of a `[]byte` and provide flexibly JSON serialization/deserialization capabilities
It is safe to use the zero value of the `Buffer` object, but the object is not explicitly synchronized. The user must make sure to apply any synchronization if need be.
You should not copy a `Buffer` object by reference
func New ¶
New creates a new buffer. Using the data provided to call SetBytes(). You may pass `nil` to the argument to create an uninitialized `Buffer` object.
If you do not need explicit initialization, it is safe to use the zero value of the `Buffer` object.
func (*Buffer) AcceptValue ¶
AcceptValue is used in by some consumers to assign the value whose type is not known before hand.
Values can be either one of the following types: `*byteslice.Buffer`, `[]byte`, or `string`.
If the value is a `*byteslice.Buffer`, a copy of the underlying is created, and assigned to receiver.
If the value is a `[]byte`, it is the same as calling `SetBytes()`
IF the value is a `string`, the string is assumed to be a base64-encoded string. Unlike in the case of `UnmarshalJSON`, the string does not need to be quoted.
func (*Buffer) B64Decoder ¶
func (b *Buffer) B64Decoder() B64Decoder
B64Decoder returns the B64Decoder associated with this object. If uninitialized, it will use the global decoder via byteslice.GlobalB64Decoder()
func (*Buffer) B64Encoder ¶
func (b *Buffer) B64Encoder() B64Encoder
B64Encoder returns the B64Encoder associated with this object. If uninitialized, will use the global decoder via byteslice.GlobalB64Encoder()
func (*Buffer) Bytes ¶
Bytes returns the raw bytes stored in the `Buffer` object.
Users need to take care of synchronization or acting upon on the returned buffer, as it will affect the actual stored `[]byte` field in the `Buffer` object.
func (Buffer) MarshalJSON ¶
MarshalJSON implements `"encoding/json".Marshaler, and provides a method to serialize a `[]byte` string to a base64 encoded JSON string.
The JSON string will be parsed using the B64Encoder object associated with this object (or the global one, if not specified).
func (*Buffer) SetB64Decoder ¶
func (b *Buffer) SetB64Decoder(dec B64Decoder) *Buffer
SetB64Decoder assigns a B64Decoder for this object.
func (*Buffer) SetEncoder ¶
func (b *Buffer) SetEncoder(enc B64Encoder) *Buffer
SetB64Encoder assigns a B64Encoder for this object.
func (*Buffer) UnmarshalJSON ¶
UnmarshalJSON implements `"encoding/json".Unmarshaler`, and provides a method to deserialize a `[]byte` string from a base64 encoded JSON string.
The JSON string will be parsed using the B64Decoder object associated with this object (or the global one, if not specified).