Beispiel #1
0
        public void RootInFilterWithInitializers()
        {
            JObject rootObject = new JObject
            {
                { "referenceDate", DateTime.MinValue },
                {
                    "dateObjectsArray",
                    new[] {
                        new JObject {
                            { "date", DateTime.MinValue }
                        },
                        new JObject {
                            { "date", DateTime.MaxValue }
                        },
                        new JObject {
                            { "date", DateTime.Now }
                        },
                        new JObject {
                            { "date", DateTime.MinValue }
                        },
                    }
                }
            };

            var result = rootObject.SelectTokens("$.dateObjectsArray[?(@.date == $.referenceDate)]").ToList();

            Assert.AreEqual(2, result.Count);
        }
Beispiel #2
0
        public void EvaluateSinglePropertyReturningArray()
        {
            var o = new JObject()
            {
                ["Blah"] = new[] { 1, 2, 3 }
            };

            var t = o.SelectTokens("Blah").First();

            Assert.IsNotNull(t);
            Assert.IsInstanceOf <IEnumerable>(t);

            t = o.SelectTokens("Blah[2]").First();
            Assert.IsInstanceOf <int>(t);
            Assert.AreEqual(3, t);
        }
Beispiel #3
0
        public void EvaluateMissingPropertyIndexWithError()
        {
            var o = new JObject {
                ["Blah"] = 1
            };

            Assert.Throws <Exception>(() => { o.SelectTokens("['Missing','Missing2']", true).First(); }, "Property 'Missing' does not exist on JObject.");
        }
Beispiel #4
0
        public void EvaluateSliceOnObjectWithError()
        {
            var o = new JObject {
                ["Blah"] = 1
            };

            Assert.Throws <Exception>(() => { o.SelectTokens("[:]", true).First(); }, @"Array slice is not valid on JObject.");
        }
Beispiel #5
0
        public void EvaluateWildcardIndexOnObjectWithError()
        {
            var o = new JObject {
                ["Blah"] = 1
            };

            Assert.Throws <Exception>(() => { o.SelectTokens("[*]", true).First(); }, @"Index * not valid on JObject.");
        }
Beispiel #6
0
        public void EvaluatePropertyWithoutError()
        {
            var o = new JObject()
            {
                ["Blah"] = 1
            };

            Assert.AreEqual(1, o.SelectTokens("Blah", true).First());
        }
Beispiel #7
0
        public void EvaluateIndexerOnObjectWithError()
        {
            var o = new JObject()
            {
                ["Blah"] = 1
            };

            Assert.Throws <ArgumentOutOfRangeException>(() => { o.SelectTokens("[1]", true).First(); }, @"Index 1 not valid on JObject.");
        }
Beispiel #8
0
        public void EvaluateIndexerOnObject()
        {
            var o = new JObject()
            {
                ["Blah"] = 1
            };

            CollectionAssert.IsEmpty(o.SelectTokens("[1]"));
        }
Beispiel #9
0
        public void EvaluateMissingProperty()
        {
            var o = new JObject()
            {
                ["Blah"] = 1
            };

            CollectionAssert.IsEmpty(o.SelectTokens("Missing[1]"));
        }
Beispiel #10
0
        public void QuoteName()
        {
            var o = new JObject()
            {
                ["Blah"] = 1
            };

            var t = o.SelectTokens("['Blah']").First();

            Assert.IsNotNull(t);
            Assert.IsInstanceOf <int>(t);
            Assert.AreEqual(1, t);
        }
Beispiel #11
0
        public void EvaluateWildcardProperty()
        {
            var o = new JObject()
            {
                ["Blah"]  = 1,
                ["Blah2"] = 2
            };

            var t = o.SelectTokens("$.*").ToList();

            Assert.IsNotNull(t);
            Assert.AreEqual(2, t.Count);
            Assert.AreEqual(1, t[0]);
            Assert.AreEqual(2, t[1]);
        }