-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonWriter.go
40 lines (35 loc) · 856 Bytes
/
JsonWriter.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
package radiowatch
import (
"bytes"
"encoding/json"
"fmt"
"os"
)
/*
JsonWriter implements the Writer interface.
It takes the TrackInfo, converts it to a JSON string and writes it to a file.
*/
type JsonWriter struct {
fileWriter
}
/*
Returns a new instance of JsonWriter.
Takes a path name at which the results are saved.
*/
func NewJsonWriter(path string) JsonWriter {
w := *new(JsonWriter)
w.setPath(path)
return w
}
/*
Concrete implementation of Writer.Write()
*/
func (j JsonWriter) Write(trackInfo TrackInfo) {
buffer, err := json.Marshal(trackInfo)
if err != nil {
fmt.Fprintf(os.Stderr, "Error while converting result from station %s to json: %s\n", trackInfo.Station, err.Error())
}
b := bytes.NewBuffer(buffer)
b.WriteString("\n")
j.writeFile(j.Path + trackInfo.NormalizedStationName() + ".rwjson", b, trackInfo.Station)
}