diff --git a/helper/jsonutil/json.go b/helper/jsonutil/json.go index 4c8d953797..c237db0f64 100644 --- a/helper/jsonutil/json.go +++ b/helper/jsonutil/json.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "reflect" ) // Encodes/Marshals the given object into JSON @@ -39,12 +38,6 @@ func DecodeJSONFromReader(r io.Reader, out interface{}) error { return fmt.Errorf("output parameter 'out' is nil") } - // Decoding requires a pointer type to be supplied - value := reflect.ValueOf(out) - if value.Kind() != reflect.Ptr { - return fmt.Errorf("decoding the value into a non-pointer type: %v", reflect.TypeOf(out)) - } - dec := json.NewDecoder(r) // While decoding JSON values, intepret the integer values as `json.Number`s instead of `float64`. diff --git a/helper/jsonutil/json_test.go b/helper/jsonutil/json_test.go new file mode 100644 index 0000000000..d7e85d2b5f --- /dev/null +++ b/helper/jsonutil/json_test.go @@ -0,0 +1,66 @@ +package jsonutil + +import ( + "bytes" + "fmt" + "reflect" + "strings" + "testing" +) + +func Test_EncodeJSON(t *testing.T) { + input := map[string]interface{}{ + "test": "data", + "validation": "process", + } + + actualBytes, err := EncodeJSON(input) + if err != nil { + t.Fatalf("failed to encode JSON: %v", err) + } + + actual := strings.TrimSpace(string(actualBytes)) + expected := `{"test":"data","validation":"process"}` + + if actual != expected { + t.Fatal("bad: encoded JSON: expected:%s\nactual:%s\n", expected, string(actualBytes)) + } +} + +func Test_DecodeJSON(t *testing.T) { + input := `{"test":"data","validation":"process"}` + + var actual map[string]interface{} + + err := DecodeJSON([]byte(input), &actual) + if err != nil { + fmt.Printf("decoding err: %v\n", err) + } + + expected := map[string]interface{}{ + "test": "data", + "validation": "process", + } + if !reflect.DeepEqual(actual, expected) { + t.Fatal("bad: expected:%#v\nactual:%#v", expected, actual) + } +} + +func Test_DecodeJSONFromReader(t *testing.T) { + input := `{"test":"data","validation":"process"}` + + var actual map[string]interface{} + + err := DecodeJSONFromReader(bytes.NewReader([]byte(input)), &actual) + if err != nil { + fmt.Printf("decoding err: %v\n", err) + } + + expected := map[string]interface{}{ + "test": "data", + "validation": "process", + } + if !reflect.DeepEqual(actual, expected) { + t.Fatal("bad: expected:%#v\nactual:%#v", expected, actual) + } +}