Gallery/Gallery.Share/Util/Converter.cs
2021-08-11 14:09:03 +08:00

103 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using Gallery.Util.Model;
namespace Gallery.Util
{
public class GalleryItemConverter : JsonConverter<GalleryItem>
{
public override GalleryItem Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
return null;
}
var item = new GalleryItem();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
return item;
}
if (reader.TokenType == JsonTokenType.PropertyName)
{
var name = reader.GetString();
reader.Read();
switch (name)
{
case nameof(GalleryItem.Id): item.Id = reader.GetInt64(); break;
case nameof(GalleryItem.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(GalleryItem.CreatedTime): item.CreatedTime = reader.GetDateTime(); break;
case nameof(GalleryItem.UpdatedTime): item.UpdatedTime = reader.GetDateTime(); break;
case nameof(GalleryItem.UserId): item.UserId = reader.GetString(); break;
case nameof(GalleryItem.UserName): item.UserName = reader.GetString(); break;
case nameof(GalleryItem.Source): item.Source = reader.GetString(); break;
case nameof(GalleryItem.PreviewUrl): item.PreviewUrl = reader.GetString(); break;
case nameof(GalleryItem.RawUrl): item.RawUrl = reader.GetString(); break;
case nameof(GalleryItem.Width): item.Width = reader.GetInt32(); break;
case nameof(GalleryItem.Height): item.Height = reader.GetInt32(); break;
case nameof(GalleryItem.BookmarkId): item.BookmarkId = reader.GetString(); break;
case nameof(GalleryItem.IsRawPage): item.IsRawPage = reader.GetBoolean(); break;
}
}
}
throw new ArgumentOutOfRangeException();
}
public override void Write(Utf8JsonWriter writer, GalleryItem value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
return;
}
writer.WriteStartObject();
writer.WriteNumber(nameof(GalleryItem.Id), value.Id);
if (value.Tags != null)
{
writer.WritePropertyName(nameof(GalleryItem.Tags));
writer.WriteStartArray();
for (var i = 0; i < value.Tags.Length; i++)
{
writer.WriteStringValue(value.Tags[i]);
}
writer.WriteEndArray();
}
writer.WriteString(nameof(GalleryItem.CreatedTime), value.CreatedTime);
writer.WriteString(nameof(GalleryItem.UpdatedTime), value.UpdatedTime);
writer.WriteString(nameof(GalleryItem.UserId), value.UserId);
writer.WriteString(nameof(GalleryItem.UserName), value.UserName);
writer.WriteString(nameof(GalleryItem.Source), value.Source);
writer.WriteString(nameof(GalleryItem.PreviewUrl), value.PreviewUrl);
writer.WriteString(nameof(GalleryItem.RawUrl), value.RawUrl);
writer.WriteNumber(nameof(GalleryItem.Width), value.Width);
writer.WriteNumber(nameof(GalleryItem.Height), value.Height);
writer.WriteString(nameof(GalleryItem.BookmarkId), value.BookmarkId);
writer.WriteBoolean(nameof(GalleryItem.IsRawPage), value.IsRawPage);
writer.WriteEndObject();
}
}
}