Пример #1
0
		public void QueryArrayItems_OrderByThenByLast_ReturnsMultipleObjects()
		{
			// use convention over configuration on the way out
			var reader = new JsonReader(new DataReaderSettings(new ConventionResolverStrategy(ConventionResolverStrategy.WordCasing.CamelCase)));
			// use POCO on the way out
			var writer = new JsonWriter(new DataWriterSettings() { PrettyPrint=true });

			string input =
@"[
	{ ""personId"": 1, ""firstName"": ""Sally"", ""lastName"": ""Smith"" },
	{ ""personId"": 2, ""firstName"": ""Bob"", ""lastName"": ""Smith"" },
	{ ""personId"": 3, ""firstName"": ""John"", ""lastName"": ""Jones"" },
	{ ""personId"": 4, ""firstName"": ""Suzie"", ""lastName"": ""Jones"" }
]";

			var people = reader.Query<Person>(input);
			var query =
				from person in people.ArrayItems()
				orderby person.LastName, person.FirstName
				select person.PersonID;

			Assert.Equal(1, query.Last());

			const string expected =
@"[
	3,
	4,
	2,
	1
]";

			string actual = writer.Write(query);

			Assert.Equal(expected, actual);
		}
Пример #2
0
		public void QueryDescendants_MatchingPropertyValue_ReturnsStronglyTypedObject()
		{
			// input from pass1.json in test suite at http://www.json.org/JSON_checker/
			const string input = @"[
    ""JSON Test Pattern pass1"",
    {""object with 1 member"":[""array with 1 element""]},
    {},
    [],
    -42,
    true,
    false,
    null,
    {
        ""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"":[  ],
        ""object"":{  },
        ""address"": ""50 St. James Street"",
        ""url"": ""http://www.JSON.org/"",
        ""comment"": ""// /* <!-- --"",
        ""# -- --> */"": "" "",
        "" s p a c e d "" :[1,2 , 3

,

4 , 5        ,          6           ,7        ],""compact"":[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 ComplexType
			{
				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 = new Uri("http://www.JSON.org/"),
				comment = "// /* <!-- --",
				Comments = " ",
				spaced = 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;",
				A_key_can_be_any_string = "A key can be any string"
			};

			var reader = new JsonReader(new DataReaderSettings(new JsonResolverStrategy()));
			var source = reader.Query<ComplexType>(input);

			var query =
				from foo in source.Descendants()
				where foo.url == new Uri("http://www.JSON.org/")
				select foo;

			var actual = query.FirstOrDefault();

			Assert.Equal(expected, actual, false);
		}
Пример #3
0
		public void QueryDescendants_WhereOrderByLast_ReturnsMultipleObjects()
		{
			// respect DataContracts on the way in
			var reader = new JsonReader(new DataReaderSettings(new DataContractResolverStrategy()));
			// use convention over configuration on the way out
			var writer = new JsonWriter(new DataWriterSettings(new ConventionResolverStrategy(ConventionResolverStrategy.WordCasing.Lowercase, "-")));

			string input =
@"[
	{ ""id"": 1, ""first"": ""Foo"", ""last"": ""Bar"" },
	{ ""id"": 2, ""first"": ""etc."", ""last"": ""et al."" },
	{ ""id"": 3, ""first"": ""Blah"", ""last"": ""Yada"" }
]";

			var people = reader.Query<Person>(input);
			var query =
				from person in people.Descendants()
				where person.PersonID == 1 || person.FirstName == "Blah"
				orderby person.PersonID
				select person;

			Assert.Equal("Yada", query.Last().LastName);

			const string expected = @"[{""person-id"":1,""first-name"":""Foo"",""last-name"":""Bar""},{""person-id"":3,""first-name"":""Blah"",""last-name"":""Yada""}]";

			string actual = writer.Write(query);

			Assert.Equal(expected, actual);
		}