Documentation ¶
Overview ¶
Package encode provides a collection of functions for safe serialization and deserialization of data between different systems, such as databases, queues, and caches.
Index ¶
- func ByteDecode(msg []byte, data any) error
- func ByteDeserialize(msg []byte, data any) error
- func ByteEncode(data any) ([]byte, error)
- func ByteSerialize(data any) ([]byte, error)
- func Decode(msg string, data any) error
- func Deserialize(msg string, data any) error
- func Encode(data any) (string, error)
- func Serialize(data any) (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ByteDecode ¶
ByteDecode decodes a byte slice message encoded with the ByteEncode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.
func ByteDeserialize ¶
ByteDeserialize decodes a string message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.
func ByteEncode ¶
ByteEncode encodes the input data to gob+base64 byte slice.
func ByteSerialize ¶
ByteSerialize encodes the input data to JSON+base64 byte slice.
func Decode ¶
Decode decodes a string message encoded with the Encode function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.
Example ¶
package main import ( "fmt" "log" "github.com/Vonage/gosrvlib/pkg/encode" ) func main() { type TestData struct { Alpha string Beta int } var data TestData msg := "Kf+BAwEBCFRlc3REYXRhAf+CAAECAQVBbHBoYQEMAAEEQmV0YQEEAAAAD/+CAQZhYmMxMjMB/gLtAA==" err := encode.Decode(msg, &data) if err != nil { log.Fatal(err) } fmt.Println(data) }
Output: {abc123 -375}
func Deserialize ¶
Deserialize decodes a byte slice message encoded with the Serialize function to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.
Example ¶
package main import ( "fmt" "log" "github.com/Vonage/gosrvlib/pkg/encode" ) func main() { type TestData struct { Alpha string Beta int } var data TestData msg := "eyJBbHBoYSI6ImFiYzEyMyIsIkJldGEiOi0zNzV9Cg==" err := encode.Deserialize(msg, &data) if err != nil { log.Fatal(err) } fmt.Println(data) }
Output: {abc123 -375}
func Encode ¶
Encode encodes the input data to gob+base64 string.
Example ¶
package main import ( "fmt" "log" "github.com/Vonage/gosrvlib/pkg/encode" ) func main() { type TestData struct { Alpha string Beta int } data := &TestData{Alpha: "test_string", Beta: -9876} v, err := encode.Encode(data) if err != nil { log.Fatal(err) } fmt.Println(v) }
Output:
func Serialize ¶
Serialize encodes the input data to JSON+base64 string.
Example ¶
package main import ( "fmt" "log" "github.com/Vonage/gosrvlib/pkg/encode" ) func main() { type TestData struct { Alpha string Beta int } data := &TestData{Alpha: "test_string", Beta: -9876} v, err := encode.Serialize(data) if err != nil { log.Fatal(err) } fmt.Println(v) }
Output: eyJBbHBoYSI6InRlc3Rfc3RyaW5nIiwiQmV0YSI6LTk4NzZ9Cg==
Types ¶
This section is empty.