private void TokenizeUserData()

in src/StreamJsonRpc/JsonMessageFormatter.cs [613:668]


        private void TokenizeUserData(JsonRpcMessage jsonRpcMessage)
        {
            try
            {
                this.serializingMessageWithId = jsonRpcMessage is IJsonRpcMessageWithId msgWithId ? msgWithId.RequestId : default;
                switch (jsonRpcMessage)
                {
                    case Protocol.JsonRpcRequest request:
                        this.serializingRequest = true;

                        if (request.ArgumentsList is not null)
                        {
                            var tokenizedArgumentsList = new JToken[request.ArgumentsList.Count];
                            for (int i = 0; i < request.ArgumentsList.Count; i++)
                            {
                                tokenizedArgumentsList[i] = this.TokenizeUserData(request.ArgumentListDeclaredTypes?[i], request.ArgumentsList[i]);
                            }

                            request.ArgumentsList = tokenizedArgumentsList;
                        }
                        else if (request.Arguments is not null)
                        {
                            if (this.ProtocolVersion.Major < 2)
                            {
                                throw new NotSupportedException(Resources.ParameterObjectsNotSupportedInJsonRpc10);
                            }

                            // Tokenize the user data using the user-supplied serializer.
                            JObject? paramsObject = JObject.FromObject(request.Arguments, this.JsonSerializer);

                            // Json.Net TypeHandling could insert a $type child JToken to the paramsObject above.
                            // This $type JToken should not be there to maintain Json RPC Spec compatibility. We will
                            // strip the token out here.
                            if (this.JsonSerializer.TypeNameHandling != TypeNameHandling.None)
                            {
                                paramsObject.Remove("$type");
                            }

                            request.Arguments = paramsObject;
                        }

                        break;
                    case Protocol.JsonRpcResult result:
                        result.Result = this.TokenizeUserData(result.ResultDeclaredType, result.Result);
                        break;
                    case Protocol.JsonRpcError error when error.Error is object:
                        error.Error.Data = error.Error.Data is object ? this.TokenizeUserData(typeof(object), error.Error.Data) : null;
                        break;
                }
            }
            finally
            {
                this.serializingMessageWithId = default;
                this.serializingRequest = false;
            }
        }