mirror of
https://github.com/IBM/fp-go.git
synced 2025-06-17 00:07:49 +02:00
24 lines
488 B
Go
24 lines
488 B
Go
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)
|
|
})
|
|
}
|