简体   繁体   中英

Unremovable Line Breaks in JavaScript string

I have a piece of code that serializes a JSON object into a string in an MVC C# controller. I am then required to set the value of a JavaScript var on the front end with this serialized JSON.

When I try to deserialize it back to a JSON object, I noticed that JSON.parse keeps breaking. Upon further inspection, the string itself seems to contain newline characters (eg \\n, \\r). However, if I attempt to remove it, the code says that the new lines do not exist.

Here's a bunch of things I've tried:

  • console.log(object.toString().indexOf("\\r")); = this returns -1. Same for \\n, \\r\\n, \\t, and \\f
  • console.log(object.toString().indexOf("\\\\r")); = same as above, with double backslashes in case it was somehow missed.
  • console.log(object.replace(/[\\t\\r\\n\\f]/gm, "")) ; = string still outputs with the same string, with all the random linebreaks included. Please correct me if my regex is wrong.
  • running this string through a site that shows hidden characters in string ( http://show-hidden-characters.dostring.com/ ) = it shows the string has End of Line (LF) characters (eg "dob":"1970-08-[End of Line(LF)])
  • replacing \\r\\n using Notepad++ = works, had 48 occurrences replaced in the string. Weird that it does not work on code though.
  • running this string through a JSON lint website ( https://jsonlint.com/ ) = shows invalid JSON, at points where there are line breaks

The code that I am using to serialize the JSON is:

converter.Options.HttpPostParameters.Add("object", JsonConvert.SerializeObject(object).Replace("\\r\\n", "").Replace("\\n", "").Replace("\\r", ""))

And I am saving it into the front end via a script tag on the Razor page like this:

<script>
    var object = "@HttpContext.Current.Request.Form["object"]";
</script>

I'm at my wit's end regarding this issue. Clearly there is a linebreak in the var, but I am just unable to detect or remove it using code. The original string in the C# controller also has these characters removed using json.Replace("\\r\\n", "").Replace("\\n", "").Replace("\\r", "") yet somehow it keeps getting added back to the same points in the JavaScript var.

Another point of frustration is that it does not seem to be consistent where these random line breaks are being added. One breaks up a date of birth string, others simply applies a newline before certain keys or values in the entire JSON object. Does anyone have any suggestions?

I'm not sure if the default has changed over time, but the current default for JSON.NET is no formatting . You can manually change this/set this if it's not the default in your version:

var noFormatting = new JsonSerializerSettings();
noFormatting.Formatting = Formatting.None;


var test = new {
    Name = "Test\r\nName",
    Value = "Test2"
};

Console.WriteLine(JsonConvert.SerializeObject(test, noFormatting));

Results in:

{"Name":"Test\r\nName","Value":"Test2"}

Or as you might see it in the debugger:

string value = "{\"Name\":\"Test\\r\\nName\",\"Value\":\"Test2\"}";

Note that there is some type caching functionality in JsonSerializerSettings , so it can be faster if you use a common instance (ie don't define it in a loop, or define it as a field in a class where it's used a lot).

Try it online

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