Does anyone know of a library function to escape and surround a string value of an object with quotes unless it is null?
For example, the function should process:
"hello"
as"\"hello\""
null
as"null"
Long.valueOf(2L)
as"\"2\""
""
as"\"\""
"I\'m \"quoted\""
as"\"I\\\'m \\\"quoted\\\""
Clearly this is trivial to implement in Java, but I'm looking for a function in an existing library (e.g. JDK, Commons Lang, Spring ...) I'd be surprised if this hasn't been done before.
For reference, this is for converting objects to JavaScript strings.