diff --git a/activity.go b/activity.go index e996ffd..3c3a737 100644 --- a/activity.go +++ b/activity.go @@ -831,12 +831,12 @@ func ToActivity(it Item) (*Activity, error) { // MarshalJSON encodes the receiver object to a JSON document. func (a Activity) MarshalJSON() ([]byte, error) { b := make([]byte, 0) - write(&b, '{') + JSONWrite(&b, '{') - if !writeActivityJSONValue(&b, a) { + if !JSONWriteActivityJSONValue(&b, a) { return nil, nil } - write(&b, '}') + JSONWrite(&b, '}') return b, nil } diff --git a/actor.go b/actor.go index d6733da..3494380 100644 --- a/actor.go +++ b/actor.go @@ -216,21 +216,21 @@ func (p *PublicKey) UnmarshalJSON(data []byte) error { func (p PublicKey) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := true - write(&b, '{') + JSONWrite(&b, '{') if v, err := p.ID.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = !writeJSONProp(&b, "id", v) + notEmpty = !JSONWriteJSONProp(&b, "id", v) } if len(p.Owner) > 0 { - notEmpty = writeIRIJSONProp(&b, "owner", p.Owner) || notEmpty + notEmpty = JSONWriteIRIJSONProp(&b, "owner", p.Owner) || notEmpty } if len(p.PublicKeyPem) > 0 { if pem, err := json.Marshal(p.PublicKeyPem); err == nil { - notEmpty = writeJSONProp(&b, "publicKeyPem", pem) || notEmpty + notEmpty = JSONWriteJSONProp(&b, "publicKeyPem", pem) || notEmpty } } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil @@ -362,46 +362,46 @@ func (a *Actor) UnmarshalJSON(data []byte) error { func (a Actor) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := false - write(&b, '{') + JSONWrite(&b, '{') OnObject(a, func(o *Object) error { - notEmpty = writeObjectJSONValue(&b, *o) + notEmpty = JSONWriteObjectJSONValue(&b, *o) return nil }) if a.Inbox != nil { - notEmpty = writeItemJSONProp(&b, "inbox", a.Inbox) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "inbox", a.Inbox) || notEmpty } if a.Outbox != nil { - notEmpty = writeItemJSONProp(&b, "outbox", a.Outbox) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "outbox", a.Outbox) || notEmpty } if a.Following != nil { - notEmpty = writeItemJSONProp(&b, "following", a.Following) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "following", a.Following) || notEmpty } if a.Followers != nil { - notEmpty = writeItemJSONProp(&b, "followers", a.Followers) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "followers", a.Followers) || notEmpty } if a.Liked != nil { - notEmpty = writeItemJSONProp(&b, "liked", a.Liked) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "liked", a.Liked) || notEmpty } if a.PreferredUsername != nil { - notEmpty = writeNaturalLanguageJSONProp(&b, "preferredUsername", a.PreferredUsername) || notEmpty + notEmpty = JSONWriteNaturalLanguageJSONProp(&b, "preferredUsername", a.PreferredUsername) || notEmpty } if a.Endpoints != nil { if v, err := a.Endpoints.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(&b, "endpoints", v) || notEmpty + notEmpty = JSONWriteJSONProp(&b, "endpoints", v) || notEmpty } } if len(a.Streams) > 0 { - notEmpty = writeItemCollectionJSONProp(&b, "streams", a.Streams) + notEmpty = JSONWriteItemCollectionJSONProp(&b, "streams", a.Streams) } if len(a.PublicKey.PublicKeyPem)+len(a.PublicKey.ID) > 0 { if v, err := a.PublicKey.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(&b, "publicKey", v) || notEmpty + notEmpty = JSONWriteJSONProp(&b, "publicKey", v) || notEmpty } } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil @@ -462,27 +462,27 @@ func (e Endpoints) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := false - write(&b, '{') + JSONWrite(&b, '{') if e.OauthAuthorizationEndpoint != nil { - notEmpty = writeItemJSONProp(&b, "oauthAuthorizationEndpoint", e.OauthAuthorizationEndpoint) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "oauthAuthorizationEndpoint", e.OauthAuthorizationEndpoint) || notEmpty } if e.OauthTokenEndpoint != nil { - notEmpty = writeItemJSONProp(&b, "oauthTokenEndpoint", e.OauthTokenEndpoint) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "oauthTokenEndpoint", e.OauthTokenEndpoint) || notEmpty } if e.ProvideClientKey != nil { - notEmpty = writeItemJSONProp(&b, "provideClientKey", e.ProvideClientKey) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "provideClientKey", e.ProvideClientKey) || notEmpty } if e.SignClientKey != nil { - notEmpty = writeItemJSONProp(&b, "signClientKey", e.SignClientKey) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "signClientKey", e.SignClientKey) || notEmpty } if e.SharedInbox != nil { - notEmpty = writeItemJSONProp(&b, "sharedInbox", e.SharedInbox) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "sharedInbox", e.SharedInbox) || notEmpty } if e.UploadMedia != nil { - notEmpty = writeItemJSONProp(&b, "uploadMedia", e.UploadMedia) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "uploadMedia", e.UploadMedia) || notEmpty } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil diff --git a/collection.go b/collection.go index 8d488a0..706ae4d 100644 --- a/collection.go +++ b/collection.go @@ -245,27 +245,27 @@ func (c *Collection) UnmarshalJSON(data []byte) error { func (c Collection) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := false - write(&b, '{') + JSONWrite(&b, '{') OnObject(c, func(o *Object) error { - notEmpty = writeObjectJSONValue(&b, *o) + notEmpty = JSONWriteObjectJSONValue(&b, *o) return nil }) if c.Current != nil { - notEmpty = writeItemJSONProp(&b, "current", c.Current) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "current", c.Current) || notEmpty } if c.First != nil { - notEmpty = writeItemJSONProp(&b, "first", c.First) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "first", c.First) || notEmpty } if c.Last != nil { - notEmpty = writeItemJSONProp(&b, "last", c.Last) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "last", c.Last) || notEmpty } - notEmpty = writeIntJSONProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty + notEmpty = JSONWriteIntJSONProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty if c.Items != nil { - notEmpty = writeItemCollectionJSONProp(&b, "items", c.Items) || notEmpty + notEmpty = JSONWriteItemCollectionJSONProp(&b, "items", c.Items) || notEmpty } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil diff --git a/collection_page.go b/collection_page.go index 45b8d4a..a40a5ef 100644 --- a/collection_page.go +++ b/collection_page.go @@ -202,36 +202,36 @@ func (c *CollectionPage) UnmarshalJSON(data []byte) error { func (c CollectionPage) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := false - write(&b, '{') + JSONWrite(&b, '{') OnObject(c, func(o *Object) error { - notEmpty = writeObjectJSONValue(&b, *o) + notEmpty = JSONWriteObjectJSONValue(&b, *o) return nil }) if c.PartOf != nil { - notEmpty = writeItemJSONProp(&b, "partOf", c.PartOf) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "partOf", c.PartOf) || notEmpty } if c.Current != nil { - notEmpty = writeItemJSONProp(&b, "current", c.Current) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "current", c.Current) || notEmpty } if c.First != nil { - notEmpty = writeItemJSONProp(&b, "first", c.First) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "first", c.First) || notEmpty } if c.Last != nil { - notEmpty = writeItemJSONProp(&b, "last", c.Last) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "last", c.Last) || notEmpty } if c.Next != nil { - notEmpty = writeItemJSONProp(&b, "next", c.Next) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "next", c.Next) || notEmpty } if c.Prev != nil { - notEmpty = writeItemJSONProp(&b, "prev", c.Prev) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "prev", c.Prev) || notEmpty } - notEmpty = writeIntJSONProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty + notEmpty = JSONWriteIntJSONProp(&b, "totalItems", int64(c.TotalItems)) || notEmpty if c.Items != nil { - notEmpty = writeItemCollectionJSONProp(&b, "items", c.Items) || notEmpty + notEmpty = JSONWriteItemCollectionJSONProp(&b, "items", c.Items) || notEmpty } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil diff --git a/encoding_json.go b/encoding_json.go index bf62fb8..9ebf1ba 100644 --- a/encoding_json.go +++ b/encoding_json.go @@ -9,102 +9,102 @@ import ( "github.com/go-ap/jsonld" ) -func writeComma(b *[]byte) { +func JSONWriteComma(b *[]byte) { if len(*b) > 1 && (*b)[len(*b)-1] != ',' { *b = append(*b, ',') } } -func writeJSONProp(b *[]byte, name string, val []byte) (notEmpty bool) { +func JSONWriteJSONProp(b *[]byte, name string, val []byte) (notEmpty bool) { if len(val) == 0 { return false } - writeComma(b) - success := writePropJSONName(b, name) && writeJSONValue(b, val) + JSONWriteComma(b) + success := JSONWritePropJSONName(b, name) && JSONWriteJSONValue(b, val) if !success { *b = (*b)[:len(*b)-1] } return success } -func write(b *[]byte, c ...byte) { +func JSONWrite(b *[]byte, c ...byte) { *b = append(*b, c...) } -func writeS(b *[]byte, s string) { +func JSONWriteS(b *[]byte, s string) { *b = append(*b, s...) } -func writePropJSONName(b *[]byte, s string) (notEmpty bool) { +func JSONWritePropJSONName(b *[]byte, s string) (notEmpty bool) { if len(s) == 0 { return false } - write(b, '"') - writeS(b, s) - write(b, '"', ':') + JSONWrite(b, '"') + JSONWriteS(b, s) + JSONWrite(b, '"', ':') return true } -func writeJSONValue(b *[]byte, s []byte) (notEmpty bool) { +func JSONWriteJSONValue(b *[]byte, s []byte) (notEmpty bool) { if len(s) == 0 { return false } - write(b, s...) + JSONWrite(b, s...) return true } -func writeNaturalLanguageJSONProp(b *[]byte, n string, nl NaturalLanguageValues) (notEmpty bool) { +func JSONWriteNaturalLanguageJSONProp(b *[]byte, n string, nl NaturalLanguageValues) (notEmpty bool) { l := nl.Count() if l > 1 { n += "Map" } if v, err := nl.MarshalJSON(); err == nil && len(v) > 0 { - return writeJSONProp(b, n, v) + return JSONWriteJSONProp(b, n, v) } return false } -func writeStringJSONProp(b *[]byte, n string, s string) (notEmpty bool) { - return writeJSONProp(b, n, []byte(fmt.Sprintf(`"%s"`, s))) +func JSONWriteStringJSONProp(b *[]byte, n string, s string) (notEmpty bool) { + return JSONWriteJSONProp(b, n, []byte(fmt.Sprintf(`"%s"`, s))) } -func writeBoolJSONProp(b *[]byte, n string, t bool) (notEmpty bool) { - return writeJSONProp(b, n, []byte(fmt.Sprintf(`"%t"`, t))) +func JSONWriteBoolJSONProp(b *[]byte, n string, t bool) (notEmpty bool) { + return JSONWriteJSONProp(b, n, []byte(fmt.Sprintf(`"%t"`, t))) } -func writeIntJSONProp(b *[]byte, n string, d int64) (notEmpty bool) { - return writeJSONProp(b, n, []byte(fmt.Sprintf("%d", d))) +func JSONWriteIntJSONProp(b *[]byte, n string, d int64) (notEmpty bool) { + return JSONWriteJSONProp(b, n, []byte(fmt.Sprintf("%d", d))) } -func writeFloatJSONProp(b *[]byte, n string, f float64) (notEmpty bool) { - return writeJSONProp(b, n, []byte(fmt.Sprintf("%f", f))) +func JSONWriteFloatJSONProp(b *[]byte, n string, f float64) (notEmpty bool) { + return JSONWriteJSONProp(b, n, []byte(fmt.Sprintf("%f", f))) } -func writeTimeJSONProp(b *[]byte, n string, t time.Time) (notEmpty bool) { +func JSONWriteTimeJSONProp(b *[]byte, n string, t time.Time) (notEmpty bool) { var tb []byte - write(&tb, '"') - writeS(&tb, t.UTC().Format(time.RFC3339)) - write(&tb, '"') - return writeJSONProp(b, n, tb) + JSONWrite(&tb, '"') + JSONWriteS(&tb, t.UTC().Format(time.RFC3339)) + JSONWrite(&tb, '"') + return JSONWriteJSONProp(b, n, tb) } -func writeDurationJSONProp(b *[]byte, n string, d time.Duration) (notEmpty bool) { +func JSONWriteDurationJSONProp(b *[]byte, n string, d time.Duration) (notEmpty bool) { if v, err := xsd.Marshal(d); err == nil { - return writeJSONProp(b, n, v) + return JSONWriteJSONProp(b, n, v) } return false } -func writeIRIJSONProp(b *[]byte, n string, i LinkOrIRI) (notEmpty bool) { +func JSONWriteIRIJSONProp(b *[]byte, n string, i LinkOrIRI) (notEmpty bool) { url := i.GetLink().String() if len(url) == 0 { return false } - writeStringJSONProp(b, n, url) + JSONWriteStringJSONProp(b, n, url) return true } -func writeItemJSONProp(b *[]byte, n string, i Item) (notEmpty bool) { +func JSONWriteItemJSONProp(b *[]byte, n string, i Item) (notEmpty bool) { if i == nil { return notEmpty } @@ -113,244 +113,244 @@ func writeItemJSONProp(b *[]byte, n string, i Item) (notEmpty bool) { if err != nil { return false } - return writeJSONProp(b, n, v) + return JSONWriteJSONProp(b, n, v) } return notEmpty } -func writeStringJSONValue(b *[]byte, s string) (notEmpty bool) { +func JSONWriteStringJSONValue(b *[]byte, s string) (notEmpty bool) { if len(s) == 0 { return false } - write(b, '"') - writeS(b, s) - write(b, '"') + JSONWrite(b, '"') + JSONWriteS(b, s) + JSONWrite(b, '"') return true } -func writeItemCollectionJSONValue(b *[]byte, col ItemCollection) (notEmpty bool) { +func JSONWriteItemCollectionJSONValue(b *[]byte, col ItemCollection) (notEmpty bool) { if len(col) == 0 { return notEmpty } - writeCommaIfNotEmpty := func(notEmpty bool) { + JSONWriteCommaIfNotEmpty := func(notEmpty bool) { if notEmpty { - write(b, ',') + JSONWrite(b, ',') } } - write(b, '[') + JSONWrite(b, '[') for i, it := range col { if im, ok := it.(json.Marshaler); ok { v, err := im.MarshalJSON() if err != nil { return false } - writeCommaIfNotEmpty(i > 0) - write(b, v...) + JSONWriteCommaIfNotEmpty(i > 0) + JSONWrite(b, v...) } } - write(b, ']') + JSONWrite(b, ']') return true } -func writeItemCollectionJSONProp(b *[]byte, n string, col ItemCollection) (notEmpty bool) { +func JSONWriteItemCollectionJSONProp(b *[]byte, n string, col ItemCollection) (notEmpty bool) { if len(col) == 0 { return notEmpty } - writeComma(b) - success := writePropJSONName(b, n) && writeItemCollectionJSONValue(b, col) + JSONWriteComma(b) + success := JSONWritePropJSONName(b, n) && JSONWriteItemCollectionJSONValue(b, col) if !success { *b = (*b)[:len(*b)-1] } return success } -func writeObjectJSONValue(b *[]byte, o Object) (notEmpty bool) { +func JSONWriteObjectJSONValue(b *[]byte, o Object) (notEmpty bool) { if v, err := o.ID.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(b, "id", v) || notEmpty + notEmpty = JSONWriteJSONProp(b, "id", v) || notEmpty } if v, err := o.Type.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(b, "type", v) || notEmpty + notEmpty = JSONWriteJSONProp(b, "type", v) || notEmpty } if v, err := o.MediaType.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(b, "mediaType", v) || notEmpty + notEmpty = JSONWriteJSONProp(b, "mediaType", v) || notEmpty } if len(o.Name) > 0 { - notEmpty = writeNaturalLanguageJSONProp(b, "name", o.Name) || notEmpty + notEmpty = JSONWriteNaturalLanguageJSONProp(b, "name", o.Name) || notEmpty } if len(o.Summary) > 0 { - notEmpty = writeNaturalLanguageJSONProp(b, "summary", o.Summary) || notEmpty + notEmpty = JSONWriteNaturalLanguageJSONProp(b, "summary", o.Summary) || notEmpty } if len(o.Content) > 0 { - notEmpty = writeNaturalLanguageJSONProp(b, "content", o.Content) || notEmpty + notEmpty = JSONWriteNaturalLanguageJSONProp(b, "content", o.Content) || notEmpty } if o.Attachment != nil { - notEmpty = writeItemJSONProp(b, "attachment", o.Attachment) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "attachment", o.Attachment) || notEmpty } if o.AttributedTo != nil { - notEmpty = writeItemJSONProp(b, "attributedTo", o.AttributedTo) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "attributedTo", o.AttributedTo) || notEmpty } if o.Audience != nil { - notEmpty = writeItemJSONProp(b, "audience", o.Audience) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "audience", o.Audience) || notEmpty } if o.Context != nil { - notEmpty = writeItemJSONProp(b, "context", o.Context) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "context", o.Context) || notEmpty } if o.Generator != nil { - notEmpty = writeItemJSONProp(b, "generator", o.Generator) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "generator", o.Generator) || notEmpty } if o.Icon != nil { - notEmpty = writeItemJSONProp(b, "icon", o.Icon) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "icon", o.Icon) || notEmpty } if o.Image != nil { - notEmpty = writeItemJSONProp(b, "image", o.Image) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "image", o.Image) || notEmpty } if o.InReplyTo != nil { - notEmpty = writeItemJSONProp(b, "inReplyTo", o.InReplyTo) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "inReplyTo", o.InReplyTo) || notEmpty } if o.Location != nil { - notEmpty = writeItemJSONProp(b, "location", o.Location) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "location", o.Location) || notEmpty } if o.Preview != nil { - notEmpty = writeItemJSONProp(b, "preview", o.Preview) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "preview", o.Preview) || notEmpty } if o.Replies != nil { - notEmpty = writeItemJSONProp(b, "replies", o.Replies) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "replies", o.Replies) || notEmpty } if o.Tag != nil { - notEmpty = writeItemJSONProp(b, "tag", o.Tag) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "tag", o.Tag) || notEmpty } if o.URL != nil { - notEmpty = writeItemJSONProp(b, "url", o.URL) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "url", o.URL) || notEmpty } if o.To != nil { - notEmpty = writeItemJSONProp(b, "to", o.To) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "to", o.To) || notEmpty } if o.Bto != nil { - notEmpty = writeItemJSONProp(b, "bto", o.Bto) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "bto", o.Bto) || notEmpty } if o.CC != nil { - notEmpty = writeItemJSONProp(b, "cc", o.CC) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "cc", o.CC) || notEmpty } if o.BCC != nil { - notEmpty = writeItemJSONProp(b, "bcc", o.BCC) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "bcc", o.BCC) || notEmpty } if !o.Published.IsZero() { - notEmpty = writeTimeJSONProp(b, "published", o.Published) || notEmpty + notEmpty = JSONWriteTimeJSONProp(b, "published", o.Published) || notEmpty } if !o.Updated.IsZero() { - notEmpty = writeTimeJSONProp(b, "updated", o.Updated) || notEmpty + notEmpty = JSONWriteTimeJSONProp(b, "updated", o.Updated) || notEmpty } if !o.StartTime.IsZero() { - notEmpty = writeTimeJSONProp(b, "startTime", o.StartTime) || notEmpty + notEmpty = JSONWriteTimeJSONProp(b, "startTime", o.StartTime) || notEmpty } if !o.EndTime.IsZero() { - notEmpty = writeTimeJSONProp(b, "endTime", o.EndTime) || notEmpty + notEmpty = JSONWriteTimeJSONProp(b, "endTime", o.EndTime) || notEmpty } if o.Duration != 0 { // TODO(marius): maybe don't use 0 as a nil value for Object types // which can have a valid duration of 0 - (Video, Audio, etc) - notEmpty = writeDurationJSONProp(b, "duration", o.Duration) || notEmpty + notEmpty = JSONWriteDurationJSONProp(b, "duration", o.Duration) || notEmpty } if o.Likes != nil { - notEmpty = writeItemJSONProp(b, "likes", o.Likes) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "likes", o.Likes) || notEmpty } if o.Shares != nil { - notEmpty = writeItemJSONProp(b, "shares", o.Shares) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "shares", o.Shares) || notEmpty } if v, err := o.Source.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(b, "source", v) || notEmpty + notEmpty = JSONWriteJSONProp(b, "source", v) || notEmpty } return notEmpty } -func writeActivityJSONValue(b *[]byte, a Activity) (notEmpty bool) { +func JSONWriteActivityJSONValue(b *[]byte, a Activity) (notEmpty bool) { OnIntransitiveActivity(a, func(i *IntransitiveActivity) error { if i == nil { return nil } - notEmpty = writeIntransitiveActivityJSONValue(b, *i) || notEmpty + notEmpty = JSONWriteIntransitiveActivityJSONValue(b, *i) || notEmpty return nil }) if a.Object != nil { - notEmpty = writeItemJSONProp(b, "object", a.Object) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "object", a.Object) || notEmpty } return notEmpty } -func writeIntransitiveActivityJSONValue(b *[]byte, i IntransitiveActivity) (notEmpty bool) { +func JSONWriteIntransitiveActivityJSONValue(b *[]byte, i IntransitiveActivity) (notEmpty bool) { OnObject(i, func(o *Object) error { if o == nil { return nil } - notEmpty = writeObjectJSONValue(b, *o) || notEmpty + notEmpty = JSONWriteObjectJSONValue(b, *o) || notEmpty return nil }) if i.Actor != nil { - notEmpty = writeItemJSONProp(b, "actor", i.Actor) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "actor", i.Actor) || notEmpty } if i.Target != nil { - notEmpty = writeItemJSONProp(b, "target", i.Target) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "target", i.Target) || notEmpty } if i.Result != nil { - notEmpty = writeItemJSONProp(b, "result", i.Result) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "result", i.Result) || notEmpty } if i.Origin != nil { - notEmpty = writeItemJSONProp(b, "origin", i.Origin) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "origin", i.Origin) || notEmpty } if i.Instrument != nil { - notEmpty = writeItemJSONProp(b, "instrument", i.Instrument) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "instrument", i.Instrument) || notEmpty } return notEmpty } -func writeQuestionJSONValue(b *[]byte, q Question) (notEmpty bool) { +func JSONWriteQuestionJSONValue(b *[]byte, q Question) (notEmpty bool) { OnIntransitiveActivity(q, func(i *IntransitiveActivity) error { if i == nil { return nil } - notEmpty = writeIntransitiveActivityJSONValue(b, *i) || notEmpty + notEmpty = JSONWriteIntransitiveActivityJSONValue(b, *i) || notEmpty return nil }) if q.OneOf != nil { - notEmpty = writeItemJSONProp(b, "oneOf", q.OneOf) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "oneOf", q.OneOf) || notEmpty } if q.AnyOf != nil { - notEmpty = writeItemJSONProp(b, "anyOf", q.AnyOf) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "anyOf", q.AnyOf) || notEmpty } - notEmpty = writeBoolJSONProp(b, "closed", q.Closed) || notEmpty + notEmpty = JSONWriteBoolJSONProp(b, "closed", q.Closed) || notEmpty return notEmpty } -func writeLinkJSONValue(b *[]byte, l Link) (notEmpty bool) { +func JSONWriteLinkJSONValue(b *[]byte, l Link) (notEmpty bool) { if v, err := l.ID.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(b, "id", v) || notEmpty + notEmpty = JSONWriteJSONProp(b, "id", v) || notEmpty } if v, err := l.Type.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(b, "type", v) || notEmpty + notEmpty = JSONWriteJSONProp(b, "type", v) || notEmpty } if v, err := l.MediaType.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(b, "mediaType", v) || notEmpty + notEmpty = JSONWriteJSONProp(b, "mediaType", v) || notEmpty } if len(l.Name) > 0 { - notEmpty = writeNaturalLanguageJSONProp(b, "name", l.Name) || notEmpty + notEmpty = JSONWriteNaturalLanguageJSONProp(b, "name", l.Name) || notEmpty } if v, err := l.Rel.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(b, "rel", v) || notEmpty + notEmpty = JSONWriteJSONProp(b, "rel", v) || notEmpty } if l.Height > 0 { - notEmpty = writeIntJSONProp(b, "height", int64(l.Height)) + notEmpty = JSONWriteIntJSONProp(b, "height", int64(l.Height)) } if l.Width > 0 { - notEmpty = writeIntJSONProp(b, "width", int64(l.Width)) + notEmpty = JSONWriteIntJSONProp(b, "width", int64(l.Width)) } if l.Preview != nil { - notEmpty = writeItemJSONProp(b, "rel", l.Preview) || notEmpty + notEmpty = JSONWriteItemJSONProp(b, "rel", l.Preview) || notEmpty } if v, err := l.Href.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(b, "href", v) || notEmpty + notEmpty = JSONWriteJSONProp(b, "href", v) || notEmpty } if len(l.HrefLang) > 0 { - notEmpty = writeStringJSONProp(b, "hrefLang", string(l.HrefLang)) || notEmpty + notEmpty = JSONWriteStringJSONProp(b, "hrefLang", string(l.HrefLang)) || notEmpty } return notEmpty } diff --git a/encoding_json_test.go b/encoding_json_test.go index d2b36b2..cdb74b5 100644 --- a/encoding_json_test.go +++ b/encoding_json_test.go @@ -5,7 +5,7 @@ import ( "time" ) -func Test_write(t *testing.T) { +func Test_JSONWrite(t *testing.T) { type args struct { b *[]byte c []byte @@ -22,7 +22,7 @@ func Test_write(t *testing.T) { } } -func Test_writeActivity(t *testing.T) { +func Test_JSONWriteActivity(t *testing.T) { type args struct { b *[]byte a Activity @@ -36,14 +36,14 @@ func Test_writeActivity(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeActivityJSONValue(tt.args.b, tt.args.a); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeActivityJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteActivityJSONValue(tt.args.b, tt.args.a); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteActivityJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeBoolProp(t *testing.T) { +func Test_JSONWriteBoolProp(t *testing.T) { type args struct { b *[]byte n string @@ -58,14 +58,14 @@ func Test_writeBoolProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeBoolJSONProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeBoolJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteBoolJSONProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteBoolJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeComma(t *testing.T) { +func Test_JSONWriteComma(t *testing.T) { type args struct { b *[]byte } @@ -81,7 +81,7 @@ func Test_writeComma(t *testing.T) { } } -func Test_writeDurationProp(t *testing.T) { +func Test_JSONWriteDurationProp(t *testing.T) { type args struct { b *[]byte n string @@ -96,14 +96,14 @@ func Test_writeDurationProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeDurationJSONProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeDurationJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteDurationJSONProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteDurationJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeFloatProp(t *testing.T) { +func Test_JSONWriteFloatProp(t *testing.T) { type args struct { b *[]byte n string @@ -118,14 +118,14 @@ func Test_writeFloatProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeFloatJSONProp(tt.args.b, tt.args.n, tt.args.f); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeFloatJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteFloatJSONProp(tt.args.b, tt.args.n, tt.args.f); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteFloatJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeIRIProp(t *testing.T) { +func Test_JSONWriteIRIProp(t *testing.T) { type args struct { b *[]byte n string @@ -140,14 +140,14 @@ func Test_writeIRIProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeIRIJSONProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeIRIJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteIRIJSONProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteIRIJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeIntProp(t *testing.T) { +func Test_JSONWriteIntProp(t *testing.T) { type args struct { b *[]byte n string @@ -162,14 +162,14 @@ func Test_writeIntProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeIntJSONProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeIntJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteIntJSONProp(tt.args.b, tt.args.n, tt.args.d); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteIntJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeIntransitiveActivity(t *testing.T) { +func Test_JSONWriteIntransitiveActivity(t *testing.T) { type args struct { b *[]byte i IntransitiveActivity @@ -183,14 +183,14 @@ func Test_writeIntransitiveActivity(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeIntransitiveActivityJSONValue(tt.args.b, tt.args.i); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeIntransitiveActivityJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteIntransitiveActivityJSONValue(tt.args.b, tt.args.i); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteIntransitiveActivityJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeItemCollection(t *testing.T) { +func Test_JSONWriteItemCollection(t *testing.T) { type args struct { b *[]byte col ItemCollection @@ -204,14 +204,14 @@ func Test_writeItemCollection(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeItemCollectionJSONValue(tt.args.b, tt.args.col); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeItemCollectionJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteItemCollectionJSONValue(tt.args.b, tt.args.col); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteItemCollectionJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeItemCollectionProp(t *testing.T) { +func Test_JSONWriteItemCollectionProp(t *testing.T) { type args struct { b *[]byte n string @@ -226,14 +226,14 @@ func Test_writeItemCollectionProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeItemCollectionJSONProp(tt.args.b, tt.args.n, tt.args.col); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeItemCollectionJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteItemCollectionJSONProp(tt.args.b, tt.args.n, tt.args.col); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteItemCollectionJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeItemProp(t *testing.T) { +func Test_JSONWriteItemProp(t *testing.T) { type args struct { b *[]byte n string @@ -248,14 +248,14 @@ func Test_writeItemProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeItemJSONProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeItemJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteItemJSONProp(tt.args.b, tt.args.n, tt.args.i); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteItemJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeNaturalLanguageProp(t *testing.T) { +func Test_JSONWriteNaturalLanguageProp(t *testing.T) { type args struct { b *[]byte n string @@ -270,14 +270,14 @@ func Test_writeNaturalLanguageProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeNaturalLanguageJSONProp(tt.args.b, tt.args.n, tt.args.nl); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeNaturalLanguageJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteNaturalLanguageJSONProp(tt.args.b, tt.args.n, tt.args.nl); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteNaturalLanguageJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeObject(t *testing.T) { +func Test_JSONWriteObject(t *testing.T) { type args struct { b *[]byte o Object @@ -291,14 +291,14 @@ func Test_writeObject(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeObjectJSONValue(tt.args.b, tt.args.o); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeObjectJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteObjectJSONValue(tt.args.b, tt.args.o); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteObjectJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeProp(t *testing.T) { +func Test_JSONWriteProp(t *testing.T) { type args struct { b *[]byte name string @@ -313,14 +313,14 @@ func Test_writeProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeJSONProp(tt.args.b, tt.args.name, tt.args.val); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteJSONProp(tt.args.b, tt.args.name, tt.args.val); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writePropName(t *testing.T) { +func Test_JSONWritePropName(t *testing.T) { type args struct { b *[]byte s string @@ -334,14 +334,14 @@ func Test_writePropName(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writePropJSONName(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writePropJSONName() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWritePropJSONName(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWritePropJSONName() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeQuestion(t *testing.T) { +func Test_JSONWriteQuestion(t *testing.T) { type args struct { b *[]byte q Question @@ -355,14 +355,14 @@ func Test_writeQuestion(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeQuestionJSONValue(tt.args.b, tt.args.q); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeQuestionJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteQuestionJSONValue(tt.args.b, tt.args.q); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteQuestionJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeS(t *testing.T) { +func Test_JSONWriteS(t *testing.T) { type args struct { b *[]byte s string @@ -379,7 +379,7 @@ func Test_writeS(t *testing.T) { } } -func Test_writeString(t *testing.T) { +func Test_JSONWriteString(t *testing.T) { type args struct { b *[]byte s string @@ -393,14 +393,14 @@ func Test_writeString(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeStringJSONValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeStringJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteStringJSONValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteStringJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeStringProp(t *testing.T) { +func Test_JSONWriteStringProp(t *testing.T) { type args struct { b *[]byte n string @@ -415,14 +415,14 @@ func Test_writeStringProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeStringJSONProp(tt.args.b, tt.args.n, tt.args.s); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeStringJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteStringJSONProp(tt.args.b, tt.args.n, tt.args.s); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteStringJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeTimeProp(t *testing.T) { +func Test_JSONWriteTimeProp(t *testing.T) { type args struct { b *[]byte n string @@ -437,14 +437,14 @@ func Test_writeTimeProp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeTimeJSONProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeTimeJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteTimeJSONProp(tt.args.b, tt.args.n, tt.args.t); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteTimeJSONProp() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } } -func Test_writeValue(t *testing.T) { +func Test_JSONWriteValue(t *testing.T) { type args struct { b *[]byte s []byte @@ -458,8 +458,8 @@ func Test_writeValue(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if gotNotEmpty := writeJSONValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty { - t.Errorf("writeJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) + if gotNotEmpty := JSONWriteJSONValue(tt.args.b, tt.args.s); gotNotEmpty != tt.wantNotEmpty { + t.Errorf("JSONWriteJSONValue() = %v, want %v", gotNotEmpty, tt.wantNotEmpty) } }) } diff --git a/intransitive_activity.go b/intransitive_activity.go index 2ff91a5..34b43fc 100644 --- a/intransitive_activity.go +++ b/intransitive_activity.go @@ -197,12 +197,12 @@ func (i *IntransitiveActivity) UnmarshalJSON(data []byte) error { // MarshalJSON encodes the receiver object to a JSON document. func (i IntransitiveActivity) MarshalJSON() ([]byte, error) { b := make([]byte, 0) - write(&b, '{') + JSONWrite(&b, '{') - if !writeIntransitiveActivityJSONValue(&b, i) { + if !JSONWriteIntransitiveActivityJSONValue(&b, i) { return nil, nil } - write(&b, '}') + JSONWrite(&b, '}') return b, nil } diff --git a/iri.go b/iri.go index 8f8eb70..b8994c8 100644 --- a/iri.go +++ b/iri.go @@ -73,9 +73,9 @@ func (i IRI) MarshalJSON() ([]byte, error) { return nil, nil } b := make([]byte, 0) - write(&b, '"') - writeS(&b, i.String()) - write(&b, '"') + JSONWrite(&b, '"') + JSONWriteS(&b, i.String()) + JSONWrite(&b, '"') return b, nil } @@ -175,19 +175,19 @@ func (i IRIs) MarshalJSON() ([]byte, error) { if len(i) == 0 { return nil, nil } - writeCommaIfNotEmpty := func(notEmpty bool) { + JSONWriteCommaIfNotEmpty := func(notEmpty bool) { if notEmpty { - writeS(&b, ",") + JSONWriteS(&b, ",") } } - write(&b, '[') + JSONWrite(&b, '[') for k, iri := range i { - writeCommaIfNotEmpty(k > 0) - write(&b, '"') - writeS(&b, iri.String()) - write(&b, '"') + JSONWriteCommaIfNotEmpty(k > 0) + JSONWrite(&b, '"') + JSONWriteS(&b, iri.String()) + JSONWrite(&b, '"') } - write(&b, ']') + JSONWrite(&b, ']') return b, nil } diff --git a/item_collection.go b/item_collection.go index 702995b..9e0acb0 100644 --- a/item_collection.go +++ b/item_collection.go @@ -39,7 +39,7 @@ func (i ItemCollection) MarshalJSON() ([]byte, error) { return nil, nil } b := make([]byte, 0) - writeItemCollectionJSONValue(&b, i) + JSONWriteItemCollectionJSONValue(&b, i) return b, nil } diff --git a/link.go b/link.go index 60be0c2..227ecb8 100644 --- a/link.go +++ b/link.go @@ -102,10 +102,10 @@ func (l Link) GetType() ActivityVocabularyType { // MarshalJSON encodes the receiver object to a JSON document. func (l Link) MarshalJSON() ([]byte, error) { b := make([]byte, 0) - write(&b, '{') + JSONWrite(&b, '{') - if writeLinkJSONValue(&b, l) { - write(&b, '}') + if JSONWriteLinkJSONValue(&b, l) { + JSONWrite(&b, '}') return b, nil } return nil, nil diff --git a/object.go b/object.go index 5889315..21ce2b1 100644 --- a/object.go +++ b/object.go @@ -109,7 +109,7 @@ func (a ActivityVocabularyType) MarshalJSON() ([]byte, error) { return nil, nil } b := make([]byte, 0) - writeStringJSONValue(&b, string(a)) + JSONWriteStringJSONValue(&b, string(a)) return b, nil } @@ -295,10 +295,10 @@ func (o *Object) UnmarshalJSON(data []byte) error { // MarshalJSON encodes the receiver object to a JSON document. func (o Object) MarshalJSON() ([]byte, error) { b := make([]byte, 0) - write(&b, '{') + JSONWrite(&b, '{') - if writeObjectJSONValue(&b, o) { - write(&b, '}') + if JSONWriteObjectJSONValue(&b, o) { + JSONWrite(&b, '}') return b, nil } return nil, nil @@ -564,7 +564,7 @@ func (m MimeType) MarshalJSON() ([]byte, error) { return nil, nil } b := make([]byte, 0) - writeStringJSONValue(&b, string(m)) + JSONWriteStringJSONValue(&b, string(m)) return b, nil } @@ -728,17 +728,17 @@ func (s *Source) UnmarshalJSON(data []byte) error { func (s Source) MarshalJSON() ([]byte, error) { b := make([]byte, 0) empty := true - write(&b, '{') + JSONWrite(&b, '{') if len(s.MediaType) > 0 { if v, err := s.MediaType.MarshalJSON(); err == nil && len(v) > 0 { - empty = !writeJSONProp(&b, "mediaType", v) + empty = !JSONWriteJSONProp(&b, "mediaType", v) } } if len(s.Content) > 0 { - empty = !writeNaturalLanguageJSONProp(&b, "content", s.Content) + empty = !JSONWriteNaturalLanguageJSONProp(&b, "content", s.Content) } if !empty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil diff --git a/ordered_collection.go b/ordered_collection.go index 24900b6..8c90291 100644 --- a/ordered_collection.go +++ b/ordered_collection.go @@ -228,27 +228,27 @@ func (o *OrderedCollection) UnmarshalJSON(data []byte) error { func (o OrderedCollection) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := false - write(&b, '{') + JSONWrite(&b, '{') OnObject(o, func(o *Object) error { - notEmpty = writeObjectJSONValue(&b, *o) + notEmpty = JSONWriteObjectJSONValue(&b, *o) return nil }) if o.Current != nil { - notEmpty = writeItemJSONProp(&b, "current", o.Current) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "current", o.Current) || notEmpty } if o.First != nil { - notEmpty = writeItemJSONProp(&b, "first", o.First) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "first", o.First) || notEmpty } if o.Last != nil { - notEmpty = writeItemJSONProp(&b, "last", o.Last) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "last", o.Last) || notEmpty } - notEmpty = writeIntJSONProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty + notEmpty = JSONWriteIntJSONProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty if o.OrderedItems != nil { - notEmpty = writeItemCollectionJSONProp(&b, "orderedItems", o.OrderedItems) || notEmpty + notEmpty = JSONWriteItemCollectionJSONProp(&b, "orderedItems", o.OrderedItems) || notEmpty } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil diff --git a/ordered_collection_page.go b/ordered_collection_page.go index 04f46d5..0a8dd15 100644 --- a/ordered_collection_page.go +++ b/ordered_collection_page.go @@ -205,36 +205,36 @@ func (o *OrderedCollectionPage) UnmarshalJSON(data []byte) error { func (o OrderedCollectionPage) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := false - write(&b, '{') + JSONWrite(&b, '{') OnObject(o, func(o *Object) error { - notEmpty = writeObjectJSONValue(&b, *o) + notEmpty = JSONWriteObjectJSONValue(&b, *o) return nil }) if o.PartOf != nil { - notEmpty = writeItemJSONProp(&b, "partOf", o.PartOf) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "partOf", o.PartOf) || notEmpty } if o.Current != nil { - notEmpty = writeItemJSONProp(&b, "current", o.Current) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "current", o.Current) || notEmpty } if o.First != nil { - notEmpty = writeItemJSONProp(&b, "first", o.First) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "first", o.First) || notEmpty } if o.Last != nil { - notEmpty = writeItemJSONProp(&b, "last", o.Last) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "last", o.Last) || notEmpty } if o.Next != nil { - notEmpty = writeItemJSONProp(&b, "next", o.Next) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "next", o.Next) || notEmpty } if o.Prev != nil { - notEmpty = writeItemJSONProp(&b, "prev", o.Prev) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "prev", o.Prev) || notEmpty } - notEmpty = writeIntJSONProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty + notEmpty = JSONWriteIntJSONProp(&b, "totalItems", int64(o.TotalItems)) || notEmpty if o.OrderedItems != nil { - notEmpty = writeItemCollectionJSONProp(&b, "orderedItems", o.OrderedItems) || notEmpty + notEmpty = JSONWriteItemCollectionJSONProp(&b, "orderedItems", o.OrderedItems) || notEmpty } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil diff --git a/place.go b/place.go index c7f6033..adc1935 100644 --- a/place.go +++ b/place.go @@ -170,32 +170,32 @@ func (p *Place) UnmarshalJSON(data []byte) error { func (p Place) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := false - write(&b, '{') + JSONWrite(&b, '{') OnObject(p, func(o *Object) error { - notEmpty = writeObjectJSONValue(&b, *o) + notEmpty = JSONWriteObjectJSONValue(&b, *o) return nil }) if p.Accuracy > 0 { - notEmpty = writeFloatJSONProp(&b, "accuracy", p.Accuracy) || notEmpty + notEmpty = JSONWriteFloatJSONProp(&b, "accuracy", p.Accuracy) || notEmpty } if p.Altitude > 0 { - notEmpty = writeFloatJSONProp(&b, "altitude", p.Altitude) || notEmpty + notEmpty = JSONWriteFloatJSONProp(&b, "altitude", p.Altitude) || notEmpty } if p.Latitude > 0 { - notEmpty = writeFloatJSONProp(&b, "latitude", p.Latitude) || notEmpty + notEmpty = JSONWriteFloatJSONProp(&b, "latitude", p.Latitude) || notEmpty } if p.Longitude > 0 { - notEmpty = writeFloatJSONProp(&b, "longitude", p.Longitude) || notEmpty + notEmpty = JSONWriteFloatJSONProp(&b, "longitude", p.Longitude) || notEmpty } if p.Radius > 0 { - notEmpty = writeIntJSONProp(&b, "radius", p.Radius) || notEmpty + notEmpty = JSONWriteIntJSONProp(&b, "radius", p.Radius) || notEmpty } if len(p.Units) > 0 { - notEmpty = writeStringJSONProp(&b, "radius", p.Units) || notEmpty + notEmpty = JSONWriteStringJSONProp(&b, "radius", p.Units) || notEmpty } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil diff --git a/profile.go b/profile.go index 8f7f14c..f5c6cd1 100644 --- a/profile.go +++ b/profile.go @@ -156,18 +156,18 @@ func (p *Profile) UnmarshalJSON(data []byte) error { func (p Profile) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := false - write(&b, '{') + JSONWrite(&b, '{') OnObject(p, func(o *Object) error { return nil }) if p.Describes != nil { - notEmpty = writeItemJSONProp(&b, "describes", p.Describes) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "describes", p.Describes) || notEmpty } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil diff --git a/question.go b/question.go index 44767f1..31936fd 100644 --- a/question.go +++ b/question.go @@ -180,12 +180,12 @@ func (q *Question) UnmarshalJSON(data []byte) error { // MarshalJSON encodes the receiver object to a JSON document. func (q Question) MarshalJSON() ([]byte, error) { b := make([]byte, 0) - write(&b, '{') + JSONWrite(&b, '{') - if !writeQuestionJSONValue(&b, q) { + if !JSONWriteQuestionJSONValue(&b, q) { return nil, nil } - write(&b, '}') + JSONWrite(&b, '}') return b, nil } diff --git a/relationship.go b/relationship.go index cb5ea0e..2921ce6 100644 --- a/relationship.go +++ b/relationship.go @@ -166,25 +166,25 @@ func (r *Relationship) UnmarshalJSON(data []byte) error { func (r Relationship) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := false - write(&b, '{') + JSONWrite(&b, '{') OnObject(r, func(o *Object) error { - notEmpty = writeObjectJSONValue(&b, *o) + notEmpty = JSONWriteObjectJSONValue(&b, *o) return nil }) if r.Subject != nil { - notEmpty = writeItemJSONProp(&b, "subject", r.Subject) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "subject", r.Subject) || notEmpty } if r.Object != nil { - notEmpty = writeItemJSONProp(&b, "object", r.Object) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "object", r.Object) || notEmpty } if r.Relationship != nil { - notEmpty = writeItemJSONProp(&b, "relationship", r.Relationship) || notEmpty + notEmpty = JSONWriteItemJSONProp(&b, "relationship", r.Relationship) || notEmpty } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil diff --git a/tombstone.go b/tombstone.go index 2d11f5c..e0dda3f 100644 --- a/tombstone.go +++ b/tombstone.go @@ -158,22 +158,22 @@ func (t *Tombstone) UnmarshalJSON(data []byte) error { func (t Tombstone) MarshalJSON() ([]byte, error) { b := make([]byte, 0) notEmpty := false - write(&b, '{') + JSONWrite(&b, '{') OnObject(t, func(o *Object) error { - notEmpty = writeObjectJSONValue(&b, *o) + notEmpty = JSONWriteObjectJSONValue(&b, *o) return nil }) if len(t.FormerType) > 0 { if v, err := t.FormerType.MarshalJSON(); err == nil && len(v) > 0 { - notEmpty = writeJSONProp(&b, "formerType", v) || notEmpty + notEmpty = JSONWriteJSONProp(&b, "formerType", v) || notEmpty } } if !t.Deleted.IsZero() { - notEmpty = writeTimeJSONProp(&b, "deleted", t.Deleted) || notEmpty + notEmpty = JSONWriteTimeJSONProp(&b, "deleted", t.Deleted) || notEmpty } if notEmpty { - write(&b, '}') + JSONWrite(&b, '}') return b, nil } return nil, nil