简体   繁体   中英

VelocityEngine removes escape characters from string

When I call:

velocityEngine.evaluate(velocityContext, writer, "", template);

with parameter.querystring = {"hi": "\\"hello\\""}

after evaluation writer contains {"hi": ""hello""}

Question:

  • Why does Velocity remove escaping characters?
  • How can I prevent removal of escaping characters?

NOTE: I don't know VelocityEngine, so take this with a grain of salt.

Why does Velocity remove escaping characters?

Most likely VelocityEngine reads the values in parameter.querystring JSON as individual String. So the String "\\"hello\\"" is read as the text "hello" . Then later in the writer, it simply pastes this String back as is.

Personally I see this as a flaw in the source code of the writer, which should have used something along the lines of Pattern.quote(String) to escape all characters before inserting it.

How can I prevent removal of escaping characters?

An additional escaped \\ can be added manually as a work-around for the behavior of the writer. So your parameter.querystring would become:

{"hi": "\\\"hello\\\""}

Basically the text \\"hello\\" , where both the \\ and " characters are escaped with an additional \\ to put it in the java String.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM