From ead69a578aef7fb06bbae360ccac6e16872b2701 Mon Sep 17 00:00:00 2001 From: Onur Filiz Date: Tue, 26 Jul 2016 15:27:08 -0700 Subject: [PATCH] Updated JSON key value store to indent output --- store/json.go | 7 ++++++- store/json_test.go | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/store/json.go b/store/json.go index 4472fbce9..98f21162f 100644 --- a/store/json.go +++ b/store/json.go @@ -94,7 +94,12 @@ func (kvs *jsonFileStore) flush() error { return err } - err = json.NewEncoder(file).Encode(&kvs.data) + buf, err := json.MarshalIndent(&kvs.data, "", "\t") + if err != nil { + return err + } + + _, err = file.Write(buf) if err != nil { return err } diff --git a/store/json_test.go b/store/json_test.go index 729be1fd1..757e96c4d 100644 --- a/store/json_test.go +++ b/store/json_test.go @@ -5,6 +5,7 @@ package store import ( "os" + "strings" "testing" ) @@ -91,10 +92,15 @@ func TestKeyValuePairsArePersistedToJSONFile(t *testing.T) { if err != nil { t.Fatalf("Failed to read from file %v", err) } - actualPair = string(data[:n-1]) file.Close() - defer os.Remove(testFileName) + os.Remove(testFileName) + + // Remove indentation to normalize the JSON encoding. + actualPair = string(data[:n]) + actualPair = strings.Replace(actualPair, " ", "", -1) + actualPair = strings.Replace(actualPair, "\t", "", -1) + actualPair = strings.Replace(actualPair, "\n", "", -1) // Fail if the contents do not match expected JSON encoding. if actualPair != expectedPair {