internal static TypedData ToTypedData()

in src/Utility/TypeExtensions.cs [211:261]


        internal static TypedData ToTypedData(this object value, bool isDurableData = false)
        {
            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();
                    break;
                case string str:
                    if (IsValidJson(str)) { typedData.Json = str; } else { typedData.String = str; }
                    break;
                default:
                    typedData.Json = ConvertToJson(originalValue, isDurableData);
                    break;
            }
            return typedData;
        }