Skip to content

Commit

Permalink
simplify json converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Sep 16, 2024
1 parent 230f454 commit 6a0364b
Show file tree
Hide file tree
Showing 31 changed files with 136 additions and 225 deletions.
2 changes: 1 addition & 1 deletion src/Starward.Core/Gacha/GachaLogItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class GachaLogItem


[JsonPropertyName("time")]
[JsonConverter(typeof(DateTimeJsonConverter))]
[JsonConverter(typeof(DateTimeStringJsonConverter))]
public DateTime Time { get; set; }


Expand Down
4 changes: 2 additions & 2 deletions src/Starward.Core/GameRecord/GameRecordJsonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Starward.Core.GameRecord.StarRail.PureFiction;
using Starward.Core.GameRecord.StarRail.SimulatedUniverse;
using Starward.Core.GameRecord.StarRail.TrailblazeCalendar;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord;
Expand All @@ -19,13 +20,12 @@ namespace Starward.Core.GameRecord;
[JsonSerializable(typeof(miHoYoApiWrapper<TrailblazeCalendarSummary>))]
[JsonSerializable(typeof(miHoYoApiWrapper<TrailblazeCalendarDetail>))]
[JsonSerializable(typeof(miHoYoApiWrapper<ForgottenHallInfo>))]
[JsonSerializable(typeof(miHoYoApiWrapper<ForgottenHallTime>))]
[JsonSerializable(typeof(miHoYoApiWrapper<PureFictionInfo>))]
[JsonSerializable(typeof(miHoYoApiWrapper<ApocalypticShadowInfo>))]
[JsonSerializable(typeof(miHoYoApiWrapper<SimulatedUniverseInfo>))]
[JsonSerializable(typeof(miHoYoApiWrapper<SimulatedUniverseTime>))]
[JsonSerializable(typeof(miHoYoApiWrapper<DeviceFpResult>))]
[JsonSerializable(typeof(miHoYoApiWrapper<ImaginariumTheaterWarpper>))]
[JsonSerializable(typeof(DateTimeObjectJsonConverter.DateTimeObject))]
internal partial class GameRecordJsonContext : JsonSerializerContext
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public class ImaginariumTheaterInfo


[JsonPropertyName("start_time")]
[JsonConverter(typeof(DateTimeJsonConverter))]
[JsonConverter(typeof(DateTimeStringJsonConverter))]
public DateTime StartTime { get; set; }

