You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decoder.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. )
  9. // Decoder is used to decode responses from Elasticsearch.
  10. // Users of elastic can implement their own marshaler for advanced purposes
  11. // and set them per Client (see SetDecoder). If none is specified,
  12. // DefaultDecoder is used.
  13. type Decoder interface {
  14. Decode(data []byte, v interface{}) error
  15. }
  16. // DefaultDecoder uses json.Unmarshal from the Go standard library
  17. // to decode JSON data.
  18. type DefaultDecoder struct{}
  19. // Decode decodes with json.Unmarshal from the Go standard library.
  20. func (u *DefaultDecoder) Decode(data []byte, v interface{}) error {
  21. return json.Unmarshal(data, v)
  22. }
  23. // NumberDecoder uses json.NewDecoder, with UseNumber() enabled, from
  24. // the Go standard library to decode JSON data.
  25. type NumberDecoder struct{}
  26. // Decode decodes with json.Unmarshal from the Go standard library.
  27. func (u *NumberDecoder) Decode(data []byte, v interface{}) error {
  28. dec := json.NewDecoder(bytes.NewReader(data))
  29. dec.UseNumber()
  30. return dec.Decode(v)
  31. }