sing-box/option/json.go
2022-07-03 03:42:57 +08:00

41 lines
826 B
Go

package option
import (
"encoding/json"
"github.com/sagernet/sing/common/x/linkedhashmap"
)
func ToMap(v any) (*linkedhashmap.Map[string, any], error) {
bytes, err := json.Marshal(v)
if err != nil {
return nil, err
}
var content linkedhashmap.Map[string, any]
err = json.Unmarshal(bytes, &content)
if err != nil {
return nil, err
}
return &content, nil
}
func MergeObjects(objects ...any) (*linkedhashmap.Map[string, any], error) {
var content linkedhashmap.Map[string, any]
for _, object := range objects {
objectMap, err := ToMap(object)
if err != nil {
return nil, err
}
content.PutAll(objectMap)
}
return &content, nil
}
func MarshallObjects(objects ...any) ([]byte, error) {
content, err := MergeObjects(objects...)
if err != nil {
return nil, err
}
return json.Marshal(content)
}