protected fun fixJsonString()

in prompt/prompt-processor/src/commonMain/kotlin/ai/koog/prompt/processor/ToolJsonFixProcessor.kt [81:114]


    protected fun fixJsonString(jsonValue: String): String {
        // Remove the surrounding quotes to work with the content
        val content = if (jsonValue.startsWith('"') && jsonValue.endsWith('"')) {
            jsonValue.drop(1).dropLast(1)
        } else {
            jsonValue
        }

        val correctString = StringBuilder()
        var i = 0
        while (i < content.length) {
            if (content[i] == '\\' && i + 1 < content.length) {
                when (content[i + 1]) {
                    '"' -> '"'
                    '\\' -> '\\'
                    '/' -> '/'
                    'b' -> '\b'
                    'n' -> '\n'
                    'r' -> '\r'
                    't' -> '\t'
                    else -> null
                }?.let {
                    correctString.append(it)
                    i++
                }
            } else {
                // Regular character
                correctString.append(content[i])
            }
            i++
        }

        return correctString.toString()
    }