github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/common/test_utils.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 ) 8 9 func LoadJSON(file string, val interface{}) error { 10 content, err := ioutil.ReadFile(file) 11 if err != nil { 12 return err 13 } 14 if err := json.Unmarshal(content, val); err != nil { 15 if syntaxerr, ok := err.(*json.SyntaxError); ok { 16 line := findLine(content, syntaxerr.Offset) 17 return fmt.Errorf("JSON syntax error at %v:%v: %v", file, line, err) 18 } 19 return fmt.Errorf("JSON unmarshal error in %v: %v", file, err) 20 } 21 return nil 22 } 23 24 func findLine(data []byte, offset int64) (line int) { 25 line = 1 26 for i, r := range string(data) { 27 if int64(i) >= offset { 28 return 29 } 30 if r == '\n' { 31 line++ 32 } 33 } 34 return 35 }