1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-12-26 14:01:20 +00:00
go_helpers/fieldcollection/marshalJSON.go

39 lines
791 B
Go
Raw Normal View History

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
}