Home | History | Annotate | Download | only in filesystem
      1 package filesystem
      2 
      3 import (
      4 	"encoding/json"
      5 	"io/ioutil"
      6 )
      7 
      8 const fourSpaces = "    "
      9 const defaultReadPermissions = 0644
     10 
     11 func WriteJsonSerializableToFile(jsonEntity interface{}, filename string) error {
     12 	serialized, err := json.MarshalIndent(jsonEntity, "", fourSpaces)
     13 
     14 	if err != nil {
     15 		return err
     16 	}
     17 
     18 	return ioutil.WriteFile(
     19 		filename,
     20 		serialized,
     21 		defaultReadPermissions,
     22 	)
     23 }
     24 
     25 func ReadFileAsJson(filename string, outputEntityAddress interface{}) error {
     26 	fileContents, err := ioutil.ReadFile(filename)
     27 
     28 	if err != nil {
     29 		return err
     30 	}
     31 
     32 	return json.Unmarshal(fileContents, outputEntityAddress)
     33 }
     34