Skip to main content

Encoding/JSON/v2 - Plugin operators

This page lists all operators available in the encoding/json/v2 sub-package of ro.

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!

Installโ€‹

First, import the sub-package in your project:

go get -u github.com/samber/ro/plugins/encoding/json/v2
  • 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[User, []byte](
    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}
    // Completed
    Prototype:
    func Marshal[T any]()
  • 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[[]byte, User](
    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}
    // Completed
    Prototype:
    func Unmarshal[T any]()