Documentation
¶
Overview ¶
Refmt is a serialization and object-mapping library.
Look to the README on github for more high-level information.
This top-level package exposes simple helper methods for most common operations. You can also compose custom marshallers/unmarshallers and serializer/deserializers by constructing a `refmt.TokenPump` with components from the packages beneath this one. For example, the `refmt.JsonEncode` helper method can be replicated by combining an `obj.Marshaller` with a `json.Encoder`.
Index ¶
- func Clone(src, dst interface{}) error
- func CloneAtlased(src, dst interface{}, atl atlas.Atlas) error
- func Marshal(opts EncodeOptions, v interface{}) ([]byte, error)
- func MarshalAtlased(opts EncodeOptions, v interface{}, atl atlas.Atlas) ([]byte, error)
- func MustClone(src, dst interface{})
- func MustCloneAtlased(src, dst interface{}, atl atlas.Atlas)
- func Unmarshal(opts DecodeOptions, data []byte, v interface{}) error
- func UnmarshalAtlased(opts DecodeOptions, data []byte, v interface{}, atl atlas.Atlas) error
- type Cloner
- type DecodeOptions
- type EncodeOptions
- type Marshaller
- type Unmarshaller
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloneAtlased ¶
func Marshal ¶
func Marshal(opts EncodeOptions, v interface{}) ([]byte, error)
func MarshalAtlased ¶
func MarshalAtlased(opts EncodeOptions, v interface{}, atl atlas.Atlas) ([]byte, error)
func MustCloneAtlased ¶
func Unmarshal ¶
func Unmarshal(opts DecodeOptions, data []byte, v interface{}) error
func UnmarshalAtlased ¶
func UnmarshalAtlased(opts DecodeOptions, data []byte, v interface{}, atl atlas.Atlas) error
Types ¶
type DecodeOptions ¶
type DecodeOptions interface {
IsDecodeOptions() // marker method.
}
type EncodeOptions ¶
type EncodeOptions interface {
IsEncodeOptions() // marker method.
}
type Marshaller ¶
type Marshaller interface {
Marshal(v interface{}) error
}
func NewMarshaller ¶
func NewMarshaller(opts EncodeOptions, wr io.Writer) Marshaller
Example (Json) ¶
package main
import (
"bytes"
"fmt"
"github.com/polydawn/refmt"
"github.com/polydawn/refmt/json"
)
func main() {
var buf bytes.Buffer
encoder := refmt.NewMarshaller(json.EncodeOptions{}, &buf)
err := encoder.Marshal(map[string]interface{}{
"x": "a",
"y": 1,
})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)
}
Output: {"x":"a","y":1} <nil>
Example (Json_prettyprint) ¶
package main
import (
"bytes"
"fmt"
"github.com/polydawn/refmt"
"github.com/polydawn/refmt/json"
)
func main() {
var buf bytes.Buffer
encoder := refmt.NewMarshaller(json.EncodeOptions{
Line: []byte{'\n'},
Indent: []byte{'\t'},
}, &buf)
err := encoder.Marshal(map[string]interface{}{
"x": "a",
"y": 1,
})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)
}
Output: { "x": "a", "y": 1 } <nil>
func NewMarshallerAtlased ¶
func NewMarshallerAtlased(opts EncodeOptions, wr io.Writer, atl atlas.Atlas) Marshaller
Example (AutogeneratedAtlas) ¶
package main
import (
"bytes"
"fmt"
"github.com/polydawn/refmt"
"github.com/polydawn/refmt/json"
"github.com/polydawn/refmt/obj/atlas"
)
func main() {
type MyType struct {
X string
Y int
}
MyType_AtlasEntry := atlas.BuildEntry(MyType{}).
StructMap().Autogenerate().
Complete()
atl := atlas.MustBuild(
MyType_AtlasEntry,
// this is a vararg... stack more entries here!
)
var buf bytes.Buffer
encoder := refmt.NewMarshallerAtlased(json.EncodeOptions{}, &buf, atl)
err := encoder.Marshal(MyType{"a", 1})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)
}
Output: {"x":"a","y":1} <nil>
Example (CustomStructMapping) ¶
package main
import (
"bytes"
"fmt"
"github.com/polydawn/refmt"
"github.com/polydawn/refmt/json"
"github.com/polydawn/refmt/obj/atlas"
)
func main() {
type MyType struct {
X string
Y int
}
MyType_AtlasEntry := atlas.BuildEntry(MyType{}).
StructMap().
AddField("X", atlas.StructMapEntry{SerialName: "overrideName"}).
// and no "Y" mapping at all!
Complete()
atl := atlas.MustBuild(
MyType_AtlasEntry,
// this is a vararg... stack more entries here!
)
var buf bytes.Buffer
encoder := refmt.NewMarshallerAtlased(json.EncodeOptions{}, &buf, atl)
err := encoder.Marshal(MyType{"a", 1})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)
}
Output: {"overrideName":"a"} <nil>
Example (TransformFuncs) ¶
package main
import (
"bytes"
"fmt"
"strings"
"github.com/polydawn/refmt"
"github.com/polydawn/refmt/json"
"github.com/polydawn/refmt/obj/atlas"
)
func main() {
type MyType struct {
X string
Y string
Z string
}
MyType_AtlasEntry := atlas.BuildEntry(MyType{}).
Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(x MyType) (string, error) {
return string(x.X) + ":" + string(x.Y) + ":" + string(x.Z), nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(x string) (MyType, error) {
ss := strings.Split(x, ":")
if len(ss) != 3 {
return MyType{}, fmt.Errorf("parsing MyType: string must have 3 parts, separated by colon")
}
return MyType{ss[0], ss[1], ss[2]}, nil
})).
Complete()
atl := atlas.MustBuild(
MyType_AtlasEntry,
// this is a vararg... stack more entries here!
)
var buf bytes.Buffer
encoder := refmt.NewMarshallerAtlased(json.EncodeOptions{}, &buf, atl)
err := encoder.Marshal(MyType{"serializes", "as", "string!"})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)
}
Output: "serializes:as:string!" <nil>
type Unmarshaller ¶
type Unmarshaller interface {
Unmarshal(v interface{}) error
}
func NewUnmarshaller ¶
func NewUnmarshaller(opts DecodeOptions, r io.Reader) Unmarshaller
func NewUnmarshallerAtlased ¶
func NewUnmarshallerAtlased(opts DecodeOptions, r io.Reader, atl atlas.Atlas) Unmarshaller
Directories
¶
| Path | Synopsis |
|---|---|
|
Package implementing the CBOR -- Concise Binary Object Notation -- http://cbor.io/ -- spec.
|
Package implementing the CBOR -- Concise Binary Object Notation -- http://cbor.io/ -- spec. |
|
cmd
|
|
|
refmt
command
|
|
|
Package implementing the JSON -- http://json.org/ -- spec.
|
Package implementing the JSON -- http://json.org/ -- spec. |
|
microbench
|
|
|
The `obj` package defines Marshaller and Unmarshaller types, which can be used to convert in-memory values to token streams, and token streams to unpack in-memory values.
|
The `obj` package defines Marshaller and Unmarshaller types, which can be used to convert in-memory values to token streams, and token streams to unpack in-memory values. |
|
atlas
Atlas types are used to define how to map Go values into refmt token streams.
|
Atlas types are used to define how to map Go values into refmt token streams. |
|
atlas/common
commonatlases is a package full of `atlas.Entry` definions for common types in the standard library.
|
commonatlases is a package full of `atlas.Entry` definions for common types in the standard library. |
|
The `shared` package defines helper types and functions used internally by all the other refmt packages; it is not user-facing.
|
The `shared` package defines helper types and functions used internally by all the other refmt packages; it is not user-facing. |
|
Testing helper functions.
|
Testing helper functions. |
|
Package containing Token struct and TokenType info.
|
Package containing Token struct and TokenType info. |
|
fixtures
Token stream test fixtures.
|
Token stream test fixtures. |
Click to show internal directories.
Click to hide internal directories.