From 25af24d7c05a8a284fd2a3fc6aa7d644f93d4924 Mon Sep 17 00:00:00 2001 From: charlie <3140647@qq.com> Date: Fri, 6 May 2022 15:31:57 +0800 Subject: [PATCH] any --- json/jsonconv.go | 1 + json/jsonconv_test.go | 17 +++++++++++++++++ json/struct_json.go | 4 ++-- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 json/jsonconv_test.go diff --git a/json/jsonconv.go b/json/jsonconv.go index 5bbaee4..26151a2 100644 --- a/json/jsonconv.go +++ b/json/jsonconv.go @@ -40,6 +40,7 @@ type CamelCase struct { func (c CamelCase) MarshalJSON() ([]byte, error) { var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`) + marshalled, err := Marshal(c.Value) converted := keyMatchRegex.ReplaceAllFunc( marshalled, diff --git a/json/jsonconv_test.go b/json/jsonconv_test.go new file mode 100644 index 0000000..48ba278 --- /dev/null +++ b/json/jsonconv_test.go @@ -0,0 +1,17 @@ +package json_test + +import ( + "testing" + + "github.com/charlienet/go-mixed/json" +) + +func TestNameConvert(t *testing.T) { + d := struct { + UserName string + Age int + }{UserName: "测试", Age: 13} + + t.Log(json.StructToJsonIndent(json.CamelCase{Value: d})) + t.Log(json.StructToJsonIndent(json.SnakeCase{Value: d})) +} diff --git a/json/struct_json.go b/json/struct_json.go index a090170..d05fd4d 100644 --- a/json/struct_json.go +++ b/json/struct_json.go @@ -2,13 +2,13 @@ package json import "github.com/charlienet/go-mixed/bytesconv" -func StructToJsonIndent(obj interface{}) string { +func StructToJsonIndent(obj any) string { b, _ := MarshalIndent(obj, "", " ") return bytesconv.BytesToString(b) } // 结构转换为json字符串 -func StructToJson(obj interface{}) string { +func StructToJson(obj any) string { b, _ := Marshal(obj) return bytesconv.BytesToString(b) }