1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-10-18 14:24:20 +00:00
go_helpers/fieldcollection/marshalJSON.go
Knut Ahlers 6efbc2dd11
Add validation to fieldcollection
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-04-03 19:03:45 +02:00

38 lines
791 B
Go

package fieldcollection
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
// Implement JSON marshalling to plain underlying map[string]any
// MarshalJSON implements json.Marshaller interface
func (f *FieldCollection) MarshalJSON() ([]byte, error) {
if f == nil || f.data == nil {
return []byte("{}"), nil
}
f.lock.RLock()
defer f.lock.RUnlock()
data, err := json.Marshal(f.data)
if err != nil {
return nil, fmt.Errorf("marshalling to JSON: %w", err)
}
return data, nil
}
// UnmarshalJSON implements json.Unmarshaller interface
func (f *FieldCollection) UnmarshalJSON(raw []byte) error {
data := make(map[string]any)
if err := json.Unmarshal(raw, &data); err != nil {
return errors.Wrap(err, "unmarshalling from JSON")
}
f.SetFromData(data)
return nil
}