Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON repair utility for robust parsing #865

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
21 changes: 10 additions & 11 deletions neo/assistant/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down