github.com/schollz/clusters@v0.0.0-20221201012527-c6c68863636f/json_importer.go (about) 1 package clusters 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 ) 7 8 type jsonImporter struct { 9 } 10 11 func JsonImporter() Importer { 12 return &jsonImporter{} 13 } 14 15 func (i *jsonImporter) Import(file string, start, end int) ([][]float64, error) { 16 if start < 0 || end < 0 || start > end { 17 return [][]float64{}, errInvalidRange 18 } 19 20 f, err := ioutil.ReadFile(file) 21 if err != nil { 22 return [][]float64{}, err 23 } 24 25 var ( 26 d = make([][]float64, 0) 27 s = end - start + 1 28 g = make([]float64, 0, s) 29 c int 30 ) 31 32 err = json.Unmarshal(f, &d) 33 if err != nil { 34 return [][]float64{}, err 35 } 36 37 for i, _ := range d { 38 c = 0 39 40 for j := start; j <= end; j++ { 41 g[c] = d[i][j] 42 c++ 43 } 44 45 d[i] = make([]float64, 0, s) 46 for j := 0; j < s; j++ { 47 d[i][j] = g[j] 48 } 49 } 50 51 return d, nil 52 }