Example #1
0
        static void Main()
        {
            var config = new Config
            {
                Variant = "Simple",
                Enabled = true,
                Urls    = { "http://example.com", "http://www.example.com" }
            };

            var xmlString = new StringBuilder();
            var xmlWriter = new SimpleXmlWriter(XmlWriter.Create(xmlString, new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                Indent             = true
            }));

            Serialize.To(xmlWriter, config);
            xmlWriter.Flush();
            Console.WriteLine(xmlString);

            const string configString =
                @"<Config>
  <Urls>
    <Item>http://example.com</Item>
  </Urls>
  <Enabled>false</Enabled>
  <Variant>Complex</Variant>
</Config>";

            var reader = new SimpleXmlReader(XmlReader.Create(new StringReader(configString)));

            config = Deserialize <Config> .From(reader);

            Debug.Assert(config.Enabled == false);
        }
Example #2
0
        public static void SerializeXml <T>(IBonded <T> obj, Stream stream)
        {
            var writer = new SimpleXmlWriter(stream, new SimpleXmlWriter.Settings {
                UseNamespaces = true
            });

            Serialize.To(writer, obj);
            writer.Flush();
        }
Example #3
0
        public static void SerializeXml <T>(T obj, Stream stream)
        {
            var hasBase = Schema <T> .RuntimeSchema.HasBase;
            var writer  = new SimpleXmlWriter(stream, new SimpleXmlWriter.Settings {
                UseNamespaces = hasBase
            });

            Serialize.To(writer, obj);
            writer.Flush();
        }
Example #4
0
        public static string SerializeXmlString <T>(T obj)
        {
            var builder = new StringBuilder();
            var writer  = new SimpleXmlWriter(XmlWriter.Create(builder, new XmlWriterSettings {
                OmitXmlDeclaration = true, Indent = true
            }));

            Serialize.To(writer, obj);
            writer.Flush();
            return(builder.ToString());
        }
        public static Stream ToXmlStream <T>(this T bondObject)
        {
            var stream = new MemoryStream();
            var writer = new SimpleXmlWriter(stream);

            Serialize.To(writer, bondObject);

            writer.Flush();
            stream.Position = 0;
            return(stream);
        }
        public Task <byte[]> Serialize <TValue>(TValue value)
        {
            var destStream = new MemoryStream();

            var writer = new SimpleXmlWriter(destStream);

            global::Bond.Serialize.To(writer, value);

            writer.Flush();

            return(Task.FromResult(destStream.ToArray()));
        }
Example #7
0
        public static void TranscodeCBXml <From>(Stream from, Stream to)
        {
            var input  = new InputStream(from, 11);
            var reader = new CompactBinaryReader <InputStream>(input);

            var hasBase = Schema <From> .RuntimeSchema.HasBase;
            var writer  = new SimpleXmlWriter(to, new SimpleXmlWriter.Settings {
                UseNamespaces = hasBase
            });

            Transcode <From> .FromTo(reader, writer);

            writer.Flush();
        }
Example #8
0
        private void Check <T>(T value, string result)
        {
            var writer  = collection.Get(typeof(T));
            var builder = new StringBuilder();

            using (
                var xmlWriter =
                    new SimpleXmlWriter(XmlWriter.Create(builder,
                                                         new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                ConformanceLevel = ConformanceLevel.Fragment
            })))
                writer.Write(value, xmlWriter);
            Assert.AreEqual(result, builder.ToString());
        }
Example #9
0
        static void Main()
        {
            var src = new Example
            {
                Widgets =
                {
                    new Widget {
                        Name = "Konfabulator", Number = 3.14
                    }
                }
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter <OutputBuffer>(output);

            Serialize.To(writer, src);

            var input  = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader <InputBuffer>(input);

            var xmlString = new StringBuilder();
            var xmlWriter = new SimpleXmlWriter(XmlWriter.Create(xmlString, new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                Indent             = true
            }));

            Transcode <Example> .FromTo(reader, xmlWriter);

            xmlWriter.Flush();

            string[] expectedLines =
                @"<Example>
  <Widgets>
    <Item>
      <Widget>
        <Name>Konfabulator</Name>
        <Number>3.14</Number>
      </Widget>
    </Item>
  </Widgets>
</Example>".Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);

            string[] actualLines = xmlString.ToString().Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);

            ThrowIfFalse(expectedLines.SequenceEqual(actualLines));
        }
