Esempio n. 1
0
        public void should_fail_on_unclosed_nested_array()
        {
            var exception = Assert.Throws <ParseException>(() => ElementNode.Parse("<yada", Options));

            const string message = "Unexpected end of file while parsing Name has occurred. Line 1, position 6.";

            exception.Message.ShouldEqual(message);
            exception.FriendlyMessage.ShouldEqual("Unable to parse xml: " + message);
        }
Esempio n. 2
0
        public void should_fail_on_unclosed_outer_object()
        {
            var exception = Assert.Throws <ParseException>(() => ElementNode.Parse("<yada>", Options));

            const string message = "Unexpected end of file has occurred. The following " +
                                   "elements are not closed: yada. Line 1, position 7.";

            exception.Message.ShouldEqual(message);
            exception.FriendlyMessage.ShouldEqual("Unable to parse xml: " + message);
        }
Esempio n. 3
0
        public void should_fail_on_missing_token()
        {
            var exception = Assert.Throws <ParseException>(() => ElementNode.Parse("<oh><hai></oh> }", Options));

            const string message = "The 'hai' start tag on line 1 position 6 does not " +
                                   "match the end tag of 'oh'. Line 1, position 12.";

            exception.Message.ShouldEqual(message);
            exception.FriendlyMessage.ShouldEqual("Unable to parse xml: " + message);
        }
Esempio n. 4
0
        public void should_read_empty_root_object()
        {
            var node = ElementNode.Parse("<Yada/>", Options);

            node.ShouldTotal(0);
            node.NodeType.ShouldEqual(NodeType.Variable);
            node.IsNamed.ShouldBeTrue();
            node.Name.ShouldEqual("Yada");
            node.Format.ShouldEqual("xml");
        }
Esempio n. 5
0
        public void should_get_attribute_value()
        {
            var node     = ElementNode.Parse("<Oh Hai=\"There\" />", Options);
            var children = node.ToList();

            children.ShouldTotal(1);
            children[0].Name.ShouldEqual("Hai");
            children[0].Value.ShouldEqual("There");
            children[0].NodeType.ShouldEqual(NodeType.Value);
            children[0].IsNamed.ShouldBeTrue();
            children[0].Parent.ShouldBeSameAs(node);
        }
Esempio n. 6
0
        public void should_get_value(string element, string expectedValue)
        {
            var node     = ElementNode.Parse("<Oh>{0}</Oh>".ToFormat(element), Options);
            var children = node.ToList();

            children.ShouldTotal(1);
            children[0].Name.ShouldEqual("Hai");
            children[0].Value.ShouldEqual(expectedValue);
            children[0].NodeType.ShouldEqual(NodeType.Variable);
            children[0].IsNamed.ShouldBeTrue();
            children[0].Parent.ShouldBeSameAs(node);
        }
Esempio n. 7
0
        public void should_exclude_attributes_when_configured()
        {
            var nodes = ElementNode.Parse("<Yada O=\"rly\"><Oh>Hai</Oh></Yada>",
                                          Options.Create(x => x.Deserialization(y => y.IgnoreXmlAttributes())))
                        .Cast <XmlNodeBase>().ToList();

            nodes.ShouldTotal(1);

            var child = nodes.First();

            child.Name.ShouldEqual("Oh");
            child.NodeType.ShouldEqual(NodeType.Variable);
            child.XmlType.ShouldEqual(XmlObjectType.Element);
            child.Value.ShouldEqual("Hai");
        }
Esempio n. 8
0
        public void should_parse_and_return_children()
        {
            var node     = ElementNode.Parse("<Yada O=\"rly\"><Oh>Hai</Oh></Yada>", Options);
            var children = node.Cast <XmlNodeBase>().ToList();

            children.ShouldTotal(2);

            var child = children.First();

            child.Name.ShouldEqual("Oh");
            child.NodeType.ShouldEqual(NodeType.Variable);
            child.XmlType.ShouldEqual(XmlObjectType.Element);
            child.Value.ShouldEqual("Hai");
            child.Parent.ShouldBeSameAs(node);

            child = children.Skip(1).First();
            child.Name.ShouldEqual("O");
            child.NodeType.ShouldEqual(NodeType.Value);
            child.XmlType.ShouldEqual(XmlObjectType.Attribute);
            child.Value.ShouldEqual("rly");
            child.Parent.ShouldBeSameAs(node);
        }
Esempio n. 9
0
        public void Setup()
        {
            var json = File.ReadAllText(@"../../Performance/model.json");

            _jsonNode = new JsonNode(json);

            Enumerable.Range(0, 10).ForEach(x =>
            {
                _object = Bender.Deserializer.Create().Deserialize(_jsonNode, typeof(Model <string>));
                Bender.Serializer.Create().SerializeNodes(_object, (n, o) =>
                                                          new JsonNode(n.NodeType, new Options()), JsonNode.NodeFormat);
            });

            var xml = File.ReadAllText(@"../../Performance/model.xml");

            _xmlNode = ElementNode.Parse(xml, Options.Create());

            Enumerable.Range(0, 10).ForEach(x =>
            {
                _object = Bender.Deserializer.Create().Deserialize(_xmlNode, typeof(Model <string>));
                Bender.Serializer.Create().SerializeNodes(_object, (n, o) =>
                                                          ElementNode.Create(n.Name, Metadata.Empty, Options.Create()), XmlNodeBase.NodeFormat);
            });
        }
Esempio n. 10
0
 public XmlNodeBase DeserializeXml(Stream stream, string xsl, Encoding encoding = null)
 {
     return(ElementNode.Parse(stream, xsl, _options, encoding ?? Encoding.UTF8));
 }
