增加基于sjson实现的IJsonWrite
This commit is contained in:
74
implement/sjson_write.go
Normal file
74
implement/sjson_write.go
Normal file
@ -0,0 +1,74 @@
|
||||
// Package implement ...
|
||||
//
|
||||
// Description : implement ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-06 11:53
|
||||
package implement
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/sjson"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func NewSjsonWrite() abstract.IJsonWrite {
|
||||
return &SjsonWrite{
|
||||
res: "",
|
||||
l: &sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
type SjsonWrite struct {
|
||||
res string
|
||||
l *sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) Set(dataPath string, data any) error {
|
||||
var (
|
||||
err error
|
||||
)
|
||||
|
||||
s.l.Lock()
|
||||
defer s.l.Unlock()
|
||||
if s.res, err = sjson.Set(s.res, dataPath, data); nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) Result() string {
|
||||
return s.res
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) Map() (map[string]any, error) {
|
||||
var (
|
||||
mapRes map[string]any
|
||||
err error
|
||||
)
|
||||
if err = s.MapWithReceiver(&mapRes); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return mapRes, nil
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) MapWithReceiver(receiver any) error {
|
||||
return serialize.JSON.UnmarshalWithNumberForString(s.res, receiver)
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) Array() ([]any, error) {
|
||||
var (
|
||||
arrRes []any
|
||||
err error
|
||||
)
|
||||
if err = s.ArrayWithReceiver(&arrRes); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return arrRes, nil
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) ArrayWithReceiver(receiver any) error {
|
||||
return serialize.JSON.UnmarshalWithNumberForString(s.res, receiver)
|
||||
}
|
Reference in New Issue
Block a user