1 Star 0 Fork 0

天雨流芳 / go-micro-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
json.go 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
天雨流芳 提交于 2024-03-19 15:24 . 增加通用文件
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.
package jsonutil
import (
"encoding/json"
"log"
"github.com/bitly/go-simplejson"
)
func Encode(o interface{}) ([]byte, error) {
return json.Marshal(o)
}
func Decode(y []byte, o interface{}) error {
return json.Unmarshal(y, o)
}
func ToString(o interface{}) string {
b, err := Encode(o)
if err != nil {
return ""
}
return string(b)
}
// FIXME: need improve performance
func ToJson(o interface{}) Json {
var j Json
j = &fakeJson{simplejson.New()}
b, err := Encode(o)
if err != nil {
log.Printf("Failed to encode [%+v] to []byte, error: %+v", o, err)
return j
}
j, err = NewJson(b)
if err != nil {
log.Printf("Failed to decode [%+v] to Json, error: %+v", o, err)
}
return j
}
type fakeJson struct {
*simplejson.Json
}
func NewJson(y []byte) (Json, error) {
j, err := simplejson.NewJson(y)
return &fakeJson{j}, err
}
func (j *fakeJson) Get(key string) Json {
return &fakeJson{j.Json.Get(key)}
}
func (j *fakeJson) GetPath(branch ...string) Json {
return &fakeJson{j.Json.GetPath(branch...)}
}
func (j *fakeJson) CheckGet(key string) (Json, bool) {
result, ok := j.Json.CheckGet(key)
return &fakeJson{result}, ok
}
//
//func (j *fakeJson) UnmarshalJSON(p []byte) error {
// return j.Json.UnmarshalJSON(p)
//}
//
//func (j *fakeJson) MarshalJSON() ([]byte, error) {
// return j.Json.MarshalJSON()
//}
1
https://gitee.com/tylf2018/go-micro-framework.git
git@gitee.com:tylf2018/go-micro-framework.git
tylf2018
go-micro-framework
go-micro-framework
a23f37e8bd2b

搜索帮助