commit 9fd67e2569b9a533b9873e3fea69df63e69749be
parent 815f768acde98e69be7d0c021f32efa933847785
Author: francoispqt <francois@parquet.ninja>
Date: Wed, 25 Apr 2018 23:26:55 +0800
update readme add documentation for encoding struct, slices and arrays
Diffstat:
M | README.md | | | 53 | +++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 53 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -200,7 +200,60 @@ func main() {
```
### Structs
+
+To encode a structure, the structure must implement the MarshalerObject interface:
+```go
+type MarshalerObject interface {
+ MarshalObject(enc *Encoder)
+ IsNil() bool
+}
+```
+MarshalObject method takes one argument, a pointer to the Encoder (*gojay.Encoder). The method must add all the keys in the JSON Object by calling Decoder's methods.
+
+IsNil method returns a boolean indicating if the interface underlying value is nil or not. It is used to safely ensure that the underlying value is not nil without using Reflection.
+
+Example of implementation:
+```go
+type user struct {
+ id int
+ name string
+ email string
+}
+// implement UnmarshalerObject
+func (u *user) MarshalObject(dec *gojay.Decoder, key string) {
+ dec.AddIntKey("id", u.id)
+ dec.AddStringKey("name", u.name)
+ dec.AddStringKey("email", u.email)
+}
+func (u *user) IsNil() bool {
+ return u == nil
+}
+```
+
### Arrays and Slices
+To encode an array or a slice, the slice/array must implement the MarshalerArray interface:
+```go
+type MarshalerArray interface {
+ MarshalArray(enc *Encoder)
+}
+```
+MarshalArray method takes one argument, a pointer to the Encoder (*gojay.Encoder). The method must add all element in the JSON Array by calling Decoder's methods.
+
+Example of implementation:
+```go
+type users []*user
+
+func (u *users) MarshalArray(dec *Decoder) error {
+ for _, e := range u {
+ err := enc.AddObject(e)
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}
+```
+
### Other types
# Benchmarks