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

View file

@ -1,5 +1,9 @@
package activitypub
import (
"strings"
)
type (
// IRI is a Internationalized Resource Identifiers (IRIs) RFC3987
IRI URI
@ -27,3 +31,15 @@ func (u URI) GetLink() URI {
func (i IRI) GetLink() URI {
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
}