2022-07-03 11:43:27 +00:00
|
|
|
package badjson
|
|
|
|
|
|
|
|
import (
|
2022-07-24 09:58:52 +00:00
|
|
|
"github.com/sagernet/sing-box/common/json"
|
2022-07-03 11:43:27 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
)
|
|
|
|
|
|
|
|
func decodeJSON(decoder *json.Decoder) (any, error) {
|
|
|
|
rawToken, err := decoder.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch token := rawToken.(type) {
|
|
|
|
case json.Delim:
|
|
|
|
switch token {
|
|
|
|
case '{':
|
|
|
|
var object JSONObject
|
|
|
|
err = object.decodeJSON(decoder)
|
2022-07-18 10:50:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rawToken, err = decoder.Token()
|
2022-07-03 11:43:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if rawToken != json.Delim('}') {
|
|
|
|
return nil, E.New("excepted object end, but got ", rawToken)
|
|
|
|
}
|
2022-07-18 10:50:19 +00:00
|
|
|
return &object, nil
|
2022-07-03 11:43:27 +00:00
|
|
|
case '[':
|
|
|
|
var array JSONArray[any]
|
|
|
|
err = array.decodeJSON(decoder)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rawToken, err = decoder.Token()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if rawToken != json.Delim(']') {
|
|
|
|
return nil, E.New("excepted array end, but got ", rawToken)
|
|
|
|
}
|
2022-07-18 10:50:19 +00:00
|
|
|
return array, nil
|
2022-07-03 11:43:27 +00:00
|
|
|
default:
|
|
|
|
return nil, E.New("excepted object or array end: ", token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rawToken, nil
|
|
|
|
}
|