Example #1
0
 private T JsonElementToItem(JsonElement jsonElement)
 {
     if (mapper != null)
     {
         var result = jsonElement.Deserialize <TSource>(jsonSerializerOptions);
         return(mapper.Invoke(result));
     }
     else
     {
         var result = jsonElement.Deserialize <T>(jsonSerializerOptions);
         return(result);
     }
 }
Example #2
0
        public static void JsonElementDeserialize_Null()
        {
            using JsonDocument document = JsonDocument.Parse("null");
            JsonElement dom = document.RootElement;
            MyPoco      obj = dom.Deserialize <MyPoco>();

            Assert.Null(obj);
        }
Example #3
0
        public static async Task <TResult> AsApiRespAsync <TResult, TImpl>(this Task <HttpResponseMessage> responseTask, CancellationToken token = default) where TImpl : TResult
        {
            using JsonDocument j = await responseTask.GetJsonAsync(token);

            JsonElement root = j.RootElement;

            return(root.CheckApiRespCode(out int?code)
                ? root.Deserialize <TImpl>()
                : throw MiraiHttpSession.GetCommonException(code !.Value, in root));
        }
Example #4
0
        public static void SerializeToElement_WithEscaping()
        {
            JsonElement dom = JsonSerializer.SerializeToElement("+");

            Assert.Equal(JsonValueKind.String, dom.ValueKind);
            Assert.Equal(Escaped_PlusSign, dom.GetRawText());

            string json = dom.Deserialize <string>();

            Assert.Equal("+", json);
        }
Example #5
0
    private static T Deserialize <T>(JsonElement element)
    {
        // If we were able to get the default options used by KubernetesJson,
        // deserialize directly, it's going to be faster.
        if (KubernetesJsonOptions.DefaultOptions is { } options)
        {
            element.Deserialize <T>(options);
        }

        // Else, fallback to the slower but more reliable method.
        return(KubernetesJson.Deserialize <T>(element.GetRawText()));
    }
Example #6
0
        public static async Task <TResult> AsApiRespAsync <TResult, TImpl>(this Task <HttpResponseMessage> responseTask, CancellationToken token = default) where TImpl : TResult
        {
            using JsonDocument j = await responseTask.GetJsonAsync(token);

            JsonElement root = j.RootElement;
            int         code = root.GetProperty("code").GetInt32();

            if (code == 0)
            {
                return(root.Deserialize <TImpl>());
            }
            throw MiraiHttpSession.GetCommonException(code, in root);
        }
Example #7
0
        public static async Task <TResult> AsNoSuccCodeApiRespAsync <TResult, TImpl>(this Task <HttpResponseMessage> responseTask, CancellationToken token = default) where TImpl : TResult
        {
            using JsonDocument j = await responseTask.GetJsonAsync(token);

            JsonElement root = j.RootElement;

            if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("code", out JsonElement codeElem))
            {
                int code = codeElem.GetInt32();
                if (code != 0)
                {
                    throw MiraiHttpSession.GetCommonException(code, in root);
                }
            }
            return(root.Deserialize <TImpl>());
        }
Example #8
0
        internal async Task <(Artifact Artifact, List <NameValueEntry> SourceEntries)> StopChunkAsync(string mode)
        {
            var result = await Connection.SendMessageToServerAsync(Guid, "tracingStopChunk", new Dictionary <string, object>
            {
                ["mode"] = mode,
            }).ConfigureAwait(false);

            var artifact = result.GetObject <Artifact>("artifact", Connection);
            List <NameValueEntry> sourceEntries = new() { };

            if (result.Value.TryGetProperty("sourceEntries", out var sourceEntriesElement))
            {
                var sourceEntriesEnumerator = sourceEntriesElement.EnumerateArray();
                while (sourceEntriesEnumerator.MoveNext())
                {
                    JsonElement current = sourceEntriesEnumerator.Current;
                    sourceEntries.Add(current.Deserialize <NameValueEntry>(new JsonSerializerOptions()
                    {
                        PropertyNameCaseInsensitive = true,
                    }));
                }
            }
            return(artifact, sourceEntries);
        }
