-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathjson_importer.go
More file actions
52 lines (41 loc) · 769 Bytes
/
json_importer.go
File metadata and controls
52 lines (41 loc) · 769 Bytes
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 clusters
import (
"encoding/json"
"io/ioutil"
)
type jsonImporter struct {
}
func JsonImporter() Importer {
return &jsonImporter{}
}
func (i *jsonImporter) Import(file string, start, end int) ([][]float64, error) {
if start < 0 || end < 0 || start > end {
return [][]float64{}, errInvalidRange
}
f, err := ioutil.ReadFile(file)
if err != nil {
return [][]float64{}, err
}
var (
d = make([][]float64, 0)
s = end - start + 1
g = make([]float64, 0, s)
c int
)
err = json.Unmarshal(f, &d)
if err != nil {
return [][]float64{}, err
}
for i, _ := range d {
c = 0
for j := start; j <= end; j++ {
g[c] = d[i][j]
c++
}
d[i] = make([]float64, 0, s)
for j := 0; j < s; j++ {
d[i][j] = g[j]
}
}
return d, nil
}