Skip to content

Commit

Permalink
chores: Add JsonDeserializerOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Raspberry-Monster committed Feb 6, 2025
1 parent f842459 commit 9b58b47
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
10 changes: 10 additions & 0 deletions HyPlayer.NeteaseApi/ApiHandlerOption.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HyPlayer.NeteaseApi.Extensions.JsonSerializer;
using HyPlayer.NeteaseApi.Serialization;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -25,6 +26,15 @@ public class ApiHandlerOption
AllowTrailingCommas = true,
Converters = { new NumberToStringConverter(), new JsonBooleanConverter() }
};
public JsonSerializerOptions JsonDeserializerOptions =
new(JsonSerializerOptions.Default)
{
NumberHandling = JsonNumberHandling.WriteAsString |
JsonNumberHandling.AllowReadingFromString,
AllowTrailingCommas = true,
Converters = { new NumberToStringConverter(), new JsonBooleanConverter() },
TypeInfoResolver= JsonSerializeContext.Default
};
}

public class AdditionalParameters {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public override Task<HttpRequestMessage> GenerateRequestMessageAsync<TActualRequ

if (req is CacheKeyEApiActualRequest)
{
var map = JsonSerializer.Deserialize<Dictionary<string, string>>(json, option.JsonSerializerOptions);
var map = JsonSerializer.Deserialize<Dictionary<string, string>>(json, option.JsonDeserializerOptions);
var header = map?.GetValueOrDefault("header");
map?.Remove("header");
map?.Remove("cache_key");
Expand Down Expand Up @@ -161,7 +161,7 @@ public override async Task<Results<TResponseModel, ErrorResultBase>> ProcessResp
try
{
var result = Encoding.UTF8.GetString(buffer);
var ret = JsonSerializer.Deserialize<TResponseModel>(result, option.JsonSerializerOptions);
var ret = JsonSerializer.Deserialize<TResponseModel>(result, option.JsonDeserializerOptions);
#if DEBUG
if (ret is null)
ret = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public override async Task<Results<TResponseModel, ErrorResultBase>> ProcessResp
if (buffer is null || buffer.Length == 0) return new ErrorResultBase(500, "返回体预读取错误");

var result = Encoding.UTF8.GetString(buffer);
var ret = JsonSerializer.Deserialize<TResponseModel>(result, option.JsonSerializerOptions);
var ret = JsonSerializer.Deserialize<TResponseModel>(result, option.JsonDeserializerOptions);
#if DEBUG
if (ret is null)
ret = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public override async Task<Results<TResponseModel, ErrorResultBase>> ProcessResp
var buffer = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
if (buffer is null || buffer.Length == 0) return new ErrorResultBase(500, "返回体预读取错误");
var result = Encoding.UTF8.GetString(buffer);
var ret = JsonSerializer.Deserialize<TResponseModel>(result, option.JsonSerializerOptions);
var ret = JsonSerializer.Deserialize<TResponseModel>(result, option.JsonDeserializerOptions);
#if DEBUG
if (ret is null)
ret = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public override async Task<Results<TResponseModel, ErrorResultBase>> ProcessResp
var buffer = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
if (buffer is null || buffer.Length == 0) return new ErrorResultBase(500, "返回体预读取错误");
var result = Encoding.UTF8.GetString(buffer);
var ret = JsonSerializer.Deserialize<TResponseModel>(result, option.JsonSerializerOptions);
var ret = JsonSerializer.Deserialize<TResponseModel>(result, option.JsonDeserializerOptions);
#if DEBUG
if (ret is null)
ret = new();
Expand Down
76 changes: 76 additions & 0 deletions HyPlayer.NeteaseApi/Models/JsonSerializeContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Text.Json.Serialization;
using HyPlayer.NeteaseApi.ApiContracts;

namespace HyPlayer.NeteaseApi.Serialization
{
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(AiDjContentRcmdInfoResponse))]
[JsonSerializable(typeof(AiDjSkipResponse))]
[JsonSerializable(typeof(AlbumResponse))]
[JsonSerializable(typeof(AlbumDetailDynamicResponse))]
[JsonSerializable(typeof(AlbumSublistResponse))]
[JsonSerializable(typeof(AlbumSubscribeResponse))]
[JsonSerializable(typeof(ArtistAlbumsResponse))]
[JsonSerializable(typeof(ArtistDetailResponse))]
[JsonSerializable(typeof(ArtistSongsResponse))]
[JsonSerializable(typeof(ArtistTopSongResponse))]
[JsonSerializable(typeof(ArtistVideoResponse))]
[JsonSerializable(typeof(CloudDeleteResponse))]
[JsonSerializable(typeof(CloudGetResponse))]
[JsonSerializable(typeof(CloudPubResponse))]
[JsonSerializable(typeof(CloudUploadCheckResponse))]
[JsonSerializable(typeof(CloudUploadCoverTokenAllocResponse))]
[JsonSerializable(typeof(CloudUploadInfoResponse))]
[JsonSerializable(typeof(CloudUploadTokenAllocResponse))]
[JsonSerializable(typeof(CommentFloorResponse))]
[JsonSerializable(typeof(CommentLikeResponse))]
[JsonSerializable(typeof(CommentsResponse))]
[JsonSerializable(typeof(DjChannelDetailResponse))]
[JsonSerializable(typeof(DjChannelProgramsResponse))]
[JsonSerializable(typeof(FmTrashResponse))]
[JsonSerializable(typeof(LikeResponse))]
[JsonSerializable(typeof(LikelistResponse))]
[JsonSerializable(typeof(LoginResponse))]
[JsonSerializable(typeof(LoginQrCodeCheckResponse))]
[JsonSerializable(typeof(LoginQrCodeUnikeyResponse))]
[JsonSerializable(typeof(LoginStatusResponse))]
[JsonSerializable(typeof(LyricResponse))]
[JsonSerializable(typeof(MusicFirstListenInfoResponse))]
[JsonSerializable(typeof(PersonalFmResponse))]
[JsonSerializable(typeof(PlaylistCategoryListResponse))]
[JsonSerializable(typeof(PlaylistCreateResponse))]
[JsonSerializable(typeof(PlaylistDeleteResponse))]
[JsonSerializable(typeof(PlaylistDetailResponse))]
[JsonSerializable(typeof(PlaylistPrivacyResponse))]
[JsonSerializable(typeof(PlaylistSubscribeResponse))]
[JsonSerializable(typeof(PlaylistTracksEditResponse))]
[JsonSerializable(typeof(PlaylistTracksGetResponse))]
[JsonSerializable(typeof(RecommendPlaylistsResponse))]
[JsonSerializable(typeof(RecommendResourceResponse))]
[JsonSerializable(typeof(RecommendSongsResponse))]
[JsonSerializable(typeof(SearchResponse))]
[JsonSerializable(typeof(SearchPlaylistResponse))]
[JsonSerializable(typeof(SearchAlbumResponse))]
[JsonSerializable(typeof(SearchUserResponse))]
[JsonSerializable(typeof(SearchLyricResponse))]
[JsonSerializable(typeof(SearchSongResponse))]
[JsonSerializable(typeof(SearchMVResponse))]
[JsonSerializable(typeof(SearchArtistResponse))]
[JsonSerializable(typeof(SearchVideoResponse))]
[JsonSerializable(typeof(SearchRadioResponse))]
[JsonSerializable(typeof(SearchSuggestionResponse))]
[JsonSerializable(typeof(SongDetailResponse))]
[JsonSerializable(typeof(SongUrlResponse))]
[JsonSerializable(typeof(SongWikiSummaryResponse))]
[JsonSerializable(typeof(ToplistResponse))]
[JsonSerializable(typeof(UserCloudResponse))]
[JsonSerializable(typeof(UserCloudDeleteResponse))]
[JsonSerializable(typeof(UserPlaylistResponse))]
[JsonSerializable(typeof(UserRecordResponse))]
[JsonSerializable(typeof(VideoDetailResponse))]
[JsonSerializable(typeof(VideoUrlResponse))]
[JsonSerializable(typeof(UserDetailResponse))]
public partial class JsonSerializeContext : JsonSerializerContext
{
}
}

0 comments on commit 9b58b47

Please sign in to comment.