diff --git a/token.go b/token.go index 0d564b9..646a6e1 100644 --- a/token.go +++ b/token.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "encoding/json" "fmt" ) @@ -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] } diff --git a/token_test.go b/token_test.go index 38a7f67..a65b477 100644 --- a/token_test.go +++ b/token_test.go @@ -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}},