public static void Serialize <T>(Stream stream, T value, ITextSerializer <T> textSerializer)
 {
     using (var textWriter = StreamWriterHelper.NewLeaveOpen(stream))
     {
         textSerializer.Serialize(textWriter, value);
     }
 }
Ejemplo n.º 2
0
        public static void Serialize <T>(Stream stream, T obj)
        {
            using (var writer = StreamWriterHelper.NewLeaveOpen(stream))
            {
                var serializer = new JsonSerializer();

                serializer.Serialize(writer, obj);
            }
        }
Ejemplo n.º 3
0
        public static void Serialize <T>(Stream stream, T obj)
        {
            using (var writer = StreamWriterHelper.NewLeaveOpen(stream))
            {
                var xmlSerializer = new XmlSerializer(typeof(T));

                xmlSerializer.Serialize(writer, obj);
            }
        }
        public static void Serialize(string solutionFilePath, SolutionFile obj, bool overwrite = true)
        {
            using (var fileStream = FileStreamHelper.NewWrite(solutionFilePath, overwrite))
                using (var textWriter = StreamWriterHelper.NewLeaveOpenAddBOM(fileStream))
                {
                    var solutionFileTextSerialializer = new SolutionFileTextSerializer();

                    solutionFileTextSerialializer.Serialize(textWriter, obj);
                }
        }
        private static void EnsureBOMProduced()
        {
            var tempFilePath = @"C:\Temp\temp.txt";

            using (var fileStream = FileStreamHelper.NewWrite(tempFilePath))
                using (var writer = StreamWriterHelper.NewLeaveOpenAddBOM(fileStream))
                {
                    writer.WriteLine("Hello world! (BOM?)");
                }
        }
Ejemplo n.º 6
0
        public async Task Serialize(string filePath, IEnumerable <ServiceDescriptorDescription> descriptions)
        {
            var descriptionsInOrderArray = descriptions
                                           .OrderBy(x => x.ServiceType)
                                           .ToArray();

            var json = JsonConvert.SerializeObject(descriptionsInOrderArray);

            using (var streamWriter = StreamWriterHelper.NewWrite(filePath))
            {
                await streamWriter.WriteAsync(json);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Writes XML without any namespaces.
        /// </summary>
        public static void SerializeWithoutNamespaces <T>(Stream stream, T obj, XmlWriterSettings xmlWriterSettings)
        {
            // Required to suppress the "xmlns:xsi" and "xmlns:xsd" on the root element.
            var xmlSerializerNamespaces = new XmlSerializerNamespaces();

            xmlSerializerNamespaces.Add(String.Empty, String.Empty);

            using (var writer = StreamWriterHelper.NewLeaveOpen(stream))
                using (var xmlWriter = XmlWriter.Create(writer, xmlWriterSettings))
                    using (var namespaceSuppressingWriter = new NamespaceSupressingXmlWriter(xmlWriter)) // Suppresses all element namespaces.
                    {
                        var xmlSerializer = new XmlSerializer(typeof(T));

                        xmlSerializer.Serialize(namespaceSuppressingWriter, obj, xmlSerializerNamespaces);
                    }
        }
        public void Serialize(Stream stream, T value)
        {
            StreamWriter GetStreamWriter()
            {
                var streamWriter = this.StreamSerializerOptions.Value.AddByteOrderMark
                    ? StreamWriterHelper.NewLeaveOpenAddBOM(stream)
                    : StreamWriterHelper.NewLeaveOpen(stream)
                ;

                return(streamWriter);
            }

            using (var textWriter = GetStreamWriter())
            {
                this.TextSerializer.Serialize(textWriter, value);
            }
        }
        public async Task Describe(IServiceCollection services)
        {
            var servicesInOrder = services.OrderBy(x => x.ServiceType.FullName);

            var serviceCollectionDescriptionFilePath = await this.ServiceCollectionDescriptionFilePathProvider.GetServiceCollectionDescriptionFilePath();

            using (var fileWriter = StreamWriterHelper.NewWrite(serviceCollectionDescriptionFilePath))
            {
                var serviceCount = services.Count;

                await fileWriter.WriteLineAsync($"Services count: {serviceCount}\n\n");

                foreach (var serviceDescriptor in servicesInOrder)
                {
                    var description = await this.ServiceDescriptorDescriber.Describe(serviceDescriptor);

                    await fileWriter.WriteLineAsync(description);
                }
            }
        }
        internal void Log(LogLevel logLevel, string categoryName, EventId eventId, string formattedStateAndException)
        {
            lock (this.LockObject)
            {
                using (var memoryStream = new MemoryStream())
                {
                    using (var writer = StreamWriterHelper.NewLeaveOpen(memoryStream))
                    {
                        this.PerformLog(writer, logLevel, categoryName, eventId, formattedStateAndException);
                    }

                    using (var fileStream = new FileStream(this.LogFilePath, FileMode.OpenOrCreate))
                    {
                        fileStream.Seek(0, SeekOrigin.End);

                        memoryStream.Seek(0, SeekOrigin.Begin);
                        memoryStream.CopyTo(fileStream);
                    }
                }
            }
        }