-
Notifications
You must be signed in to change notification settings - Fork 2
/
Example.fan
54 lines (42 loc) · 1.5 KB
/
Example.fan
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
using afJson
class Example {
Void main() {
// write some JSON...
json := """{
"name" : "Emma",
"sex" : "female",
"likes" : ["Cakes","Adventure"],
"car" : {
"name" : "Golf",
"brand" : "VW"
},
"score" : 9
}"""
// ...and WHAM! A fully inflated domain object!
friend := (Friend) Json().fromJson(json, Friend#)
echo(friend.name) // --> Emma
echo(friend.car.name) // --> Golf
friend.score = 11
friend.car = null
// we can event convert the other way!
moarJson := Json().toJson(friend)
echo(moarJson)
// --> {"name":"Emma","sex":"female","score":11,"likes":["Cakes","Adventure"]}
}
}
class Friend {
@JsonProperty Str name
@JsonProperty Sex sex
@JsonProperty Int score
@JsonProperty Str[] likes
@JsonProperty Car? car // embedded objects!
new make(|This| f) { f(this) }
}
class Car {
@JsonProperty Str name
@JsonProperty Str brand
new make(|This| f) { f(this) }
}
enum class Sex {
male, female;
}