Jsonconvert serializeobject not escaping single quote Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to escape braces (curly brackets) in a format string in .NETCan I escape a double quote in a verbatim string literal?Escape curly brace '{' in String.FormatHow to escape double quotes in JSONJSON.Parse,'Uncaught SyntaxError: Unexpected token oParsing json response with escape / special charactersEscaping Characters When Building JSON in PHP and Echoing It in <script>How to convert to json specific class c# ?I have errorCBOR serialization (string escaping)Can't parse a JSON string serialized by .NET Serializer
Resize vertical bars (absolute-value symbols)
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
Where is the Next Backup Size entry on iOS 12?
Mounting TV on a weird wall that has some material between the drywall and stud
How can I save and copy a screenhot at the same time?
RSA find public exponent
What is the difference between a "ranged attack" and a "ranged weapon attack"?
What initially awakened the Balrog?
How many time has Arya actually used Needle?
Why are vacuum tubes still used in amateur radios?
Is openssl rand command cryptographically secure?
What is the "studentd" process?
A proverb that is used to imply that you have unexpectedly faced a big problem
Is CEO the "profession" with the most psychopaths?
Why complex landing gears are used instead of simple,reliability and light weight muscle wire or shape memory alloys?
Asymptotics question
Relating to the President and obstruction, were Mueller's conclusions preordained?
Putting class ranking in CV, but against dept guidelines
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
After Sam didn't return home in the end, were he and Al still friends?
Did any compiler fully use 80-bit floating point?
Why weren't discrete x86 CPUs ever used in game hardware?
What is the origin of 落第?
The Nth Gryphon Number
Jsonconvert serializeobject not escaping single quote
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to escape braces (curly brackets) in a format string in .NETCan I escape a double quote in a verbatim string literal?Escape curly brace '{' in String.FormatHow to escape double quotes in JSONJSON.Parse,'Uncaught SyntaxError: Unexpected token oParsing json response with escape / special charactersEscaping Characters When Building JSON in PHP and Echoing It in <script>How to convert to json specific class c# ?I have errorCBOR serialization (string escaping)Can't parse a JSON string serialized by .NET Serializer
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
C#, I have an Automobile class and in that class i have a vehicleTrim field.
I use JsonConvert.SerializeObject
to serialize that class and it is not escaping the single quote.
This is causing an issue when i try to set the value of an object in the web via window.localStorage.setItem
function.
example:
public class Automobile
public string vehicleTrim get; set;
var test = new Automobile()
vehicleTrim = "designer's package"
;
var serialized = JsonConvert.SerializeObject(test, Formatting.None);
// serialized output: "vehicleTrim":"designer's package"
// expected output : "vehicleTrim":"designer's package"
so now i want to set this json object to the localstorage of my web by calling this
var jsSetScript = $"window.localStorage.setItem('automobile', 'serialized');";
await Control.EvaluateJavascriptAsync(jsSetScript);
EvaluateJavascriptAsync returns this error trying to read the json SyntaxError: Unexpected identifier 's'. Expected ')' to end an argument list.
I manaully tried this with the escaped single quote and it was fine. So the question is how can i make serializedobject method escape the single quote?
c# json serialization
add a comment |
C#, I have an Automobile class and in that class i have a vehicleTrim field.
I use JsonConvert.SerializeObject
to serialize that class and it is not escaping the single quote.
This is causing an issue when i try to set the value of an object in the web via window.localStorage.setItem
function.
example:
public class Automobile
public string vehicleTrim get; set;
var test = new Automobile()
vehicleTrim = "designer's package"
;
var serialized = JsonConvert.SerializeObject(test, Formatting.None);
// serialized output: "vehicleTrim":"designer's package"
// expected output : "vehicleTrim":"designer's package"
so now i want to set this json object to the localstorage of my web by calling this
var jsSetScript = $"window.localStorage.setItem('automobile', 'serialized');";
await Control.EvaluateJavascriptAsync(jsSetScript);
EvaluateJavascriptAsync returns this error trying to read the json SyntaxError: Unexpected identifier 's'. Expected ')' to end an argument list.
I manaully tried this with the escaped single quote and it was fine. So the question is how can i make serializedobject method escape the single quote?
c# json serialization
2
The single quote character in a string is valid JSON, soJsonConvert
has no need or reason to escape it. If you are going to be embedding it into aneval
string the way you are doing, you're going to have to manually escape it yourself after serialization. (i.e.serialized.Replace("'", "\'");
)
– Abion47
Mar 8 at 22:01
hmm i c so the way i embed it inside the eval is sketchy. Guess ill have to manaully escape it. Thanks!
– Vibol
Mar 8 at 22:15
add a comment |
C#, I have an Automobile class and in that class i have a vehicleTrim field.
I use JsonConvert.SerializeObject
to serialize that class and it is not escaping the single quote.
This is causing an issue when i try to set the value of an object in the web via window.localStorage.setItem
function.
example:
public class Automobile
public string vehicleTrim get; set;
var test = new Automobile()
vehicleTrim = "designer's package"
;
var serialized = JsonConvert.SerializeObject(test, Formatting.None);
// serialized output: "vehicleTrim":"designer's package"
// expected output : "vehicleTrim":"designer's package"
so now i want to set this json object to the localstorage of my web by calling this
var jsSetScript = $"window.localStorage.setItem('automobile', 'serialized');";
await Control.EvaluateJavascriptAsync(jsSetScript);
EvaluateJavascriptAsync returns this error trying to read the json SyntaxError: Unexpected identifier 's'. Expected ')' to end an argument list.
I manaully tried this with the escaped single quote and it was fine. So the question is how can i make serializedobject method escape the single quote?
c# json serialization
C#, I have an Automobile class and in that class i have a vehicleTrim field.
I use JsonConvert.SerializeObject
to serialize that class and it is not escaping the single quote.
This is causing an issue when i try to set the value of an object in the web via window.localStorage.setItem
function.
example:
public class Automobile
public string vehicleTrim get; set;
var test = new Automobile()
vehicleTrim = "designer's package"
;
var serialized = JsonConvert.SerializeObject(test, Formatting.None);
// serialized output: "vehicleTrim":"designer's package"
// expected output : "vehicleTrim":"designer's package"
so now i want to set this json object to the localstorage of my web by calling this
var jsSetScript = $"window.localStorage.setItem('automobile', 'serialized');";
await Control.EvaluateJavascriptAsync(jsSetScript);
EvaluateJavascriptAsync returns this error trying to read the json SyntaxError: Unexpected identifier 's'. Expected ')' to end an argument list.
I manaully tried this with the escaped single quote and it was fine. So the question is how can i make serializedobject method escape the single quote?
c# json serialization
c# json serialization
asked Mar 8 at 21:58
VibolVibol
171113
171113
2
The single quote character in a string is valid JSON, soJsonConvert
has no need or reason to escape it. If you are going to be embedding it into aneval
string the way you are doing, you're going to have to manually escape it yourself after serialization. (i.e.serialized.Replace("'", "\'");
)
– Abion47
Mar 8 at 22:01
hmm i c so the way i embed it inside the eval is sketchy. Guess ill have to manaully escape it. Thanks!
– Vibol
Mar 8 at 22:15
add a comment |
2
The single quote character in a string is valid JSON, soJsonConvert
has no need or reason to escape it. If you are going to be embedding it into aneval
string the way you are doing, you're going to have to manually escape it yourself after serialization. (i.e.serialized.Replace("'", "\'");
)
– Abion47
Mar 8 at 22:01
hmm i c so the way i embed it inside the eval is sketchy. Guess ill have to manaully escape it. Thanks!
– Vibol
Mar 8 at 22:15
2
2
The single quote character in a string is valid JSON, so
JsonConvert
has no need or reason to escape it. If you are going to be embedding it into an eval
string the way you are doing, you're going to have to manually escape it yourself after serialization. (i.e. serialized.Replace("'", "\'");
)– Abion47
Mar 8 at 22:01
The single quote character in a string is valid JSON, so
JsonConvert
has no need or reason to escape it. If you are going to be embedding it into an eval
string the way you are doing, you're going to have to manually escape it yourself after serialization. (i.e. serialized.Replace("'", "\'");
)– Abion47
Mar 8 at 22:01
hmm i c so the way i embed it inside the eval is sketchy. Guess ill have to manaully escape it. Thanks!
– Vibol
Mar 8 at 22:15
hmm i c so the way i embed it inside the eval is sketchy. Guess ill have to manaully escape it. Thanks!
– Vibol
Mar 8 at 22:15
add a comment |
1 Answer
1
active
oldest
votes
"'"
is not even a valid JSON string literal. From the JSON spec:
Thus '
does not need to be escaped, but if it is, it must appear as "u0027"
. Only the 8 listed characters have a special, abbreviated escaping syntax. (For further details see RFC 8259.)
If "u0027"
meets your needs, then setting JsonSerializerSettings.StringEscapeHandling
to StringEscapeHandling.EscapeHtml
should do the trick. From the docs:
StringEscapeHandling Enumeration
Specifies how strings are escaped when writing JSON text.
Default 0 Only control characters (e.g. newline) are escaped.
EscapeNonAscii 1 All non-ASCII and control characters (e.g. newline) are escaped.
EscapeHtml 2 HTML (<, >, &, ', ") and control characters (e.g. newline) are escaped.
Thus the following now succeeds:
var settings = new JsonSerializerSettings
StringEscapeHandling = StringEscapeHandling.EscapeHtml,
;
var serialized = JsonConvert.SerializeObject(test, Formatting.None, settings);
Console.WriteLine(serialized);
// Outputs "vehicleTrim":"designeru0027s package"
Assert.IsTrue(!serialized.Contains('''));
// Succeeds
Demo fiddle here.
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f55071571%2fjsonconvert-serializeobject-not-escaping-single-quote%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
"'"
is not even a valid JSON string literal. From the JSON spec:
Thus '
does not need to be escaped, but if it is, it must appear as "u0027"
. Only the 8 listed characters have a special, abbreviated escaping syntax. (For further details see RFC 8259.)
If "u0027"
meets your needs, then setting JsonSerializerSettings.StringEscapeHandling
to StringEscapeHandling.EscapeHtml
should do the trick. From the docs:
StringEscapeHandling Enumeration
Specifies how strings are escaped when writing JSON text.
Default 0 Only control characters (e.g. newline) are escaped.
EscapeNonAscii 1 All non-ASCII and control characters (e.g. newline) are escaped.
EscapeHtml 2 HTML (<, >, &, ', ") and control characters (e.g. newline) are escaped.
Thus the following now succeeds:
var settings = new JsonSerializerSettings
StringEscapeHandling = StringEscapeHandling.EscapeHtml,
;
var serialized = JsonConvert.SerializeObject(test, Formatting.None, settings);
Console.WriteLine(serialized);
// Outputs "vehicleTrim":"designeru0027s package"
Assert.IsTrue(!serialized.Contains('''));
// Succeeds
Demo fiddle here.
add a comment |
"'"
is not even a valid JSON string literal. From the JSON spec:
Thus '
does not need to be escaped, but if it is, it must appear as "u0027"
. Only the 8 listed characters have a special, abbreviated escaping syntax. (For further details see RFC 8259.)
If "u0027"
meets your needs, then setting JsonSerializerSettings.StringEscapeHandling
to StringEscapeHandling.EscapeHtml
should do the trick. From the docs:
StringEscapeHandling Enumeration
Specifies how strings are escaped when writing JSON text.
Default 0 Only control characters (e.g. newline) are escaped.
EscapeNonAscii 1 All non-ASCII and control characters (e.g. newline) are escaped.
EscapeHtml 2 HTML (<, >, &, ', ") and control characters (e.g. newline) are escaped.
Thus the following now succeeds:
var settings = new JsonSerializerSettings
StringEscapeHandling = StringEscapeHandling.EscapeHtml,
;
var serialized = JsonConvert.SerializeObject(test, Formatting.None, settings);
Console.WriteLine(serialized);
// Outputs "vehicleTrim":"designeru0027s package"
Assert.IsTrue(!serialized.Contains('''));
// Succeeds
Demo fiddle here.
add a comment |
"'"
is not even a valid JSON string literal. From the JSON spec:
Thus '
does not need to be escaped, but if it is, it must appear as "u0027"
. Only the 8 listed characters have a special, abbreviated escaping syntax. (For further details see RFC 8259.)
If "u0027"
meets your needs, then setting JsonSerializerSettings.StringEscapeHandling
to StringEscapeHandling.EscapeHtml
should do the trick. From the docs:
StringEscapeHandling Enumeration
Specifies how strings are escaped when writing JSON text.
Default 0 Only control characters (e.g. newline) are escaped.
EscapeNonAscii 1 All non-ASCII and control characters (e.g. newline) are escaped.
EscapeHtml 2 HTML (<, >, &, ', ") and control characters (e.g. newline) are escaped.
Thus the following now succeeds:
var settings = new JsonSerializerSettings
StringEscapeHandling = StringEscapeHandling.EscapeHtml,
;
var serialized = JsonConvert.SerializeObject(test, Formatting.None, settings);
Console.WriteLine(serialized);
// Outputs "vehicleTrim":"designeru0027s package"
Assert.IsTrue(!serialized.Contains('''));
// Succeeds
Demo fiddle here.
"'"
is not even a valid JSON string literal. From the JSON spec:
Thus '
does not need to be escaped, but if it is, it must appear as "u0027"
. Only the 8 listed characters have a special, abbreviated escaping syntax. (For further details see RFC 8259.)
If "u0027"
meets your needs, then setting JsonSerializerSettings.StringEscapeHandling
to StringEscapeHandling.EscapeHtml
should do the trick. From the docs:
StringEscapeHandling Enumeration
Specifies how strings are escaped when writing JSON text.
Default 0 Only control characters (e.g. newline) are escaped.
EscapeNonAscii 1 All non-ASCII and control characters (e.g. newline) are escaped.
EscapeHtml 2 HTML (<, >, &, ', ") and control characters (e.g. newline) are escaped.
Thus the following now succeeds:
var settings = new JsonSerializerSettings
StringEscapeHandling = StringEscapeHandling.EscapeHtml,
;
var serialized = JsonConvert.SerializeObject(test, Formatting.None, settings);
Console.WriteLine(serialized);
// Outputs "vehicleTrim":"designeru0027s package"
Assert.IsTrue(!serialized.Contains('''));
// Succeeds
Demo fiddle here.
answered Mar 8 at 23:16
dbcdbc
56.1k878131
56.1k878131
add a comment |
add a comment |
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.
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%2f55071571%2fjsonconvert-serializeobject-not-escaping-single-quote%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
2
The single quote character in a string is valid JSON, so
JsonConvert
has no need or reason to escape it. If you are going to be embedding it into aneval
string the way you are doing, you're going to have to manually escape it yourself after serialization. (i.e.serialized.Replace("'", "\'");
)– Abion47
Mar 8 at 22:01
hmm i c so the way i embed it inside the eval is sketchy. Guess ill have to manaully escape it. Thanks!
– Vibol
Mar 8 at 22:15