serialize/json.go

243 lines
5.5 KiB
Go
Raw Normal View History

// Package serialize ...
//
// Description : json 工具函数
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-09-14 8:38 下午
package serialize
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
)
var (
// JSON ...
JSON *ownJSON
)
func init() {
JSON = &ownJSON{}
}
// ownJSON ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:01 2022/5/14
type ownJSON struct {
}
// UnmarshalWithNumber 解析json
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:39 下午 2021/9/14
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) UnmarshalWithNumber(byteData []byte, receiver any) error {
decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber()
return decoder.Decode(receiver)
}
2023-12-24 20:54:39 +08:00
// UnmarshalWithNumberIgnoreError 反序列化且忽略error
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:46 2023/12/24
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) UnmarshalWithNumberIgnoreError(byteData []byte, receiver any) {
2023-12-24 20:54:39 +08:00
_ = oj.UnmarshalWithNumber(byteData, receiver)
}
// UnmarshalWithNumberForIOReader ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:43 下午 2021/9/14
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) error {
decoder := json.NewDecoder(ioReader)
decoder.UseNumber()
return decoder.Decode(receiver)
}
2023-12-24 20:54:39 +08:00
// UnmarshalWithNumberForIOReaderIgnoreError 反序列化忽略Error
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:47 2023/12/24
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver any) {
2023-12-24 20:54:39 +08:00
_ = oj.UnmarshalWithNumberForIOReader(ioReader, receiver)
return
}
// UnmarshalWithNumberForString 字符串转结构体
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:50 2023/7/22
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver any) error {
return oj.UnmarshalWithNumber([]byte(input), receiver)
}
2023-12-24 20:54:39 +08:00
// UnmarshalWithNumberForStringIgnoreError 反序列化并且忽略error
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:48 2023/12/24
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) UnmarshalWithNumberForStringIgnoreError(input string, receiver any) {
2023-12-24 20:54:39 +08:00
oj.UnmarshalWithNumberIgnoreError([]byte(input), receiver)
}
// MarshalForByte 序列化并返回字节数组
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:56 2023/7/22
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) MarshalForByte(input any) []byte {
2023-09-01 18:21:38 +08:00
buffer := bytes.NewBuffer([]byte{})
2023-09-01 18:32:03 +08:00
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
2023-09-01 18:32:03 +08:00
_ = encoder.Encode(input)
2023-09-01 18:21:38 +08:00
return buffer.Bytes()
}
// MarshalForString 序列化并返回字符串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:56 2023/7/22
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) MarshalForString(input any) string {
2023-09-01 18:32:03 +08:00
return string(oj.MarshalForByte(input))
}
2023-12-29 18:26:59 +08:00
// Transition 数据结构转换 map/slice/struct => struct | struct => map/slice/struct
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:25 2023/12/29
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) Transition(input any, receiver any) error {
2023-12-29 18:26:59 +08:00
return oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver)
}
2024-01-10 17:01:08 +08:00
// TransitionIgnoreError ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:57 2024/1/10
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) TransitionIgnoreError(input any, receiver any) {
2024-01-10 17:01:08 +08:00
_ = oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver)
}
// MergeDataForMap 合并数据到map
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:48 2024/1/10
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...any) (map[string]any, error) {
res := make(map[string]any)
2024-01-10 17:01:08 +08:00
for _, data := range dataList {
if nil == data {
continue
}
var (
err error
2024-10-23 17:04:10 +08:00
itemRes map[string]any
2024-01-10 17:01:08 +08:00
)
if err = oj.Transition(data, &itemRes); nil != err {
if ignoreError {
continue
}
return res, err
}
for k, v := range itemRes {
res[k] = v
}
}
return res, nil
}
// MergeDataForMapIgnoreError 多个数据合并到一个Map
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:00 2024/1/10
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) MergeDataForMapIgnoreError(dataList ...any) map[string]any {
2024-01-10 17:01:08 +08:00
res, _ := oj.MergeDataForMap(true, dataList)
return res
}
2024-01-10 17:08:03 +08:00
// MergeDataForReceiver 合并数据并转换
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:07 2024/1/10
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) MergeDataForReceiver(receiver any, dataList ...any) error {
2024-01-10 17:08:03 +08:00
res, err := oj.MergeDataForMap(false, dataList)
if nil != err {
return err
}
if err = oj.Transition(res, receiver); nil != err {
return err
}
return nil
}
// ConsoleOutput ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:45 下午 2021/11/5
2024-10-23 17:04:10 +08:00
func (oj *ownJSON) ConsoleOutput(data any) {
var out bytes.Buffer
switch reflect.TypeOf(data).Kind() {
case reflect.Slice:
fallthrough
case reflect.Array:
fallthrough
case reflect.Map:
fallthrough
2023-09-09 14:51:33 +08:00
case reflect.Struct:
fallthrough
case reflect.Ptr:
2023-09-09 14:51:33 +08:00
_ = json.Indent(&out, []byte(oj.MarshalForString(data)+"\n"), "", "\t")
_, _ = out.WriteTo(os.Stdout)
return
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
fallthrough
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
fallthrough
case reflect.Float32:
fallthrough
case reflect.Float64:
fallthrough
case reflect.String:
_ = json.Indent(&out, []byte(fmt.Sprintf("%v\n", data)), "", "\t")
_, _ = out.WriteTo(os.Stdout)
return
default:
fmt.Println("")
}
}