支持从json动态生成结构体,并转化为指定格式 #6
| @ -10,12 +10,13 @@ package wrapper | |||||||
| import ( | import ( | ||||||
| 	"encoding/xml" | 	"encoding/xml" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"fmt" | ||||||
| 	dynamicstruct "git.zhangdeman.cn/zhangdeman/dynamic-struct" | 	dynamicstruct "git.zhangdeman.cn/zhangdeman/dynamic-struct" | ||||||
| 	"git.zhangdeman.cn/zhangdeman/serialize" | 	"git.zhangdeman.cn/zhangdeman/serialize" | ||||||
| 	"github.com/tidwall/gjson" | 	"github.com/tidwall/gjson" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func NewJson(sourceData string) (*ownJson, error) { | func NewJson(sourceData string, o *Option) (*ownJson, error) { | ||||||
| 	gjsonRes := gjson.Parse(sourceData) | 	gjsonRes := gjson.Parse(sourceData) | ||||||
| 	if gjsonRes.Value() == nil { | 	if gjsonRes.Value() == nil { | ||||||
| 		return nil, errors.New("source data parse result is nil") | 		return nil, errors.New("source data parse result is nil") | ||||||
| @ -24,10 +25,17 @@ func NewJson(sourceData string) (*ownJson, error) { | |||||||
| 	if !gjsonRes.IsObject() { | 	if !gjsonRes.IsObject() { | ||||||
| 		return nil, errors.New("source result is not map or struct Marshal string") | 		return nil, errors.New("source result is not map or struct Marshal string") | ||||||
| 	} | 	} | ||||||
|  | 	if nil == o { | ||||||
|  | 		o = &Option{} | ||||||
|  | 	} | ||||||
|  | 	if o.XmlName == "" { | ||||||
|  | 		o.XmlName = "XmlData" | ||||||
|  | 	} | ||||||
| 	oj := &ownJson{ | 	oj := &ownJson{ | ||||||
| 		sourceData:    sourceData, | 		sourceData:    sourceData, | ||||||
| 		gjsonResult:   gjsonRes, | 		gjsonResult:   gjsonRes, | ||||||
| 		structBuilder: dynamicstruct.NewStruct(nil), | 		structBuilder: dynamicstruct.NewStruct(nil), | ||||||
|  | 		o:             o, | ||||||
| 	} | 	} | ||||||
| 	if err := oj.GenerateStruct(); nil != err { | 	if err := oj.GenerateStruct(); nil != err { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| @ -40,6 +48,7 @@ type ownJson struct { | |||||||
| 	gjsonResult   gjson.Result           // 数据源解析为gjson | 	gjsonResult   gjson.Result           // 数据源解析为gjson | ||||||
| 	structBuilder dynamicstruct.IBuilder // 结构体构造器 | 	structBuilder dynamicstruct.IBuilder // 结构体构造器 | ||||||
| 	structRes     any                    // 解析的结构体值 | 	structRes     any                    // 解析的结构体值 | ||||||
|  | 	o             *Option                // 一些操作选项 | ||||||
| } | } | ||||||
|  |  | ||||||
| // GenerateStruct 生成结构体字段列表 | // GenerateStruct 生成结构体字段列表 | ||||||
| @ -49,7 +58,7 @@ func (oj *ownJson) GenerateStruct() error { | |||||||
| 		return true | 		return true | ||||||
| 	}) | 	}) | ||||||
| 	// 追加xml的标签 | 	// 追加xml的标签 | ||||||
| 	oj.structBuilder.AddField("XMLName", "", xml.Name{}, `json:"-" xml:"XmlData"`, false) | 	oj.structBuilder.AddField("XMLName", "", xml.Name{}, fmt.Sprintf(`json:"-" toml:"-" yml:"-" init:"-" xml:"%v"`, oj.o.XmlName), false) | ||||||
| 	val := oj.structBuilder.Build().New() | 	val := oj.structBuilder.Build().New() | ||||||
| 	if err := serialize.JSON.UnmarshalWithNumber([]byte(oj.sourceData), &val); nil != err { | 	if err := serialize.JSON.UnmarshalWithNumber([]byte(oj.sourceData), &val); nil != err { | ||||||
| 		return err | 		return err | ||||||
|  | |||||||
| @ -14,7 +14,7 @@ import ( | |||||||
|  |  | ||||||
| func TestNewJson(t *testing.T) { | func TestNewJson(t *testing.T) { | ||||||
| 	sourceData := `{"name": "test", "age":18,"company":{"address": "Beijing", "name":"lala"},"index":[1,2,3,4], "deep":[{"name":"a"}]}` | 	sourceData := `{"name": "test", "age":18,"company":{"address": "Beijing", "name":"lala"},"index":[1,2,3,4], "deep":[{"name":"a"}]}` | ||||||
| 	instance, iErr := NewJson(sourceData) | 	instance, iErr := NewJson(sourceData, &Option{XmlName: "ResponseData"}) | ||||||
| 	fmt.Println(iErr) | 	fmt.Println(iErr) | ||||||
| 	res, err := instance.Marshal("xml") | 	res, err := instance.Marshal("xml") | ||||||
| 	fmt.Println(err) | 	fmt.Println(err) | ||||||
|  | |||||||
							
								
								
									
										13
									
								
								wrapper/option.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								wrapper/option.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,13 @@ | |||||||
|  | // Package wrapper ... | ||||||
|  | // | ||||||
|  | // Description : wrapper ... | ||||||
|  | // | ||||||
|  | // Author : go_developer@163.com<白茶清欢> | ||||||
|  | // | ||||||
|  | // Date : 2025-04-28 17:35 | ||||||
|  | package wrapper | ||||||
|  |  | ||||||
|  | // Option 生成结构体之后的可用选项 | ||||||
|  | type Option struct { | ||||||
|  | 	XmlName string `json:"xml_name"` // 如果目标格式是 xml, 必须指定xmlName作为根节点 | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user