Skip to content

Commit b8c4685

Browse files
committed
Merge pull request bitly#26 from Quantisan/feature/null-string-in-array
StringArray handle JSON null string as "" and add test for it
2 parents bc0f349 + 1cfceb0 commit b8c4685

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

simplejson.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func (j *Json) StringArray() ([]string, error) {
176176
}
177177
retArr := make([]string, 0, len(arr))
178178
for _, a := range arr {
179+
if a == nil {
180+
retArr = append(retArr, "")
181+
continue
182+
}
179183
s, ok := a.(string)
180184
if !ok {
181185
return nil, err

simplejson_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func TestSimplejson(t *testing.T) {
1313
js, err := NewJson([]byte(`{
1414
"test": {
1515
"string_array": ["asdf", "ghjk", "zxcv"],
16+
"string_array_null": ["abc", null, "efg"],
1617
"array": [1, "2", 3],
1718
"arraywithsubs": [{"subkeyone": 1},
1819
{"subkeytwo": 2, "subkeythree": 3}],
@@ -78,6 +79,12 @@ func TestSimplejson(t *testing.T) {
7879
assert.Equal(t, strs[1], "ghjk")
7980
assert.Equal(t, strs[2], "zxcv")
8081

82+
strs2, err := js.Get("test").Get("string_array_null").StringArray()
83+
assert.Equal(t, err, nil)
84+
assert.Equal(t, strs2[0], "abc")
85+
assert.Equal(t, strs2[1], "")
86+
assert.Equal(t, strs2[2], "efg")
87+
8188
gp, _ := js.GetPath("test", "string").String()
8289
assert.Equal(t, "simplejson", gp)
8390

0 commit comments

Comments
 (0)