Line breaks in JSON

rikki cattermole rikki at cattermole.co.nz
Thu May 10 15:01:57 UTC 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/


More information about the Digitalmars-d-learn mailing list