Skip to content
Open
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
28 changes: 14 additions & 14 deletions src/main/kotlin/Util/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,19 @@ inline fun <reified T> templateBasedReconstruction(text: String): T?
}


// Pre-compiled patterns for extracting key-value pairs from malformed JSON
private val jsonExtractionPatterns = listOf(
// "key": "value" or 'key': 'value'
Regex("\"([^\"]+)\"\\s*:\\s*\"([^\"]*(?:\\\\.[^\"]*)*)\""),
Regex("'([^']+)'\\s*:\\s*'([^']*(?:\\\\.[^']*)*)'"),
// "key": value (unquoted values)
Regex("\"([^\"]+)\"\\s*:\\s*([^,}\\]\\n]+?)(?=[,}\\]\\n]|$)"),
// key: "value" (unquoted keys)
Regex("([a-zA-Z_][\\w\\s-]*)\\s*:\\s*\"([^\"]*(?:\\\\.[^\"]*)*)\""),
// key: value (both unquoted)
Regex("([a-zA-Z_][\\w\\s-]*)\\s*:\\s*([^,}\\]\\n]+?)(?=[,}\\]\\n]|$)")
)

/**
* Extracts key-value pairs from malformed JSON using regex patterns as fallback.
* Returns a Map that can be manually processed when standard JSON parsing fails.
Expand All @@ -477,20 +490,7 @@ fun extractJsonData(malformedJson: String): Map<String, String>
{
val result = mutableMapOf<String, String>()

// More comprehensive pattern for key-value pairs
val patterns = listOf(
// "key": "value" or 'key': 'value'
Regex("\"([^\"]+)\"\\s*:\\s*\"([^\"]*(?:\\\\.[^\"]*)*)\""),
Regex("'([^']+)'\\s*:\\s*'([^']*(?:\\\\.[^']*)*)'"),
// "key": value (unquoted values)
Regex("\"([^\"]+)\"\\s*:\\s*([^,}\\]\\n]+?)(?=[,}\\]\\n]|$)"),
// key: "value" (unquoted keys)
Regex("([a-zA-Z_][\\w\\s-]*)\\s*:\\s*\"([^\"]*(?:\\\\.[^\"]*)*)\""),
// key: value (both unquoted)
Regex("([a-zA-Z_][\\w\\s-]*)\\s*:\\s*([^,}\\]\\n]+?)(?=[,}\\]\\n]|$)")
)

for(pattern in patterns)
for(pattern in jsonExtractionPatterns)
{
pattern.findAll(malformedJson).forEach { match ->
val key = match.groupValues[1].trim()
Expand Down