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 5package elastic 6 7import ( 8 "bytes" 9 "encoding/json" 10) 11 12// Decoder is used to decode responses from Elasticsearch. 13// Users of elastic can implement their own marshaler for advanced purposes 14// and set them per Client (see SetDecoder). If none is specified, 15// DefaultDecoder is used. 16type Decoder interface { 17 Decode(data []byte, v interface{}) error 18} 19 20// DefaultDecoder uses json.Unmarshal from the Go standard library 21// to decode JSON data. 22type DefaultDecoder struct{} 23 24// Decode decodes with json.Unmarshal from the Go standard library. 25func (u *DefaultDecoder) Decode(data []byte, v interface{}) error { 26 return json.Unmarshal(data, v) 27} 28 29// NumberDecoder uses json.NewDecoder, with UseNumber() enabled, from 30// the Go standard library to decode JSON data. 31type NumberDecoder struct{} 32 33// Decode decodes with json.Unmarshal from the Go standard library. 34func (u *NumberDecoder) Decode(data []byte, v interface{}) error { 35 dec := json.NewDecoder(bytes.NewReader(data)) 36 dec.UseNumber() 37 return dec.Decode(v) 38} 39