json_filter/implement/sjson_write.go

75 lines
1.3 KiB
Go

// 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)
}