Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- fixes a bug where new lines in string value would produce invalid JSON #26

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [0.5.3] - 2022-06-09

### Changed

- Fixed a bug where new lines in string values would not be escaped generating invalid JSON.

## [0.5.2] - 2022-06-07

### Changed
Expand Down
2 changes: 1 addition & 1 deletion json_serialization_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (w *JsonSerializationWriter) writeRawValue(value string) {
w.writer = append(w.writer, value)
}
func (w *JsonSerializationWriter) writeStringValue(value string) {
value = strings.ReplaceAll(value, "\"", "\\\"")
value = strings.ReplaceAll(strings.ReplaceAll(value, "\"", "\\\""), "\n", "\\n")
w.writeRawValue("\"" + value + "\"")
}
func (w *JsonSerializationWriter) writePropertyName(key string) {
Expand Down
9 changes: 9 additions & 0 deletions json_serialization_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,12 @@ func TestWriteInvalidAdditionalData(t *testing.T) {
expErr := fmt.Sprintf("unsupported AdditionalData type: %T", value)
assert.EqualErrorf(t, err, expErr, "Error should be: %v, got: %v", expErr, err)
}

func TestEscapesNewLinesInStrings(t *testing.T) {
serializer := NewJsonSerializationWriter()
value := "value\nwith\nnew\nlines"
serializer.WriteStringValue("key", &value)
result, err := serializer.GetSerializedContent()
assert.Nil(t, err)
assert.Equal(t, "\"key\":\"value\\nwith\\nnew\\nlines\"", string(result[:]))
}