Debian Codegraph @

decoding_json.go

raw symbols SCIP
1 package activitypub
2
3 import (
4 "encoding"
5 "encoding/json"
6 "fmt"
7 "net/url"
8 "reflect"
9 "strings"
10 "time"
11
12 "github.com/valyala/fastjson"
13 )
14
15 var (
16 apUnmarshalerType = reflect.TypeOf(new(Item)).Elem()
17 unmarshalerType = reflect.TypeOf(new(json.Unmarshaler)).Elem()
18 textUnmarshalerType = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
19 )
20
21 // ItemTyperFunc will return an instance of a struct that implements activitypub.Item
22 // The default for this package is GetItemByType but can be overwritten
23 var ItemTyperFunc TyperFn = GetItemByType
24
25 // JSONItemUnmarshal can be set externally to populate a custom object based on its type
26 var JSONItemUnmarshal JSONUnmarshalerFn = nil
27
28 // IsNotEmpty checks if an object is empty
29 var IsNotEmpty NotEmptyCheckerFn = NotEmpty
30
31 // TyperFn is the type of the function which returns an Item struct instance
32 // for a specific ActivityVocabularyType
33 type TyperFn func(Typer) (Item, error)
34
35 // JSONUnmarshalerFn is the type of the function that will load the data from a fastjson.Value into an Item
36 // that the current package doesn't know about.
37 type JSONUnmarshalerFn func(Typer, *fastjson.Value, Item) error
38
39 // NotEmptyCheckerFn is the type of the function that checks if an object is empty
40 type NotEmptyCheckerFn func(Item) bool
41
42 func JSONGetID(val *fastjson.Value) ID {
43 i := val.Get("id").GetStringBytes()
44 return ID(i)
45 }
46
47 func JSONGetTypes(val *fastjson.Value) Typer {
48 value := val.Get("type")
49 if value == nil {
50 return nil
51 }
52 switch value.Type() {
53 case fastjson.TypeString:
54 return ActivityVocabularyType(value.GetStringBytes())
55 case fastjson.TypeArray:
56 valArr, err := value.Array()
57 if err != nil {
58 return ActivityVocabularyTypes{}
59 }
60 types := make(ActivityVocabularyTypes, len(valArr))
61 for i, v := range valArr {
62 types[i] = ActivityVocabularyType(v.GetStringBytes())
63 }
64 return types
65 default:
66 return nil
67 }
68 }
69
70 func JSONGetMimeType(val *fastjson.Value, prop string) MimeType {
71 if !val.Exists(prop) {
72 return ""
73 }
74 t := val.GetStringBytes(prop)
75 return MimeType(t)
76 }
77
78 func JSONGetInt(val *fastjson.Value, prop string) int64 {
79 if !val.Exists(prop) {
80 return 0
81 }
82 i := val.Get(prop).GetInt64()
83 return i
84 }
85
86 func JSONGetFloat(val *fastjson.Value, prop string) float64 {
87 if !val.Exists(prop) {
88 return 0.0
89 }
90 f := val.Get(prop).GetFloat64()
91 return f
92 }
93
94 func JSONGetString(val *fastjson.Value, prop string) string {
95 if !val.Exists(prop) {
96 return ""
97 }
98 s := val.Get(prop).GetStringBytes()
99 return string(s)
100 }
101
102 func JSONGetBytes(val *fastjson.Value, prop string) []byte {
103 if !val.Exists(prop) {
104 return nil
105 }
106 s := val.Get(prop).GetStringBytes()
107 return s
108 }
109
110 func JSONGetBoolean(val *fastjson.Value, prop string) bool {
111 if !val.Exists(prop) {
112 return false
113 }
114 t, _ := val.Get(prop).Bool()
115 return t
116 }
117
118 func JSONGetNaturalLanguageField(val *fastjson.Value, prop string) NaturalLanguageValues {
119 n := make(NaturalLanguageValues)
120 if val == nil {
121 return n
122 }
123 v := val.Get(prop)
124 if v == nil {
125 return nil
126 }
127 switch v.Type() {
128 case fastjson.TypeObject:
129 ob, _ := v.Object()
130 ob.Visit(func(key []byte, v *fastjson.Value) {
131 cont := Content{}
132 ref := MakeRef(key)
133 if err := cont.UnmarshalJSON(v.GetStringBytes()); err == nil {
134 if ref != NilLangRef || len(cont) > 0 {
135 n[ref] = cont
136 }
137 }
138 })
139 case fastjson.TypeString:
140 if raw := v.GetStringBytes(); len(raw) > 0 {
141 n[DefaultLang] = raw
142 }
143 }
144
145 return n
146 }
147
148 func JSONGetTime(val *fastjson.Value, prop string) time.Time {
149 t := time.Time{}
150 if val == nil {
151 return t
152 }
153
154 if str := val.Get(prop).GetStringBytes(); len(str) > 0 {
155 t.UnmarshalText(str)
156 return t.UTC()
157 }
158 return t
159 }
160
161 func JSONGetDuration(val *fastjson.Value, prop string) time.Duration {
162 if str := val.Get(prop).GetStringBytes(); len(str) > 0 {
163 // TODO(marius): this needs to be replaced to be compatible with xsd:duration
164 d, _ := time.ParseDuration(string(str))
165 return d
166 }
167 return 0
168 }
169
170 func JSONGetPublicKey(val *fastjson.Value, prop string) PublicKey {
171 key := PublicKey{}
172 if val == nil {
173 return key
174 }
175 val = val.Get(prop)
176 if val == nil {
177 return key
178 }
179 JSONLoadPublicKey(val, &key)
180 return key
181 }
182
183 func JSONItemsFn(val *fastjson.Value) (Item, error) {
184 if val.Type() == fastjson.TypeArray {
185 it := val.GetArray()
186 items := make(ItemCollection, 0)
187 for _, v := range it {
188 if it, _ := JSONLoadItem(v); it != nil {
189 _ = items.Append(it)
190 }
191 }
192 return items, nil
193 }
194 return JSONLoadItem(val)
195 }
196
197 func looksLikeALink(val *fastjson.Value) bool {
198 return val.Exists("href")
199 }
200
201 func JSONLoadItem(val *fastjson.Value) (Item, error) {
202 typ := JSONGetTypes(val)
203 if typ == nil {
204 typ = NilType
205 }
206
207 if EmptyTypes(typ.AsTypes()...) && val.Type() == fastjson.TypeString {
208 // try to see if it's an IRI
209 if i, ok := asIRI(val); ok {
210 return i, nil
211 }
212 }
213 i, err := ItemTyperFunc(typ)
214 if err != nil || IsNil(i) {
215 return nil, nil
216 }
217 var empty = func(i Item) bool { return !IsNotEmpty(i) }
218
219 // NOTE(marius): see note for the [GetItemByType] switch, the same applies here.
220 switch {
221 case CollectionType.Match(typ):
222 err = OnCollection(i, func(c *Collection) error {
223 return JSONLoadCollection(val, c)
224 })
225 case OrderedCollectionType.Match(typ):
226 err = OnOrderedCollection(i, func(c *OrderedCollection) error {
227 return JSONLoadOrderedCollection(val, c)
228 })
229 case CollectionPageType.Match(typ):
230 err = OnCollectionPage(i, func(p *CollectionPage) error {
231 return JSONLoadCollectionPage(val, p)
232 })
233 case OrderedCollectionPageType.Match(typ):
234 err = OnOrderedCollectionPage(i, func(p *OrderedCollectionPage) error {
235 return JSONLoadOrderedCollectionPage(val, p)
236 })
237 case PlaceType.Match(typ):
238 err = OnPlace(i, func(p *Place) error {
239 return JSONLoadPlace(val, p)
240 })
241 case ProfileType.Match(typ):
242 err = OnProfile(i, func(p *Profile) error {
243 return JSONLoadProfile(val, p)
244 })
245 case RelationshipType.Match(typ):
246 err = OnRelationship(i, func(r *Relationship) error {
247 return JSONLoadRelationship(val, r)
248 })
249 case TombstoneType.Match(typ):
250 err = OnTombstone(i, func(t *Tombstone) error {
251 return JSONLoadTombstone(val, t)
252 })
253 case QuestionType.Match(typ):
254 err = OnQuestion(i, func(q *Question) error {
255 return JSONLoadQuestion(val, q)
256 })
257 case NilType.Match(typ):
258 if looksLikeALink(val) {
259 // NOTE(marius): this handles Links without a type
260 return JSONLoadLink(val)
261 }
262 err = OnObject(i, func(ob *Object) error {
263 // NOTE(marius): this handles Tags which usually don't have types
264 return JSONLoadObject(val, ob)
265 })
266 case ActivityVocabularyTypes{ObjectType, ArticleType, AudioType, DocumentType, EventType, ImageType, NoteType, PageType, VideoType}.Match(typ):
267 err = OnObject(i, func(ob *Object) error {
268 return JSONLoadObject(val, ob)
269 })
270 case ActivityVocabularyTypes{LinkType, MentionType}.Match(typ):
271 // NOTE(marius): if we have a clear link type, we override
272 i = new(Link)
273 err = OnLink(i, func(l *Link) error {
274 return jsonLoadToLink(val, l)
275 })
276 case ActivityVocabularyTypes{ActivityType, AcceptType, AddType, AnnounceType, BlockType, CreateType, DeleteType, DislikeType,
277 FlagType, FollowType, IgnoreType, InviteType, JoinType, LeaveType, LikeType, ListenType, MoveType, OfferType,
278 RejectType, ReadType, RemoveType, TentativeRejectType, TentativeAcceptType, UndoType, UpdateType, ViewType}.Match(typ):
279 err = OnActivity(i, func(act *Activity) error {
280 return JSONLoadActivity(val, act)
281 })
282 case ActivityVocabularyTypes{IntransitiveActivityType, ArriveType, TravelType}.Match(typ):
283 err = OnIntransitiveActivity(i, func(act *IntransitiveActivity) error {
284 return JSONLoadIntransitiveActivity(val, act)
285 })
286 case ActivityVocabularyTypes{ActorType, ApplicationType, GroupType, OrganizationType, PersonType, ServiceType}.Match(typ):
287 err = OnActor(i, func(a *Actor) error {
288 return JSONLoadActor(val, a)
289 })
290 default:
291 if JSONItemUnmarshal == nil {
292 return nil, fmt.Errorf("unable to unmarshal custom type %s, you need to set a correct function for JSONItemUnmarshal", typ)
293 }
294 err = JSONItemUnmarshal(typ, val, i)
295 }
296 if err != nil {
297 return nil, err
298 }
299 if empty(i) {
300 return nil, nil
301 }
302
303 return i, nil
304 }
305
306 func JSONUnmarshalToItem(val *fastjson.Value) Item {
307 var (
308 i Item
309 err error
310 )
311 switch val.Type() {
312 case fastjson.TypeArray:
313 i, err = JSONItemsFn(val)
314 case fastjson.TypeObject:
315 i, err = JSONLoadItem(val)
316 case fastjson.TypeString:
317 if iri, ok := asIRI(val); ok {
318 // try to see if it's an IRI
319 i = iri
320 }
321 }
322 if err != nil {
323 return nil
324 }
325 return i
326 }
327
328 func asIRI(val *fastjson.Value) (IRI, bool) {
329 if val == nil {
330 return NilIRI, true
331 }
332 s := strings.Trim(val.String(), `"`)
333 u, err := url.ParseRequestURI(s)
334 if err == nil && len(u.Scheme) > 0 && len(u.Host) > 0 {
335 // try to see if it's an IRI
336 return IRI(s), true
337 }
338 return EmptyIRI, false
339 }
340
341 func JSONGetItem(val *fastjson.Value, prop string) Item {
342 if val == nil {
343 return nil
344 }
345 if val = val.Get(prop); val == nil {
346 return nil
347 }
348 switch val.Type() {
349 case fastjson.TypeString:
350 if i, ok := asIRI(val); ok {
351 // try to see if it's an IRI
352 return i
353 }
354 case fastjson.TypeArray:
355 it, _ := JSONItemsFn(val)
356 return it
357 case fastjson.TypeObject:
358 it, _ := JSONLoadItem(val)
359 return it
360 case fastjson.TypeNumber:
361 fallthrough
362 case fastjson.TypeNull:
363 fallthrough
364 default:
365 return nil
366 }
367 return nil
368 }
369
370 func JSONGetURIItem(val *fastjson.Value, prop string) Item {
371 if val == nil {
372 return nil
373 }
374 if val = val.Get(prop); val == nil {
375 return nil
376 }
377 switch val.Type() {
378 case fastjson.TypeObject:
379 if it, _ := JSONLoadItem(val); it != nil {
380 return it
381 }
382 case fastjson.TypeArray:
383 if it, _ := JSONItemsFn(val); it != nil {
384 return it
385 }
386 case fastjson.TypeString:
387 return IRI(val.GetStringBytes())
388 }
389
390 return nil
391 }
392
393 func JSONGetItems(val *fastjson.Value, prop string) ItemCollection {
394 if val == nil {
395 return nil
396 }
397 if val = val.Get(prop); val == nil {
398 return nil
399 }
400
401 it := make(ItemCollection, 0)
402 switch val.Type() {
403 case fastjson.TypeArray:
404 for _, v := range val.GetArray() {
405 if i, _ := JSONLoadItem(v); i != nil {
406 _ = it.Append(i)
407 }
408 }
409 case fastjson.TypeObject:
410 if i := JSONGetItem(val, prop); i != nil {
411 _ = it.Append(i)
412 }
413 case fastjson.TypeString:
414 if iri := val.GetStringBytes(); len(iri) > 0 {
415 _ = it.Append(IRI(iri))
416 }
417 }
418 if len(it) == 0 {
419 return nil
420 }
421 return it
422 }
423
424 func JSONGetLangRefField(val *fastjson.Value, prop string) LangRef {
425 s := val.Get(prop).GetStringBytes()
426 return MakeRef(s)
427 }
428
429 func JSONGetIRI(val *fastjson.Value, prop string) IRI {
430 s := val.Get(prop).GetStringBytes()
431 return IRI(s)
432 }
433
434 // UnmarshalJSON tries to detect the type of the object in the json data and then outputs a matching
435 // ActivityStreams object, if possible
436 func UnmarshalJSON(data []byte) (Item, error) {
437 if len(data) == 0 {
438 return nil, nil
439 }
440 p := fastjson.Parser{}
441 val, err := p.ParseBytes(data)
442 if err != nil {
443 return nil, err
444 }
445 return JSONUnmarshalToItem(val), nil
446 }
447
448 func GetItemByType(typ Typer) (Item, error) {
449 if typ == nil {
450 typ = NilType
451 }
452 switch {
453 case ActivityVocabularyTypes{CollectionType}.Match(typ):
454 return &Collection{Type: typ}, nil
455 case ActivityVocabularyTypes{OrderedCollectionType}.Match(typ):
456 return &OrderedCollection{Type: typ}, nil
457 case ActivityVocabularyTypes{CollectionPageType}.Match(typ):
458 return &CollectionPage{Type: typ}, nil
459 case ActivityVocabularyTypes{OrderedCollectionPageType}.Match(typ):
460 return &OrderedCollectionPage{Type: typ}, nil
461 case ActivityVocabularyTypes{PlaceType}.Match(typ):
462 return &Place{Type: typ}, nil
463 case ActivityVocabularyTypes{ProfileType}.Match(typ):
464 return &Profile{Type: typ}, nil
465 case ActivityVocabularyTypes{RelationshipType}.Match(typ):
466 return &Relationship{Type: typ}, nil
467 case ActivityVocabularyTypes{TombstoneType}.Match(typ):
468 return &Tombstone{Type: typ}, nil
469 case ActivityVocabularyTypes{QuestionType}.Match(typ):
470 return &Question{Type: typ}, nil
471 case ActivityVocabularyTypes{ObjectType, ArticleType, AudioType, DocumentType, EventType, ImageType, NoteType, PageType, VideoType}.Match(typ):
472 return ObjectNew(typ), nil
473 case ActivityVocabularyTypes{LinkType, MentionType}.Match(typ):
474 return &Link{Type: typ}, nil
475 case ActivityVocabularyTypes{ActivityType, AcceptType, AddType, AnnounceType, BlockType, CreateType, DeleteType, DislikeType,
476 FlagType, FollowType, IgnoreType, InviteType, JoinType, LeaveType, LikeType, ListenType, MoveType, OfferType,
477 RejectType, ReadType, RemoveType, TentativeRejectType, TentativeAcceptType, UndoType, UpdateType, ViewType}.Match(typ):
478 return &Activity{Type: typ}, nil
479 case ActivityVocabularyTypes{IntransitiveActivityType, ArriveType, TravelType}.Match(typ):
480 return &IntransitiveActivity{Type: typ}, nil
481 case ActivityVocabularyTypes{ActorType, ApplicationType, GroupType, OrganizationType, PersonType, ServiceType}.Match(typ):
482 return &Actor{Type: typ}, nil
483 case ActivityVocabularyTypes{NilType}.Match(typ):
484 fallthrough
485 default:
486 // when no type is available use a plain Object
487 return &Object{}, nil
488 }
489 }
490
491 func JSONGetActorEndpoints(val *fastjson.Value, prop string) *Endpoints {
492 if val == nil {
493 return nil
494 }
495 if val = val.Get(prop); val == nil {
496 return nil
497 }
498
499 e := Endpoints{}
500 e.UploadMedia = JSONGetURIItem(val, "uploadMedia")
501 e.OauthAuthorizationEndpoint = JSONGetURIItem(val, "oauthAuthorizationEndpoint")
502 e.OauthTokenEndpoint = JSONGetURIItem(val, "oauthTokenEndpoint")
503 e.SharedInbox = JSONGetURIItem(val, "sharedInbox")
504 e.ProvideClientKey = JSONGetURIItem(val, "provideClientKey")
505 e.SignClientKey = JSONGetURIItem(val, "signClientKey")
506
507 return &e
508 }
509
510 func JSONLoadObject(val *fastjson.Value, o *Object) error {
511 o.ID = JSONGetID(val)
512 o.Type = JSONGetTypes(val)
513 o.Name = JSONGetNaturalLanguageField(val, "name")
514 o.Content = JSONGetNaturalLanguageField(val, "content")
515 o.Summary = JSONGetNaturalLanguageField(val, "summary")
516 o.Context = JSONGetItem(val, "context")
517 o.URL = JSONGetURIItem(val, "url")
518 o.MediaType = JSONGetMimeType(val, "mediaType")
519 o.Generator = JSONGetItem(val, "generator")
520 o.AttributedTo = JSONGetItem(val, "attributedTo")
521 o.Attachment = JSONGetItem(val, "attachment")
522 o.Location = JSONGetItem(val, "location")
523 o.Published = JSONGetTime(val, "published")
524 o.StartTime = JSONGetTime(val, "startTime")
525 o.EndTime = JSONGetTime(val, "endTime")
526 o.Duration = JSONGetDuration(val, "duration")
527 o.Icon = JSONGetItem(val, "icon")
528 o.Preview = JSONGetItem(val, "preview")
529 o.Image = JSONGetItem(val, "image")
530 o.Updated = JSONGetTime(val, "updated")
531 o.InReplyTo = JSONGetItem(val, "inReplyTo")
532 o.To = JSONGetItems(val, "to")
533 o.Audience = JSONGetItems(val, "audience")
534 o.Bto = JSONGetItems(val, "bto")
535 o.CC = JSONGetItems(val, "cc")
536 o.BCC = JSONGetItems(val, "bcc")
537 o.Replies = JSONGetItem(val, "replies")
538 o.Tag = JSONGetItems(val, "tag")
539 o.Likes = JSONGetItem(val, "likes")
540 o.Shares = JSONGetItem(val, "shares")
541 o.Source = GetAPSource(val)
542 return nil
543 }
544
545 func JSONLoadIntransitiveActivity(val *fastjson.Value, i *IntransitiveActivity) error {
546 i.Actor = JSONGetItem(val, "actor")
547 i.Target = JSONGetItem(val, "target")
548 i.Result = JSONGetItem(val, "result")
549 i.Origin = JSONGetItem(val, "origin")
550 i.Instrument = JSONGetItem(val, "instrument")
551 return OnObject(i, func(o *Object) error {
552 return JSONLoadObject(val, o)
553 })
554 }
555
556 func JSONLoadActivity(val *fastjson.Value, a *Activity) error {
557 a.Object = JSONGetItem(val, "object")
558 return OnIntransitiveActivity(a, func(i *IntransitiveActivity) error {
559 return JSONLoadIntransitiveActivity(val, i)
560 })
561 }
562
563 func JSONLoadQuestion(val *fastjson.Value, q *Question) error {
564 q.OneOf = JSONGetItem(val, "oneOf")
565 q.AnyOf = JSONGetItem(val, "anyOf")
566 q.Closed = JSONGetBoolean(val, "closed")
567 return OnIntransitiveActivity(q, func(i *IntransitiveActivity) error {
568 return JSONLoadIntransitiveActivity(val, i)
569 })
570 }
571
572 func JSONLoadActor(val *fastjson.Value, a *Actor) error {
573 a.PreferredUsername = JSONGetNaturalLanguageField(val, "preferredUsername")
574 a.Followers = JSONGetItem(val, "followers")
575 a.Following = JSONGetItem(val, "following")
576 a.Inbox = JSONGetItem(val, "inbox")
577 a.Outbox = JSONGetItem(val, "outbox")
578 a.Liked = JSONGetItem(val, "liked")
579 a.Endpoints = JSONGetActorEndpoints(val, "endpoints")
580 a.Streams = JSONGetItems(val, "streams")
581 a.PublicKey = JSONGetPublicKey(val, "publicKey")
582 return OnObject(a, func(o *Object) error {
583 return JSONLoadObject(val, o)
584 })
585 }
586
587 func JSONLoadCollection(val *fastjson.Value, c *Collection) error {
588 c.Current = JSONGetItem(val, "current")
589 c.First = JSONGetItem(val, "first")
590 c.Last = JSONGetItem(val, "last")
591 c.TotalItems = uint(JSONGetInt(val, "totalItems"))
592 c.Items = JSONGetItems(val, "items")
593 return OnObject(c, func(o *Object) error {
594 return JSONLoadObject(val, o)
595 })
596 }
597
598 func JSONLoadCollectionPage(val *fastjson.Value, c *CollectionPage) error {
599 c.Next = JSONGetItem(val, "next")
600 c.Prev = JSONGetItem(val, "prev")
601 c.PartOf = JSONGetItem(val, "partOf")
602 return OnCollection(c, func(c *Collection) error {
603 return JSONLoadCollection(val, c)
604 })
605 }
606
607 func JSONLoadOrderedCollection(val *fastjson.Value, c *OrderedCollection) error {
608 c.Current = JSONGetItem(val, "current")
609 c.First = JSONGetItem(val, "first")
610 c.Last = JSONGetItem(val, "last")
611 c.TotalItems = uint(JSONGetInt(val, "totalItems"))
612 c.OrderedItems = JSONGetItems(val, "orderedItems")
613 return OnObject(c, func(o *Object) error {
614 return JSONLoadObject(val, o)
615 })
616 }
617
618 func JSONLoadOrderedCollectionPage(val *fastjson.Value, c *OrderedCollectionPage) error {
619 c.Next = JSONGetItem(val, "next")
620 c.Prev = JSONGetItem(val, "prev")
621 c.PartOf = JSONGetItem(val, "partOf")
622 c.StartIndex = uint(JSONGetInt(val, "startIndex"))
623 return OnOrderedCollection(c, func(c *OrderedCollection) error {
624 return JSONLoadOrderedCollection(val, c)
625 })
626 }
627
628 func JSONLoadPlace(val *fastjson.Value, p *Place) error {
629 p.Accuracy = JSONGetFloat(val, "accuracy")
630 p.Altitude = JSONGetFloat(val, "altitude")
631 p.Latitude = JSONGetFloat(val, "latitude")
632 p.Longitude = JSONGetFloat(val, "longitude")
633 p.Radius = JSONGetInt(val, "radius")
634 p.Units = JSONGetString(val, "units")
635 return OnObject(p, func(o *Object) error {
636 return JSONLoadObject(val, o)
637 })
638 }
639
640 func JSONLoadProfile(val *fastjson.Value, p *Profile) error {
641 p.Describes = JSONGetItem(val, "describes")
642 return OnObject(p, func(o *Object) error {
643 return JSONLoadObject(val, o)
644 })
645 }
646
647 func JSONLoadRelationship(val *fastjson.Value, r *Relationship) error {
648 r.Subject = JSONGetItem(val, "subject")
649 r.Object = JSONGetItem(val, "object")
650 r.Relationship = JSONGetItem(val, "relationship")
651 return OnObject(r, func(o *Object) error {
652 return JSONLoadObject(val, o)
653 })
654 }
655
656 func JSONLoadTombstone(val *fastjson.Value, t *Tombstone) error {
657 t.FormerType = ActivityVocabularyType(JSONGetString(val, "formerType"))
658 t.Deleted = JSONGetTime(val, "deleted")
659 return OnObject(t, func(o *Object) error {
660 return JSONLoadObject(val, o)
661 })
662 }
663
664 func jsonLoadToLink(val *fastjson.Value, l *Link) error {
665 l.ID = JSONGetID(val)
666 l.Type = JSONGetTypes(val)
667 l.MediaType = JSONGetMimeType(val, "mediaType")
668 l.Preview = JSONGetItem(val, "preview")
669 if h := JSONGetInt(val, "height"); h != 0 {
670 l.Height = uint(h)
671 }
672 if w := JSONGetInt(val, "width"); w != 0 {
673 l.Width = uint(w)
674 }
675 l.Name = JSONGetNaturalLanguageField(val, "name")
676 if hrefLang := JSONGetLangRefField(val, "hrefLang"); hrefLang.Valid() {
677 l.HrefLang = hrefLang
678 }
679 if href := JSONGetURIItem(val, "href"); href != nil {
680 ll := href.GetLink()
681 if len(ll) > 0 {
682 l.Href = ll
683 }
684 }
685 if rel := JSONGetURIItem(val, "rel"); rel != nil {
686 rr := rel.GetLink()
687 if len(rr) > 0 {
688 l.Rel = rr
689 }
690 }
691 return nil
692 }
693
694 func JSONLoadLink(val *fastjson.Value) (Item, error) {
695 l := new(Link)
696 return l, jsonLoadToLink(val, l)
697 }
698
699 func JSONLoadPublicKey(val *fastjson.Value, p *PublicKey) error {
700 p.ID = JSONGetID(val)
701 p.Owner = JSONGetIRI(val, "owner")
702 if pub := val.GetStringBytes("publicKeyPem"); len(pub) > 0 {
703 p.PublicKeyPem = string(pub)
704 }
705 return nil
706 }