Skip to content

Commit

Permalink
Replaced JSON.Net with System.Text.Json.
Browse files Browse the repository at this point in the history
With this commit, we switch to an official Microsoft-developed JSON library. See https://github.com/dotnet/corefx/issues/33115 for more information about the advantages of this new library,
  • Loading branch information
Felix-Dev committed Dec 23, 2019
1 parent 6a27d2a commit 621cdb1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ATG_Notifier.ViewModels/ATG_Notifier.ViewModels.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.17" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="System.Text.Json" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using ATG_Notifier.ViewModels.Models;
using ATG_Notifier.ViewModels.Services;
using HtmlAgilityPack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

Expand All @@ -18,9 +17,6 @@ public class RawSourceChecker
private const string ATGRawSourcePollingUrl = "https://m.zongheng.com/h5/ajax/chapter/list?h5=1&bookId=408586&pageNum=1&pageSize=1&chapterId=0&asc=1";
private const string ATGRawSourceBaseUrl = "http://book.zongheng.com/chapter/408586/";

private const string JsonChapterNumberTitlePath = "chapterlist.chapters[0].chapterName";
private const string JsonChapterIdPath = "chapterlist.chapters[0].chapterId";

private readonly ILogService logService;
private readonly IWebService webService;

Expand All @@ -44,33 +40,45 @@ public RawSourceChecker(IWebService webService, ILogService logService)
}

// The obtained data is saved in a JSON format. Parse the JSON string to obtain the latest chapter data.
JObject json;
JsonDocument document;
try
{
json = JObject.Parse(data);
document = JsonDocument.Parse(data);
}
catch (JsonReaderException)
catch (JsonException)
{
logService.Log(LogType.Error, "Downloaded chapter info is not in JSON format!\n");
return null;
}

var chapterNumberAndTitle = json.SelectToken(JsonChapterNumberTitlePath)?.ToString();
if (chapterNumberAndTitle == null)
// json layout:
// chapterId = chapterlist.chapters[0].chapterId
// chapterName = chapterlist.chapters[0].chapterName

if (!document.RootElement.TryGetProperty("chapterlist", out JsonElement chapterListNode)
|| !chapterListNode.TryGetProperty("chapters", out JsonElement chapters)
|| chapters.GetArrayLength() == 0)
{
logService.Log(LogType.Error, "The current chapter information could not be retrieved!\n");
logService.Log(LogType.Error, "Downloaded chapter info has a different structure! The app might need to be updated.\n");
return null;
}

var chapterId = json.SelectToken(JsonChapterIdPath)?.ToString();
if (chapterId == null)
if (!chapters[0].TryGetProperty("chapterId", out JsonElement idElement))
{
logService.Log(LogType.Error, "The current chapter ID could not be retrieved!\n");
return null;
}
string chapterId = idElement.ToString();

if (!chapters[0].TryGetProperty("chapterName", out JsonElement nameElement))
{
logService.Log(LogType.Error, "The current chapter name could not be retrieved!\n");
return null;
}
var chapterNumberAndTitle = nameElement.ToString();

document.Dispose();

// TODO: Check here if the latest retrieved chapter info differs from the most-recently obtained latest
// chapter info.
if (mostRecentChapterId == chapterId)
{
return null;
Expand Down

0 comments on commit 621cdb1

Please sign in to comment.