Java Scriptlet double quote escaping problem
up vote
0
down vote
favorite
Server side returns String representing following object:
value = {"attrName": "attrValue is "some value""};
Client side/JSP:
Case 1:
JSON.parse('<%= value %>');
Case 2:
JSON.parse("<%= value %>");
In case 1
JSON parse error is generated because JavaScript looks like JSON.parse('{"attrName": "attrValue is "some value""}');
when value
gets inserted - JSON is trully invalid.
In case 2
syntax error is generated because JavaScript looks like JSON.parse("{"attrName": "attrValue is "some value""}");
- JSON.parse("{"a
messes everything up.
Problem: looking for explanation why Strings are inserted in a such way and for a possible global solution (when code changes are not needed for all occurences of JSON.parse
in the code)
Update 1:
Java file generated from case 1 JSP:
out.write("rnsomeStringsJSON.parse('");
out.print(value);
out.write('" ));rnsomeMoreStrings");
Java file generated from case 2 JSP:
out.write("rnsomeStringsJSON.parse(");
out.print(value);
out.write(" ));rnsomeMoreStrings");
java string jsp escaping scriptlet
add a comment |
up vote
0
down vote
favorite
Server side returns String representing following object:
value = {"attrName": "attrValue is "some value""};
Client side/JSP:
Case 1:
JSON.parse('<%= value %>');
Case 2:
JSON.parse("<%= value %>");
In case 1
JSON parse error is generated because JavaScript looks like JSON.parse('{"attrName": "attrValue is "some value""}');
when value
gets inserted - JSON is trully invalid.
In case 2
syntax error is generated because JavaScript looks like JSON.parse("{"attrName": "attrValue is "some value""}");
- JSON.parse("{"a
messes everything up.
Problem: looking for explanation why Strings are inserted in a such way and for a possible global solution (when code changes are not needed for all occurences of JSON.parse
in the code)
Update 1:
Java file generated from case 1 JSP:
out.write("rnsomeStringsJSON.parse('");
out.print(value);
out.write('" ));rnsomeMoreStrings");
Java file generated from case 2 JSP:
out.write("rnsomeStringsJSON.parse(");
out.print(value);
out.write(" ));rnsomeMoreStrings");
java string jsp escaping scriptlet
What string does your Java code actual return?value = { ...
isn't valid Java code, nor a valid Java string.
– Tim Biegeleisen
Nov 19 at 10:56
@TimBiegeleisen. Server side returns a valid JSON. How do I know: a) Google Gson is used to turn List into JSON b) everything works fine as long as double quotes are not introduced into attribute's value. I have writtenvalue = ...
to emphasize that double quotes are correctly escaped in generated JSON
– user435421
Nov 19 at 11:02
Your first case looks suspicious to me. What would those escaped double quotes suddenly become literals, and the literals also remain as literals?
– Tim Biegeleisen
Nov 19 at 11:05
@TimBiegeleisen if I understand you correctly then yes, that is exactly what is happening. That is the reason why I am having this issue. Problem is that when JSP engine writes"attrValue is \"some value\""
(using JspWriter.print) it (the engine) for some reason translates that String to"attrValue is "some value""
(case 1) instead of"attrValue is "some value""
(case 2)
– user435421
Nov 19 at 13:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Server side returns String representing following object:
value = {"attrName": "attrValue is "some value""};
Client side/JSP:
Case 1:
JSON.parse('<%= value %>');
Case 2:
JSON.parse("<%= value %>");
In case 1
JSON parse error is generated because JavaScript looks like JSON.parse('{"attrName": "attrValue is "some value""}');
when value
gets inserted - JSON is trully invalid.
In case 2
syntax error is generated because JavaScript looks like JSON.parse("{"attrName": "attrValue is "some value""}");
- JSON.parse("{"a
messes everything up.
Problem: looking for explanation why Strings are inserted in a such way and for a possible global solution (when code changes are not needed for all occurences of JSON.parse
in the code)
Update 1:
Java file generated from case 1 JSP:
out.write("rnsomeStringsJSON.parse('");
out.print(value);
out.write('" ));rnsomeMoreStrings");
Java file generated from case 2 JSP:
out.write("rnsomeStringsJSON.parse(");
out.print(value);
out.write(" ));rnsomeMoreStrings");
java string jsp escaping scriptlet
Server side returns String representing following object:
value = {"attrName": "attrValue is "some value""};
Client side/JSP:
Case 1:
JSON.parse('<%= value %>');
Case 2:
JSON.parse("<%= value %>");
In case 1
JSON parse error is generated because JavaScript looks like JSON.parse('{"attrName": "attrValue is "some value""}');
when value
gets inserted - JSON is trully invalid.
In case 2
syntax error is generated because JavaScript looks like JSON.parse("{"attrName": "attrValue is "some value""}");
- JSON.parse("{"a
messes everything up.
Problem: looking for explanation why Strings are inserted in a such way and for a possible global solution (when code changes are not needed for all occurences of JSON.parse
in the code)
Update 1:
Java file generated from case 1 JSP:
out.write("rnsomeStringsJSON.parse('");
out.print(value);
out.write('" ));rnsomeMoreStrings");
Java file generated from case 2 JSP:
out.write("rnsomeStringsJSON.parse(");
out.print(value);
out.write(" ));rnsomeMoreStrings");
java string jsp escaping scriptlet
java string jsp escaping scriptlet
edited Nov 19 at 13:09
asked Nov 19 at 10:54
user435421
188416
188416
What string does your Java code actual return?value = { ...
isn't valid Java code, nor a valid Java string.
– Tim Biegeleisen
Nov 19 at 10:56
@TimBiegeleisen. Server side returns a valid JSON. How do I know: a) Google Gson is used to turn List into JSON b) everything works fine as long as double quotes are not introduced into attribute's value. I have writtenvalue = ...
to emphasize that double quotes are correctly escaped in generated JSON
– user435421
Nov 19 at 11:02
Your first case looks suspicious to me. What would those escaped double quotes suddenly become literals, and the literals also remain as literals?
– Tim Biegeleisen
Nov 19 at 11:05
@TimBiegeleisen if I understand you correctly then yes, that is exactly what is happening. That is the reason why I am having this issue. Problem is that when JSP engine writes"attrValue is \"some value\""
(using JspWriter.print) it (the engine) for some reason translates that String to"attrValue is "some value""
(case 1) instead of"attrValue is "some value""
(case 2)
– user435421
Nov 19 at 13:04
add a comment |
What string does your Java code actual return?value = { ...
isn't valid Java code, nor a valid Java string.
– Tim Biegeleisen
Nov 19 at 10:56
@TimBiegeleisen. Server side returns a valid JSON. How do I know: a) Google Gson is used to turn List into JSON b) everything works fine as long as double quotes are not introduced into attribute's value. I have writtenvalue = ...
to emphasize that double quotes are correctly escaped in generated JSON
– user435421
Nov 19 at 11:02
Your first case looks suspicious to me. What would those escaped double quotes suddenly become literals, and the literals also remain as literals?
– Tim Biegeleisen
Nov 19 at 11:05
@TimBiegeleisen if I understand you correctly then yes, that is exactly what is happening. That is the reason why I am having this issue. Problem is that when JSP engine writes"attrValue is \"some value\""
(using JspWriter.print) it (the engine) for some reason translates that String to"attrValue is "some value""
(case 1) instead of"attrValue is "some value""
(case 2)
– user435421
Nov 19 at 13:04
What string does your Java code actual return?
value = { ...
isn't valid Java code, nor a valid Java string.– Tim Biegeleisen
Nov 19 at 10:56
What string does your Java code actual return?
value = { ...
isn't valid Java code, nor a valid Java string.– Tim Biegeleisen
Nov 19 at 10:56
@TimBiegeleisen. Server side returns a valid JSON. How do I know: a) Google Gson is used to turn List into JSON b) everything works fine as long as double quotes are not introduced into attribute's value. I have written
value = ...
to emphasize that double quotes are correctly escaped in generated JSON– user435421
Nov 19 at 11:02
@TimBiegeleisen. Server side returns a valid JSON. How do I know: a) Google Gson is used to turn List into JSON b) everything works fine as long as double quotes are not introduced into attribute's value. I have written
value = ...
to emphasize that double quotes are correctly escaped in generated JSON– user435421
Nov 19 at 11:02
Your first case looks suspicious to me. What would those escaped double quotes suddenly become literals, and the literals also remain as literals?
– Tim Biegeleisen
Nov 19 at 11:05
Your first case looks suspicious to me. What would those escaped double quotes suddenly become literals, and the literals also remain as literals?
– Tim Biegeleisen
Nov 19 at 11:05
@TimBiegeleisen if I understand you correctly then yes, that is exactly what is happening. That is the reason why I am having this issue. Problem is that when JSP engine writes
"attrValue is \"some value\""
(using JspWriter.print) it (the engine) for some reason translates that String to "attrValue is "some value""
(case 1) instead of "attrValue is "some value""
(case 2)– user435421
Nov 19 at 13:04
@TimBiegeleisen if I understand you correctly then yes, that is exactly what is happening. That is the reason why I am having this issue. Problem is that when JSP engine writes
"attrValue is \"some value\""
(using JspWriter.print) it (the engine) for some reason translates that String to "attrValue is "some value""
(case 1) instead of "attrValue is "some value""
(case 2)– user435421
Nov 19 at 13:04
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373085%2fjava-scriptlet-double-quote-escaping-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What string does your Java code actual return?
value = { ...
isn't valid Java code, nor a valid Java string.– Tim Biegeleisen
Nov 19 at 10:56
@TimBiegeleisen. Server side returns a valid JSON. How do I know: a) Google Gson is used to turn List into JSON b) everything works fine as long as double quotes are not introduced into attribute's value. I have written
value = ...
to emphasize that double quotes are correctly escaped in generated JSON– user435421
Nov 19 at 11:02
Your first case looks suspicious to me. What would those escaped double quotes suddenly become literals, and the literals also remain as literals?
– Tim Biegeleisen
Nov 19 at 11:05
@TimBiegeleisen if I understand you correctly then yes, that is exactly what is happening. That is the reason why I am having this issue. Problem is that when JSP engine writes
"attrValue is \"some value\""
(using JspWriter.print) it (the engine) for some reason translates that String to"attrValue is "some value""
(case 1) instead of"attrValue is "some value""
(case 2)– user435421
Nov 19 at 13:04