Newtonsoft.Json icon indicating copy to clipboard operation
Newtonsoft.Json copied to clipboard

JsonSerializationException possibly having too long message

Open MihaMarkic opened this issue 2 years ago • 1 comments

Not sure whether this can be classified as a bug, but it could be problematic. I'm deserializing a type from a stream. The content is like 100MB long. And this works fine. One day somebody messed up the source and instead of being JSON text, it became a string containing JSON text (probably serialized two times or something). So I'm trying to deserialize a 100MB string instead of JSON now and deserializer, correctly, throws an exception. However, the problem is that JsonSerializationException.Message contains text "Error converting value "....."" where it concatenates entire input string which is more than 200MB. It also does some additional copies internally and at the peak of Exception it consumes like more than 1.5GB of memory by looking at memory profiler.

Expected behavior

JsonSerializationException.Message trims source to a manageable length. (perhaps configured somewhere).

Actual behavior

Throws an JsonSerializationException with a Message containing more than 200MB worth of text.

Steps to reproduce

I.e. try converting any string to a custom type.

void Main()
{
	string text = new string(' ', 1_000_000);
	var deserialized = new JsonSerializer().Deserialize(new StringReader($"\"{text}\""), typeof(Tubo));
}

public record Tubo { }

MihaMarkic avatar Jan 09 '22 13:01 MihaMarkic

Could be considered along side https://github.com/JamesNK/Newtonsoft.Json/issues/2703

daniellawrence1976 avatar Jul 06 '22 15:07 daniellawrence1976