升级json操作, 升级为面向接口实现 #10

Merged
zhangdeman merged 7 commits from feature/upgrade_get_set into master 2025-05-06 14:14:30 +08:00
3 changed files with 30 additions and 0 deletions
Showing only changes of commit 042aad254b - Show all commits

View File

@ -31,4 +31,6 @@ type IJsonRead interface {
Array(dataPath string) ([]any, error)
// ArrayWithReceiver 通过指针接收
ArrayWithReceiver(dataPath string, receiver any) error
// Value 自适应类型数据读取
Value(dataPath string, dataType string, defaultValue any) (any, error)
}

View File

@ -10,6 +10,8 @@ package filter
import (
"encoding/json"
"fmt"
"git.zhangdeman.cn/zhangdeman/json_filter/abstract"
"git.zhangdeman.cn/zhangdeman/json_filter/implement"
"reflect"
"strings"
@ -21,6 +23,22 @@ import (
"errors"
)
func NewFilterWithJson(sourceData string, filterRuleList []MapRule, jsonRead abstract.IJsonRead, jsonWrite abstract.IJsonWrite) *filter {
if nil == jsonRead {
jsonRead = implement.NewGjsonRead(sourceData)
}
if nil == jsonWrite {
jsonWrite = implement.NewSjsonWrite()
}
return &filter{
jsonRaad: jsonRead,
jsonWrite: jsonWrite,
sourceData: sourceData,
formatResult: "",
filterRuleList: filterRuleList,
}
}
// NewFilter 过滤器实例
//
// Author : go_developer@163.com<白茶清欢>
@ -30,6 +48,8 @@ func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
return &filter{
sourceData: sourceData,
formatResult: "{}",
jsonRaad: implement.NewGjsonRead(sourceData),
jsonWrite: implement.NewSjsonWrite(),
filterRuleList: filterRuleList,
}
}
@ -40,6 +60,8 @@ func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
//
// Date : 11:58 2022/7/4
type filter struct {
jsonRaad abstract.IJsonRead
jsonWrite abstract.IJsonWrite
sourceData string
formatResult string
filterRuleList []MapRule

View File

@ -118,3 +118,9 @@ func (g *gjsonRead) ArrayWithReceiver(dataPath string, receiver any) error {
}
return serialize.JSON.UnmarshalWithNumber([]byte(strVal), receiver)
}
func (g *gjsonRead) Value(dataPath string, dataType string, defaultValue any) (any, error) {
if len(dataType) == 0 {
dataType = g.Type(dataPath)
}
return gjson_hack.Value(consts.DataType(dataType), g.gjsonResult.Get(dataPath), defaultValue)
}