1use jsonapis::Builder as JsonApisBuilder;
2use serde_json::{json, Value};
3
4fn main() {
5 let document = jsonapis::DocumentBuilder::default()
6 .jsonapi(jsonapis::Version::new(0))
7 .meta1("current_page", 1)
8 .meta1("items_per_page", 2)
9 .meta1("total_pages", 3)
10 .meta1("total_items", 6)
11 .link("self", "http://example.com/posts.json?page=1")
12 .link("first", "http://example.com/posts.json?page=1")
13 .link("last", "http://example.com/posts.json?page=3")
14 .link("next", "http://example.com/posts.json?page=2")
15 .data(vec![
16 jsonapis::ResourceBuilder::new_with_id("posts", "1")
17 .link("self", "http://example.com/posts/1.json")
18 .attr("title", "Some blog post")
19 .attr("summary", "Here is the beginning of some blog post.")
20 .rel(
21 "author",
22 jsonapis::ResourceBuilder::new_with_id("users", "1")
23 .link("self", "http://example.com/users/1.json")
24 .attr("username", "alice"),
25 ),
26 jsonapis::ResourceBuilder::new_with_id("posts", "2")
27 .link("self", "http://example.com/posts/2.json")
28 .attr("title", "Other blog post")
29 .attr("summary", "Here is the beginning of other blog post.")
30 .rel(
31 "author",
32 jsonapis::ResourceBuilder::new_with_id("users", "2")
33 .link("self", "http://example.com/users/2.json")
34 .attr("username", "bob"),
35 ),
36 ])
37 .unwrap();
38
39 let expected_value = json!({
40 "jsonapi": json!({
41 "version": json!("1.0"),
42 "meta": json!(null),
43 }),
44 "meta": json!({
45 "current_page": json!(1),
46 "items_per_page": json!(2),
47 "total_pages": json!(3),
48 "total_items": json!(6),
49 }),
50 "links": json!({
51 "self": json!("http://example.com/posts.json?page=1"),
52 "related": json!(null),
53 "first": json!("http://example.com/posts.json?page=1"),
54 "last": json!("http://example.com/posts.json?page=3"),
55 "prev": json!(null),
56 "next": json!("http://example.com/posts.json?page=2"),
57 "about": json!(null),
58 }),
59 "data": json!([
60 json!({
61 "type": json!("posts"),
62 "id": json!("1"),
63 "meta": json!(null),
64 "links": json!({
65 "self": json!("http://example.com/posts/1.json"),
66 "related": json!(null),
67 "first": json!(null),
68 "last": json!(null),
69 "prev": json!(null),
70 "next": json!(null),
71 "about": json!(null),
72 }),
73 "attributes": json!({
74 "title": json!("Some blog post"),
75 "summary": json!("Here is the beginning of some blog post."),
76 }),
77 "relationships": json!({
78 "author": json!({
79 "meta": json!(null),
80 "links": json!(null),
81 "data": json!({
82 "type": json!("users"),
83 "id": json!("1"),
84 "meta": json!(null),
85 "links": json!({
86 "self": json!("http://example.com/users/1.json"),
87 "related": json!(null),
88 "first": json!(null),
89 "last": json!(null),
90 "prev": json!(null),
91 "next": json!(null),
92 "about": json!(null),
93 }),
94 "attributes": json!({
95 "username": json!("alice"),
96 }),
97 "relationships": json!(null),
98 }),
99 }),
100 }),
101 }),
102 json!({
103 "type": json!("posts"),
104 "id": json!("2"),
105 "meta": json!(null),
106 "links": json!({
107 "self": json!("http://example.com/posts/2.json"),
108 "related": json!(null),
109 "first": json!(null),
110 "last": json!(null),
111 "prev": json!(null),
112 "next": json!(null),
113 "about": json!(null),
114 }),
115 "attributes": json!({
116 "title": json!("Other blog post"),
117 "summary": json!("Here is the beginning of other blog post."),
118 }),
119 "relationships": json!({
120 "author": json!({
121 "meta": json!(null),
122 "links": json!(null),
123 "data": json!({
124 "type": json!("users"),
125 "id": json!("2"),
126 "meta": json!(null),
127 "links": json!({
128 "self": json!("http://example.com/users/2.json"),
129 "related": json!(null),
130 "first": json!(null),
131 "last": json!(null),
132 "prev": json!(null),
133 "next": json!(null),
134 "about": json!(null),
135 }),
136 "attributes": json!({
137 "username": json!("bob"),
138 }),
139 "relationships": json!(null),
140 }),
141 }),
142 }),
143 }),
144 ]),
145 "errors": json!(null),
146 });
147
148 let actual_json = serde_json::to_string(&document).unwrap();
149
150 let actual_value: Value = serde_json::from_str(&actual_json).unwrap();
151
152 println!("{:#?}", actual_value);
153
154 assert_eq!(actual_value, expected_value);
155}