序列化约束升级 #1

Merged
zhangdeman merged 5 commits from feature/xml into master 2024-10-23 18:42:59 +08:00
5 changed files with 108 additions and 11 deletions
Showing only changes of commit 82ce67170a - Show all commits

View File

@ -28,7 +28,9 @@ type Serializable interface {
// UnmarshalWithNumberForStringIgnoreError 序列化字符串反解析为结构体,并忽略异常
UnmarshalWithNumberForStringIgnoreError(input string, receiver any)
// MarshalForByte 序列化为字节数组
MarshalForByte(input any) []byte
MarshalForByte(input any) ([]byte, error)
MarshalForByteIgnoreError(input any) []byte
// MarshalForString 序列化为字符串
MarshalForString(input any) string
MarshalForString(input any) (string, error)
MarshalForStringIgnoreError(input any) string
}

2
go.mod
View File

@ -11,4 +11,4 @@ require (
gopkg.in/yaml.v3 v3.0.1
)
require git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241015035056-b6f4a6512763
require git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059

2
go.sum
View File

@ -8,6 +8,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed h1:BGv+y6
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241015035056-b6f4a6512763 h1:aG59AjZAn/Y+smWoa6l6HQB+yh/Rpml79EvmfjEb6Ks=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241015035056-b6f4a6512763/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 h1:TPAYdTKKUjgxtCnK38d1Tb4teyQp1C7wYHPdR32yZtM=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810093304-ccb4045065c4 h1:MgZ2f1BkUtfP8D6uDX9S8o+V4wYUw76HlAw3xCH9epA=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810093304-ccb4045065c4/go.mod h1:tPl6isAsRQLZrX8G3/8fCdhN4OcZypdmdHR/PDkd2PY=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230815042559-b34984be7444 h1:JVp575weLUX4sfhgjjxotJPxfHio7Ua8KHH3LMRRs2E=

41
json.go
View File

@ -97,12 +97,24 @@ func (oj *ownJSON) UnmarshalWithNumberForStringIgnoreError(input string, receive
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:56 2023/7/22
func (oj *ownJSON) MarshalForByte(input any) []byte {
func (oj *ownJSON) MarshalForByte(input any) ([]byte, error) {
buffer := bytes.NewBuffer([]byte{})
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
_ = encoder.Encode(input)
return buffer.Bytes()
if err := encoder.Encode(input); nil != err {
return nil, err
}
return buffer.Bytes(), nil
}
// MarshalForByteIgnoreError 序列化并忽略异常
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:16 2024/10/23
func (oj *ownJSON) MarshalForByteIgnoreError(input any) []byte {
byteData, _ := oj.MarshalForByte(input)
return byteData
}
// MarshalForString 序列化并返回字符串
@ -110,8 +122,21 @@ func (oj *ownJSON) MarshalForByte(input any) []byte {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:56 2023/7/22
func (oj *ownJSON) MarshalForString(input any) string {
return string(oj.MarshalForByte(input))
func (oj *ownJSON) MarshalForString(input any) (string, error) {
byteData, err := oj.MarshalForByte(input)
if nil != err {
return "", err
}
return string(byteData), nil
}
// MarshalForStringIgnoreError ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:19 2024/10/23
func (oj *ownJSON) MarshalForStringIgnoreError(input any) string {
return string(oj.MarshalForByteIgnoreError(input))
}
// Transition 数据结构转换 map/slice/struct => struct | struct => map/slice/struct
@ -120,7 +145,7 @@ func (oj *ownJSON) MarshalForString(input any) string {
//
// Date : 18:25 2023/12/29
func (oj *ownJSON) Transition(input any, receiver any) error {
return oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver)
return oj.UnmarshalWithNumber(oj.MarshalForByteIgnoreError(input), receiver)
}
// TransitionIgnoreError ...
@ -129,7 +154,7 @@ func (oj *ownJSON) Transition(input any, receiver any) error {
//
// Date : 16:57 2024/1/10
func (oj *ownJSON) TransitionIgnoreError(input any, receiver any) {
_ = oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver)
_ = oj.UnmarshalWithNumber(oj.MarshalForByteIgnoreError(input), receiver)
}
// MergeDataForMap 合并数据到map
@ -205,7 +230,7 @@ func (oj *ownJSON) ConsoleOutput(data any) {
case reflect.Struct:
fallthrough
case reflect.Ptr:
_ = json.Indent(&out, []byte(oj.MarshalForString(data)+"\n"), "", "\t")
_ = json.Indent(&out, []byte(oj.MarshalForStringIgnoreError(data)+"\n"), "", "\t")
_, _ = out.WriteTo(os.Stdout)
return
case reflect.Int:

68
yml.go Normal file
View File

@ -0,0 +1,68 @@
// Package serialize ...
//
// Description : serialize ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-10-23 17:06
package serialize
import (
"bytes"
"gopkg.in/yaml.v3"
"io"
)
var (
Yml = ownYml{}
)
type ownYml struct{}
func (o *ownYml) MarshalForByte(input any) ([]byte, error) {
return yaml.Marshal(input)
}
func (o *ownYml) MarshalForByteIgnoreError(input any) []byte {
byteData, _ := o.MarshalForByte(input)
return byteData
}
func (o *ownYml) MarshalForString(input any) (string, error) {
byteData, err := o.MarshalForByte(input)
if nil != err {
return "", err
}
return string(byteData), nil
}
func (o *ownYml) MarshalForStringIgnoreError(input any) string {
return string(o.MarshalForByteIgnoreError(input))
}
func (o *ownYml) UnmarshalWithNumber(byteData []byte, receiver any) error {
return yaml.NewDecoder(bytes.NewReader(byteData)).Decode(receiver)
}
func (o *ownYml) UnmarshalWithNumberIgnoreError(byteData []byte, receiver any) {
_ = o.UnmarshalWithNumber(byteData, receiver)
return
}
func (o *ownYml) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) error {
return yaml.NewDecoder(ioReader).Decode(receiver)
}
func (o *ownYml) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver any) {
_ = o.UnmarshalWithNumberForIOReader(ioReader, receiver)
return
}
func (o *ownYml) UnmarshalWithNumberForString(input string, receiver any) error {
return o.UnmarshalWithNumber([]byte(input), receiver)
}
func (o *ownYml) UnmarshalWithNumberForStringIgnoreError(input string, receiver any) {
_ = o.UnmarshalWithNumberForString(input, receiver)
return
}