github.com/richardwilkes/toolbox@v1.121.0/xio/fs/json_test.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package fs_test 11 12 import ( 13 "os" 14 "testing" 15 16 "github.com/richardwilkes/toolbox/check" 17 "github.com/richardwilkes/toolbox/xio/fs" 18 ) 19 20 func TestLoadSaveJSON(t *testing.T) { 21 type data struct { 22 Name string 23 Count int 24 } 25 value := &data{ 26 Name: "Rich", 27 Count: 22, 28 } 29 f, err := os.CreateTemp("", "json_test") 30 check.NoError(t, err) 31 check.NoError(t, f.Close()) 32 check.NoError(t, fs.SaveJSONWithMode(f.Name(), value, false, 0o600)) 33 var value2 data 34 check.NoError(t, fs.LoadJSON(f.Name(), &value2)) 35 check.NoError(t, os.Remove(f.Name())) 36 check.Equal(t, value, &value2) 37 }