public void CanReadComplexObject( )
        {
            var complex = new ComplexObject {
                Name = "Name", Version = new Version("1.2.3.4"), TypeCode = TypeCode.DBNull
            };

            using var output = new MemoryStream( );
            using var writer = new IO.BinaryWriterWith7BitEncoding(output);

            BinaryFormatWriter.Write(writer, typeof(ComplexObject), complex);

            using var input  = new MemoryStream(output.ToArray( ));
            using var reader = new IO.BinaryReaderWith7BitEncoding(input);

            var deserializedComplex = BinaryFormatReader.Read <ComplexObject> (reader);

            Assert.NotNull(deserializedComplex);
            if (deserializedComplex == null)
            {
                return;
            }

            Assert.Equal(complex.Name, deserializedComplex.Name);
            Assert.Equal(complex.Version, deserializedComplex.Version);
            Assert.Equal(complex.TypeCode, deserializedComplex.TypeCode);
            Assert.Null(deserializedComplex.Parent);
        }
Example #2
0
        /// <summary>
        /// Saves the PList to the specified stream.
        /// </summary>
        /// <param name="rootNode">Root node of the PList structure.</param>
        /// <param name="stream">The stream in which the PList is saves.</param>
        /// <param name="format">The format of the PList (Binary/Xml).</param>
        public static void Save(PNode rootNode, Stream stream, PListFormat format)
        {
            if (format == PListFormat.Xml)
            {
                var sets = new XmlWriterSettings
                {
                    Encoding     = Encoding.UTF8,
                    Indent       = true,
                    IndentChars  = "\t",
                    NewLineChars = "\n",
                };

                using (var xmlWriter = XmlWriter.Create(stream, sets))
                {
                    xmlWriter.WriteStartDocument();
                    xmlWriter.WriteDocType("plist", "-//Apple Computer//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);

                    // write out nodes, wrapped in plist root element
                    xmlWriter.WriteStartElement("plist");
                    xmlWriter.WriteAttributeString("version", "1.0");
                    rootNode.WriteXml(xmlWriter);
                    xmlWriter.WriteEndElement();
                    xmlWriter.Flush();
                }
            }
            else
            {
                var writer = new BinaryFormatWriter();
                writer.Write(stream, rootNode);
            }
        }
        public void CanReadObjectUsingConverter( )
        {
            var surrogate = new BinaryFormatSurrogate( );

            surrogate.AddConverter(ReadComplexObjectNameOnly, WriteComplexObjectNameOnly);

            var complex = new ComplexObject {
                Name = "Name", Version = new Version("1.2.3.4"), TypeCode = TypeCode.DBNull
            };

            using var output = new MemoryStream( );
            using var writer = new IO.BinaryWriterWith7BitEncoding(output);

            BinaryFormatWriter.Write(writer, typeof(ComplexObject), complex, surrogate);

            using var input  = new MemoryStream(output.ToArray( ));
            using var reader = new IO.BinaryReaderWith7BitEncoding(input);

            var deserializedComplex = BinaryFormatReader.Read <ComplexObject> (reader, surrogate);

            Assert.NotNull(deserializedComplex);
            if (deserializedComplex == null)
            {
                return;
            }

            Assert.Equal(complex.Name, deserializedComplex.Name);
            Assert.Equal(default, deserializedComplex.Version);
Example #4
0
        /// <summary>
        /// Saves the PList to the specified stream.
        /// </summary>
        /// <param name="rootNode">Root node of the PList structure.</param>
        /// <param name="stream">The stream in which the PList is saves.</param>
        /// <param name="format">The format of the PList (Binary/Xml).</param>
        public static void Save(PNode rootNode, Stream stream, PListFormat format)
        {
            if (format == PListFormat.Xml)
            {
                const string newLine = "\n";

                var sets = new XmlWriterSettings
                {
                    Encoding     = Encoding.UTF8,
                    Indent       = true,
                    IndentChars  = "\t",
                    NewLineChars = newLine,
                };

                using (var tmpStream = new MemoryStream())
                {
                    using (var xmlWriter = XmlWriter.Create(tmpStream, sets))
                    {
                        xmlWriter.WriteStartDocument();
                        xmlWriter.WriteDocType("plist", "-//Apple Computer//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);

                        // write out nodes, wrapped in plist root element
                        xmlWriter.WriteStartElement("plist");
                        xmlWriter.WriteAttributeString("version", "1.0");
                        rootNode.WriteXml(xmlWriter);
                        xmlWriter.WriteEndElement();
                        xmlWriter.Flush();
                    }

                    // XmlWriter always inserts a space before element closing (e.g. <true />)
                    // whereas the Apple parser can't deal with the space and expects <true/>
                    tmpStream.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(tmpStream))
                        using (var writer = new StreamWriter(stream, Encoding.UTF8, 4096, true))
                        {
                            writer.NewLine = newLine;
                            for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
                            {
                                if (line.Trim() == "<true />")
                                {
                                    line = line.Replace("<true />", "<true/>");
                                }
                                if (line.Trim() == "<false />")
                                {
                                    line = line.Replace("<false />", "<false/>");
                                }

                                writer.WriteLine(line);
                            }
                        }
                }
            }
            else
            {
                var writer = new BinaryFormatWriter();
                writer.Write(stream, rootNode);
            }
        }
        public void CanWriteNullString( )
        {
            using var stream = new MemoryStream( );
            using var writer = new IO.BinaryWriterWith7BitEncoding(stream);

            BinaryFormatWriter.Write <string> (writer, null);

            Assert.Equal(stream.ToArray( ), new byte [] { 0 });
        }
        public void CanWriteNullObject( )
        {
            var complex = (ComplexObject?)null;

            using var stream = new MemoryStream( );
            using var writer = new IO.BinaryWriterWith7BitEncoding(stream);

            BinaryFormatWriter.Write <ComplexObject> (writer, complex);

            Assert.Equal(stream.ToArray( ), new byte [] { 0 });
        }
        public void CanWriteComplexObject( )
        {
            var complex = new ComplexObject {
                Name = "Name", Version = new Version("1.2.3.4"), TypeCode = TypeCode.DBNull
            };

            using var stream = new MemoryStream( );
            using var writer = new IO.BinaryWriterWith7BitEncoding(stream);

            BinaryFormatWriter.Write <ComplexObject> (writer, complex);

            Assert.Equal(stream.ToArray( ),
                         new byte [] { 1, 1, 4, (byte)'N', (byte)'a', (byte)'m', (byte)'e',
                                       1, 1, 2, 3, 4,
                                       (byte)TypeCode.DBNull,
                                       0 });
        }