-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample6_test.go
76 lines (65 loc) · 2.33 KB
/
example6_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package jsonface_test
// This example shows how to use the jsonface.Unmarshal() function directly for
// advanced situations. For normal cases, you'd use jsonface.GlobalUnmarshal() instead.
import (
"jsonface"
"fmt"
"math"
"encoding/json"
)
type (
Transporter interface {
Transport(distance_km float64) (time_hours float64)
}
Bike struct{ NumGears int }
Bus struct{ LineName string }
Tesla struct{ Charge float64 } // Charge is a number between 0 and 1.
)
func (me Bike) Transport(distance float64) (time float64) {
// A Bike can go at least 8 km/h, and even faster with more gears:
return distance / ( 8 + math.Sqrt(float64(me.NumGears)) )
}
func (me Bus) Transport(distance float64) (time float64) {
// Some bus lines are slower than others:
var speed float64
switch me.LineName {
case "7": speed=10
case "185": speed=12
default: panic(fmt.Errorf("Unknown Bus Line: %s",me.LineName))
}
return distance/speed
}
func (me Tesla) Transport(distance float64) (time float64) {
// A Tesla goes slower as it loses charge.
// For simplicity of this example, the car does not lose charge during transportation.
speed := 100*me.Charge
return distance/speed
}
func Transporter_UnmarshalJSON(bs []byte) (interface{},error) {
var data struct { Type string }
err := json.Unmarshal(bs, &data); if err!=nil { return nil,err }
switch data.Type {
case "Bike":
var bike Bike
err := json.Unmarshal(bs, &bike); if err!=nil { return nil,err }
return bike,nil
case "Bus":
var bus Bus
err := json.Unmarshal(bs, &bus); if err!=nil { return nil,err }
return bus,nil
case "Tesla":
var tesla Tesla
err := json.Unmarshal(bs, &tesla); if err!=nil { return nil,err }
return tesla,nil
default: return nil,fmt.Errorf("Unknown Transporter Type: %s",bs)
}
}
func ExampleUnmarshal() {
var ts []Transporter
bs := []byte(`[{ "Type":"Bike", "NumGears":9 }, { "Type":"Bus", "LineName":"7" }]`)
cbmap := jsonface.CBMap{ "jsonface_test.Transporter":Transporter_UnmarshalJSON }
err := jsonface.Unmarshal(bs, &ts, cbmap); if err!=nil { panic(err) }
fmt.Printf("%#v\n",ts)
// Output:
// []jsonface_test.Transporter{jsonface_test.Bike{NumGears:9}, jsonface_test.Bus{LineName:"7"}}
}