Skip to content

Commit 2db336e

Browse files
committed
Fix an issue regarding the JSON EscapeHTML flag
1 parent 4e6450b commit 2db336e

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- Nothing yet
11+
12+
## [v2.4.1] - 2023-10-18
13+
14+
### Fixed
15+
16+
- JSON output now acts as expected regarding the EscapeHTML flag.
17+
1018
## [v2.4.0] - 2023-10-18
1119

1220
### Added
@@ -626,7 +634,8 @@ See [documentation](https://daseldocs.tomwright.me) for all changes.
626634

627635
- Everything!
628636

629-
[unreleased]: https://github.com/TomWright/dasel/compare/v2.4.0...HEAD
637+
[unreleased]: https://github.com/TomWright/dasel/compare/v2.4.1...HEAD
638+
[v2.4.1]: https://github.com/TomWright/dasel/compare/v2.4.0...v2.4.1
630639
[v2.4.0]: https://github.com/TomWright/dasel/compare/v2.3.6...v2.4.0
631640
[v2.3.6]: https://github.com/TomWright/dasel/compare/v2.3.5...v2.3.6
632641
[v2.3.5]: https://github.com/TomWright/dasel/compare/v2.3.4...v2.3.5

dencoding/json_encoder.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"io"
77
)
88

9+
// lastOptions contains the options that the last JSONEncoder was created with.
10+
// Find a better way of passing this information into nested MarshalJSON calls.
11+
var lastOptions []JSONEncoderOption
12+
913
// JSONEncoder wraps a standard json encoder to implement custom ordering logic.
1014
type JSONEncoder struct {
1115
encoder *json.Encoder
@@ -20,6 +24,7 @@ func NewJSONEncoder(w io.Writer, options ...JSONEncoderOption) *JSONEncoder {
2024
for _, o := range options {
2125
o.ApplyEncoder(encoder)
2226
}
27+
lastOptions = options
2328
return encoder
2429
}
2530

@@ -64,21 +69,20 @@ func (option jsonEncodeIndent) ApplyEncoder(encoder *JSONEncoder) {
6469
// MarshalJSON JSON encodes the map and returns the bytes.
6570
// This maintains ordering.
6671
func (m *Map) MarshalJSON() ([]byte, error) {
72+
6773
buf := new(bytes.Buffer)
6874
buf.Write([]byte(`{`))
75+
encoder := NewJSONEncoder(buf, lastOptions...)
6976
for i, key := range m.keys {
7077
last := i == len(m.keys)-1
71-
keyBytes, err := json.Marshal(key)
72-
if err != nil {
78+
79+
if err := encoder.Encode(key); err != nil {
7380
return nil, err
7481
}
75-
buf.Write(keyBytes)
7682
buf.Write([]byte(`:`))
77-
valBytes, err := json.Marshal(m.data[key])
78-
if err != nil {
83+
if err := encoder.Encode(m.data[key]); err != nil {
7984
return nil, err
8085
}
81-
buf.Write(valBytes)
8286
if !last {
8387
buf.Write([]byte(`,`))
8488
}

internal/command/select_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,46 @@ d.e.f`)),
336336
nil,
337337
))
338338

339+
t.Run("Issue351 incorrectly escaped html, default false", runTest(
340+
[]string{"-r", "json"},
341+
[]byte(`{
342+
"field1": "A",
343+
"field2": "A > B && B > C"
344+
}`),
345+
newline([]byte(`{
346+
"field1": "A",
347+
"field2": "A > B && B > C"
348+
}`)),
349+
nil,
350+
nil,
351+
))
352+
353+
t.Run("Issue351 incorrectly escaped html, specific false", runTest(
354+
[]string{"-r", "json", "--escape-html=false"},
355+
[]byte(`{
356+
"field1": "A",
357+
"field2": "A > B && B > C"
358+
}`),
359+
newline([]byte(`{
360+
"field1": "A",
361+
"field2": "A > B && B > C"
362+
}`)),
363+
nil,
364+
nil,
365+
))
366+
367+
t.Run("Issue351 correctly escaped html", runTest(
368+
[]string{"-r", "json", "--escape-html=true"},
369+
[]byte(`{
370+
"field1": "A",
371+
"field2": "A > B && B > C"
372+
}`),
373+
newline([]byte(`{
374+
"field1": "A",
375+
"field2": "A \u003e B \u0026\u0026 B \u003e C"
376+
}`)),
377+
nil,
378+
nil,
379+
))
380+
339381
}

0 commit comments

Comments
 (0)