Skip to content

Commit

Permalink
Исправить JsonReaderException (#838)
Browse files Browse the repository at this point in the history
* Исправить JsonReaderException

* Тест PrettyPrintJson
  • Loading branch information
itsokto authored and inyutin-maxim committed May 3, 2019
1 parent 0c84643 commit 7fe6dea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
8 changes: 8 additions & 0 deletions VkNet.Tests/Utils/UtilitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
17 changes: 7 additions & 10 deletions VkNet/Utils/Utilities.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -97,16 +97,13 @@ public static IEnumerable<T> Convert<T>(this VkResponseArray response, Func<VkRe
/// <returns> Json </returns>
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;
}
}

Expand Down

0 comments on commit 7fe6dea

Please sign in to comment.