-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35e7014
commit f75e37a
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package json | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
|
||
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 nil, E.Extend(syntaxError, "row ", row, ", column ", column) | ||
} | ||
return nil, err | ||
} |