Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion stores/json/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,24 @@ func (store Store) treeBranchFromJSONDecoder(dec *json.Decoder) (sops.TreeBranch
}
}

// Encoder to disable escaping html symbols
// See: https://github.com/getsops/sops/issues/881
func jsonMarshal(v interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encode appends a newline (https://pkg.go.dev/encoding/json#Encoder.Encode), which json.Marshal apparently does not do. (This makes the unit tests fail.) This is apparently because Encoder is for JSON streams, while Marshal produces a single JSON file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply adding code which removes a trailing newline (if exists) probably suffices to fix this. I don't see another way to do this with Go's API.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we stick with newline in new files instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is called from encodeValue and used during serialization of any JSON value, so if you use this to create JSON of a map, you get someting like

{
  "foo"
: "bar"
,
  "baz"
: 42
,
  "bam"
: null
}

return buffer.Bytes(), err
}

func (store Store) encodeValue(v interface{}) ([]byte, error) {
switch v := v.(type) {
case sops.TreeBranch:
return store.encodeTree(v)
case []interface{}:
return store.encodeArray(v)
default:
return json.Marshal(v)
return jsonMarshal(v)
}
}

Expand Down