github.com/googollee/clic@v0.1.5/source/file_codec.go (about) 1 package source 2 3 import ( 4 "encoding/json" 5 "os" 6 ) 7 8 type JSON struct{} 9 10 func (JSON) TagName() string { 11 return "json" 12 } 13 14 func (JSON) ExtName() string { 15 return "json" 16 } 17 18 func (JSON) Encode(path string, v any) error { 19 f, err := os.Create(path) 20 if err != nil { 21 return err 22 } 23 defer f.Close() 24 25 return json.NewEncoder(f).Encode(v) 26 } 27 28 func (JSON) Decode(path string, v any) error { 29 f, err := os.Open(path) 30 if err != nil { 31 return err 32 } 33 defer f.Close() 34 35 return json.NewDecoder(f).Decode(v) 36 }