From 7fe6dea6fe29bb72fa77c5f2fa48db1cb0bd0b4c Mon Sep 17 00:00:00 2001 From: itsokto <36706257+itsokto@users.noreply.github.com> Date: Sat, 4 May 2019 00:18:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20JsonReaderException=20(#838)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Исправить JsonReaderException * Тест PrettyPrintJson --- VkNet.Tests/Utils/UtilitiesTests.cs | 8 ++++++++ VkNet/Utils/Utilities.cs | 17 +++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) 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; } }