MCPcopy Create free account
hub / github.com/canopy-network/canopy / SaveJSONToFile

Function SaveJSONToFile

lib/util.go:493–508  ·  view source on GitHub ↗

SaveJSONToFile() saves a json object to a file

(j any, dataDirPath, filePath string)

Source from the content-addressed store, hash-verified

491
492// SaveJSONToFile() saves a json object to a file
493func SaveJSONToFile(j any, dataDirPath, filePath string) (err ErrorI) {
494 // convert the object into json bytes
495 jsonBytes, err := MarshalJSONIndent(j)
496 // if an error occurred during the conversion
497 if err != nil {
498 // exit with error
499 return
500 }
501 // attempt to write the json bytes to a json file at the path
502 if e := os.WriteFile(filepath.Join(dataDirPath, filePath), jsonBytes, os.ModePerm); e != nil {
503 // exit with error
504 return ErrWriteFile(e)
505 }
506 // exit
507 return
508}
509
510// NewAny() converts a proto.Message into an anypb.Any type
511func NewAny(message proto.Message) (*anypb.Any, ErrorI) {

Callers 2

SaveToFileMethod · 0.92
SaveToFileMethod · 0.92

Calls 2

MarshalJSONIndentFunction · 0.85
ErrWriteFileFunction · 0.85

Tested by

no test coverage detected