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");









share|improve this question
























  • 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

















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");









share|improve this question
























  • 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















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");









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




















  • 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


















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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy