Skip to content

Commit

Permalink
Allow any JSON string to be parsed (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickschaper authored Jan 7, 2020
1 parent 76304da commit 53c7671
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion templatefunctions/js_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (j JSON) Stringify(x interface{}) string {

// Parse Stringify parses a string and returns an object
func (j JSON) Parse(x string) pugjs.Object {
m := make(map[string]interface{})
var m interface{}
err := json.Unmarshal([]byte(x), &m)
if err != nil {
panic(err)
Expand Down
8 changes: 5 additions & 3 deletions templatefunctions/js_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ func TestJsJSON(t *testing.T) {

func TestJSON_Parse(t *testing.T) {
var jsJSON flamingo.TemplateFunc = new(JsJSON)

json := jsJSON.Func(context.Background()).(func() JSON)()
result := json.Parse(`{"foo":123}`)

assert.Implements(t, (*pugjs.Object)(nil), result)
obj := json.Parse(`{"foo":123}`)
assert.Implements(t, (*pugjs.Object)(nil), obj)

arr := json.Parse(`["testing123", 123, {"an": "object"}]`)
assert.Implements(t, (*pugjs.Object)(nil), arr)
}

0 comments on commit 53c7671

Please sign in to comment.