Merge pull request #11533 from hqhq/hq_fix_decode

fix decode data loss when using int64 in json
This commit is contained in:
Tibor Vass 2015-03-25 10:17:10 -06:00
commit 639d60b538
2 changed files with 24 additions and 1 deletions

View file

@ -189,7 +189,10 @@ func (decoder *Decoder) Decode() (*Env, error) {
// is returned.
func (env *Env) Decode(src io.Reader) error {
m := make(map[string]interface{})
if err := json.NewDecoder(src).Decode(&m); err != nil {
d := json.NewDecoder(src)
// We need this or we'll lose data when we decode int64 in json
d.UseNumber()
if err := d.Decode(&m); err != nil {
return err
}
for k, v := range m {

View file

@ -78,6 +78,26 @@ func TestSetenv(t *testing.T) {
}
}
func TestDecodeEnv(t *testing.T) {
job := mkJob(t, "dummy")
type tmp struct {
Id1 int64
Id2 int64
}
body := []byte("{\"tags\":{\"Id1\":123, \"Id2\":1234567}}")
if err := job.DecodeEnv(bytes.NewBuffer(body)); err != nil {
t.Fatalf("DecodeEnv failed: %v", err)
}
mytag := tmp{}
if val := job.GetenvJson("tags", &mytag); val != nil {
t.Fatalf("GetenvJson returns incorrect value: %s", val)
}
if mytag.Id1 != 123 || mytag.Id2 != 1234567 {
t.Fatal("Get wrong values set by job.DecodeEnv")
}
}
func TestSetenvBool(t *testing.T) {
job := mkJob(t, "dummy")
job.SetenvBool("foo", true)