diff --git a/VkNet/Model/BotsLongPollHistoryResponse.cs b/VkNet/Model/BotsLongPollHistoryResponse.cs index 48db4e3d1..3f0568e50 100644 --- a/VkNet/Model/BotsLongPollHistoryResponse.cs +++ b/VkNet/Model/BotsLongPollHistoryResponse.cs @@ -14,13 +14,11 @@ public class BotsLongPollHistoryResponse /// /// Номер последнего события, начиная с которого нужно получать данные; /// - [JsonProperty("ts")] public string Ts { get; set; } /// /// Обновления группы /// - [JsonConverter(typeof(GroupUpdateJsonConverter))] - [JsonProperty("updates")] + [JsonProperty(ItemConverterType = typeof(GroupUpdateJsonConverter))] public List Updates { get; set; } } \ No newline at end of file diff --git a/VkNet/Model/GroupUpdate/GroupUpdate.cs b/VkNet/Model/GroupUpdate/GroupUpdate.cs index 9d3f9b991..07dd83984 100644 --- a/VkNet/Model/GroupUpdate/GroupUpdate.cs +++ b/VkNet/Model/GroupUpdate/GroupUpdate.cs @@ -2,6 +2,7 @@ using Newtonsoft.Json; using VkNet.Enums.SafetyEnums; using VkNet.Utils; +using VkNet.Utils.JsonConverter; namespace VkNet.Model.GroupUpdate; @@ -19,18 +20,20 @@ public class GroupUpdate /// /// Тип обновления /// + [JsonConverter(typeof(UpdateTypeConverter))] public UpdateType Type { get; set; } /// /// ID группы /// [JsonProperty("group_id")] + [JsonConverter(typeof(GroupIdConverter))] public GroupId GroupId { get; set; } /// /// Secret Key для Callback /// - [JsonProperty("secret")] + [JsonConverter(typeof(SecretConverter))] public Secret Secret { get; set; } /// diff --git a/VkNet/Utils/JsonConverter/GroupIdConverter.cs b/VkNet/Utils/JsonConverter/GroupIdConverter.cs new file mode 100644 index 000000000..c7387f727 --- /dev/null +++ b/VkNet/Utils/JsonConverter/GroupIdConverter.cs @@ -0,0 +1,25 @@ +using Newtonsoft.Json; +using System; +using VkNet.Model.GroupUpdate; + +namespace VkNet.Utils.JsonConverter; + +/// +public class GroupIdConverter : JsonConverter +{ + /// + public override GroupId ReadJson(JsonReader reader, Type objectType, GroupId existingValue, bool hasExistingValue, JsonSerializer serializer) + { + if (reader.Value == null) + return null; + + var id = Convert.ToUInt64(reader.Value); + return new GroupId(id); + } + + /// + public override void WriteJson(JsonWriter writer, GroupId value, JsonSerializer serializer) + { + writer.WriteValue(value?.Value); + } +} \ No newline at end of file diff --git a/VkNet/Utils/JsonConverter/GroupUpdateJsonConverter.cs b/VkNet/Utils/JsonConverter/GroupUpdateJsonConverter.cs index 9e1b7a7fe..65ad86dec 100644 --- a/VkNet/Utils/JsonConverter/GroupUpdateJsonConverter.cs +++ b/VkNet/Utils/JsonConverter/GroupUpdateJsonConverter.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; +using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using VkNet.Enums.SafetyEnums; using VkNet.Infrastructure; using VkNet.Model; using VkNet.Model.Attachments; @@ -26,115 +22,69 @@ public class GroupUpdateJsonConverter : Newtonsoft.Json.JsonConverter /// public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - if (!objectType.IsGenericType) - { - throw new TypeAccessException(); - } - if (reader.TokenType == JsonToken.Null) { return null; } - if (reader.TokenType != JsonToken.StartArray) - { - return null; - } - - var keyType = objectType.GetGenericArguments()[0]; - - var constructedListType = typeof(List<>).MakeGenericType(keyType); - - var list = (IList) Activator.CreateInstance(type: constructedListType); - - var obj = JArray.Load(reader: reader); - - foreach (var item in obj) - { - var resObj = item["object"].ToString(); - var resObj1 = resObj.Contains("client_info"); - - var type = item["type"] - .ToString(); - - var groupUpdate = type switch - { - "message_new" or "message_edit" or "message_reply" => resObj1 - ? CreateTyped(JsonConvert.DeserializeObject(resObj)) - : CreateTyped(JsonConvert.DeserializeObject(resObj)), - "message_allow" => CreateTyped(JsonConvert.DeserializeObject(resObj)), - "message_typing_state" => CreateTyped(JsonConvert.DeserializeObject(resObj)), - "vkpay_transaction" => CreateTyped(JsonConvert.DeserializeObject(resObj)), - "like_add" => CreateTyped(JsonConvert.DeserializeObject(resObj)), - "like_remove" => CreateTyped(JsonConvert.DeserializeObject(resObj)), - "group_change_settings" => CreateTyped(JsonConvert.DeserializeObject(resObj)), - "message_deny" => CreateTyped(JsonConvert.DeserializeObject(resObj)), - "photo_new" => CreateTyped(JsonConvert.DeserializeObject(resObj)), - "photo_comment_new" or "photo_comment_edit" or "photo_comment_restore" => CreateTyped(JsonConvert.DeserializeObject(resObj)), - "photo_comment_delete" => CreateTyped(JsonConvert.DeserializeObject(resObj)), - "audio_new" => CreateTyped(JsonConvert.DeserializeObject