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
Dr. Carsten Leue b25de3c7c3 doc: fix case
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-07-18 15:57:54 +02:00

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)
})
}