-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser.go
More file actions
80 lines (68 loc) · 2.05 KB
/
Copy pathjson_parser.go
File metadata and controls
80 lines (68 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package clicky
import (
"encoding/json"
"strings"
)
// JSONParser handles lenient JSON parsing
type JSONParser struct{}
// NewJSONParser creates a new JSON parser
func NewJSONParser() *JSONParser {
return &JSONParser{}
}
// ParseJSON parses JSON in a lenient way, allowing for various formats
func ParseJSON(data []byte) (interface{}, error) {
var result interface{}
// First try standard JSON parsing
if err := json.Unmarshal(data, &result); err == nil {
return result, nil
}
// Try parsing as string (in case it's quoted JSON)
var str string
if err := json.Unmarshal(data, &str); err == nil {
// Try parsing the string as JSON
if err := json.Unmarshal([]byte(str), &result); err == nil {
return result, nil
}
// Return as string if it's not parseable JSON
return str, nil
}
// Try parsing with relaxed rules (remove comments, trailing commas, etc.)
cleaned := cleanJSONString(string(data))
if err := json.Unmarshal([]byte(cleaned), &result); err == nil {
return result, nil
}
// As last resort, return the original string
return string(data), nil
}
// Parse is an alias for ParseJSON for consistency with other parsers
func (p *JSONParser) Parse(data []byte) (interface{}, error) {
return ParseJSON(data)
}
// cleanJSONString attempts to clean JSON string for parsing
func cleanJSONString(s string) string {
// Remove single-line comments
lines := strings.Split(s, "\n")
var cleaned []string
for _, line := range lines {
// Remove // comments
if idx := strings.Index(line, "//"); idx != -1 {
line = line[:idx]
}
// Remove /* */ comments (simple implementation)
if start := strings.Index(line, "/*"); start != -1 {
if end := strings.Index(line[start:], "*/"); end != -1 {
line = line[:start] + line[start+end+2:]
}
}
// Remove trailing commas before } or ]
line = strings.TrimSpace(line)
if strings.HasSuffix(line, ",}") {
line = line[:len(line)-2] + "}"
}
if strings.HasSuffix(line, ",]") {
line = line[:len(line)-2] + "]"
}
cleaned = append(cleaned, line)
}
return strings.Join(cleaned, "\n")
}