Help improve this documentation
This documentation is still new and evolving. If you spot any mistakes, unclear explanations, or missing details, please open an issue.
Your feedback helps us improve!
Encoding/JSON operatorsโ
This page lists all operators available in the encoding/json sub-package of ro.
Installโ
First, import the sub-package in your project:
go get -u github.com/samber/ro/plugins/encoding/json
Marshalโ
Encodes values to JSON format.
import (
"fmt"
"github.com/samber/ro"
rojson "github.com/samber/ro/plugins/encoding/json"
)
type User struct {
ID int
Name string
Age int
}
obs := ro.Pipe[User, []byte](
ro.Just(User{ID: 1, Name: "Alice", Age: 30}),
rojson.Marshal[User](),
)
sub := obs.Subscribe(ro.NewObserver(
func(data []byte) {
fmt.Printf("Next: %s\n", string(data))
},
func(err error) {
fmt.Printf("Error: %s\n", err.Error())
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// Next: {"id":1,"name":"Alice","age":30}
// CompletedPrototype:func Marshal[T any]()Unmarshalโ
Decodes JSON format to typed values.
import (
"fmt"
"github.com/samber/ro"
rojson "github.com/samber/ro/plugins/encoding/json"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
obs := ro.Pipe[[]byte, User](
ro.Just([]byte(`{"id":1,"name":"Alice","age":30}`)),
rojson.Unmarshal[User](),
)
sub := obs.Subscribe(ro.NewObserver(
func(user User) {
fmt.Printf("Next: {ID:%d Name:%s Age:%d}\n", user.ID, user.Name, user.Age)
},
func(err error) {
fmt.Printf("Error: %s\n", err.Error())
},
func() {
fmt.Println("Completed")
},
))
defer sub.Unsubscribe()
// Next: {ID:1 Name:Alice Age:30}
// CompletedPrototype:func Unmarshal[T any]()