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/v2 operatorsโ
This page lists all operators available in the encoding/json/v2
sub-package of ro.
Installโ
First, import the sub-package in your project:
go get -u github.com/samber/ro/plugins/encoding/json/v2
Marshalโ
Encodes values to JSON format using json/v2 (Go 1.25+).
import (
"fmt"
"github.com/samber/ro"
rojsonv2 "github.com/samber/ro/plugins/encoding/json/v2"
)
obs := ro.Pipe(
ro.Just(User{ID: 1, Name: "Alice", Age: 30}),
rojsonv2.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 using json/v2 (Go 1.25+).
import (
"fmt"
"github.com/samber/ro"
rojsonv2 "github.com/samber/ro/plugins/encoding/json/v2"
)
obs := ro.Pipe(
ro.Just([]byte(`{"id":1,"name":"Alice","age":30}`)),
rojsonv2.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]()