All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.javalin.http.util.JsonEscapeUtil.kt Maven / Gradle / Ivy

There is a newer version: 6.3.0
Show newest version
/*
 * Javalin - https://javalin.io
 * Copyright 2017 David Åse
 * Licensed under Apache 2.0: https://github.com/tipsy/javalin/blob/master/LICENSE
 */

package io.javalin.http.util

object JsonEscapeUtil {
    fun escape(str: String): String {
        val builder = StringBuilder(str.length)
        for (ch in str) {
            builder.append(
                when (ch) {
                    '\"' -> "\\\""
                    '\n' -> "\\n"
                    '\r' -> "\\r"
                    '\\' -> "\\\\"
                    '\t' -> "\\t"
                    '\b' -> "\\b"
                    else -> ch
                }
            )
        }
        return builder.toString()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy