Documentation ¶
Overview ¶
Package serializer provides a middle layer between the gss package and the lower-level packages. This package provides a simple api in the builder pattern for serializing/deserializing objects.
Index ¶
- Constants
- Variables
- type ErrUnknownFormat
- type ErrUnknownOption
- type MarshalFunc
- type Serializer
- func (s *Serializer) Comment(comment string) *Serializer
- func (s *Serializer) Deserialize(b []byte) (interface{}, error)
- func (s *Serializer) DropCR(dropCR bool) *Serializer
- func (s *Serializer) EscapeColon(escapeColon bool) *Serializer
- func (s *Serializer) EscapeEqual(escapeEqual bool) *Serializer
- func (s *Serializer) EscapeNewLine(escapeNewLine bool) *Serializer
- func (s *Serializer) EscapePrefix(escapePrefix string) *Serializer
- func (s *Serializer) EscapeSpace(escapeSpace bool) *Serializer
- func (s *Serializer) ExpandHeader(expandHeader bool) *Serializer
- func (s *Serializer) Fit(f bool) *Serializer
- func (s *Serializer) Format(format string) *Serializer
- func (s *Serializer) FormatSpecifier(formatSpecifier string) *Serializer
- func (s *Serializer) Header(header []interface{}) *Serializer
- func (s *Serializer) KeySerializer(keySerializer stringify.Stringer) *Serializer
- func (s *Serializer) KeyValueSeparator(keyValueSeparator string) *Serializer
- func (s *Serializer) LazyQuotes(lazyQuotes bool) *Serializer
- func (s *Serializer) Limit(limit int) *Serializer
- func (s *Serializer) LineSeparator(lineSeparator string) *Serializer
- func (s *Serializer) Pretty(pretty bool) *Serializer
- func (s *Serializer) Reversed(reversed bool) *Serializer
- func (s *Serializer) ScannerBufferSize(scannerBufferSize int) *Serializer
- func (s *Serializer) Serialize(object interface{}) ([]byte, error)
- func (s *Serializer) SkipBlanks(skipBlanks bool) *Serializer
- func (s *Serializer) SkipComments(skipComments bool) *Serializer
- func (s *Serializer) SkipLines(skipLines int) *Serializer
- func (s *Serializer) Sorted(sorted bool) *Serializer
- func (s *Serializer) Trim(trim bool) *Serializer
- func (s *Serializer) Type(t reflect.Type) *Serializer
- func (s *Serializer) UnescapeColon(unescapeColon bool) *Serializer
- func (s *Serializer) UnescapeEqual(unescapeEqual bool) *Serializer
- func (s *Serializer) UnescapeNewLine(unescapeNewLine bool) *Serializer
- func (s *Serializer) UnescapeSpace(unescapeSpace bool) *Serializer
- func (s *Serializer) ValueSerializer(valueSerializer stringify.Stringer) *Serializer
- type UnmarshalFunc
- type UnmarshalTypeFunc
Examples ¶
- Serializer.Deserialize (Json)
- Serializer.Deserialize (Jsonl)
- Serializer.Deserialize (Properties)
- Serializer.Deserialize (Tags)
- Serializer.Deserialize (Toml)
- Serializer.Deserialize (Yaml)
- Serializer.Serialize (Go)
- Serializer.Serialize (Json)
- Serializer.Serialize (Jsonl)
- Serializer.Serialize (Properties)
- Serializer.Serialize (Tags)
- Serializer.Serialize (Toml)
- Serializer.Serialize (Yaml)
Constants ¶
const ( NoSkip = 0 // used as SkipLines parameter to indicate no skipping when reading NoLimit = -1 // used to indicate that there is no limit on reading or writing, depending on context. NoComment = "" // used to indicate that there is no comment prefix to consider. )
const ( FormatBSON = "bson" // Binary JSON FormatCSV = "csv" // Comma-Separated Values FormatFmt = "fmt" // Formatter FormatGo = "go" // Native Golang print format FormatGob = "gob" // Native Golang binary format FormatHCL = "hcl" // HashiCorp Configuration Language FormatJSON = "json" // JSON FormatJSONL = "jsonl" // JSON Lines FormatProperties = "properties" // Properties FormatTags = "tags" // Tags (a=b c=d ...) FormatTOML = "toml" // TOML FormatTSV = "tsv" // Tab-Separated Values FormatYAML = "yaml" // YAML )
Variables ¶
var ( Formats = []string{ FormatBSON, FormatCSV, FormatFmt, FormatGo, FormatGob, FormatHCL, FormatJSON, FormatJSONL, FormatProperties, FormatTags, FormatTOML, FormatTSV, FormatYAML, } ErrMissingKeyValueSeparator = errors.New("missing key-value separator") ErrMissingLineSeparator = errors.New("missing line separator") )
var ( // UnmarshalFuncs contains a map of functions for unmarshaling formatted bytes into objects. UnmarshalFuncs = map[string]UnmarshalFunc{ FormatBSON: bson.Unmarshal, FormatJSON: json.Unmarshal, FormatTOML: toml.Unmarshal, FormatYAML: yaml.Unmarshal, } // UnmarshalTypeFuncs contains a map of functions for unmarshaling formatted bytes into objects. UnmarshalTypeFuncs = map[string]UnmarshalTypeFunc{ FormatBSON: bson.UnmarshalType, FormatJSON: json.UnmarshalType, FormatTOML: toml.UnmarshalType, FormatYAML: yaml.UnmarshalType, } // MarshalTypeFuncs contains a map of functions for marshaling objects into formatted bytes. MarshalFuncs = map[string]MarshalFunc{ FormatTOML: toml.Marshal, FormatYAML: yaml.Marshal, } )
Functions ¶
This section is empty.
Types ¶
type ErrUnknownFormat ¶
type ErrUnknownFormat struct {
Name string // the name of the unknown format
}
ErrUnknownFormat is used when an unknown format is provided.
func (ErrUnknownFormat) Error ¶
func (e ErrUnknownFormat) Error() string
Error returns the error formatted as a string.
type ErrUnknownOption ¶
type ErrUnknownOption struct {
Name string // the name of the unknown option
}
ErrUnknownOption is used when an unknown option is provided.
func (ErrUnknownOption) Error ¶
func (e ErrUnknownOption) Error() string
Error returns the error formatted as a string.
type MarshalFunc ¶
MarshalFunc is a function for marshalling an object into bytes.
type Serializer ¶
type Serializer struct {
// contains filtered or unexported fields
}
Serializer is a struct for serializing/deserializing objects. This is the workhorse of the gss package.
func NewWithOptions ¶
func NewWithOptions(format string, options ...map[string]interface{}) (*Serializer, error)
NewWithOptions returns a new serializer configured with the given options.
func (*Serializer) Comment ¶
func (s *Serializer) Comment(comment string) *Serializer
Comment sets the comment of the serializer.
func (*Serializer) Deserialize ¶
func (s *Serializer) Deserialize(b []byte) (interface{}, error)
Deserialize deserializes the input slice of bytes into an object and returns an error, if any. Formats jsonl and tags return slices. If the type is not set, then returns a slice of type []interface{}.
Example (Json) ¶
in := `{"foo":"bar"}` s := New(FormatJSON) out, err := s.Deserialize([]byte(in)) if err != nil { panic(err) } fmt.Println(out)
Output: map[foo:bar]
Example (Jsonl) ¶
in := ` {"a": "b"} {"c": "d"} {"e": "f"} false true "foo" "bar" ` s := New(FormatJSONL).LineSeparator("\n").Trim(true) out, err := s.Deserialize([]byte(in)) if err != nil { panic(err) } fmt.Println(out)
Output: [<nil> map[a:b] map[c:d] map[e:f] false true foo bar <nil>]
Example (Properties) ¶
in := "a=x\nb=y\nc=z" s := New(FormatProperties).LineSeparator("\n") out, err := s.Deserialize([]byte(in)) if err != nil { panic(err) } fmt.Println(out)
Output: map[a:x b:y c:z]
Example (Tags) ¶
in := "hello=\"beautiful world\"" s := New(FormatTags).KeyValueSeparator("=").LineSeparator("\n") out, err := s.Deserialize([]byte(in)) if err != nil { panic(err) } fmt.Println(out)
Output: [map[hello:beautiful world]]
Example (Toml) ¶
in := "a = 1.0\nb = 2.0\nc = 3.0\n" s := New(FormatTOML) out, err := s.Deserialize([]byte(in)) if err != nil { panic(err) } fmt.Println(out)
Output: map[a:1 b:2 c:3]
Example (Yaml) ¶
in := `a: x b: "y" c: z` s := New(FormatYAML) out, err := s.Deserialize([]byte(in)) if err != nil { panic(err) } fmt.Println(out)
Output: map[a:x b:y c:z]
func (*Serializer) DropCR ¶
func (s *Serializer) DropCR(dropCR bool) *Serializer
DropCR enables/disables dropping the carriage return character if it terminates a line.
func (*Serializer) EscapeColon ¶
func (s *Serializer) EscapeColon(escapeColon bool) *Serializer
EscapeSpace enables/disables escaping the colon character.
func (*Serializer) EscapeEqual ¶
func (s *Serializer) EscapeEqual(escapeEqual bool) *Serializer
EscapeSpace enables/disables escaping the equal character.
func (*Serializer) EscapeNewLine ¶
func (s *Serializer) EscapeNewLine(escapeNewLine bool) *Serializer
EscapeSpace enables/disables escaping the new line character.
func (*Serializer) EscapePrefix ¶
func (s *Serializer) EscapePrefix(escapePrefix string) *Serializer
EscapePrefix sets the prefix for escaping text. Used with the properties format. If the escape prefix is not set, then the serializer doesn't escape/unescape any text.
func (*Serializer) EscapeSpace ¶
func (s *Serializer) EscapeSpace(escapeSpace bool) *Serializer
EscapeSpace enables/disables escaping the whitespace character.
func (*Serializer) ExpandHeader ¶
func (s *Serializer) ExpandHeader(expandHeader bool) *Serializer
ExpandHeader enables/disables dynamically expanding the header. Dynamically expanding the header requires buffering the output in memory.
func (*Serializer) Fit ¶ added in v0.0.9
func (s *Serializer) Fit(f bool) *Serializer
Fit sets the fit of the serializer.
func (*Serializer) Format ¶
func (s *Serializer) Format(format string) *Serializer
Format sets the format of the serializer.
func (*Serializer) FormatSpecifier ¶ added in v0.0.9
func (s *Serializer) FormatSpecifier(formatSpecifier string) *Serializer
FormatSpecifier sets the format sepcifier of the serializer.
func (*Serializer) Header ¶
func (s *Serializer) Header(header []interface{}) *Serializer
Header sets the header of the serializer.
func (*Serializer) KeySerializer ¶
func (s *Serializer) KeySerializer(keySerializer stringify.Stringer) *Serializer
KeySerializer sets the function for serializing keys as strings for the csv, tsv, and properties formats.
func (*Serializer) KeyValueSeparator ¶
func (s *Serializer) KeyValueSeparator(keyValueSeparator string) *Serializer
KeyValueSeparator sets the key-value separator of the serializer.
func (*Serializer) LazyQuotes ¶
func (s *Serializer) LazyQuotes(lazyQuotes bool) *Serializer
LazyQuotes enables/disables lazy quotes when reading from an input formatted as separated values, e.g., CSV or TSV.
func (*Serializer) Limit ¶
func (s *Serializer) Limit(limit int) *Serializer
Limit sets the limit of the serializer.
func (*Serializer) LineSeparator ¶
func (s *Serializer) LineSeparator(lineSeparator string) *Serializer
LineSeparator sets the line separator of the serializer.
func (*Serializer) Pretty ¶
func (s *Serializer) Pretty(pretty bool) *Serializer
Pretty enables/disables pretty output.
func (*Serializer) Reversed ¶
func (s *Serializer) Reversed(reversed bool) *Serializer
Reversed enables/disables reversed output.
func (*Serializer) ScannerBufferSize ¶ added in v0.0.9
func (s *Serializer) ScannerBufferSize(scannerBufferSize int) *Serializer
ScannerBufferSize sets the initial scanner buffer size.
func (*Serializer) Serialize ¶
func (s *Serializer) Serialize(object interface{}) ([]byte, error)
Serialize serializes an object into a slice of byte and returns and error, if any.
Example (Go) ¶
in := map[string]interface{}{ "a": "1", "b": "2", "c": "3", } s := New(FormatGo) out, err := s.Serialize(in) if err != nil { panic(err) } fmt.Println(string(out))
Output: map[string]interface {}{"a":"1", "b":"2", "c":"3"}
Example (Json) ¶
in := map[interface{}]interface{}{ "foo": "bar", } s := New(FormatJSON).Sorted(true) out, err := s.Serialize(in) if err != nil { panic(err) } fmt.Println(string(out))
Output: {"foo":"bar"}
Example (Jsonl) ¶
in := []map[string]interface{}{ map[string]interface{}{ "a": "1", "b": "2", "c": "3", }, map[string]interface{}{ "a": "4", "b": "5", "c": "6", }, } s := New(FormatJSONL).Limit(-1).LineSeparator("\n") out, err := s.Serialize(in) if err != nil { panic(err) } fmt.Println(string(out))
Output: {"a":"1","b":"2","c":"3"} {"a":"4","b":"5","c":"6"}
Example (Properties) ¶
in := map[string]interface{}{ "a": "1", "b": "2", "c": "3", } s := New(FormatProperties).Sorted(true).LineSeparator("\n").KeyValueSeparator("=") out, err := s.Serialize(in) if err != nil { panic(err) } fmt.Println(string(out))
Output: a=1 b=2 c=3
Example (Tags) ¶
in := map[interface{}]interface{}{ "hello": "beautiful world", } s := New(FormatTags). KeyValueSeparator("="). LineSeparator("\n"). ValueSerializer(stringify.NewStringer("", false, false, false)). Limit(-1). Sorted(true) out, err := s.Serialize(in) if err != nil { panic(err) } fmt.Println(string(out))
Output: hello="beautiful world"
Example (Toml) ¶
in := map[string]interface{}{ "a": "1", "b": "2", "c": "3", } s := New(FormatTOML) out, err := s.Serialize(in) if err != nil { panic(err) } fmt.Println(string(out))
Output: a = "1" b = "2" c = "3"
Example (Yaml) ¶
in := map[string]interface{}{ "a": "1", "b": "2", "c": "3", } s := New(FormatYAML) out, err := s.Serialize(in) if err != nil { panic(err) } fmt.Println(string(out))
Output: a: "1" b: "2" c: "3"
func (*Serializer) SkipBlanks ¶
func (s *Serializer) SkipBlanks(skipBlanks bool) *Serializer
Comment sets the comment of the serializer.
func (*Serializer) SkipComments ¶
func (s *Serializer) SkipComments(skipComments bool) *Serializer
Comment sets the comment of the serializer.
func (*Serializer) SkipLines ¶
func (s *Serializer) SkipLines(skipLines int) *Serializer
SkipLines sets the number of lines to skip from the beginning of the input.
func (*Serializer) Sorted ¶
func (s *Serializer) Sorted(sorted bool) *Serializer
Sorted enables/disables sorted output.
func (*Serializer) Trim ¶
func (s *Serializer) Trim(trim bool) *Serializer
Trim enables/disables trimming whitespace from input lines.
func (*Serializer) Type ¶
func (s *Serializer) Type(t reflect.Type) *Serializer
Type sets the optional type for deserialization. If no type is given, then the type is inferred from the source.
func (*Serializer) UnescapeColon ¶
func (s *Serializer) UnescapeColon(unescapeColon bool) *Serializer
UnescapeColon enables/disables unescaping the colon character.
func (*Serializer) UnescapeEqual ¶
func (s *Serializer) UnescapeEqual(unescapeEqual bool) *Serializer
UnescapeEqual enables/disables unescpaing the equal character.
func (*Serializer) UnescapeNewLine ¶
func (s *Serializer) UnescapeNewLine(unescapeNewLine bool) *Serializer
UnescapeNewLine enables/disables unescaping the new line character.
func (*Serializer) UnescapeSpace ¶
func (s *Serializer) UnescapeSpace(unescapeSpace bool) *Serializer
UnescapeSpace enables/disables unescaping the whitespace character.
func (*Serializer) ValueSerializer ¶
func (s *Serializer) ValueSerializer(valueSerializer stringify.Stringer) *Serializer
ValueSerializer sets the function for serializing values as strings for the csv, tsv, and properties formats.
type UnmarshalFunc ¶
UnmarshalFunc is a function for unmarshaling bytes into an object of a given type.