Skip to content

Commit 39a9bfb

Browse files
committed
Add NumberDecoder
When decoding JSON, elastic by default uses `DefaultDecoder` which uses `json.Unmarshal` behind the scenes. This commit adds a `NumberDecoder` which uses `json.NewDecoder(...)` and `json.UseNumber()` for use cases like those described in #891. To switch to `NumberDecoder`, do: ``` client, err := elastic.NewClient( elastic.SetDecoder(&elastic.NumberDecoder{}), ) ``` Of course, you can always provide your own decoder, as described in https://github.com/olivere/elastic/wiki/Decoder.
1 parent 793c92c commit 39a9bfb

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

decoder.go

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package elastic
66

77
import (
8+
"bytes"
89
"encoding/json"
910
)
1011

@@ -24,3 +25,14 @@ type DefaultDecoder struct{}
2425
func (u *DefaultDecoder) Decode(data []byte, v interface{}) error {
2526
return json.Unmarshal(data, v)
2627
}
28+
29+
// NumberDecoder uses json.NewDecoder, with UseNumber() enabled, from
30+
// the Go standard library to decode JSON data.
31+
type NumberDecoder struct{}
32+
33+
// Decode decodes with json.Unmarshal from the Go standard library.
34+
func (u *NumberDecoder) Decode(data []byte, v interface{}) error {
35+
dec := json.NewDecoder(bytes.NewReader(data))
36+
dec.UseNumber()
37+
return dec.Decode(v)
38+
}

0 commit comments

Comments
 (0)