diff --git a/VkNet.Tests/Utils/UtilitiesTests.cs b/VkNet.Tests/Utils/UtilitiesTests.cs index 4bf44ff42..e58120dc9 100644 --- a/VkNet.Tests/Utils/UtilitiesTests.cs +++ b/VkNet.Tests/Utils/UtilitiesTests.cs @@ -51,5 +51,13 @@ public void JsonConvertWrite() var attribute = Attribute.GetCustomAttribute(typeof(VkCollection<>), typeof(DataContractAttribute)); Assert.That(attribute, Is.Null); } + + [Test] + public void PrettyPrintJsonShouldNotThrowException() + { + const string invalidJson = "ERROR"; + + Assert.DoesNotThrow(() => Utilities.PrettyPrintJson(invalidJson)); + } } } \ No newline at end of file diff --git a/VkNet/Utils/Utilities.cs b/VkNet/Utils/Utilities.cs index 16c6dcef4..8155f4463 100644 --- a/VkNet/Utils/Utilities.cs +++ b/VkNet/Utils/Utilities.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace VkNet.Utils { @@ -97,16 +97,13 @@ public static IEnumerable Convert(this VkResponseArray response, Func Json public static string PrettyPrintJson(string json) { - using (var stringReader = new StringReader(json)) + try { - using (var stringWriter = new StringWriter()) - { - var jsonReader = new JsonTextReader(stringReader); - var jsonWriter = new JsonTextWriter(stringWriter) { Formatting = Formatting.Indented }; - jsonWriter.WriteToken(jsonReader); - - return stringWriter.ToString(); - } + return JToken.Parse(json).ToString(Formatting.Indented); + } + catch (JsonReaderException) + { + return json; } }