Refactor json

This commit is contained in:
世界 2022-07-03 19:43:27 +08:00
parent dd56b2584b
commit 28b865acf0
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
8 changed files with 179 additions and 188 deletions

View file

@ -4,15 +4,15 @@ import (
"bytes"
"github.com/goccy/go-json"
"github.com/sagernet/sing-box/common/linkedhashmap"
"github.com/sagernet/sing-box/common/badjson"
)
func ToMap(v any) (*linkedhashmap.Map[string, any], error) {
func ToMap(v any) (*badjson.JSONObject, error) {
inputContent, err := json.Marshal(v)
if err != nil {
return nil, err
}
var content linkedhashmap.Map[string, any]
var content badjson.JSONObject
err = content.UnmarshalJSON(inputContent)
if err != nil {
return nil, err
@ -20,8 +20,8 @@ func ToMap(v any) (*linkedhashmap.Map[string, any], error) {
return &content, nil
}
func MergeObjects(objects ...any) (*linkedhashmap.Map[string, any], error) {
var content linkedhashmap.Map[string, any]
func MergeObjects(objects ...any) (*badjson.JSONObject, error) {
var content badjson.JSONObject
for _, object := range objects {
objectMap, err := ToMap(object)
if err != nil {
@ -45,12 +45,14 @@ func UnmarshallExcluded(inputContent []byte, parentObject any, object any) error
if err != nil {
return err
}
var content linkedhashmap.Map[string, any]
var content badjson.JSONObject
err = content.UnmarshalJSON(inputContent)
if err != nil {
return err
}
content.RemoveAll(parentContent.Keys())
for _, key := range parentContent.Keys() {
content.Remove(key)
}
inputContent, err = content.MarshalJSON()
if err != nil {
return err