mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-02 03:17:37 +03:00
badjson: Fix Listable
This commit is contained in:
parent
e52e04f721
commit
fdca9b3f8e
2 changed files with 10 additions and 10 deletions
|
@ -11,7 +11,7 @@ import (
|
|||
)
|
||||
|
||||
func Omitempty[T any](ctx context.Context, value T) (T, error) {
|
||||
objectContent, err := json.Marshal(value)
|
||||
objectContent, err := json.MarshalContext(ctx, value)
|
||||
if err != nil {
|
||||
return common.DefaultValue[T](), E.Cause(err, "marshal object")
|
||||
}
|
||||
|
|
|
@ -9,24 +9,24 @@ import (
|
|||
|
||||
type Listable[T any] []T
|
||||
|
||||
func (l Listable[T]) MarshalJSON() ([]byte, error) {
|
||||
func (l Listable[T]) MarshalJSONContext(ctx context.Context) ([]byte, error) {
|
||||
arrayList := []T(l)
|
||||
if len(arrayList) == 1 {
|
||||
return json.Marshal(arrayList[0])
|
||||
}
|
||||
return json.Marshal(arrayList)
|
||||
return json.MarshalContext(ctx, arrayList)
|
||||
}
|
||||
|
||||
func (l *Listable[T]) UnmarshalJSONContext(ctx context.Context, content []byte) error {
|
||||
err := json.UnmarshalContextDisallowUnknownFields(ctx, content, (*[]T)(l))
|
||||
var singleItem T
|
||||
err := json.UnmarshalContextDisallowUnknownFields(ctx, content, &singleItem)
|
||||
if err == nil {
|
||||
*l = []T{singleItem}
|
||||
return nil
|
||||
}
|
||||
var singleItem T
|
||||
newError := json.UnmarshalContextDisallowUnknownFields(ctx, content, &singleItem)
|
||||
if newError != nil {
|
||||
return E.Errors(err, newError)
|
||||
newErr := json.UnmarshalContextDisallowUnknownFields(ctx, content, (*[]T)(l))
|
||||
if newErr == nil {
|
||||
return nil
|
||||
}
|
||||
*l = []T{singleItem}
|
||||
return nil
|
||||
return E.Errors(err, newErr)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue