-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonapi.go
74 lines (65 loc) · 3.65 KB
/
jsonapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"github.com/berlincount/jsonstruct"
"bytes"
"fmt"
"reflect"
"strings"
"time"
"github.com/google/jsonapi"
)
func main() {
// JSONAPI uses an extended Datatype that we like
jsonstruct.MapType("time.Time", reflect.TypeOf(time.Now()))
// Remarshal JSONAPI data as created by testBlogForCreate in
// https://github.com/google/jsonapi/blob/master/examples/app.go
blogStructJSON := `
{"struct": "comment",
"fields": [
{"name": "ID", "type": "int", "tags": "jsonapi:\"primary,comments\""},
{"name": "PostID", "type": "int", "tags": "jsonapi:\"attr,post_id\""},
{"name": "Body", "type": "string", "tags": "jsonapi:\"attr,body\""}
]}
{"struct": "post",
"fields": [
{"name": "ID", "type": "int", "tags": "jsonapi:\"primary,posts\""},
{"name": "BlogID", "type": "int", "tags": "jsonapi:\"attr,blog_id\""},
{"name": "Title", "type": "string", "tags": "jsonapi:\"attr,title\""},
{"name": "Body", "type": "string", "tags": "jsonapi:\"attr,body\""},
{"name": "Comments", "type": "[]*comment", "tags": "jsonapi:\"relation,comments\""}
]}
{"struct": "blog",
"fields": [
{"name": "ID", "type": "int", "tags": "jsonapi:\"primary,blogs\""},
{"name": "Title", "type": "string", "tags": "jsonapi:\"attr,title\""},
{"name": "Posts", "type": "[]*post", "tags": "jsonapi:\"relation,posts\""},
{"name": "CurrentPost", "type": "*post", "tags": "jsonapi:\"relation,current_post\""},
{"name": "CurrentPostID", "type": "int", "tags": "jsonapi:\"attr,current_post_id\""},
{"name": "CreatedAt", "type": "time.Time", "tags": "jsonapi:\"attr,created_at\""},
{"name": "ViewCount", "type": "int", "tags": "jsonapi:\"attr,view_count\""}
]}
`
decodedStructs, err := jsonstruct.Decode(strings.NewReader(blogStructJSON))
if err != nil {
panic("something went terribly wrong decoding the structures")
}
if len(decodedStructs) != 3 {
panic("something went rather unexpected decoding the structures")
}
blogStructType := decodedStructs["blog"]
blogStructValue := reflect.New(blogStructType)
blogStructInterface := blogStructValue.Interface()
err = jsonapi.UnmarshalPayload(strings.NewReader(`
{"data":{"type":"blogs","id":"1","attributes":{"created_at":1480279183,"current_post_id":0,"title":"Title 1","view_count":0},"relationships":{"current_post":{"data":{"type":"posts","id":"1","attributes":{"blog_id":0,"body":"Bar","title":"Foo"},"relationships":{"comments":{"data":[{"type":"comments","id":"1","attributes":{"body":"foo","post_id":0}},{"type":"comments","id":"2","attributes":{"body":"bar","post_id":0}}]}}}},"posts":{"data":[{"type":"posts","id":"1","attributes":{"blog_id":0,"body":"Bar","title":"Foo"},"relationships":{"comments":{"data":[{"type":"comments","id":"1","attributes":{"body":"foo","post_id":0}},{"type":"comments","id":"2","attributes":{"body":"bar","post_id":0}}]}}},{"type":"posts","id":"2","attributes":{"blog_id":0,"body":"Bas","title":"Fuubar"},"relationships":{"comments":{"data":[{"type":"comments","id":"1","attributes":{"body":"foo","post_id":0}},{"type":"comments","id":"3","attributes":{"body":"bas","post_id":0}}]}}}]}}}}
`), blogStructInterface)
if err != nil {
panic("something went terribly wrong unmarshalling the payload")
}
// data can be found in orderly structures with same signature as in original example now
buf := bytes.NewBuffer(nil)
err = jsonapi.MarshalOnePayloadEmbedded(buf, blogStructInterface)
if err != nil {
panic("something went terribly wrong remarshalling the payload")
}
fmt.Println(buf.String())
}