public JToken Serialize()

in src/StreamJsonRpc/JsonMessageFormatter.cs [446:487]


        public JToken Serialize(JsonRpcMessage message)
        {
            try
            {
                this.observedTransmittedRequestWithStringId |= message is JsonRpcRequest request && request.RequestId.String is not null;

                // Pre-tokenize the user data so we can use their custom converters for just their data and not for the base message.
                this.TokenizeUserData(message);

                var json = JToken.FromObject(message, DefaultSerializer);

                // Fix up dropped fields that are mandatory
                if (message is Protocol.JsonRpcResult && json["result"] is null)
                {
                    json["result"] = JValue.CreateNull();
                }

                if (this.ProtocolVersion.Major == 1 && json["id"] is null)
                {
                    // JSON-RPC 1.0 requires the id property to be present even for notifications.
                    json["id"] = JValue.CreateNull();
                }

                // Copy over extra top-level properties.
                if (message is IMessageWithTopLevelPropertyBag { TopLevelPropertyBag: { } bag })
                {
                    foreach (JProperty property in bag.Properties)
                    {
                        if (json[property.Name] is null)
                        {
                            json[property.Name] = property.Value;
                        }
                    }
                }

                return json;
            }
            catch (Exception ex)
            {
                throw new JsonSerializationException(string.Format(CultureInfo.CurrentCulture, Resources.ErrorWritingJsonRpcMessage, ex.GetType().Name, ex.Message), ex);
            }
        }