Add JSON repair utility for robust parsing

- Integrate jsonrepair library to improve JSON parsing resilience
- Replace manual JSON repair attempts with a more comprehensive repair method
- Update go.mod and go.sum to include jsonrepair dependency
- Simplify ParseJSON function with a single repair approach
This commit is contained in:
Max 2025-02-11 17:00:22 +08:00
parent 8bc7f6ddb1
commit 2eaee671da
3 changed files with 13 additions and 11 deletions

1
go.mod
View file

@ -26,6 +26,7 @@ require (
github.com/spf13/cast v1.7.1
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.10.0
github.com/watchfultele/jsonrepair v0.0.0-20250207052432-e4397ed42611
github.com/xuri/excelize/v2 v2.9.0
github.com/yaoapp/gou v0.10.3
github.com/yaoapp/kun v0.9.0

2
go.sum
View file

@ -272,6 +272,8 @@ github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/watchfultele/jsonrepair v0.0.0-20250207052432-e4397ed42611 h1:CWCIUJ4cqhc3ct+HbV4EJtBdRFjUVOBne8mmIHXxU6A=
github.com/watchfultele/jsonrepair v0.0.0-20250207052432-e4397ed42611/go.mod h1:63urp6bG6c9cw3DuFtNEr5g9tbjFXaPTn0sBiBDVB7g=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=

View file

@ -5,10 +5,10 @@ import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/watchfultele/jsonrepair"
)
func getTimestamp(v interface{}) (int64, error) {
@ -56,10 +56,6 @@ func stringHash(v string) string {
}
// ParseJSON attempts to parse a potentially malformed JSON string
// It tries different approaches:
// 1. Parse as-is
// 2. Add a missing closing brace
// 3. Remove an extra closing brace
func ParseJSON(jsonStr string, v interface{}) error {
// Try parsing as-is first
err := jsoniter.UnmarshalFromString(jsonStr, v)
@ -73,12 +69,15 @@ func ParseJSON(jsonStr string, v interface{}) error {
return nil
}
// Try removing last closing brace if it exists
if strings.HasSuffix(jsonStr, "}") {
trimmed := strings.TrimSuffix(jsonStr, "}")
if err := jsoniter.UnmarshalFromString(trimmed, v); err == nil {
return nil
}
// Try repairing the JSON
repaired, err := jsonrepair.JSONRepair(jsonStr)
if err != nil {
return originalErr
}
// Try parsing the repaired JSON
if err := jsoniter.UnmarshalFromString(repaired, v); err == nil {
return nil
}
// If all attempts fail, return the original error