Skip to content

Commit 713ea14

Browse files
committed
Sets escapeHTML to false on quoteString encoder; fixes #22
1 parent fbf32d1 commit 713ea14

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

token.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
)
@@ -136,11 +137,16 @@ func valueTokenFromInterface(v interface{}) token {
136137
// quoteString takes a string and returns a quoted and
137138
// escaped string valid for use in gron output
138139
func quoteString(s string) string {
139-
out, err := json.Marshal(s)
140+
out := &bytes.Buffer{}
141+
j := json.NewEncoder(out)
142+
j.SetEscapeHTML(false)
143+
err := j.Encode(s)
140144
if err != nil {
141145
// It shouldn't be possible to be given a string we can't marshal
142146
// so just bomb out in spectacular style
143147
panic(fmt.Sprintf("failed to marshal string: %s", s))
144148
}
145-
return string(out)
149+
// *json.Encoder's Encode method appends a newline char that
150+
// we don't want so slice it off the end
151+
return out.String()[:out.Len()-1]
146152
}

token_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func TestValueTokenFromInterface(t *testing.T) {
1414
{make([]interface{}, 0), token{"[]", typEmptyArray}},
1515
{json.Number("1.2"), token{"1.2", typNumber}},
1616
{"foo", token{`"foo"`, typString}},
17+
{"<3", token{`"<3"`, typString}},
1718
{true, token{"true", typTrue}},
1819
{false, token{"false", typFalse}},
1920
{nil, token{"null", typNull}},

0 commit comments

Comments
 (0)