public static void ConvertValueToStream()

in src/WebJobs.Script/Binding/FunctionBinding.cs [299:355]


        public static void ConvertValueToStream(object value, Stream stream)
        {
            Stream valueStream = value as Stream;
            if (valueStream == null)
            {
                // Convert the value to bytes and write it
                // to the stream
                byte[] bytes = null;
                Type type = value.GetType();
                if (type == typeof(byte[]))
                {
                    bytes = (byte[])value;
                }
                else if (type == typeof(string))
                {
                    bytes = Encoding.UTF8.GetBytes((string)value);
                }
                else if (type == typeof(int))
                {
                    bytes = BitConverter.GetBytes((int)value);
                }
                else if (type == typeof(long))
                {
                    int val = unchecked((int)((long)value));
                    bytes = BitConverter.GetBytes(val);
                }
                else if (type == typeof(bool))
                {
                    bytes = BitConverter.GetBytes((bool)value);
                }
                else if (type == typeof(double))
                {
                    bytes = BitConverter.GetBytes((double)value);
                }
                else if (value is JToken)
                {
                    JToken jToken = (JToken)value;
                    string json = jToken.ToString(Formatting.None);
                    bytes = Encoding.UTF8.GetBytes(json);
                }
                else if (value is ExpandoObject)
                {
                    string json = Utility.ToJson((ExpandoObject)value, Formatting.None);
                    bytes = Encoding.UTF8.GetBytes(json);
                }

                using (valueStream = new MemoryStream(bytes))
                {
                    valueStream.CopyTo(stream);
                }
            }
            else
            {
                // value is already a stream, so copy it directly
                valueStream.CopyTo(stream);
            }
        }