Fix quotes being kept for string type aliases in UnmarshalJSON methods

This commit is contained in:
Marius Orcsik 2018-07-18 16:29:47 +02:00
parent 12f481a243
commit 6b75ad6d68
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
2 changed files with 31 additions and 12 deletions

View file

@ -4,9 +4,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"sort" "sort"
"strings"
"time" "time"
"github.com/buger/jsonparser"
) )
// ObjectID designates an unique global identifier. // ObjectID designates an unique global identifier.
@ -93,12 +92,13 @@ type (
ActivityObject ActivityObject
GetID() ObjectID GetID() ObjectID
GetType() ActivityVocabularyType GetType() ActivityVocabularyType
//UnmarshalJSON([]byte) error
} }
// ObjectsArr is a named type for matching an ObjectOrLink slice type to Collection interface // ObjectsArr is a named type for matching an ObjectOrLink slice type to Collection interface
ObjectsArr []ObjectOrLink ObjectsArr []ObjectOrLink
// LinkOrURI is an interface that GetID and GetLink structs implement, and at the same time // LinkOrURI is an interface that GetID and GetLink structs implement, and at the same time
// they are kept disjointed // they are kept disjointed
LinkOrURI interface{ LinkOrURI interface {
GetLink() URI GetLink() URI
} }
// ImageOrLink is an interface that Image and GetLink structs implement // ImageOrLink is an interface that Image and GetLink structs implement
@ -360,18 +360,21 @@ func recipientsDeduplication(recArgs ...*ObjectsArr) error {
return nil return nil
} }
func (i *ObjectID) MarshalJSON(data []byte) error { // UnmarshalJSON
*i = ObjectID(data) func (i *ObjectID) UnmarshalJSON(data []byte) error {
*i = ObjectID(strings.Trim(string(data), "\""))
return nil return nil
} }
func (c *ContentType) MarshalJSON(data []byte) error { // UnmarshalJSON
*c = ContentType(data) func (c *ContentType) UnmarshalJSON(data []byte) error {
*c = ContentType(strings.Trim(string(data), "\""))
return nil return nil
} }
func (o *Object) MarshalJSON(data []byte) error { //func (o *Object) UnmarshalJSON(data []byte) error {
val, _, _, _ := jsonparser.Get(data, "ID") // //_o, err := unmarshalJSONFromType(data, ObjectType)
o.ID.MarshalJSON(val) //
return nil // //*o = _o.(Object)
} // return nil
//}

View file

@ -1,5 +1,9 @@
package activitypub package activitypub
import (
"strings"
)
type ( type (
// IRI is a Internationalized Resource Identifiers (IRIs) RFC3987 // IRI is a Internationalized Resource Identifiers (IRIs) RFC3987
IRI URI IRI URI
@ -27,3 +31,15 @@ func (u URI) GetLink() URI {
func (i IRI) GetLink() URI { func (i IRI) GetLink() URI {
return URI(i) return URI(i)
} }
// UnmarshalJSON
func (u *URI) UnmarshalJSON(s []byte) error {
*u = URI(strings.Trim(string(s), "\""))
return nil
}
// UnmarshalJSON
func (i *IRI) UnmarshalJSON(s []byte) error {
*i = IRI(strings.Trim(string(s), "\""))
return nil
}