mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 16:41:30 +00:00
49 lines
967 B
Go
49 lines
967 B
Go
package badjson
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
"github.com/goccy/go-json"
|
|
)
|
|
|
|
type JSONArray[T any] []T
|
|
|
|
func (a JSONArray[T]) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal([]T(a))
|
|
}
|
|
|
|
func (a *JSONArray[T]) UnmarshalJSON(content []byte) error {
|
|
decoder := json.NewDecoder(bytes.NewReader(content))
|
|
arrayStart, err := decoder.Token()
|
|
if err != nil {
|
|
return err
|
|
} else if arrayStart != json.Delim('[') {
|
|
return E.New("excepted array start, but got ", arrayStart)
|
|
}
|
|
err = a.decodeJSON(decoder)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
arrayEnd, err := decoder.Token()
|
|
if err != nil {
|
|
return err
|
|
} else if arrayEnd != json.Delim(']') {
|
|
return E.New("excepted array end, but got ", arrayEnd)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *JSONArray[T]) decodeJSON(decoder *json.Decoder) error {
|
|
for decoder.More() {
|
|
var item T
|
|
err := decoder.Decode(&item)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*a = append(*a, item)
|
|
}
|
|
return nil
|
|
}
|