Esempio n. 11
0
 public object DeserializeXml(Stream stream, string xsl, Type type, Encoding encoding = null)
 {
     return(Deserialize(ElementNode.Parse(stream, xsl, _options, encoding ?? Encoding.UTF8), type));
 }
Esempio n. 12
0
 public XmlNodeBase DeserializeXml(byte[] bytes, string xsl, Encoding encoding = null)
 {
     return(ElementNode.Parse(bytes, xsl, _options, encoding ?? Encoding.UTF8));
 }
Esempio n. 13
0
 public object DeserializeXml(byte[] bytes, Type type, Encoding encoding = null)
 {
     return(Deserialize(ElementNode.Parse(bytes, _options, encoding ?? Encoding.UTF8), type));
 }
Esempio n. 14
0
 public void should_return_null_when_there_is_no_match()
 {
     ElementNode.Parse("<Yada></Yada>", Options).GetNode("Oh").ShouldBeNull();
 }
Esempio n. 15
0
 public void should_get_node_by_name()
 {
     ElementNode.Parse("<Yada><Oh>hai</Oh></Yada>", Options).GetNode("Oh").Value.ShouldEqual("hai");
 }
Esempio n. 16
0
 public object DeserializeXml(string xml, string xsl, Type type)
 {
     return(Deserialize(ElementNode.Parse(xml, xsl, _options), type));
 }
Esempio n. 17
0
 public XmlNodeBase DeserializeXml(string xml, string xsl)
 {
     return(ElementNode.Parse(xml, xsl, _options));
 }
Esempio n. 18
0
        public void Setup()
        {
            var benchmarks = new Benchmarks
            {
                // Json

                new BenchmarkSerializer(Benchmarks.BenderSerializer, Format.Json,
                                        x => new JsonNode(x),
                                        (n, t) => Bender.Deserializer.Create().Deserialize(n, t),
                                        x => Bender.Serializer.Create().SerializeNodes(x, (n, o) =>
                                                                                       new JsonNode(n.NodeType, new Options()), JsonNode.NodeFormat),
                                        x => x.Encode().ReadToEnd()),

                new BenchmarkSerializer("JavaScriptSerializer", Format.Json,
                                        (s, t) => new JavaScriptSerializer().Deserialize(s, t),
                                        x => new JavaScriptSerializer().Serialize(x)),

                new BenchmarkSerializer("DataContractJsonSerializer", Format.Json,
                                        (s, t) => new DataContractJsonSerializer(t).Deserialize(s, t),
                                        x => new DataContractJsonSerializer(x.GetType()).Serialize(x)),

                new BenchmarkSerializer("JSON.NET", Format.Json,
                                        JsonConvert.DeserializeObject,
                                        JsonConvert.SerializeObject),

                new BenchmarkSerializer("ServiceStack", Format.Json,
                                        JsonSerializer.DeserializeFromString,
                                        JsonSerializer.SerializeToString),

                new BenchmarkSerializer("fastJSON", Format.Json,
                                        (s, t) => JSON.Instance.ToObject(s, t),
                                        x => JSON.Instance.ToJSON(x, new JSONParameters {
                    UseExtensions = false
                })),

                // Xml

                new BenchmarkSerializer(Benchmarks.BenderSerializer, Format.Xml,
                                        x => ElementNode.Parse(x, Options.Create()),
                                        (n, t) => Bender.Deserializer.Create().Deserialize(n, t),
                                        x => Bender.Serializer.Create().SerializeNodes(x, (n, o) =>
                                                                                       ElementNode.Create(n.Name, Metadata.Empty, Options.Create()), XmlNodeBase.NodeFormat),
                                        x => x.Encode().ReadToEnd()),

                new BenchmarkSerializer("XmlSerializer", Format.Xml,
                                        (s, t) => new XmlSerializer(t).Deserialize(s, t),
                                        x => new XmlSerializer(x.GetType()).Serialize(x)),

                new BenchmarkSerializer("DataContractSerializer", Format.Xml,
                                        (s, t) => new DataContractSerializer(t).Deserialize(s, t),
                                        x => new DataContractSerializer(x.GetType()).Serialize(x)),

                new BenchmarkSerializer("ServiceStack", Format.Xml,
                                        ServiceStack.Text.XmlSerializer.DeserializeFromString,
                                        ServiceStack.Text.XmlSerializer.SerializeToString)
            };

            benchmarks.Run(typeof(Model <>), Format.Xml, "Performance/model.xml");
            benchmarks.Run(typeof(Model <>), Format.Json, "Performance/model.json");

            const string benchmarkPath = "Performance/Benchmarks/";

            Console.Write(benchmarks.GetTextSummaryReport());
            Console.WriteLine();
            Console.Write(benchmarks.GetTextDetailReport());
            Console.WriteLine();
            Console.Write(benchmarks.GetTextDetailReport(benchmarkPath + "Report.Detail.txt"));

            benchmarks.SaveSummaryGraph(benchmarkPath + "All.png");
            benchmarks.SaveSummaryGraph(benchmarkPath + "Cold.png", warm: false);
            benchmarks.SaveSummaryGraph(benchmarkPath + "Warm.png", false);

            benchmarks.SaveJsonSummaryReport(benchmarkPath + "Report.json");
            benchmarks.SaveTextSummaryReport(benchmarkPath + "Report.txt");
            benchmarks.SaveTextDetailReport(benchmarkPath + "Report.Detail.txt");

            var results = benchmarks.GetResults().OrderBy(x => x.Name).ToList();

            _xmlResults  = results.Where(x => x.Format == Format.Xml).ToList();
            _jsonResults = results.Where(x => x.Format == Format.Json).ToList();
        }