diff --git a/eventcontent.go b/eventcontent.go index a84090cf..b1103909 100644 --- a/eventcontent.go +++ b/eventcontent.go @@ -418,11 +418,11 @@ func (v *levelJSONValue) UnmarshalJSON(data []byte) error { var err error // First try to unmarshal as an int64. - if err = json.Unmarshal(data, &int64Value); err != nil { + if int64Value, err = strconv.ParseInt(string(data), 10, 64); err != nil { // If unmarshalling as an int64 fails try as a string. if err = json.Unmarshal(data, &stringValue); err != nil { // If unmarshalling as a string fails try as a float. - if err = json.Unmarshal(data, &floatValue); err != nil { + if floatValue, err = strconv.ParseFloat(string(data), 64); err != nil { return err } int64Value = int64(floatValue) diff --git a/eventcontent_test.go b/eventcontent_test.go index 9c719afa..f87aa31a 100644 --- a/eventcontent_test.go +++ b/eventcontent_test.go @@ -20,6 +20,27 @@ import ( "testing" ) +func BenchmarkLevelJSONValueInt(b *testing.B) { + for n := 0; n < b.N; n++ { + var value []levelJSONValue + _ = json.Unmarshal([]byte(`[1, 2, 3]`), &value) + } +} + +func BenchmarkLevelJSONValueFloat(b *testing.B) { + for n := 0; n < b.N; n++ { + var value []levelJSONValue + _ = json.Unmarshal([]byte(`[1.1, 1.2, 1.3]`), &value) + } +} + +func BenchmarkLevelJSONValueString(b *testing.B) { + for n := 0; n < b.N; n++ { + var value []levelJSONValue + _ = json.Unmarshal([]byte(`["1", "2", "3"]`), &value) + } +} + func TestLevelJSONValueValid(t *testing.T) { var values []levelJSONValue input := `[0,"1",2.0]`