1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
json.go 2.12 KB
一键复制 编辑 原始数据 按行查看 历史
Tudor Golubenco 提交于 2017-04-20 10:50 . Fix JSON panic (#4042) (#4052)
package reader
import (
"bytes"
"encoding/json"
"fmt"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/jsontransform"
"github.com/elastic/beats/libbeat/logp"
)
const (
JsonErrorKey = "json_error"
)
type JSON struct {
reader Reader
cfg *JSONConfig
}
// NewJSONReader creates a new reader that can decode JSON.
func NewJSON(r Reader, cfg *JSONConfig) *JSON {
return &JSON{reader: r, cfg: cfg}
}
// decodeJSON unmarshals the text parameter into a MapStr and
// returns the new text column if one was requested.
func (r *JSON) decodeJSON(text []byte) ([]byte, common.MapStr) {
var jsonFields map[string]interface{}
err := unmarshal(text, &jsonFields)
if err != nil || jsonFields == nil {
logp.Err("Error decoding JSON: %v", err)
if r.cfg.AddErrorKey {
jsonFields = common.MapStr{JsonErrorKey: fmt.Sprintf("Error decoding JSON: %v", err)}
}
return text, jsonFields
}
if len(r.cfg.MessageKey) == 0 {
return []byte(""), jsonFields
}
textValue, ok := jsonFields[r.cfg.MessageKey]
if !ok {
if r.cfg.AddErrorKey {
jsonFields[JsonErrorKey] = fmt.Sprintf("Key '%s' not found", r.cfg.MessageKey)
}
return []byte(""), jsonFields
}
textString, ok := textValue.(string)
if !ok {
if r.cfg.AddErrorKey {
jsonFields[JsonErrorKey] = fmt.Sprintf("Value of key '%s' is not a string", r.cfg.MessageKey)
}
return []byte(""), jsonFields
}
return []byte(textString), jsonFields
}
// unmarshal is equivalent with json.Unmarshal but it converts numbers
// to int64 where possible, instead of using always float64.
func unmarshal(text []byte, fields *map[string]interface{}) error {
dec := json.NewDecoder(bytes.NewReader(text))
dec.UseNumber()
err := dec.Decode(fields)
if err != nil {
return err
}
jsontransform.TransformNumbers(*fields)
return nil
}
// Next decodes JSON and returns the filled Line object.
func (r *JSON) Next() (Message, error) {
message, err := r.reader.Next()
if err != nil {
return message, err
}
var fields common.MapStr
message.Content, fields = r.decodeJSON(message.Content)
message.AddFields(common.MapStr{"json": fields})
return message, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v5.5.2

搜索帮助