mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
any
This commit is contained in:
@ -40,6 +40,7 @@ type CamelCase struct {
|
|||||||
|
|
||||||
func (c CamelCase) MarshalJSON() ([]byte, error) {
|
func (c CamelCase) MarshalJSON() ([]byte, error) {
|
||||||
var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
|
var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
|
||||||
|
|
||||||
marshalled, err := Marshal(c.Value)
|
marshalled, err := Marshal(c.Value)
|
||||||
converted := keyMatchRegex.ReplaceAllFunc(
|
converted := keyMatchRegex.ReplaceAllFunc(
|
||||||
marshalled,
|
marshalled,
|
||||||
|
17
json/jsonconv_test.go
Normal file
17
json/jsonconv_test.go
Normal file
@ -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}))
|
||||||
|
}
|
@ -2,13 +2,13 @@ package json
|
|||||||
|
|
||||||
import "github.com/charlienet/go-mixed/bytesconv"
|
import "github.com/charlienet/go-mixed/bytesconv"
|
||||||
|
|
||||||
func StructToJsonIndent(obj interface{}) string {
|
func StructToJsonIndent(obj any) string {
|
||||||
b, _ := MarshalIndent(obj, "", " ")
|
b, _ := MarshalIndent(obj, "", " ")
|
||||||
return bytesconv.BytesToString(b)
|
return bytesconv.BytesToString(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 结构转换为json字符串
|
// 结构转换为json字符串
|
||||||
func StructToJson(obj interface{}) string {
|
func StructToJson(obj any) string {
|
||||||
b, _ := Marshal(obj)
|
b, _ := Marshal(obj)
|
||||||
return bytesconv.BytesToString(b)
|
return bytesconv.BytesToString(b)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user