-
Notifications
You must be signed in to change notification settings - Fork 1
/
priceLevel.go
55 lines (41 loc) · 1022 Bytes
/
priceLevel.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
package common
import (
"errors"
"strconv"
jsoniter "github.com/json-iterator/go"
)
// PriceLevel is a common structure for bids and asks in the
// order book.
// Redefining the standard package
var json = jsoniter.ConfigCompatibleWithStandardLibrary
var ErrPriceLevel = errors.New("failed to parse price level")
type PriceLevel struct {
Price float64
Quantity float64
}
func (p *PriceLevel) UnmarshalJSON(data []byte) error {
var items []string
if err := json.Unmarshal(data, &items); err != nil {
return err
}
if len(items) != 2 {
return ErrPriceLevel
}
price, err := strconv.ParseFloat(items[0], 64)
if err != nil {
return err
}
Quantity, err := strconv.ParseFloat(items[1], 64)
if err != nil {
return err
}
p.Price = price
p.Quantity = Quantity
return nil
}
func (p *PriceLevel) MarshalJSON() ([]byte, error) {
items := [2]string{}
items[0] = strconv.FormatFloat(p.Price, 'f', -1, 64)
items[1] = strconv.FormatFloat(p.Quantity, 'f', -1, 64)
return json.Marshal(items)
}