コード例 #1
0
        public void GetTokens_ExpandoObjectNested_ReturnsObjectTokens()
        {
            dynamic input = new ExpandoObject();

            input.foo   = true;
            input.array = new object[]
            {
                false, 1, 2, Math.PI, null, "hello world."
            };

            var expected = new[]
            {
                ModelGrammar.TokenObjectBegin("object"),
                ModelGrammar.TokenProperty("foo"),
                ModelGrammar.TokenPrimitive(true),
                ModelGrammar.TokenProperty("array"),
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenPrimitive(false),
                ModelGrammar.TokenPrimitive(1),
                ModelGrammar.TokenPrimitive(2),
                ModelGrammar.TokenPrimitive(Math.PI),
                ModelGrammar.TokenPrimitive(null),
                ModelGrammar.TokenPrimitive("hello world."),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenObjectEnd
            };

            var walker = new ModelWalker(new DataWriterSettings());
            var actual = walker.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
コード例 #2
0
        public void GetTokens_ArrayMultiItem_ReturnsArrayTokens()
        {
            var input = new object[]
            {
                false,
                true,
                null,
                'a',
                'b',
                'c',
                1,
                2,
                3
            };

            var expected = new[]
            {
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenFalse,
                ModelGrammar.TokenTrue,
                ModelGrammar.TokenNull,
                ModelGrammar.TokenPrimitive('a'),
                ModelGrammar.TokenPrimitive('b'),
                ModelGrammar.TokenPrimitive('c'),
                ModelGrammar.TokenPrimitive(1),
                ModelGrammar.TokenPrimitive(2),
                ModelGrammar.TokenPrimitive(3),
                ModelGrammar.TokenArrayEnd
            };

            var walker = new ModelWalker(new DataWriterSettings());
            var actual = walker.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
コード例 #3
0
        public void GetTokens_ListEmpty_ReturnsEmptyArrayTokens()
        {
            var input = new List <object>(0);

            var expected = new[]
            {
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenArrayEnd
            };

            var walker = new ModelWalker(new DataWriterSettings());
            var actual = walker.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
コード例 #4
0
        public void GetTokens_ArraySingleItem_ReturnsSingleItemArrayTokens()
        {
            var input = new object[]
            {
                null
            };

            var expected = new[]
            {
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenNull,
                ModelGrammar.TokenArrayEnd
            };

            var walker = new ModelWalker(new DataWriterSettings());
            var actual = walker.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
コード例 #5
0
        public void GetTokens_GraphCycleTypeIgnore_ReplacesCycleStartWithNull()
        {
            var input = new Person
            {
                Name   = "John, Jr.",
                Father = new Person
                {
                    Name = "John, Sr."
                },
                Mother = new Person
                {
                    Name = "Sally"
                }
            };

            // create multiple cycles
            input.Father.Children = input.Mother.Children = new Person[]
            {
                input
            };

            var walker = new ModelWalker(new DataWriterSettings
            {
                GraphCycles = GraphCycleType.Ignore
            });

            var expected = new[]
            {
                ModelGrammar.TokenObjectBegin("Person"),
                ModelGrammar.TokenProperty("Name"),
                ModelGrammar.TokenPrimitive("John, Jr."),

                ModelGrammar.TokenProperty("Father"),
                ModelGrammar.TokenObjectBegin("Person"),
                ModelGrammar.TokenProperty("Name"),
                ModelGrammar.TokenPrimitive("John, Sr."),
                ModelGrammar.TokenProperty("Father"),
                ModelGrammar.TokenNull,
                ModelGrammar.TokenProperty("Mother"),
                ModelGrammar.TokenNull,
                ModelGrammar.TokenProperty("Children"),
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenNull,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenObjectEnd,

                ModelGrammar.TokenProperty("Mother"),
                ModelGrammar.TokenObjectBegin("Person"),
                ModelGrammar.TokenProperty("Name"),
                ModelGrammar.TokenPrimitive("Sally"),
                ModelGrammar.TokenProperty("Father"),
                ModelGrammar.TokenNull,
                ModelGrammar.TokenProperty("Mother"),
                ModelGrammar.TokenNull,
                ModelGrammar.TokenProperty("Children"),
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenNull,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenObjectEnd,

                ModelGrammar.TokenProperty("Children"),
                ModelGrammar.TokenNull,

                ModelGrammar.TokenObjectEnd
            };

            var actual = walker.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
コード例 #6
0
        public void GetTokens_GraphComplex_ReturnsObjectTokens()
        {
            var input = new object[] {
                "JSON Test Pattern pass1",
                new Dictionary <string, object>
                {
                    { "object with 1 member", new[] { "array with 1 element" } },
                },
                new Dictionary <string, object>(),
                new object[0],
                -42,
                true,
                false,
                null,
                new Dictionary <string, object> {
                    { "integer", 1234567890 },
                    { "real", -9876.543210 },
                    { "e", 0.123456789e-12 },
                    { "E", 1.234567890E+34 },
                    { "", 23456789012E66 },
                    { "zero", 0 },
                    { "one", 1 },
                    { "space", " " },
                    { "quote", "\"" },
                    { "backslash", "\\" },
                    { "controls", "\b\f\n\r\t" },
                    { "slash", "/ & /" },
                    { "alpha", "abcdefghijklmnopqrstuvwyz" },
                    { "ALPHA", "ABCDEFGHIJKLMNOPQRSTUVWYZ" },
                    { "digit", "0123456789" },
                    { "0123456789", "digit" },
                    { "special", "`1~!@#$%^&*()_+-={':[,]}|;.</>?" },
                    { "hex", "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A" },
                    { "true", true },
                    { "false", false },
                    { "null", null },
                    { "array", new object[0] },
                    { "object", new Dictionary <string, object>() },
                    { "address", "50 St. James Street" },
                    { "url", "http://www.JSON.org/" },
                    { "comment", "// /* <!-- --" },
                    { "# -- --> */", " " },
                    { " s p a c e d ", new [] { 1, 2, 3, 4, 5, 6, 7 } },
                    { "compact", new [] { 1, 2, 3, 4, 5, 6, 7 } },
                    { "jsontext", "{\"object with 1 member\":[\"array with 1 element\"]}" },
                    { "quotes", "&#34; \u0022 %22 0x22 034 &#x22;" },
                    { "/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?", "A key can be any string" }
                },
                0.5,
                98.6,
                99.44,
                1066,
                1e1,
                0.1e1,
                1e-1,
                1e00,
                2e+00,
                2e-00,
                "rosebud"
            };

            var expected = new[]
            {
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenPrimitive("JSON Test Pattern pass1"),
                ModelGrammar.TokenObjectBegin("object"),
                ModelGrammar.TokenProperty("object with 1 member"),
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenPrimitive("array with 1 element"),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenObjectEnd,
                ModelGrammar.TokenObjectBegin("object"),
                ModelGrammar.TokenObjectEnd,
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenPrimitive(-42),
                ModelGrammar.TokenTrue,
                ModelGrammar.TokenFalse,
                ModelGrammar.TokenNull,
                ModelGrammar.TokenObjectBegin("object"),
                ModelGrammar.TokenProperty("integer"),
                ModelGrammar.TokenPrimitive(1234567890),
                ModelGrammar.TokenProperty("real"),
                ModelGrammar.TokenPrimitive(-9876.543210),
                ModelGrammar.TokenProperty("e"),
                ModelGrammar.TokenPrimitive(0.123456789e-12),
                ModelGrammar.TokenProperty("E"),
                ModelGrammar.TokenPrimitive(1.234567890E+34),
                ModelGrammar.TokenProperty(""),
                ModelGrammar.TokenPrimitive(23456789012E66),
                ModelGrammar.TokenProperty("zero"),
                ModelGrammar.TokenPrimitive(0),
                ModelGrammar.TokenProperty("one"),
                ModelGrammar.TokenPrimitive(1),
                ModelGrammar.TokenProperty("space"),
                ModelGrammar.TokenPrimitive(" "),
                ModelGrammar.TokenProperty("quote"),
                ModelGrammar.TokenPrimitive("\""),
                ModelGrammar.TokenProperty("backslash"),
                ModelGrammar.TokenPrimitive("\\"),
                ModelGrammar.TokenProperty("controls"),
                ModelGrammar.TokenPrimitive("\b\f\n\r\t"),
                ModelGrammar.TokenProperty("slash"),
                ModelGrammar.TokenPrimitive("/ & /"),
                ModelGrammar.TokenProperty("alpha"),
                ModelGrammar.TokenPrimitive("abcdefghijklmnopqrstuvwyz"),
                ModelGrammar.TokenProperty("ALPHA"),
                ModelGrammar.TokenPrimitive("ABCDEFGHIJKLMNOPQRSTUVWYZ"),
                ModelGrammar.TokenProperty("digit"),
                ModelGrammar.TokenPrimitive("0123456789"),
                ModelGrammar.TokenProperty("0123456789"),
                ModelGrammar.TokenPrimitive("digit"),
                ModelGrammar.TokenProperty("special"),
                ModelGrammar.TokenPrimitive("`1~!@#$%^&*()_+-={':[,]}|;.</>?"),
                ModelGrammar.TokenProperty("hex"),
                ModelGrammar.TokenPrimitive("\u0123\u4567\u89AB\uCDEF\uabcd\uef4A"),
                ModelGrammar.TokenProperty("true"),
                ModelGrammar.TokenTrue,
                ModelGrammar.TokenProperty("false"),
                ModelGrammar.TokenFalse,
                ModelGrammar.TokenProperty("null"),
                ModelGrammar.TokenNull,
                ModelGrammar.TokenProperty("array"),
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenProperty("object"),
                ModelGrammar.TokenObjectBegin("object"),
                ModelGrammar.TokenObjectEnd,
                ModelGrammar.TokenProperty("address"),
                ModelGrammar.TokenPrimitive("50 St. James Street"),
                ModelGrammar.TokenProperty("url"),
                ModelGrammar.TokenPrimitive("http://www.JSON.org/"),
                ModelGrammar.TokenProperty("comment"),
                ModelGrammar.TokenPrimitive("// /* <!-- --"),
                ModelGrammar.TokenProperty("# -- --> */"),
                ModelGrammar.TokenPrimitive(" "),
                ModelGrammar.TokenProperty(" s p a c e d "),
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenPrimitive(1),
                ModelGrammar.TokenPrimitive(2),
                ModelGrammar.TokenPrimitive(3),
                ModelGrammar.TokenPrimitive(4),
                ModelGrammar.TokenPrimitive(5),
                ModelGrammar.TokenPrimitive(6),
                ModelGrammar.TokenPrimitive(7),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenProperty("compact"),
                ModelGrammar.TokenArrayBegin("array"),
                ModelGrammar.TokenPrimitive(1),
                ModelGrammar.TokenPrimitive(2),
                ModelGrammar.TokenPrimitive(3),
                ModelGrammar.TokenPrimitive(4),
                ModelGrammar.TokenPrimitive(5),
                ModelGrammar.TokenPrimitive(6),
                ModelGrammar.TokenPrimitive(7),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenProperty("jsontext"),
                ModelGrammar.TokenPrimitive("{\"object with 1 member\":[\"array with 1 element\"]}"),
                ModelGrammar.TokenProperty("quotes"),
                ModelGrammar.TokenPrimitive("&#34; \u0022 %22 0x22 034 &#x22;"),
                ModelGrammar.TokenProperty("/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"),
                ModelGrammar.TokenPrimitive("A key can be any string"),
                ModelGrammar.TokenObjectEnd,
                ModelGrammar.TokenPrimitive(0.5),
                ModelGrammar.TokenPrimitive(98.6),
                ModelGrammar.TokenPrimitive(99.44),
                ModelGrammar.TokenPrimitive(1066),
                ModelGrammar.TokenPrimitive(10.0),
                ModelGrammar.TokenPrimitive(1.0),
                ModelGrammar.TokenPrimitive(0.1),
                ModelGrammar.TokenPrimitive(1.0),
                ModelGrammar.TokenPrimitive(2.0),
                ModelGrammar.TokenPrimitive(2.0),
                ModelGrammar.TokenPrimitive("rosebud"),
                ModelGrammar.TokenArrayEnd
            };

            var walker = new ModelWalker(new DataWriterSettings());
            var actual = walker.GetTokens(input).ToArray();

            Assert.Equal(expected, actual);
        }
コード例 #7
0
        public void Format_ArrayNestedDeeply_ReturnsExpectedArray()
        {
            // input from pass2.json in test suite at http://www.json.org/JSON_checker/
            var input = new[]
            {
                MarkupGrammar.TokenElementBegin(new DataName("array")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenElementBegin(new DataName("item")),
                MarkupGrammar.TokenPrimitive("Not too deep"),
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd,
                MarkupGrammar.TokenElementEnd
            };
            var expected = new[]
            {
                ModelGrammar.TokenArrayBeginUnnamed,
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenArrayBegin(new DataName("item")),
                ModelGrammar.TokenPrimitive("Not too deep"),
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd,
                ModelGrammar.TokenArrayEnd
            };

            var transformer = new XmlReader.XmlInTransformer(new DataReaderSettings());
            var actual      = transformer.Transform(input).ToArray();

            Assert.Equal(expected, actual);
        }
コード例 #8
0
ファイル: XmlInTransformer.cs プロジェクト: lgh7010/projectcc
            private IList <Token <ModelTokenType> > TransformElement(IStream <Token <MarkupTokenType> > input, bool isStandAlone)
            {
                Token <MarkupTokenType> token = input.Peek();

                DataName elementName = this.DecodeName(token.Name, typeof(Object));
                bool     isVoid      = (token.TokenType == MarkupTokenType.ElementVoid);

                input.Pop();

                IDictionary <DataName, IList <IList <Token <ModelTokenType> > > > children = null;

                while (!input.IsCompleted)
                {
                    token = input.Peek();
                    if (token.TokenType == MarkupTokenType.ElementEnd ||
                        (isVoid && token.TokenType != MarkupTokenType.Attribute))
                    {
                        if (!isVoid)
                        {
                            input.Pop();
                        }

                        List <Token <ModelTokenType> > output = new List <Token <ModelTokenType> >();

                        if ((children == null) ||
                            (children.Count <= 1) ||
                            elementName == XmlInTransformer.DefaultArrayName)
                        {
                            DataName childName = DataName.Empty;
                            IList <IList <Token <ModelTokenType> > > items = null;
                            if (children != null)
                            {
                                // grab the first
                                using (var enumerator = children.GetEnumerator())
                                {
                                    if (enumerator.MoveNext())
                                    {
                                        items     = enumerator.Current.Value;
                                        childName = enumerator.Current.Key;
                                    }
                                }
                            }

                            if ((items != null && items.Count > 1) ||
                                (items == null && elementName == XmlInTransformer.DefaultArrayName) ||
                                childName == XmlInTransformer.DefaultItemName)
                            {
                                // if only child has more than one grandchild
                                // then whole element is acutally an array
                                output.Add(elementName.IsEmpty ? ModelGrammar.TokenArrayBeginUnnamed : ModelGrammar.TokenArrayBegin(this.DecodeName(elementName, typeof(Array))));

                                if (items != null)
                                {
                                    foreach (var item in items)
                                    {
                                        output.AddRange(item);
                                    }
                                }

                                output.Add(ModelGrammar.TokenArrayEnd);
                                return(output);
                            }
                        }

                        if (isStandAlone)
                        {
                            output.Add(elementName.IsEmpty ? ModelGrammar.TokenObjectBeginUnnamed : ModelGrammar.TokenObjectBegin(elementName));
                        }

                        if (children != null)
                        {
                            foreach (var property in children)
                            {
                                if (property.Value.Count == 1)
                                {
                                    if (isStandAlone)
                                    {
                                        // if the parent is a stand alone object then child is a property
                                        DataName name = this.DecodeName(property.Key, typeof(Object));
                                        output.Add(name.IsEmpty ? ModelGrammar.TokenProperty(elementName) : ModelGrammar.TokenProperty(name));
                                    }
                                    output.AddRange(property.Value[0]);
                                    continue;
                                }

                                if (property.Key.IsEmpty)
                                {
                                    // skip mixed content
                                    continue;
                                }

                                // wrap values in array
                                output.Add(property.Key.IsEmpty ? ModelGrammar.TokenArrayBeginUnnamed : ModelGrammar.TokenArrayBegin(this.DecodeName(property.Key, typeof(Array))));
                                foreach (var item in property.Value)
                                {
                                    output.AddRange(item);
                                }
                                output.Add(ModelGrammar.TokenArrayEnd);
                            }
                        }
                        else if (!isStandAlone)
                        {
                            output.Add(ModelGrammar.TokenNull);
                        }

                        if (isStandAlone)
                        {
                            output.Add(ModelGrammar.TokenObjectEnd);
                        }

                        return(output);
                    }

                    DataName propertyName = token.Name;
                    if (token.TokenType == MarkupTokenType.Attribute)
                    {
                        input.Pop();
                    }

                    if (children == null)
                    {
                        children = new Dictionary <DataName, IList <IList <Token <ModelTokenType> > > >();
                    }
                    if (!children.ContainsKey(propertyName))
                    {
                        children[propertyName] = new List <IList <Token <ModelTokenType> > >();
                    }

                    var child = this.TransformValue(input, !isStandAlone);
                    if (child.Count == 1 &&
                        child[0].TokenType == ModelTokenType.Primitive &&
                        child[0].Value != null &&
                        CharUtility.IsNullOrWhiteSpace(child[0].ValueAsString()))
                    {
                        // skip whitespace mixed content
                        continue;
                    }

                    children[propertyName].Add(child);
                }

                throw new TokenException <MarkupTokenType>(
                          token,
                          XmlInTransformer.ErrorUnterminatedObject);
            }