-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break out classes and use JsonElementSerializer
- Loading branch information
Showing
9 changed files
with
231 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Chronicler.Converters; | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Chronicler | ||
{ | ||
[JsonConverter(typeof(ChronicleConverter))] | ||
public class Chronicle | ||
{ | ||
[JsonPropertyName("chronicle_chapter")] | ||
public List<ChronicleChapter> Chapters { get; set; } | ||
|
||
public int Character { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Chronicler.Converters; | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Chronicler | ||
{ | ||
[JsonConverter(typeof(ChronicleChapterConverter))] | ||
public class ChronicleChapter | ||
{ | ||
[JsonPropertyName("chronicle_entry")] | ||
public List<ChronicleEntry> Entries { get; set; } | ||
|
||
public int Year { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Chronicler | ||
{ | ||
public class ChronicleEntry | ||
{ | ||
[JsonPropertyName("text")] | ||
public string Text { get; set; } | ||
|
||
[JsonPropertyName("picture")] | ||
public string Picture { get; set; } | ||
|
||
[JsonPropertyName("portrait")] | ||
public int Portrait { get; set; } | ||
|
||
[JsonPropertyName("portrait_culture")] | ||
public string PortraitCulture { get; set; } | ||
|
||
[JsonPropertyName("portrait_title_tier")] | ||
public int PortraitTitleTier { get; set; } | ||
|
||
[JsonPropertyName("portrait_government")] | ||
public string PortraitGovernment { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Chronicler.Converters | ||
{ | ||
internal class ChronicleChapterConverter : JsonConverter<ChronicleChapter> | ||
{ | ||
public override ChronicleChapter Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var entries = new List<ChronicleEntry>(); | ||
int? year = null; | ||
|
||
while (reader.Read()) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.PropertyName when reader.ValueTextEquals("chronicle_entry"): | ||
var entry = JsonSerializer.Deserialize<ChronicleEntry>(ref reader, options); | ||
entries.Add(entry); | ||
break; | ||
case JsonTokenType.PropertyName when reader.ValueTextEquals("year"): | ||
reader.Read(); | ||
year = reader.GetInt32(); | ||
break; | ||
} | ||
} | ||
|
||
if (!year.HasValue) | ||
throw new JsonException($"required property `{nameof(year)}` was not found"); | ||
|
||
return new ChronicleChapter | ||
{ | ||
Entries = entries, | ||
Year = year.Value | ||
}; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, ChronicleChapter value, JsonSerializerOptions options) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Chronicler.Converters | ||
{ | ||
internal class ChronicleCollectionConverter : JsonConverter<ChronicleCollection> | ||
{ | ||
public override ChronicleCollection Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var chronicles = new List<Chronicle>(); | ||
while (reader.Read()) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.PropertyName when reader.ValueTextEquals("chronicle"): | ||
var chronicle = JsonSerializer.Deserialize<Chronicle>(ref reader, options); | ||
chronicles.Add(chronicle); | ||
break; | ||
} | ||
} | ||
|
||
return new ChronicleCollection | ||
{ | ||
Chronicles = chronicles | ||
}; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, ChronicleCollection value, JsonSerializerOptions options) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Chronicler.Converters | ||
{ | ||
internal class ChronicleConverter : JsonConverter<Chronicle> | ||
{ | ||
public override Chronicle Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var chapters = new List<ChronicleChapter>(); | ||
int? character = null; | ||
|
||
while (reader.Read()) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonTokenType.PropertyName when reader.ValueTextEquals("chronicle_chapter"): | ||
var chapter = JsonSerializer.Deserialize<ChronicleChapter>(ref reader, options); | ||
chapters.Add(chapter); | ||
break; | ||
case JsonTokenType.PropertyName when reader.ValueTextEquals("character"): | ||
reader.Read(); | ||
character = reader.GetInt32(); | ||
break; | ||
} | ||
} | ||
|
||
if (!character.HasValue) | ||
throw new JsonException($"required property `{nameof(character)}` was not found"); | ||
|
||
return new Chronicle | ||
{ | ||
Chapters = chapters, | ||
Character = character.Value | ||
}; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, Chronicle value, JsonSerializerOptions options) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
Oops, something went wrong.