Skip to content

Commit

Permalink
Migrate from Newtonsoft.Json to System.Text.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
abock committed Nov 15, 2021
1 parent 09a1002 commit 1fb2268
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 38 deletions.
1 change: 0 additions & 1 deletion Goodbye.WordPress/Goodbye.WordPress.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

<ItemGroup>
<PackageReference Include="MySqlConnector" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="ReverseMarkdown" Version="3.12.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="SharpYaml" Version="1.6.6" />
Expand Down
17 changes: 12 additions & 5 deletions Goodbye.WordPress/JsonPostReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;

using Newtonsoft.Json;

namespace Goodbye.WordPress
{
public sealed class JsonPostReader : IPostReader
Expand All @@ -22,9 +22,16 @@ public JsonPostReader(string jsonFile)
public async IAsyncEnumerable<Post> ReadPostsAsync(
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var serializer = JsonSerializer.CreateDefault();
var reader = new StreamReader(File.OpenRead(_jsonFile));
var posts = serializer.Deserialize<List<Post>>(new JsonTextReader(reader))
var posts = await JsonSerializer.DeserializeAsync<List<Post>>(
File.OpenRead(_jsonFile),
new JsonSerializerOptions
{
Converters =
{
new JsonStringEnumConverter()
}
},
cancellationToken)
?? new List<Post>();

foreach (var post in posts)
Expand Down
43 changes: 11 additions & 32 deletions Goodbye.WordPress/WordPressExporterDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,39 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

using Serilog;

using Newtonsoft.Json;
using Serilog.Events;

using SharpYaml.Serialization;
using System.Net;
using Serilog.Events;

namespace Goodbye.WordPress
{
public class WordPressExporterDelegate
{
sealed class PostResourceDownloadStatusConverter : JsonConverter<PostResourceDownloadStatus>
{
public override PostResourceDownloadStatus ReadJson(
JsonReader reader,
Type objectType,
PostResourceDownloadStatus existingValue,
bool hasExistingValue,
JsonSerializer serializer)
=> PostResourceDownloadStatus.NotAttempted;

public override void WriteJson(
JsonWriter writer,
PostResourceDownloadStatus value,
JsonSerializer serializer)
=> writer.WriteValue((value == PostResourceDownloadStatus.AlreadyDownloaded
? PostResourceDownloadStatus.Succeeded
: value)
.ToString());
}

public static Encoding Utf8NoBomEncoding { get; } = new UTF8Encoding(false);

protected virtual HttpClient HttpClient { get; } = new HttpClient(
protected virtual HttpClient HttpClient { get; } = new(
new HttpClientHandler
{
AllowAutoRedirect = true
});

protected virtual JsonSerializerSettings JsonSerializerSettings { get; } = new JsonSerializerSettings
protected virtual JsonSerializerOptions JsonSerializerOptions { get; } = new()
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull | JsonIgnoreCondition.WhenWritingDefault,
Converters =
{
new PostResourceDownloadStatusConverter()
new JsonStringEnumConverter()
}
};

Expand Down Expand Up @@ -102,9 +81,9 @@ public virtual void WriteArchive(

File.WriteAllText(
exporter.ArchiveOutputFilePath,
JsonConvert.SerializeObject(
JsonSerializer.Serialize(
exporter.Posts.Select(post => WriteArchivePostSelector(exporter, post)),
JsonSerializerSettings),
JsonSerializerOptions),
Utf8NoBomEncoding);
}

Expand Down

0 comments on commit 1fb2268

Please sign in to comment.