-
Notifications
You must be signed in to change notification settings - Fork 136
/
marshal_test.go
52 lines (41 loc) · 1.25 KB
/
marshal_test.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
package main
import (
"encoding/json"
"testing"
)
func TestThatJSONCanBeRoundtrippedUsingGeneratedStructs(t *testing.T) {
j := `{"address":{"county":"countyValue"},"name":"nameValue"}`
e := &Example{}
err := json.Unmarshal([]byte(j), e)
if err != nil {
t.Fatal("Failed to unmarshall JSON with error ", err)
}
if e.Address.County != "countyValue" {
t.Errorf("the county value was not found, expected 'countyValue' got '%s'", e.Address.County)
}
op, err := json.Marshal(e)
if err != nil {
t.Error("Failed to marshal JSON with error ", err)
}
if string(op) != j {
t.Errorf("expected %s, got %s", j, string(op))
}
}
type Address struct {
County string `json:"county,omitempty"`
District string `json:"district,omitempty"`
FlatNumber string `json:"flatNumber,omitempty"`
HouseName string `json:"houseName,omitempty"`
HouseNumber string `json:"houseNumber,omitempty"`
Postcode string `json:"postcode,omitempty"`
Street string `json:"street,omitempty"`
Town string `json:"town,omitempty"`
}
type Example struct {
Address *Address `json:"address,omitempty"`
Name string `json:"name,omitempty"`
Status *Status `json:"status,omitempty"`
}
type Status struct {
Favouritecat string `json:"favouritecat,omitempty"`
}