digitalmars.D.learn - Line breaks in JSON
- bachmeier (25/25) May 10 2018 I'm using std.json for the first time. I want to download the
 - rikki cattermole (12/40) May 10 2018 You'll need to unescape them (which is pretty easy, a simple replacement...
 
I'm using std.json for the first time. I want to download the 
contents of a markdown file from a web server. When I do that, 
the line breaks are escaped, which I don't want. Here's an 
example:
import std.conv, std.json, std.stdio;
void main() {
	string data =
"This is a paragraph
with line breaks
that shouldn't appear
when the file is loaded
in a text editor.";
   string msg = JSONValue(["markdown": data]).toString;
   string msg2 = parseJSON(msg)["markdown"].to!string;
   writeln(data);
   writeln(msg2);
}
Output:
This is a paragraph
with line breaks
that shouldn't appear
when the file is loaded
in a text editor.
"This is a paragraph\nwith line breaks\nthat shouldn't 
appear\nwhen the file is loaded\nin a text editor."
 May 10 2018
On 11/05/2018 2:56 AM, bachmeier wrote:
 I'm using std.json for the first time. I want to download the contents 
 of a markdown file from a web server. When I do that, the line breaks 
 are escaped, which I don't want. Here's an example:
 
 import std.conv, std.json, std.stdio;
 
 void main() {
      string data =
 "This is a paragraph
 with line breaks
 that shouldn't appear
 when the file is loaded
 in a text editor.";
    string msg = JSONValue(["markdown": data]).toString;
    string msg2 = parseJSON(msg)["markdown"].to!string;
    writeln(data);
    writeln(msg2);
 }
 
 Output:
 
 This is a paragraph
 with line breaks
 that shouldn't appear
 when the file is loaded
 in a text editor.
 "This is a paragraph\nwith line breaks\nthat shouldn't appear\nwhen the 
 file is loaded\nin a text editor."
You'll need to unescape them (which is pretty easy, a simple replacement 
here).
For reference, this is invalid json[0]:
```
{
	"1
	2
	3 "
}
```
[0] https://jsonlint.com/
 May 10 2018
On Thursday, 10 May 2018 at 15:01:57 UTC, rikki cattermole wrote:
 You'll need to unescape them (which is pretty easy, a simple 
 replacement here).
 For reference, this is invalid json[0]:
 ```
 {
 	"1
 	2
 	3 "
 }
 ```
 [0] https://jsonlint.com/
So I see the answer is that I don't understand json. A search 
reveals that this is a common mistake.
 May 10 2018
On Thursday, 10 May 2018 at 15:01:57 UTC, rikki cattermole wrote:
 [snip]
 You'll need to unescape them (which is pretty easy, a simple 
 replacement here).
 For reference, this is invalid json[0]:
 ```
 {
 	"1
 	2
 	3 "
 }
 ```
 [0] https://jsonlint.com/
I don't see an unescape function in phobos and below doesn't seem 
to work
string msg2 = parseJSON(msg)["markdown"].to!string.replace("\n", 
" ");
 May 10 2018
On Thursday, 10 May 2018 at 17:59:26 UTC, jmh530 wrote:On Thursday, 10 May 2018 at 15:01:57 UTC, rikki cattermole wrote:I used replace("\\n", "\n")[snip] You'll need to unescape them (which is pretty easy, a simple replacement here). For reference, this is invalid json[0]: ``` { "1 2 3 " } ``` [0] https://jsonlint.com/I don't see an unescape function in phobos and below doesn't seem to work string msg2 = parseJSON(msg)["markdown"].to!string.replace("\n", " ");
 May 10 2018
On Thursday, 10 May 2018 at 18:21:17 UTC, bachmeier wrote:
 [snip]
 I used replace("\\n", "\n")
Ah, I always forget the extra \.
 May 10 2018








 
 
 
 bachmeier <no spam.net> 