-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
63 lines (52 loc) · 1.23 KB
/
json.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
package format
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/u3mur4/crypto-price/exchange"
)
type jsonCandle struct {
High float64 `json:"high"`
Open float64 `json:"open"`
Close float64 `json:"close"`
Low float64 `json:"low"`
Percent float64 `json:"percent"`
Color string `json:"color"`
}
type jsonChart struct {
Exchange string `json:"exchange"`
Base string `json:"base"`
Quote string `json:"quote"`
Candle jsonCandle `json:"candle"`
}
type jsonFormat struct {
Output io.Writer
}
// NewJSON displays the market as json format
func NewJSON() Formatter {
return &jsonFormat{
Output: os.Stdout,
}
}
func (j *jsonFormat) Open() {}
func convertCandles(candle exchange.Candle) (newCandles jsonCandle) {
return jsonCandle{
High: candle.High,
Open: candle.Open,
Close: candle.Close,
Low: candle.Low,
Percent: candle.Percent(),
Color: color(candle).Hex(),
}
}
func (j *jsonFormat) Show(market exchange.Market) {
b, _ := json.Marshal(&jsonChart{
Exchange: market.Exchange,
Base: market.Base,
Quote: market.Quote,
Candle: convertCandles(market.Candle),
})
fmt.Fprintf(j.Output, "%s\n", string(b))
}
func (j *jsonFormat) Close() {}