1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-06-17 00:07:49 +02:00
Files
fp-go/json/json.go

24 lines
488 B
Go
Raw Normal View History

package json
import (
"encoding/json"
E "github.com/ibm/fp-go/either"
)
// Unmarshal parses a JSON data structure from bytes
func Unmarshal[A any](data []byte) E.Either[error, A] {
return E.TryCatchError(func() (A, error) {
var result A
err := json.Unmarshal(data, &result)
return result, err
})
}
// Marshal converts a data structure to json
func Marshal[A any](a A) E.Either[error, []byte] {
return E.TryCatchError(func() ([]byte, error) {
return json.Marshal(a)
})
}