diff --git a/src/MongoDB.Bson/IO/JsonWriter.cs b/src/MongoDB.Bson/IO/JsonWriter.cs index 4c769cb1e90..86e45c7fb94 100644 --- a/src/MongoDB.Bson/IO/JsonWriter.cs +++ b/src/MongoDB.Bson/IO/JsonWriter.cs @@ -814,12 +814,12 @@ protected override void Dispose(bool disposing) // private methods private string EscapedString(string value) { - if (value.All(c => !NeedsEscaping(c))) + if (!NeedsEscaping(value)) { return value; } - var sb = new StringBuilder(value.Length); + var sb = new StringBuilder(value.Length * 2); foreach (char c in value) { @@ -935,6 +935,19 @@ private string GuidToString(BsonBinarySubType subType, byte[] bytes, GuidReprese } } + private static bool NeedsEscaping(string text) + { + foreach (var letter in text) + { + if (NeedsEscaping(letter)) + { + return true; + } + } + + return false; + } + private static bool NeedsEscaping(char c) { switch (c)