replace Newtonsoft.Json by System.Text.Json

This commit is contained in:
Tsanie Lily 2021-08-04 15:03:15 +08:00
parent c727c6de87
commit a553b156f7
9 changed files with 375 additions and 45 deletions

View File

@ -29,8 +29,8 @@
<string>com.apple.share-services</string>
</dict>
<key>CFBundleShortVersionString</key>
<string>2.0.803</string>
<string>2.1.804</string>
<key>CFBundleVersion</key>
<string>33</string>
<string>34</string>
</dict>
</plist>

View File

@ -15,6 +15,7 @@
<MtouchEnableSGenConc>true</MtouchEnableSGenConc>
<MtouchHttpClientHandler>NSUrlSessionHandler</MtouchHttpClientHandler>
<ProvisioningType>automatic</ProvisioningType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
<DebugSymbols>true</DebugSymbols>
@ -188,7 +189,7 @@
<ItemGroup>
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2083" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>
<Import Project="..\Gallery\Gallery.projitems" Label="Shared" Condition="Exists('..\Gallery\Gallery.projitems')" />
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
@ -220,4 +221,4 @@
<BundleResource Include="Resources\download.png" />
<BundleResource Include="Resources\no_profile.png" />
</ItemGroup>
</Project>
</Project>

View File

@ -79,9 +79,9 @@
</dict>
</array>
<key>CFBundleShortVersionString</key>
<string>2.0.803</string>
<string>2.1.804</string>
<key>CFBundleVersion</key>
<string>33</string>
<string>34</string>
<key>CFBundleDevelopmentRegion</key>
<string>China</string>
</dict>

View File

@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Gallery.Resources;
using Gallery.UI;
using Gallery.UI.Theme;
@ -897,7 +898,6 @@ namespace Gallery.Illust
ImageSource ProfileImage { get; }
}
[JsonObject(MemberSerialization.OptIn)]
public class IllustItem : BindableObject, IIllustItem
{
private static IllustItem empty;
@ -924,7 +924,6 @@ namespace Gallery.Illust
public static readonly BindableProperty BookmarkIdProperty = BindableProperty.Create(
nameof(BookmarkId), typeof(string), typeof(IllustItem));
[JsonProperty]
public string Title { get; set; }
public int Rank { get; set; }
public string RankTitle => Rank > 0 ? $"#{Rank} {Title}" : Title;
@ -950,49 +949,31 @@ namespace Gallery.Illust
}
public bool IsPlaying { get; set; }
[JsonProperty]
public string Id { get; set; }
[JsonProperty]
public string BookmarkId
{
get => (string)GetValue(BookmarkIdProperty);
set => SetValue(BookmarkIdProperty, value);
}
[JsonProperty]
public DateTime FavoriteDateUtc { get; set; }
[JsonProperty]
public string ImageUrl { get; set; }
[JsonProperty]
public bool IsRestrict { get; set; }
[JsonProperty]
public string[] Tags { get; set; }
[JsonProperty]
public string ProfileUrl { get; set; }
[JsonProperty]
public string UserId { get; set; }
[JsonProperty]
public string UserName { get; set; }
[JsonProperty]
public int Width { get; set; }
[JsonProperty]
public int Height { get; set; }
[JsonProperty]
public int PageCount { get; set; }
[JsonProperty]
public double ImageHeightValue
{
get => ImageHeight.IsAuto ? -1 : ImageHeight.Value;
set => ImageHeight = value > 0 ? value : GridLength.Auto;
}
[JsonProperty]
public int YesRank { get; set; }
[JsonProperty]
public int RatingCount { get; set; }
[JsonProperty]
public int ViewCount { get; set; }
[JsonProperty]
public long UploadTimestamp { get; set; }
[JsonProperty]
public IllustType IllustType { get; set; }
public string PageCountText => $"{StyleDefinition.IconLayer} {PageCount}";

View File

