| // Copyright 2021 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| package jsonutil | |
| import ( | |
| "encoding/json" | |
| "os" | |
| ) | |
| // WriteToFile writes data as JSON into a file. | |
| func WriteToFile(path string, v any) error { | |
| raw, err := json.MarshalIndent(v, "", " ") | |
| if err != nil { | |
| return err | |
| } | |
| return os.WriteFile(path, raw, 0o600) | |
| } | |
| // ReadFromFile reads data as JSON from a file. | |
| func ReadFromFile(path string, v any) error { | |
| raw, err := os.ReadFile(path) | |
| if err != nil { | |
| return err | |
| } | |
| return json.Unmarshal(raw, v) | |
| } |