File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -25,3 +25,13 @@ func createPath(path string) []string {
2525 }
2626 return keys
2727}
28+
29+ // EscapePath to escape a path
30+ // Example
31+ // - By default jsonmap.Set("message.raw", "hello world !")
32+ // => { "message": { "raw": "hello world !" }}
33+ // - With escape jsonmap.Set(jsonmap.EscapePath("message.raw", "hello world !"))
34+ // => { "message.raw": "hello world !" }}
35+ func EscapePath (path string ) string {
36+ return strings .Replace (path , "." , "\\ ." , - 1 )
37+ }
Original file line number Diff line number Diff line change 1+ package jsonmap_test
2+
3+ import (
4+ "testing"
5+
6+ "github.com/datasweet/jsonmap"
7+ "github.com/stretchr/testify/assert"
8+ )
9+
10+ func TestEscapePath (t * testing.T ) {
11+ json := jsonmap .New ()
12+ json .Set ("message.raw" , "hello world !" )
13+ assert .JSONEq (t , `{ "message": { "raw": "hello world !" }}` , json .Stringify ())
14+
15+ json = jsonmap .New ()
16+ json .Set (jsonmap .EscapePath ("message.raw" ), "hello world !" )
17+ assert .JSONEq (t , `{ "message.raw": "hello world !" }` , json .Stringify ())
18+ }
You can’t perform that action at this time.
0 commit comments