@ -177,9 +177,9 @@ namespace Gallery.Illust
var data = Stores.LoadIllustRankingData(lastQueryKey, queryDate, currentPage, out lastError, force);
if (data != null)
{
if (int.TryParse(data.next, out int next))
if (data.next != null)
{
nextPage = next;
nextPage = data.next.Value;
}
var date = data.date;
DateTime now;

View File

@ -6,9 +6,9 @@ using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Gallery.Utils
{
@ -116,7 +116,7 @@ namespace Gallery.Utils
{
try
{
result = JsonConvert.DeserializeObject<T>(content);
result = JsonSerializer.Deserialize<T>(content);
file = namehandler(result);
rtn = true;
}
@ -158,11 +158,11 @@ namespace Gallery.Utils
error = null;
if (nojson)
{
return JsonConvert.DeserializeObject<T>("{}");
return JsonSerializer.Deserialize<T>("{}");
}
else
{
return JsonConvert.DeserializeObject<T>(content);
return JsonSerializer.Deserialize<T>(content);
}
}
catch (Exception ex)

View File

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using Gallery.Illust;
using Xamarin.Forms;
@ -9,54 +9,84 @@ namespace Gallery.Utils
{
public class IllustResponse<T>
{
[JsonInclude]
public bool error;
[JsonInclude]
public string message;
[JsonInclude]
public T body;
}
public class BookmarkResultData
{
[JsonInclude]
public string last_bookmark_id;
[JsonInclude]
public string stacc_status_id;
}
public class Illust
{
[JsonInclude]
public string illustId;
[JsonInclude]
public string illustTitle;
[JsonInclude]
public string id;
[JsonInclude]
public string title;
[JsonInclude]
public int illustType;
[JsonInclude]
public int xRestrict;
[JsonInclude]
public string url;
[JsonInclude]
public string description;
[JsonInclude]
public string[] tags;
[JsonInclude]
public string userId;
[JsonInclude]
public string userName;
[JsonInclude]
public int width;
[JsonInclude]
public int height;
[JsonInclude]
public int pageCount;
[JsonInclude]
public IllustBookmark bookmarkData;
[JsonInclude]
public string alt;
[JsonInclude]
public IllustUrls urls;
[JsonInclude]
public string seriesId;
[JsonInclude]
public string seriesTitle;
[JsonInclude]
public string profileImageUrl;
public class IllustUrls
{
[JsonProperty("250x250")]
[JsonInclude]
[JsonPropertyName("250x250")]
public string x250;
[JsonProperty("360x360")]
[JsonInclude]
[JsonPropertyName("360x360")]
public string x360;
[JsonProperty("540x540")]
[JsonInclude]
[JsonPropertyName("540x540")]
public string x540;
}
public class IllustBookmark
{
[JsonInclude]
public string id;
[JsonProperty("private")]
[JsonInclude]
[JsonPropertyName("private")]
public bool isPrivate;
}
@ -84,115 +114,174 @@ namespace Gallery.Utils
public class User
{
[JsonInclude]
public string userId;
[JsonInclude]
public string name;
[JsonInclude]
public string image;
[JsonInclude]
public string imageBig;
[JsonInclude]
public bool premium;
[JsonInclude]
public bool isFollowed;
//public string background;
[JsonInclude]
public int partial;
}
public class IllustFavoriteData : IllustResponse<IllustFavoriteBody> { }
public class IllustFavoriteBody
{
[JsonInclude]
public int total;
[JsonInclude]
public Illust[] works;
}
public class IllustData : IllustResponse<IllustBody> { }
public class IllustBody
{
[JsonInclude]
public Page page;
[JsonInclude]
public Thumbnail thumbnails;
[JsonInclude]
public User[] users;
public class Page
{
[JsonInclude]
public int[] follow;
[JsonInclude]
public Recommends recommend;
[JsonInclude]
public RecommendByTag[] recommendByTags;
[JsonInclude]
public Ranking ranking;
[JsonInclude]
public RecommendUser[] recommendUser;
[JsonInclude]
public EditorRecommend[] editorRecommend;
[JsonInclude]
public string[] newPost;
public class Recommends
{
[JsonInclude]
public string[] ids;
}
public class RecommendByTag
{
[JsonInclude]
public string tag;
[JsonInclude]
public string[] ids;
}
public class Ranking
{
[JsonInclude]
public RankingItem[] items;
[JsonInclude]
public string date;
public class RankingItem
{
[JsonInclude]
public string rank;
[JsonInclude]
public string id;
}
}
public class RecommendUser
{
[JsonInclude]
public int id;
[JsonInclude]
public string[] illustIds;
}
public class EditorRecommend
{
[JsonInclude]
public string illustId;
[JsonInclude]
public string comment;
}
}
public class Thumbnail
{
[JsonInclude]
public Illust[] illust;
}
}
public class IllustPreloadBody
{
[JsonInclude]
public Dictionary<string, Illust> illust;
[JsonInclude]
public Dictionary<string, User> user;
public class Illust
{
[JsonInclude]
public string illustId;
[JsonInclude]
public string illustTitle;
[JsonInclude]
public string illustComment;
[JsonInclude]
public string id;
[JsonInclude]
public string title;
[JsonInclude]
public string description;
[JsonInclude]
public int illustType;
[JsonInclude]
public DateTime createDate;
[JsonInclude]
public DateTime uploadDate;
[JsonInclude]
public int xRestrict;
[JsonInclude]
public IllustUrls urls;
[JsonInclude]
public IllustTag tags;
[JsonInclude]
public string alt;
[JsonInclude]
public string userId;
[JsonInclude]
public string userName;
[JsonInclude]
public string userAccount;
//public Dictionary<string, Illust> userIllusts;
[JsonInclude]
public int width;
[JsonInclude]
public int height;
[JsonInclude]
public int pageCount;
[JsonInclude]
public int bookmarkCount;
[JsonInclude]
public int likeCount;
[JsonInclude]
public int commentCount;
[JsonInclude]
public int responseCount;
[JsonInclude]
public int viewCount;
[JsonInclude]
public bool isOriginal;
[JsonInclude]
public IllustBookmark bookmarkData;
public IllustItem CopyToItem(IllustItem item)
@ -223,38 +312,56 @@ namespace Gallery.Utils
public class IllustBookmark
{
[JsonInclude]
public string id;
[JsonProperty("private")]
[JsonInclude]
[JsonPropertyName("private")]
public bool isPrivate;
}
public class IllustUrls
{
[JsonInclude]
public string mini;
[JsonInclude]
public string thumb;
[JsonInclude]
public string small;
[JsonInclude]
public string regular;
[JsonInclude]
public string original;
}
public class IllustTag
{
[JsonInclude]
public string authorId;
[JsonInclude]
public bool isLocked;
[JsonInclude]
public IllustTagItem[] tags;
[JsonInclude]
public bool writable;
public class IllustTagItem
{
[JsonInclude]
public string tag;
[JsonInclude]
public bool locked;
[JsonInclude]
public bool deletable;
[JsonInclude]
public string userId;
[JsonInclude]
public IllustTranslate translation;
[JsonInclude]
public string userName;
public class IllustTranslate
{
[JsonInclude]
public string en;
}
}
@ -265,15 +372,22 @@ namespace Gallery.Utils
public class IllustPageData : IllustResponse<IllustPageBody[]> { }
public class IllustPageBody
{
[JsonInclude]
public Urls urls;
[JsonInclude]
public int width;
[JsonInclude]
public int height;
public class Urls
{
[JsonInclude]
public string thumb_mini;
[JsonInclude]
public string small;
[JsonInclude]
public string regular;
[JsonInclude]
public string original;
}
}
@ -281,33 +395,43 @@ namespace Gallery.Utils
public class IllustRecommendsData : IllustResponse<IllustRecommendsBody> { }
public class IllustRecommendsBody
{
[JsonInclude]
public Illust[] illusts;
[JsonInclude]
public string[] nextIds;
}
public class IllustUserListData : IllustResponse<IllustUserListBody> { }
public class IllustUserListBody
{
[JsonInclude]
public Dictionary<string, object> illusts;
}
public class IllustUserData : IllustResponse<IllustUserBody> { }
public class IllustUserBody
{
[JsonInclude]
public Dictionary<string, Illust> works;
}
public class IllustUgoiraData : IllustResponse<IllustUgoiraBody> { }
public class IllustUgoiraBody
{
[JsonInclude]
public string src;
[JsonInclude]
public string originalSrc;
[JsonInclude]
public string mime_type;
[JsonInclude]
public Frame[] frames;
public class Frame
{
[JsonInclude]
public string file;
[JsonInclude]
public int delay;
public string FilePath;

View File

@ -1,76 +1,139 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Gallery.Illust;
namespace Gallery.Utils
{
public class IllustRankingData
{
[JsonInclude]
public Content[] contents;
[JsonInclude]
public string mode;
[JsonInclude]
public string content;
[JsonInclude]
public int page;
public string prev;
public string next;
[JsonInclude]
[JsonConverter(typeof(Int32Converter))]
public int? prev;
[JsonInclude]
[JsonConverter(typeof(Int32Converter))]
public int? next;
[JsonInclude]
public string date;
[JsonInclude]
[JsonConverter(typeof(StringConverter))]
public string prev_date;
[JsonInclude]
[JsonConverter(typeof(StringConverter))]
public string next_date;
[JsonInclude]
public int rank_total;
public class Content
{
[JsonInclude]
public string title;
[JsonInclude]
public string date;
[JsonInclude]
public string[] tags;
[JsonInclude]
public string url;
[JsonInclude]
public string illust_type;
[JsonInclude]
public string illust_book_style;
[JsonInclude]
public string illust_page_count;
[JsonInclude]
public string user_name;
[JsonInclude]
public string profile_img;
[JsonInclude]
public ContentType illust_content_type;
[JsonInclude]
public object illust_series; // bool, Series
[JsonInclude]
public long illust_id;
[JsonInclude]
public int width;
[JsonInclude]
public int height;
[JsonInclude]
public long user_id;
[JsonInclude]
public int rank;
[JsonInclude]
public int yes_rank;
[JsonInclude]
public int rating_count;
[JsonInclude]
public int view_count;
[JsonInclude]
public long illust_upload_timestamp;
[JsonInclude]
public string attr;
[JsonInclude]
public bool is_bookmarked;
[JsonInclude]
public bool bookmarkable;
[JsonInclude]
public string bookmark_id;
[JsonInclude]
public string bookmark_illust_restrict;
public class ContentType
{
[JsonInclude]
public int sexual;
[JsonInclude]
public bool lo;
[JsonInclude]
public bool grotesque;
[JsonInclude]
public bool violent;
[JsonInclude]
public bool homosexual;
[JsonInclude]
public bool drug;
[JsonInclude]
public bool thoughts;
[JsonInclude]
public bool antisocial;
[JsonInclude]
public bool religion;
[JsonInclude]
public bool original;
[JsonInclude]
public bool furry;
[JsonInclude]
public bool bl;
[JsonInclude]
public bool yuri;
}
public class Series
{
[JsonInclude]
public string illust_series_caption;
[JsonInclude]
public string illust_series_content_count;
[JsonInclude]
public string illust_series_content_illust_id;
[JsonInclude]
public string illust_series_content_order;
[JsonInclude]
public string illust_series_create_datetime;
[JsonInclude]
public string illust_series_id;
[JsonInclude]
public string illust_series_title;
[JsonInclude]
public string illust_series_user_id;
[JsonInclude]
public string page_url;
}
@ -121,21 +184,67 @@ namespace Gallery.Utils
public class IllustGlobalData
{
[JsonInclude]
public string token;
[JsonInclude]
public string oneSignalAppId;
[JsonInclude]
public UserData userData;
public class UserData
{
[JsonInclude]
public string id;
[JsonInclude]
public string pixivId;
[JsonInclude]
public string name;
[JsonInclude]
public string profileImg;
[JsonInclude]
public string profileImgBig;
[JsonInclude]
public bool premium;
[JsonInclude]
public int xRestrict;
[JsonInclude]
public bool adult;
[JsonInclude]
public bool safeMode;
}
}
public class Int32Converter : JsonConverter<int?>
{
public override int? Read(ref Utf8JsonReader reader, System.Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Number)
{
return reader.GetInt32();
}
return null;
}
public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)
{
throw new System.NotImplementedException();
}
}
public class StringConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, System.Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
return reader.GetString();
}
return null;
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
throw new System.NotImplementedException();
}
}
}

View File

@ -5,9 +5,9 @@ using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Gallery.Illust;
using Xamarin.Essentials;
using Xamarin.Forms;
@ -147,7 +147,7 @@ namespace Gallery.Utils
}
try
{
return JsonConvert.DeserializeObject<T>(content);
return JsonSerializer.Deserialize<T>(content);
}
catch (Exception ex)
{
@ -166,7 +166,7 @@ namespace Gallery.Utils
string content;
try
{
content = JsonConvert.SerializeObject(obj, Formatting.None);
content = JsonSerializer.Serialize(obj);
}
catch (Exception ex)
{
@ -651,6 +651,7 @@ namespace Gallery.Utils
public FavoriteList Illusts { get; set; }
}
[JsonConverter(typeof(FavoriteListConverter))]
public class FavoriteList : List<IllustItem>
{
public bool Changed { get; private set; }
@ -683,6 +684,120 @@ namespace Gallery.Utils
}
}
public class FavoriteListConverter : JsonConverter<FavoriteList>
{
public override FavoriteList Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var list = new FavoriteList();
if (reader.TokenType != JsonTokenType.StartArray)
{
return list;
}
var item = new IllustItem();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndArray)
{
return list;
}
if (reader.TokenType == JsonTokenType.EndObject)
{
list.Add(item);
item = new IllustItem();
}
else if (reader.TokenType == JsonTokenType.PropertyName)
{
var name = reader.GetString();
reader.Read();
switch (name)
{
case nameof(IllustItem.Title): item.Title = reader.GetString(); break;
case nameof(IllustItem.Id): item.Id = reader.GetString(); break;
case nameof(IllustItem.BookmarkId): item.BookmarkId = reader.GetString(); break;
case nameof(IllustItem.FavoriteDateUtc): item.FavoriteDateUtc = reader.GetDateTime(); break;
case nameof(IllustItem.ImageUrl): item.ImageUrl = reader.GetString(); break;
case nameof(IllustItem.IsRestrict): item.IsRestrict = reader.GetBoolean(); break;
case nameof(IllustItem.Tags):
if (reader.TokenType == JsonTokenType.StartArray)
{
var tags = new List<string>();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndArray)
{
break;
}
if (reader.TokenType == JsonTokenType.String)
{
tags.Add(reader.GetString());
}
}
item.Tags = tags.ToArray();
}
break;
case nameof(IllustItem.ProfileUrl): item.ProfileUrl = reader.GetString(); break;
case nameof(IllustItem.UserId): item.UserId = reader.GetString(); break;
case nameof(IllustItem.UserName): item.UserName = reader.GetString(); break;
case nameof(IllustItem.Width): item.Width = reader.GetInt32(); break;
case nameof(IllustItem.Height): item.Height = reader.GetInt32(); break;
case nameof(IllustItem.PageCount): item.PageCount = reader.GetInt32(); break;
case nameof(IllustItem.ImageHeightValue): item.ImageHeightValue = reader.GetDouble(); break;
case nameof(IllustItem.YesRank): item.YesRank = reader.GetInt32(); break;
case nameof(IllustItem.RatingCount): item.RatingCount = reader.GetInt32(); break;
case nameof(IllustItem.ViewCount): item.ViewCount = reader.GetInt32(); break;
case nameof(IllustItem.UploadTimestamp): item.UploadTimestamp = reader.GetInt64(); break;
case nameof(IllustItem.IllustType): item.IllustType = (IllustType)reader.GetInt32(); break;
}
}
}
throw new JsonException("unexpected favorite list ending");
}
public override void Write(Utf8JsonWriter writer, FavoriteList value, JsonSerializerOptions options)
{
writer.WriteStartArray();
if (value != null)
{
for (var i = 0; i < value.Count; i++)
{
var item = value[i];
writer.WriteStartObject();
writer.WriteString(nameof(IllustItem.Title), item.Title);
writer.WriteString(nameof(IllustItem.Id), item.Id);
writer.WriteString(nameof(IllustItem.BookmarkId), item.BookmarkId);
writer.WriteString(nameof(IllustItem.FavoriteDateUtc), item.FavoriteDateUtc);
writer.WriteString(nameof(IllustItem.ImageUrl), item.ImageUrl);
writer.WriteBoolean(nameof(IllustItem.IsRestrict), item.IsRestrict);
if (item.Tags != null)
{
writer.WritePropertyName(nameof(IllustItem.Tags));
writer.WriteStartArray();
for (var n = 0; n < item.Tags.Length; n++)
{
writer.WriteStringValue(item.Tags[n]);
}
writer.WriteEndArray();
}
writer.WriteString(nameof(IllustItem.ProfileUrl), item.ProfileUrl);
writer.WriteString(nameof(IllustItem.UserId), item.UserId);
writer.WriteString(nameof(IllustItem.UserName), item.UserName);
writer.WriteNumber(nameof(IllustItem.Width), item.Width);
writer.WriteNumber(nameof(IllustItem.Height), item.Height);
writer.WriteNumber(nameof(IllustItem.PageCount), item.PageCount);
writer.WriteNumber(nameof(IllustItem.ImageHeightValue), item.ImageHeightValue);
writer.WriteNumber(nameof(IllustItem.YesRank), item.YesRank);
writer.WriteNumber(nameof(IllustItem.RatingCount), item.RatingCount);
writer.WriteNumber(nameof(IllustItem.ViewCount), item.ViewCount);
writer.WriteNumber(nameof(IllustItem.UploadTimestamp), item.UploadTimestamp);
writer.WriteNumber(nameof(IllustItem.IllustType), (int)item.IllustType);
writer.WriteEndObject();
}
}
writer.WriteEndArray();
}
}
public enum SyncType
{
None = 0,