Skip to content

Commit a07d3b8

Browse files
authored
Merge pull request iancoleman#15 from khos2ow/not-escape
Add SetEscapeHTML func to control escape flag
2 parents ac98e3e + 7894308 commit a07d3b8

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

orderedmap.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package orderedmap
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"errors"
67
"sort"
@@ -42,17 +43,23 @@ func (a ByPair) Swap(i, j int) { a.Pairs[i], a.Pairs[j] = a.Pairs[j], a.Pai
4243
func (a ByPair) Less(i, j int) bool { return a.LessFunc(a.Pairs[i], a.Pairs[j]) }
4344

4445
type OrderedMap struct {
45-
keys []string
46-
values map[string]interface{}
46+
keys []string
47+
values map[string]interface{}
48+
escapeHTML bool
4749
}
4850

4951
func New() *OrderedMap {
5052
o := OrderedMap{}
5153
o.keys = []string{}
5254
o.values = map[string]interface{}{}
55+
o.escapeHTML = true
5356
return &o
5457
}
5558

59+
func (o *OrderedMap) SetEscapeHTML(on bool) {
60+
o.escapeHTML = on
61+
}
62+
5663
func (o *OrderedMap) Get(key string) (interface{}, bool) {
5764
val, exists := o.values[key]
5865
return val, exists
@@ -315,11 +322,14 @@ func (o OrderedMap) MarshalJSON() ([]byte, error) {
315322
s = s + `"` + kEscaped + `":`
316323
// add value
317324
v := o.values[k]
318-
vBytes, err := json.Marshal(v)
325+
buffer := new(bytes.Buffer)
326+
encoder := json.NewEncoder(buffer)
327+
encoder.SetEscapeHTML(o.escapeHTML)
328+
err := encoder.Encode(v)
319329
if err != nil {
320330
return []byte{}, err
321331
}
322-
s = s + string(vBytes) + ","
332+
s = s + buffer.String() + ","
323333
}
324334
if len(o.keys) > 0 {
325335
s = s[0 : len(s)-1]

orderedmap_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"sort"
7+
"strings"
78
"testing"
89
)
910

@@ -113,6 +114,8 @@ func TestMarshalJSON(t *testing.T) {
113114
o.Set("number", 3)
114115
// string
115116
o.Set("string", "x")
117+
// string
118+
o.Set("specialstring", "\\.<>[]{}_-")
116119
// new value keeps key in old position
117120
o.Set("number", 4)
118121
// keys not sorted alphabetically
@@ -138,7 +141,7 @@ func TestMarshalJSON(t *testing.T) {
138141
}
139142
s := string(b)
140143
// check json is correctly ordered
141-
if s != `{"number":4,"string":"x","z":1,"a":2,"b":3,"slice":["1",1],"orderedmap":{"e":1,"a":2},"test\"ing":9}` {
144+
if s != `{"number":4,"string":"x","specialstring":"\\.\u003c\u003e[]{}_-","z":1,"a":2,"b":3,"slice":["1",1],"orderedmap":{"e":1,"a":2},"test\"ing":9}` {
142145
t.Error("JSON Marshal value is incorrect", s)
143146
}
144147
// convert to indented json
@@ -150,6 +153,7 @@ func TestMarshalJSON(t *testing.T) {
150153
ei := `{
151154
"number": 4,
152155
"string": "x",
156+
"specialstring": "\\.\u003c\u003e[]{}_-",
153157
"z": 1,
154158
"a": 2,
155159
"b": 3,
@@ -170,6 +174,23 @@ func TestMarshalJSON(t *testing.T) {
170174
}
171175
}
172176

177+
func TestMarshalJSONNoEscapeHTML(t *testing.T) {
178+
o := New()
179+
o.SetEscapeHTML(false)
180+
// string special characters
181+
o.Set("specialstring", "\\.<>[]{}_-")
182+
// convert to json
183+
b, err := o.MarshalJSON()
184+
if err != nil {
185+
t.Error("Marshalling json", err)
186+
}
187+
s := strings.Replace(string(b), "\n", "", -1)
188+
// check json is correctly ordered
189+
if s != `{"specialstring":"\\.<>[]{}_-"}` {
190+
t.Error("JSON Marshal value is incorrect", s)
191+
}
192+
}
193+
173194
func TestUnmarshalJSON(t *testing.T) {
174195
s := `{
175196
"number": 4,

readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ func main() {
2121
// use New() instead of o := map[string]interface{}{}
2222
o := orderedmap.New()
2323

24+
// use SetEscapeHTML() to whether escape problematic HTML characters or not, defaults is true
25+
o.SetEscapeHTML(false)
26+
2427
// use Set instead of o["a"] = 1
2528
o.Set("a", 1)
2629

30+
// add some value with special characters
31+
o.Set("b", "\\.<>[]{}_-")
32+
2733
// use Get instead of i, ok := o["a"]
2834
val, ok := o.Get("a")
2935

@@ -44,10 +50,10 @@ func main() {
4450
// all maps (including nested maps) will be parsed as orderedmaps
4551
s := `{"a": 1}`
4652
err := json.Unmarshal([]byte(s), &o)
47-
53+
4854
// sort the keys
4955
o.SortKeys(sort.Strings)
50-
56+
5157
// sort by Pair
5258
o.Sort(func(a *Pair, b *Pair) bool {
5359
return a.Value().(float64) < b.Value().(float64)

0 commit comments

Comments
 (0)