Skip to content

Commit

Permalink
Break out classes and use JsonElementSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
jzebedee committed Oct 5, 2019
1 parent 6ea878b commit b284a0e
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 151 deletions.
15 changes: 15 additions & 0 deletions Chronicler/Chronicle.cs
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; }
}
}
15 changes: 15 additions & 0 deletions Chronicler/ChronicleChapter.cs
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; }
}
}
152 changes: 2 additions & 150 deletions Chronicler/ChronicleCollection.cs
Original file line number Diff line number Diff line change
@@ -1,159 +1,11 @@
using System;
using Chronicler.Converters;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Chronicler
{
internal class ChronicleCollectionConverter : JsonConverter<ChronicleCollection>
{
//JsonConverter<T> GetConverter<T>() => options.Converters
// .Where(converter => converter.CanConvert(typeof(T)))
// .OfType<JsonConverter<T>>()
// .First();

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();
}
}

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();
}
}

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();
}
}

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; }
}

[JsonConverter(typeof(ChronicleChapterConverter))]
public class ChronicleChapter
{
[JsonPropertyName("chronicle_entry")]
public List<ChronicleEntry> Entries { get; set; }

public int Year { get; set; }
}

[JsonConverter(typeof(ChronicleConverter))]
public class Chronicle
{
[JsonPropertyName("chronicle_chapter")]
public List<ChronicleChapter> Chapters { get; set; }

public int Character { get; set; }
}

[JsonConverter(typeof(ChronicleCollectionConverter))]
public class ChronicleCollection
{
Expand Down Expand Up @@ -201,7 +53,7 @@ public static ChronicleCollection ParseJson(JsonDocument ck2json)
})
});

return JsonSerializer.Deserialize<ChronicleCollection>(chronicleCollection.ToString());
return new JsonElementSerializer(chronicleCollection).ToObject<ChronicleCollection>();
}
}
}
25 changes: 25 additions & 0 deletions Chronicler/ChronicleEntry.cs
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; }
}
}
2 changes: 1 addition & 1 deletion Chronicler/Chronicler.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 16.0.29319.158
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chronicler", "Chronicler.csproj", "{21E7FB31-022C-4917-A69C-5B428A67187E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChronicleTests", "..\test\ChronicleTests\ChronicleTests.csproj", "{3577DBE9-BCF0-471A-9E74-FBFF1445D9C8}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChroniclerTests", "..\test\ChronicleTests\ChroniclerTests.csproj", "{3577DBE9-BCF0-471A-9E74-FBFF1445D9C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
45 changes: 45 additions & 0 deletions Chronicler/Converters/ChronicleChapterConverter.cs
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();
}
}
}
35 changes: 35 additions & 0 deletions Chronicler/Converters/ChronicleCollectionConverter.cs
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();
}
}
}
45 changes: 45 additions & 0 deletions Chronicler/Converters/ChronicleConverter.cs
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();
}
}
}
Loading

0 comments on commit b284a0e

Please sign in to comment.