Skip to content

Commit

Permalink
fix json-marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcarenza committed Jul 28, 2022
1 parent 6cd6590 commit c8e9e7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 2 additions & 3 deletions jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ We wrap "github.com/PaesslerAG/jsonpath" so that we can implement some json inte
import (
"context"
"encoding/json"
"strings"

"github.com/PaesslerAG/gval"
"github.com/PaesslerAG/jsonpath"
Expand Down Expand Up @@ -46,8 +45,8 @@ func (jp *JsonPath) UnmarshalJSON(path []byte) error {
return nil
}

func (jp *JsonPath) MarshalJSON() ([]byte, error) {
return []byte(`"` + strings.TrimRight(jp.String(), `"`) + `"`), nil
func (jp JsonPath) MarshalJSON() ([]byte, error) {
return json.Marshal(jp.String())
}

func (jp *JsonPath) String() string {
Expand Down
13 changes: 13 additions & 0 deletions jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ func TestJsonUnmarshal(t *testing.T) {
return
}
}

func TestJsonMarshal(t *testing.T) {
jp := MustParsePath(`$["key with spaces"]`)
b, err := json.Marshal(jp)
if err != nil {
t.Error(err)
return
}
if string(b) != `"$[\"key with spaces\"]"` {
t.Error("value is incorrect")
return
}
}

0 comments on commit c8e9e7e

Please sign in to comment.