Example #9
0
        private async void ReceiveMessageLoop(InternalSessionInfo session, CancellationToken token)
        {
            using ClientWebSocket ws = new ClientWebSocket();
            try
            {
                await ws.ConnectAsync(new Uri($"ws://{session.Options.Host}:{session.Options.Port}/all?sessionKey={session.SessionKey}"), token);

                while (true)
                {
                    using MemoryStream ms = await ws.ReceiveFullyAsync(token);

                    JsonElement root = JsonSerializer.Deserialize <JsonElement>(new ReadOnlySpan <byte>(ms.GetBuffer(), 0, (int)ms.Length));
                    switch (root.GetProperty("type").GetString())
                    {
                    case "BotOnlineEvent":
                    {
                        _ = InvokeAsync(Plugins, BotOnlineEvt, this, root.Deserialize <BotEventArgs>());
                        break;
                    }

                    case "BotOfflineEventActive":
                    {
                        _ = InvokeAsync(Plugins, BotPositiveOfflineEvt, this, root.Deserialize <BotEventArgs>());
                        break;
                    }

                    case "BotOfflineEventForce":
                    {
                        _ = InvokeAsync(Plugins, BotKickedOfflineEvt, this, root.Deserialize <BotEventArgs>());
                        break;
                    }

                    case "BotOfflineEventDropped":
                    {
                        _ = InvokeAsync(Plugins, BotDroppedEvt, this, root.Deserialize <BotEventArgs>());
                        break;
                    }

                    case "BotReloginEvent":
                    {
                        _ = InvokeAsync(Plugins, BotReloginEvt, this, root.Deserialize <BotEventArgs>());
                        break;
                    }

                    case "BotInvitedJoinGroupRequestEvent":
                    {
                        _ = InvokeAsync(Plugins, BotInvitedJoinGroupEvt, this, root.Deserialize <CommonGroupApplyEventArgs>());
                        break;
                    }

                    case "FriendMessage":
                    {
                        _ = InvokeAsync(Plugins, FriendMessageEvt, this, root.Deserialize <FriendMessageEventArgs>());
                        break;
                    }

                    case "GroupMessage":
                    {
                        _ = InvokeAsync(Plugins, GroupMessageEvt, this, root.Deserialize <GroupMessageEventArgs>());
                        break;
                    }

                    case "TempMessage":
                    {
                        _ = InvokeAsync(Plugins, TempMessageEvt, this, root.Deserialize <TempMessageEventArgs>());
                        break;
                    }

                    case "GroupRecallEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupMessageRevokedEvt, this, root.Deserialize <GroupMessageRevokedEventArgs>());
                        break;
                    }

                    case "FriendRecallEvent":
                    {
                        _ = InvokeAsync(Plugins, FriendMessageRevokedEvt, this, root.Deserialize <FriendMessageRevokedEventArgs>());
                        break;
                    }

                    case "BotGroupPermissionChangeEvent":
                    {
                        _ = InvokeAsync(Plugins, BotGroupPermissionChangedEvt, this, root.Deserialize <BotGroupPermissionChangedEventArgs>());
                        break;
                    }

                    case "BotMuteEvent":
                    {
                        _ = InvokeAsync(Plugins, BotMutedEvt, this, root.Deserialize <BotMutedEventArgs>());
                        break;
                    }

                    case "BotUnmuteEvent":
                    {
                        _ = InvokeAsync(Plugins, BotUnmutedEvt, this, root.Deserialize <BotUnmutedEventArgs>());
                        break;
                    }

                    case "BotJoinGroupEvent":
                    {
                        _ = InvokeAsync(Plugins, BotJoinedGroupEvt, this, root.Deserialize <GroupEventArgs>());
                        break;
                    }

                    case "BotLeaveEventActive":
                    {
                        _ = InvokeAsync(Plugins, BotPositiveLeaveGroupEvt, this, root.Deserialize <GroupEventArgs>());
                        break;
                    }

                    case "BotLeaveEventKick":
                    {
                        _ = InvokeAsync(Plugins, BotKickedOutEvt, this, root.Deserialize <GroupEventArgs>());
                        break;
                    }

                    case "GroupNameChangeEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupNameChangedEvt, this, root.Deserialize <GroupStringPropertyChangedEventArgs>());
                        break;
                    }

                    case "GroupEntranceAnnouncementChangeEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupEntranceAnnouncementChangedEvt, this, root.Deserialize <GroupStringPropertyChangedEventArgs>());
                        break;
                    }

                    case "GroupMuteAllEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupMuteAllChangedEvt, this, root.Deserialize <GroupBoolPropertyChangedEventArgs>());
                        break;
                    }

                    case "GroupAllowAnonymousChatEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupAnonymousChatChangedEvt, this, root.Deserialize <GroupBoolPropertyChangedEventArgs>());
                        break;
                    }

                    case "GroupAllowConfessTalkEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupConfessTalkChangedEvt, this, root.Deserialize <GroupBoolPropertyChangedEventArgs>());
                        break;
                    }

                    case "GroupAllowMemberInviteEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupMemberInviteChangedEvt, this, root.Deserialize <GroupBoolPropertyChangedEventArgs>());
                        break;
                    }

                    case "MemberJoinEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupMemberJoinedEvt, this, root.Deserialize <MemberEventArgs>());
                        break;
                    }

                    case "MemberLeaveEventKick":
                    {
                        _ = InvokeAsync(Plugins, GroupMemberKickedEvt, this, root.Deserialize <MemberOperatingEventArgs>());
                        break;
                    }

                    case "MemberLeaveEventQuit":
                    {
                        _ = InvokeAsync(Plugins, GroupMemberPositiveLeaveEvt, this, root.Deserialize <MemberEventArgs>());
                        break;
                    }

                    case "MemberCardChangeEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupMemberCardChangedEvt, this, root.Deserialize <GroupMemberStringPropertyChangedEventArgs>());
                        break;
                    }

                    case "MemberSpecialTitleChangeEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupMemberSpecialTitleChangedEvt, this, root.Deserialize <GroupMemberStringPropertyChangedEventArgs>());
                        break;
                    }

                    case "MemberPermissionChangeEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupMemberPermissionChangedEvt, this, root.Deserialize <GroupMemberPermissionChangedEventArgs>());
                        break;
                    }

                    case "MemberMuteEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupMemberMutedEvt, this, root.Deserialize <GroupMemberMutedEventArgs>());
                        break;
                    }

                    case "MemberUnmuteEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupMemberUnmutedEvt, this, root.Deserialize <GroupMemberUnmutedEventArgs>());
                        break;
                    }

                    case "NewFriendRequestEvent":
                    {
                        _ = InvokeAsync(Plugins, NewFriendApplyEvt, this, root.Deserialize <NewFriendApplyEventArgs>());
                        break;
                    }

                    case "MemberJoinRequestEvent":
                    {
                        _ = InvokeAsync(Plugins, GroupApplyEvt, this, root.Deserialize <CommonGroupApplyEventArgs>());
                        break;
                    }

                    default:
                    {
                        _ = InvokeAsync(Plugins, UnknownMessageEvt, this, new UnknownMessageEventArgs(root.Clone()));
                        break;
                    }
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception e)
            {
                if (Interlocked.CompareExchange(ref SessionInfo, null, session) != null)
                {
                    _ = InternalReleaseAsync(session, default); // 不异步等待, 省的抛错没地捕获
                    _ = InvokeAsync(Plugins, DisconnectedEvt, this, new DisconnectedEventArgs(e));
                }
            }
        }
 public T ObjectView <T>()
 {
     return(_original.Deserialize <T>());
 }
Example #11
0
 public override T?Deserialize <T>(JsonElement element) where T : default =>
 element.Deserialize <T>(_options);
 public Task Save(JsonElement gameJson) => _gameRepository.SaveGameAsync(gameJson.Deserialize <Game>());