Example #10
0
        public void XmlSerialization_NullNonNullableString_Throws()
        {
            var xmlString = new StringBuilder();
            var xmlWriter = new SimpleXmlWriter(XmlWriter.Create(xmlString));

            var nullString = new BasicTypes {
                _str = null
            };

            Assert.Throws <NullReferenceException>(() => Serialize.To(xmlWriter, nullString));

            var nullWString = new BasicTypes {
                _wstr = null
            };

            Assert.Throws <NullReferenceException>(() => Serialize.To(xmlWriter, nullWString));
        }
Example #11
0
        static void Main()
        {
            var src = new Example
            {
                Widgets =
                {
                    new Widget {
                        Name = "Konfabulator", Number = 3.14
                    }
                }
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter <OutputBuffer>(output);

            Serialize.To(writer, src);

            var input  = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader <InputBuffer>(input);

            var xmlString = new StringBuilder();
            var xmlWriter = new SimpleXmlWriter(XmlWriter.Create(xmlString, new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                Indent             = true
            }));

            Transcode <Example> .FromTo(reader, xmlWriter);

            xmlWriter.Flush();

            Debug.Assert(xmlString.ToString() ==
                         @"<Example>
  <Widgets>
    <Item>
      <Widget>
        <Name>Konfabulator</Name>
        <Number>3.14</Number>
      </Widget>
    </Item>
  </Widgets>
</Example>");
        }
Example #12
0
        void RoundtripObjectBuffer <T>(T obj)
        {
            // SimpleBinary
            {
                var output = new OutputBuffer();
                var writer = new SimpleBinaryWriter <OutputBuffer>(output);
                Serialize.To(writer, obj);

                var input  = new InputBuffer(output.Data);
                var reader = new SimpleBinaryReader <InputBuffer>(input);
                T   obj2   = Deserialize <T> .From(reader);

                Assert.True(Comparer.Equal <T>(obj, obj2));
            }

            // CompactBinary
            {
                var output = new OutputBuffer();
                var writer = new CompactBinaryWriter <OutputBuffer>(output);
                Serialize.To(writer, obj);

                var input  = new InputBuffer(output.Data);
                var reader = new CompactBinaryReader <InputBuffer>(input);
                T   obj2   = new Deserializer <CompactBinaryReader <InputBuffer> >(typeof(T)).Deserialize <T>(reader);

                Assert.True(Comparer.Equal <T>(obj, obj2));
            }

            // SimpleXML
            {
                var xmlString = new StringBuilder();
                var xmlWriter = new SimpleXmlWriter(XmlWriter.Create(xmlString));

                Serialize.To(xmlWriter, obj);
                xmlWriter.Flush();

                var reader = new SimpleXmlReader(XmlReader.Create(new StringReader(xmlString.ToString())));
                T   obj2   = new Deserializer <SimpleXmlReader>(typeof(T)).Deserialize <T>(reader);

                Assert.True(Comparer.Equal <T>(obj, obj2));
            }
        }
Example #13
0
        public void RountripFoo()
        {
            var sb  = new StringBuilder();
            var foo = new Foo {
                Value = 1
            };

            using (XmlWriter xmlWriter = XmlWriter.Create(sb))
            {
                var writer = new SimpleXmlWriter(xmlWriter);
                Serialize.To(writer, foo);
                writer.Flush();
            }
            var xml = sb.ToString();

            Console.Write(xml);
            using (var xmlReader = XmlReader.Create(new StringReader(xml)))
            {
                var reader       = new SimpleXmlReader(xmlReader);
                var roundtripped = Deserialize <Foo> .From(reader);

                Assert.AreEqual(foo.Value, roundtripped.Value);
            }
        }