1

I am new to JSON and I have been assigned a project where I need to consume it from a third-party web service. I decided to use Json.NET because it looks to be pretty complete and it seems to just figure out a lot of stuff.

There is a lot of text and I'm having trouble maintaining the carriage return/line feed pairs.

I can duplicate the problem using this test:

public class MyClass
{
    public string value { get; set; }
}

[Test]
public void HandleNewLines2()
{
    var value = "ALERT \r\n\r\n\r\n.  5010";
    var json = @"{""value"": """ + value + @"""}";
    MyClass myClass = JsonConvert.DeserializeObject<MyClass>(json);
    Assert.IsNotNull(myClass);
    Assert.AreEqual(value, myClass.value);
}

It fails with this:

HandleNewLines2 : Failed  Expected string length 19 but was 16. Strings differ at index 7.
  Expected: "ALERT \r\n\r\n\r\n.  5010"
  But was:  "ALERT \r\r\r.  5010"
  --------------------^

The line feeds (\n) get removed in the JsonTextReader::MoveNext method by the following:

    case CarriageReturnValue:
      if (_reader.Peek() == LineFeedValue)
        _reader.Read();

      _currentLineNumber++;
      _currentLinePosition = 0;
      break;

Is there a way to maintain the carriage return/line feed pairs in the value?

Ben
  • 51,770
  • 36
  • 127
  • 149
user1106421
  • 39
  • 1
  • 3

0 Answers0