Set the ItemTyperFunc when doing UnmarshalJSON
This commit is contained in:
parent
14d9cc820b
commit
385e1cd273
5 changed files with 16 additions and 1 deletions
|
@ -1532,6 +1532,9 @@ func (u *Update) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// UnmarshalJSON
|
||||
func (c *Create) UnmarshalJSON(data []byte) error {
|
||||
if ItemTyperFunc == nil {
|
||||
ItemTyperFunc = JSONGetItemByType
|
||||
}
|
||||
a := Activity(*c)
|
||||
err := a.UnmarshalJSON(data)
|
||||
|
||||
|
|
|
@ -203,6 +203,9 @@ func (o OrderedCollection) IsObject() bool {
|
|||
|
||||
// UnmarshalJSON
|
||||
func (o *OrderedCollection) UnmarshalJSON(data []byte) error {
|
||||
if ItemTyperFunc == nil {
|
||||
ItemTyperFunc = JSONGetItemByType
|
||||
}
|
||||
o.Parent.UnmarshalJSON(data)
|
||||
|
||||
o.TotalItems = uint(JSONGetInt(data, "totalItems"))
|
||||
|
|
3
link.go
3
link.go
|
@ -116,6 +116,9 @@ func (m Mention) GetType() ActivityVocabularyType {
|
|||
|
||||
// UnmarshalJSON
|
||||
func (l *Link) UnmarshalJSON(data []byte) error {
|
||||
if ItemTyperFunc == nil {
|
||||
ItemTyperFunc = JSONGetItemByType
|
||||
}
|
||||
l.ID = JSONGetObjectID(data)
|
||||
l.Type = JSONGetType(data)
|
||||
l.MediaType = JSONGetMimeType(data)
|
||||
|
|
|
@ -531,6 +531,9 @@ func (c *MimeType) UnmarshalJSON(data []byte) error {
|
|||
|
||||
// UnmarshalJSON
|
||||
func (o *Object) UnmarshalJSON(data []byte) error {
|
||||
if ItemTyperFunc == nil {
|
||||
ItemTyperFunc = JSONGetItemByType
|
||||
}
|
||||
o.ID = JSONGetObjectID(data)
|
||||
o.Type = JSONGetType(data)
|
||||
o.Name = JSONGetNaturalLanguageField(data, "name")
|
||||
|
|
|
@ -19,7 +19,7 @@ var (
|
|||
|
||||
// ItemTyperFunc will return an instance of a struct that implements activitystreams.Item
|
||||
// The default for this package is JSONGetItemByType but can be overwritten
|
||||
var ItemTyperFunc TyperFunction = JSONGetItemByType
|
||||
var ItemTyperFunc TyperFunction
|
||||
|
||||
// TyperFunction is the type of the function which returns an activitystreams.Item struct instance
|
||||
// for a specific ActivityVocabularyType
|
||||
|
@ -234,6 +234,9 @@ func JSONGetIRI(data []byte, prop string) IRI {
|
|||
// UnmarshalJSON tries to detect the type of the object in the json data and then outputs a matching
|
||||
// ActivityStreams object, if possible
|
||||
func UnmarshalJSON(data []byte) (Item, error) {
|
||||
if ItemTyperFunc == nil {
|
||||
ItemTyperFunc = JSONGetItemByType
|
||||
}
|
||||
return JSONUnmarshalToItem(data), nil
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue