Fix leftover array and map values

fixes #81
This commit is contained in:
tidwall 2018-08-02 08:58:17 -07:00
parent f92dbfc6b2
commit 1e3f6aeaa5
2 changed files with 27 additions and 0 deletions

View File

@ -357,24 +357,30 @@ func (t Result) arrayOrMap(vc byte, valueize bool) (r arrayOrMapResult) {
if (json[i] >= '0' && json[i] <= '9') || json[i] == '-' { if (json[i] >= '0' && json[i] <= '9') || json[i] == '-' {
value.Type = Number value.Type = Number
value.Raw, value.Num = tonum(json[i:]) value.Raw, value.Num = tonum(json[i:])
value.Str = ""
} else { } else {
continue continue
} }
case '{', '[': case '{', '[':
value.Type = JSON value.Type = JSON
value.Raw = squash(json[i:]) value.Raw = squash(json[i:])
value.Str, value.Num = "", 0
case 'n': case 'n':
value.Type = Null value.Type = Null
value.Raw = tolit(json[i:]) value.Raw = tolit(json[i:])
value.Str, value.Num = "", 0
case 't': case 't':
value.Type = True value.Type = True
value.Raw = tolit(json[i:]) value.Raw = tolit(json[i:])
value.Str, value.Num = "", 0
case 'f': case 'f':
value.Type = False value.Type = False
value.Raw = tolit(json[i:]) value.Raw = tolit(json[i:])
value.Str, value.Num = "", 0
case '"': case '"':
value.Type = String value.Type = String
value.Raw, value.Str = tostr(json[i:]) value.Raw, value.Str = tostr(json[i:])
value.Num = 0
} }
i += len(value.Raw) - 1 i += len(value.Raw) - 1

View File

@ -1406,3 +1406,24 @@ func TestDuplicateKeys(t *testing.T) {
t.Fatal("should be valid") t.Fatal("should be valid")
} }
} }
func TestArrayValues(t *testing.T) {
var json = `{"array": ["PERSON1","PERSON2",0],}`
values := Get(json, "array").Array()
var output string
for i, val := range values {
if i > 0 {
output += "\n"
}
output += fmt.Sprintf("%#v", val)
}
expect := strings.Join([]string{
`gjson.Result{Type:3, Raw:"\"PERSON1\"", Str:"PERSON1", Num:0, Index:0}`,
`gjson.Result{Type:3, Raw:"\"PERSON2\"", Str:"PERSON2", Num:0, Index:0}`,
`gjson.Result{Type:2, Raw:"0", Str:"", Num:0, Index:0}`,
}, "\n")
if output != expect {
t.Fatalf("expected '%v', got '%v'", expect, output)
}
}