Skip to content

Commit

Permalink
Sets escapeHTML to false on quoteString encoder; fixes #22
Browse files Browse the repository at this point in the history
  • Loading branch information
tomnomnom committed Sep 16, 2016
1 parent fbf32d1 commit 713ea14
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
10 changes: 8 additions & 2 deletions token.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
)
Expand Down Expand Up @@ -136,11 +137,16 @@ func valueTokenFromInterface(v interface{}) token {
// quoteString takes a string and returns a quoted and
// escaped string valid for use in gron output
func quoteString(s string) string {
out, err := json.Marshal(s)
out := &bytes.Buffer{}
j := json.NewEncoder(out)
j.SetEscapeHTML(false)
err := j.Encode(s)
if err != nil {
// It shouldn't be possible to be given a string we can't marshal
// so just bomb out in spectacular style
panic(fmt.Sprintf("failed to marshal string: %s", s))
}
return string(out)
// *json.Encoder's Encode method appends a newline char that
// we don't want so slice it off the end
return out.String()[:out.Len()-1]
}
1 change: 1 addition & 0 deletions token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestValueTokenFromInterface(t *testing.T) {
{make([]interface{}, 0), token{"[]", typEmptyArray}},
{json.Number("1.2"), token{"1.2", typNumber}},
{"foo", token{`"foo"`, typString}},
{"<3", token{`"<3"`, typString}},
{true, token{"true", typTrue}},
{false, token{"false", typFalse}},
{nil, token{"null", typNull}},
Expand Down

0 comments on commit 713ea14

Please sign in to comment.