[JsonPropertyName("end_time")]
[JsonConverter(typeof(DateTimeJsonConverter))]
[JsonConverter(typeof(DateTimeStringJsonConverter))]
public DateTime EndTime { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Starward.Core.GameRecord.StarRail.SimulatedUniverse;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.Genshin.ImaginariumTheater;
Expand Down Expand Up @@ -46,7 +46,7 @@ public class ImaginariumTheaterRoundsData
/// 完成时间
/// </summary>
[JsonPropertyName("finish_date_time")]
[JsonConverter(typeof(SimulatedUniverseTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime FinishDateTime { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Starward.Core.GameRecord.StarRail.SimulatedUniverse;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.Genshin.ImaginariumTheater;
Expand All @@ -24,11 +24,11 @@ public class ImaginariumTheaterSchedule
public int ScheduleId { get; set; }

[JsonPropertyName("start_date_time")]
[JsonConverter(typeof(SimulatedUniverseTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime StartDateTime { get; set; }

[JsonPropertyName("end_date_time")]
[JsonConverter(typeof(SimulatedUniverseTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime EndDateTime { get; set; }


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.Genshin.SpiralAbyss;

Expand All @@ -14,7 +15,7 @@ public class SpiralAbyssBattle


[JsonPropertyName("timestamp")]
[JsonConverter(typeof(SpiralAbyssTimeJsonConverter))]
[JsonConverter(typeof(TimestampStringJsonConverter))]
public DateTimeOffset Time { get; set; }


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.Genshin.SpiralAbyss;

Expand All @@ -14,12 +15,12 @@ public class SpiralAbyssInfo


[JsonPropertyName("start_time")]
[JsonConverter(typeof(SpiralAbyssTimeJsonConverter))]
[JsonConverter(typeof(TimestampStringJsonConverter))]
public DateTimeOffset StartTime { get; set; }


[JsonPropertyName("end_time")]
[JsonConverter(typeof(SpiralAbyssTimeJsonConverter))]
[JsonConverter(typeof(TimestampStringJsonConverter))]
public DateTimeOffset EndTime { get; set; }


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.Json;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.Genshin.TravelersDiary;
Expand Down Expand Up @@ -34,7 +34,8 @@ public class TravelersDiaryAwardItem
/// <summary>
/// 获取时间,UTC+8
/// </summary>
[JsonPropertyName("time"), JsonConverter(typeof(TravelersDiaryDateTimeJsonConverter))]
[JsonPropertyName("time")]
[JsonConverter(typeof(DateTimeStringJsonConverter))]
public DateTime Time { get; set; }


Expand All @@ -47,16 +48,3 @@ public class TravelersDiaryAwardItem

}


internal class TravelersDiaryDateTimeJsonConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.Parse(reader.GetString()!);
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Text.Json;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.Genshin.TravelersDiary;
Expand Down Expand Up @@ -27,7 +27,7 @@ public abstract class TravelersDiaryBase
/// 当前日期
/// </summary>
[JsonPropertyName("date")]
[JsonConverter(typeof(TravelersDiaryDateJsonConverter))]
[JsonConverter(typeof(DateTimeStringJsonConverter))]
public DateTime Date { get; set; }

/// <summary>
Expand All @@ -54,16 +54,3 @@ public abstract class TravelersDiaryBase

}


internal class TravelersDiaryDateJsonConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateTime.Parse(reader.GetString()!);
}

public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Starward.Core.GameRecord.StarRail.ForgottenHall;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.StarRail.ApocalypticShadow;
Expand Down Expand Up @@ -28,7 +28,7 @@ public class ApocalypticShadowFloorDetail
public bool IsFast { get; set; }

[JsonPropertyName("last_update_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime LastUpdateTime { get; set; }

[JsonIgnore]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Starward.Core.GameRecord.StarRail.ForgottenHall;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.StarRail.ApocalypticShadow;
Expand All @@ -14,11 +14,11 @@ public class ApocalypticShadowInfo
public int ScheduleId { get; set; }

[JsonPropertyName("begin_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime BeginTime { get; set; }

[JsonPropertyName("end_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime EndTime { get; set; }

[JsonPropertyName("upper_boss_icon")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Starward.Core.GameRecord.StarRail.ForgottenHall;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.StarRail.ApocalypticShadow;
Expand All @@ -10,11 +10,11 @@ public class ApocalypticShadowMeta
public int ScheduleId { get; set; }

[JsonPropertyName("begin_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime BeginTime { get; set; }

[JsonPropertyName("end_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime EndTime { get; set; }

[JsonPropertyName("status")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Starward.Core.GameRecord.StarRail.ForgottenHall;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.StarRail.ApocalypticShadow;
Expand All @@ -7,7 +7,7 @@ public class ApocalypticShadowNode
{

[JsonPropertyName("challenge_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime ChallengeTime { get; set; }

[JsonPropertyName("avatars")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.StarRail.ForgottenHall;

Expand All @@ -14,11 +15,11 @@ public class ForgottenHallInfo
public int ScheduleId { get; set; }

[JsonPropertyName("begin_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime BeginTime { get; set; }

[JsonPropertyName("end_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime EndTime { get; set; }

[JsonPropertyName("star_num")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.StarRail.ForgottenHall;

Expand All @@ -10,11 +11,11 @@ public class ForgottenHallMeta
public int ScheduleId { get; set; }

[JsonPropertyName("begin_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime BeginTime { get; set; }

[JsonPropertyName("end_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime EndTime { get; set; }

[JsonPropertyName("status")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Text.Json.Serialization;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.StarRail.ForgottenHall;

public class ForgottenHallNode
{
[JsonPropertyName("challenge_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime ChallengeTime { get; set; }

[JsonPropertyName("avatars")]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Starward.Core.GameRecord.StarRail.ForgottenHall;
using Starward.Core.JsonConverter;
using System.Text.Json.Serialization;

namespace Starward.Core.GameRecord.StarRail.PureFiction;
Expand All @@ -14,11 +14,11 @@ public class PureFictionInfo
public int ScheduleId { get; set; }

[JsonPropertyName("begin_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime BeginTime { get; set; }

[JsonPropertyName("end_time")]
[JsonConverter(typeof(ForgottenHallTimeJsonConverter))]
[JsonConverter(typeof(DateTimeObjectJsonConverter))]
public DateTime EndTime { get; set; }

[JsonPropertyName("star_num")]
Expand Down
Loading

0 comments on commit 6a0364b

Please sign in to comment.