diff options
author | Marius Orcsik | 2019-12-19 13:35:05 +0100 |
---|---|---|
committer | Marius Orcsik | 2019-12-19 14:45:50 +0100 |
commit | 66855adb1e343de292167241d9bad05f927300a8 (patch) | |
tree | 8fd542189fd49bb57b6fdf1236ad5821d0c47b2c /ordered_collection_page.go | |
parent | 1659f7231e4f782b69f200aae3a11025efb572ba (diff) |
Added collection/ordered_collection and their pages Json marshal methods
Diffstat (limited to 'ordered_collection_page.go')
-rw-r--r-- | ordered_collection_page.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/ordered_collection_page.go b/ordered_collection_page.go index df868a6..12b25c1 100644 --- a/ordered_collection_page.go +++ b/ordered_collection_page.go @@ -1,6 +1,7 @@ package activitypub import ( + "bytes" "errors" "github.com/buger/jsonparser" "time" @@ -260,6 +261,60 @@ func (o *OrderedCollectionPage) UnmarshalJSON(data []byte) error { } return nil } +// MarshalJSON +func (c OrderedCollectionPage) MarshalJSON() ([]byte, error) { + b := bytes.Buffer{} + notEmpty := false + b.Write([]byte{'{'}) + + OnObject(c, func(o *Object) error { + notEmpty = writeObject(&b, *o) + return nil + }) + writeComma := func() { b.WriteString(",") } + writeCommaIfNotEmpty := func(notEmpty bool) { + if notEmpty { + writeComma() + } + } + if c.Current != nil { + writeCommaIfNotEmpty(notEmpty) + notEmpty = writeItemProp(&b, "current", c.Current) || notEmpty + } + if c.First != nil { + writeCommaIfNotEmpty(notEmpty) + notEmpty = writeItemProp(&b, "first", c.First) || notEmpty + } + if c.Last != nil { + writeCommaIfNotEmpty(notEmpty) + notEmpty = writeItemProp(&b, "last", c.Last) || notEmpty + } + if c.OrderedItems != nil { + writeCommaIfNotEmpty(notEmpty) + notEmpty = writeItemCollectionProp(&b, "orderedItems", c.OrderedItems) || notEmpty + } + if c.PartOf != nil { + writeCommaIfNotEmpty(notEmpty) + notEmpty = writeItemProp(&b, "partOf", c.PartOf) || notEmpty + } + if c.Next != nil { + writeCommaIfNotEmpty(notEmpty) + notEmpty = writeItemProp(&b, "next", c.Next) || notEmpty + } + if c.Prev != nil { + writeCommaIfNotEmpty(notEmpty) + notEmpty = writeItemProp(&b, "prev", c.Prev) || notEmpty + } + + writeCommaIfNotEmpty(notEmpty) + notEmpty = writeIntProp(&b, "totalItems", int(c.TotalItems)) || notEmpty + + if notEmpty { + b.Write([]byte{'}'}) + return b.Bytes(), nil + } + return nil, nil +} // ToOrderedCollectionPage func ToOrderedCollectionPage(it Item) (*OrderedCollectionPage, error) { |