Generating newlines instead of CRLFs in Json.Net

For my unix/java friends I would like to send newlines ('\n') instead of a CRLF ( '\r\n') in Json.Net. I tried setting a StreamWriter to use a newline without any success.

I think Json.Net code is using Environment.NewLine instead of calling TextWriter.WriteNewLine(). Changing Environment.NewLine is not an option because I'm running as a server and the newline encoding is based on the request.

Is there some other way to force newline for crlf?

Here's my code -

using (var streamWriter = new StreamWriter(writeStream, new UTF8Encoding(false))
{
     NewLine = "\n"
})
using (var jsonWriter = new JsonTextWriter(streamWriter) 
{ 
     CloseOutput = true, 
     Indentation = 2, 
     Formatting = Formatting.Indented 
})
{
       // serialise object to JSON
}

After delving into the Json.Net code, I see the issue is with JsonTextWriter.WriteIndent, thanks Athari.

Instead of _writer.Write(Environment.NewLine); it should be _writer.WriteLine();.

I've posted a pull request to github. https://github.com/JamesNK/Newtonsoft.Json/pull/271

CRLFs and newlines · Issue #270 · JamesNK/Newtonsoft.Json , For my unix/java friends I would like to send newlines ('\n') instead of a CRLF ( '\r\​n'). together to host and review code, manage projects, and build software together. Net code, I see the issue is with JsonTextWriter. For my unix/java friends I would like to send newlines (' ') instead of a CRLF ( '\r '). I tried setting a StreamWriter to use a newline without any success. I think your code is using Environment.NewLine instead of calling TextWriter.


If you want to customize indentation whitespace, just override JsonTextWriter.WriteIndent:

public class JsonTextWriterEx : JsonTextWriter
{
    public string NewLine { get; set; }

    public JsonTextWriterEx (TextWriter textWriter) : base(textWriter)
    {
        NewLine = Environment.NewLine;
    }

    protected override void WriteIndent ()
    {
        if (Formatting == Formatting.Indented) {
            WriteWhitespace(NewLine);
            int currentIndentCount = Top * Indentation;
            for (int i = 0; i < currentIndentCount; i++)
                WriteIndentSpace();
        }
    }
}

[Solved] Generating newlines instead of CRLFs in Json.Net on c# , For my unix/java friends I would like to send newlines ('\n') instead of a CRLF ( '\r\​n') in Json.Net. I tried setting a StreamWriter to use a newline  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Remove/Replace newline character from JSON in .Net using JsonConvert


On the wire protocol for just about everthing is that a line ends with crlf. If you feel that you really must do this, post processing is the way to go -- write it out to a string and then change the string before returning it.

Note that this is a lot of extra processing for what I would consider an actual negative. Not recommended.

Rapid Deployment of Integrated WebSphere Solutions in Your Cloud, Normally, when using JSON, all fields are populated. on Linux, if you create the .json and script files in Windows, make sure that you use an editor new line (​UNIX mode) instead of a typical Windows-style carriage return-line feed (CRLF). It's JSON fragments, separated by newlines, used for the reasons above. The Github repo you link to was created long after people started using newlines. The only libraries mentioned are the author's own, never updated. That's not a format's site, that's a failed PR stunt – Panagiotis Kanavos Oct 11 '17 at 7:32


I see several solutions here that are configuring the serializer. But if you want quick and dirty, just replace the characters with what you want. After all, JSON is just a string.

string json = JsonConvert.SerializeObject(myObject);
json = json.Replace("\r\n", "\n");

How do I remove \, \r, \n from a JSON response in .NET core 2 web API, If you don't want new lines in your json try using. Hide Copy Code. Formatting.​None instead of. Hide Copy Code. Formatting.Indented. From @kromatic on September 21, 2017 20:58 Steps to reproduce Run dotnet new classlib in new project folder. Expected behavior On Linux, this should create Class1.cs and .csproj files with LF line endings.


I know this is an old question but I had a hard time getting the accepted answer to work and maintain proper indented formatting. As a plus, I am also not creating an override to make this work.

Here is how I got this to work correctly:

using (var writer = new StringWriter())
{
    writer.NewLine = "\r\n";
    var serializer = JsonSerializer.Create(
        new JsonSerializerSettings
        {
            Formatting = Formatting.Indented
        });

    serializer.Serialize(writer, data);
    // do something with the written string
}

I am assuming that the code changes referenced in this question enabled the setting of NewLine on the StringWriter to be respected by the serializer.

Environment.NewLine Property (System), The functionality provided by NewLine is often what is meant by the terms newline, line feed, line break, carriage return, CRLF, and end of line. NewLine can be  I'm still on preview5, I tried System.Text.Json and found some incompatible with Newtonsoft.Json. for json string with a trailling " ", when using JsonSerializer.Parse I got an exception says 'The provided data of length 108 has remaining bytes 1.'


Newline, Newline is a control character or sequence of control characters in a character encoding To facilitate the creation of portable programs, programming languages provide Instead, they provide functions for writing a full line that automatically add the file myfile.txt myfile.txt: ASCII English text, with CRLF line terminators. Hello. I stumbled upon some problematic issue. I am programatically generating values for a rich-text field. I want to fill a rich-text field with a string containing wiki-markup with table symbols: ||text1||text2|| \\\\ |text3|text4| and I hope to get the string broken into separare table rows like


JavaScript: Optimizing Native JavaScript: Designing, Programming, , Thus, a regular JSON object might look like: { "name": "John Jones", "age": 12 } and "age": 12 }); Your code must create the function that is called by the JSONP (line feed) to end lines where Windows uses CRLF (carriage return/line feed). JSON deserialization with JSON.net: basics I’ve been contemplating an article about handling JSON for some time now, but it turned out to be a rather long article. So I’d thought to try something new, and write a short series in three parts.


HTTP/1.1: HTTP Message, Generating newlines instead of CRLFs in Json.Net. For my unix/java friends I would like to send newlines ('\n') instead of a CRLF ( '\r\n') in Json.Net. Json.NET supports Windows, Windows Store, Windows Phone, Mono, and Xamarin. Most Popular .NET library Over 100 million downloads and counting, Json.NET is the number one library on NuGet.