-
Notifications
You must be signed in to change notification settings - Fork 0
/
document_json.go
34 lines (28 loc) · 1020 Bytes
/
document_json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package streams
import (
"encoding/json"
"github.com/benpate/derp"
"github.com/benpate/hannibal/property"
)
/******************************************
* Custom JSON Marshalling / Unmarshalling
******************************************/
// MarshalJSON implements the json.Marshaller interface,
// and provides a custom marshalling into JSON --
// essentially just aiming the marshaller at the
// Document's value.
func (document Document) MarshalJSON() ([]byte, error) {
return json.Marshal(document.value.Raw())
}
// UnmarshalJSON implements the json.Unmarshaller interface,
// and provides a custom un-marshalling from JSON --
// essentially just aiming the unmashaller at the
// Document's value
func (document *Document) UnmarshalJSON(bytes []byte) error {
value := document.value.Raw()
if err := json.Unmarshal(bytes, &value); err != nil {
return derp.Wrap(err, "streams.Document.UnmarshalJSON", "Error unmarshalling JSON into Document")
}
document.value = property.NewValue(value)
return nil
}