mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-04 20:37:40 +03:00
25 lines
696 B
Go
25 lines
696 B
Go
package json
|
|
|
|
import (
|
|
"bytes"
|
|
"strings"
|
|
|
|
"github.com/sagernet/sing/common"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
)
|
|
|
|
func UnmarshalExtended[T any](content []byte) (T, error) {
|
|
decoder := NewDecoder(NewCommentFilter(bytes.NewReader(content)))
|
|
var value T
|
|
err := decoder.Decode(&value)
|
|
if err == nil {
|
|
return value, err
|
|
}
|
|
if syntaxError, isSyntaxError := err.(*SyntaxError); isSyntaxError {
|
|
prefix := string(content[:syntaxError.Offset])
|
|
row := strings.Count(prefix, "\n") + 1
|
|
column := len(prefix) - strings.LastIndex(prefix, "\n") - 1
|
|
return common.DefaultValue[T](), E.Extend(syntaxError, "row ", row, ", column ", column)
|
|
}
|
|
return common.DefaultValue[T](), err
|
|
}
|