Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 378
Good morning! Here's your coding interview problem for today.

This problem was asked by Coinbase.

Write a function that takes in a number, string, list, or dictionary and returns its JSON encoding. It should also handle nulls.

For example, given the following input:
```
[None, 123, ["a", "b"], {"c":"d"}]
```
You should return the following, as a string:
```
'[null, 123, ["a", "b"], {"c": "d"}]'
```

Pull request description:
Day378
Resolves #762.

Patch:
diff --git a/README.md b/README.md
index 376991b..d7d471a 100644
--- a/README.md
+++ b/README.md
@@ -373,4 +373,5 @@ problems from
 * [Day 375](https://github.com/vaskoz/dailycodingproblem-go/issues/756)
 * [Day 376](https://github.com/vaskoz/dailycodingproblem-go/issues/758)
 * [Day 377](https://github.com/vaskoz/dailycodingproblem-go/issues/760)
+* [Day 378](https://github.com/vaskoz/dailycodingproblem-go/issues/762)
 
diff --git a/day378/problem.go b/day378/problem.go
new file mode 100644
index 0000000..9bfed50
--- /dev/null
+++ b/day378/problem.go
@@ -0,0 +1,14 @@
+package day378
+
+import "encoding/json"
+
+// ToJSON converts arbitrary data into a json string.
+// Delegates to json.Marshal because it exists.
+func ToJSON(data interface{}) string {
+	b, err := json.Marshal(data)
+	if err != nil {
+		return ""
+	}
+
+	return string(b)
+}


Test patch:
diff --git a/day378/problem_test.go b/day378/problem_test.go
new file mode 100644
index 0000000..0344310
--- /dev/null
+++ b/day378/problem_test.go
@@ -0,0 +1,36 @@
+package day378
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	input    interface{}
+	expected string
+}{
+	{
+		[]interface{}{nil, 123, []string{"a", "b"}, map[string]string{"c": "d"}},
+		`[null,123,["a","b"],{"c":"d"}]`,
+	},
+	{
+		func() {},
+		"",
+	},
+}
+
+func TestToJSON(t *testing.T) {
+	t.Parallel()
+
+	for _, tc := range testcases {
+		if result := ToJSON(tc.input); result != tc.expected {
+			t.Errorf("Expected %v, got %v", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkToJSON(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			ToJSON(tc.input)
+		}
+	}
+}