mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-09 18:43:14 +00:00
Improve usages of json.Unmarshal
This commit is contained in:
parent
6aa8e2e490
commit
54b65b809f
|
@ -1,8 +1,6 @@
|
||||||
package option
|
package option
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
|
|
||||||
"github.com/sagernet/sing/common"
|
"github.com/sagernet/sing/common"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
"github.com/sagernet/sing/common/json"
|
"github.com/sagernet/sing/common/json"
|
||||||
|
@ -69,7 +67,5 @@ func UnmarshallExcluded(inputContent []byte, parentObject any, object any) error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
decoder := json.NewDecoder(bytes.NewReader(inputContent))
|
return json.UnmarshalDisallowUnknownFields(inputContent, object)
|
||||||
decoder.DisallowUnknownFields()
|
|
||||||
return decoder.Decode(object)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,12 +128,12 @@ func (l Listable[T]) MarshalJSON() ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Listable[T]) UnmarshalJSON(content []byte) error {
|
func (l *Listable[T]) UnmarshalJSON(content []byte) error {
|
||||||
err := json.Unmarshal(content, (*[]T)(l))
|
err := json.UnmarshalDisallowUnknownFields(content, (*[]T)(l))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var singleItem T
|
var singleItem T
|
||||||
newError := json.Unmarshal(content, &singleItem)
|
newError := json.UnmarshalDisallowUnknownFields(content, &singleItem)
|
||||||
if newError != nil {
|
if newError != nil {
|
||||||
return E.Errors(err, newError)
|
return E.Errors(err, newError)
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,5 +26,5 @@ func (o *UDPOverTCPOptions) UnmarshalJSON(bytes []byte) error {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return json.Unmarshal(bytes, (*_UDPOverTCPOptions)(o))
|
return json.UnmarshalDisallowUnknownFields(bytes, (*_UDPOverTCPOptions)(o))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue