internal static TypedData ToTypedData(this object value, PowerShellManager psHelper)
        {
            if (value is TypedData self)
            {
                return(self);
            }

            TypedData typedData = new TypedData();

            if (value == null)
            {
                return(typedData);
            }

            // Save the original value.
            // We will use the original value when converting to JSON, so members added by ETS can be captured in the serialization.
            var originalValue = value;

            if (value is PSObject psObj && psObj.BaseObject != null)
            {
                value = psObj.BaseObject;
            }

            switch (value)
            {
            case double d:
                typedData.Double = d;
                break;

            case long l:
                typedData.Int = l;
                break;

            case int i:
                typedData.Int = i;
                break;

            case byte[] arr:
                typedData.Bytes = ByteString.CopyFrom(arr);
                break;

            case Stream s:
                typedData.Stream = ByteString.FromStream(s);
                break;

            case HttpResponseContext http:
                typedData.Http = http.ToRpcHttp(psHelper);
                break;

            case string str:
                if (IsValidJson(str))
                {
                    typedData.Json = str;
                }
                else
                {
                    typedData.String = str;
                }
                break;

            default:
                if (psHelper == null)
                {
                    throw new ArgumentNullException(nameof(psHelper));
                }
                typedData.Json = psHelper.ConvertToJson(originalValue);
                break;
            }
            return(typedData);
        }