Skip to content

Commit

Permalink
Change kjson.Unmarshal to use UseNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
xorkevin committed Apr 17, 2023
1 parent 08fea0e commit fdb0008
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion service/conduit/valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func validTheme(theme string) error {
if len(theme) > lengthCapTheme {
return governor.ErrWithRes(nil, http.StatusBadRequest, "", "Theme must be shorter than 4096 characters")
}
if err := json.Valid([]byte(theme)); err != nil {
if !json.Valid([]byte(theme)) {
return governor.ErrWithRes(nil, http.StatusBadRequest, "", "Theme is invalid JSON")
}
return nil
Expand Down
13 changes: 11 additions & 2 deletions util/kjson/kjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kjson
import (
"bytes"
"encoding/json"
"errors"
)

// Marshal marshals json without escaping html
Expand All @@ -16,7 +17,15 @@ func Marshal(v interface{}) ([]byte, error) {
return b.Bytes(), nil
}

// Unmarshal is [encoding/json.Unmarshal]
// Unmarshal unmarshals json with the option UseNumber
func Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
if !json.Valid(data) {
return errors.New("Invalid json")
}
j := json.NewDecoder(bytes.NewReader(data))
j.UseNumber()
if err := j.Decode(v); err != nil {
return err
}
return nil
}

0 comments on commit fdb0008

Please sign in to comment.