summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/test/System.Json.Test.Integration/JsonValueLinqExtensionsIntegrationTest.cs
blob: 7efd67d87f3e3962eafa803a4e3b4f936e6e39e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Linq;
using Xunit;

namespace System.Json
{
    /// <summary>
    /// Tests for the Linq extensions to the <see cref="JsonValue"/> types.
    /// </summary>
    public class JsonValueLinqExtensionsIntegrationTest
    {
        /// <summary>
        /// Test for the <see cref="JsonValueLinqExtensions.ToJsonArray"/> method.
        /// </summary>
        [Fact]
        public void ToJsonArrayTest()
        {
            string json = "{\"SearchResponse\":{\"Phonebook\":{\"Results\":[{\"name\":1,\"rating\":1}, {\"name\":2,\"rating\":2}, {\"name\":3,\"rating\":3}]}}}";
            string expected = "[{\"name\":2,\"rating\":2},{\"name\":3,\"rating\":3}]";

            JsonValue jv = JsonValue.Parse(json);
            double rating = 1;
            var jsonResult = from n in jv.ValueOrDefault("SearchResponse", "Phonebook", "Results")
                             where n.Value.ValueOrDefault("rating").ReadAs<double>(0.0) > rating
                             select n.Value;
            var ja = jsonResult.ToJsonArray();

            Assert.Equal(expected, ja.ToString());
        }

        /// <summary>
        /// Test for the <see cref="JsonValueLinqExtensions.ToJsonObject"/> method.
        /// </summary>
        [Fact]
        public void ToJsonObjectTest()
        {
            string json = "{\"Name\":\"Bill Gates\",\"Age\":23,\"AnnualIncome\":45340.45,\"MaritalStatus\":\"Single\",\"EducationLevel\":\"MiddleSchool\",\"SSN\":432332453,\"CellNumber\":2340393420}";
            string expected = "{\"AnnualIncome\":45340.45,\"SSN\":432332453,\"CellNumber\":2340393420}";

            JsonValue jv = JsonValue.Parse(json);
            decimal decVal;
            var jsonResult = from n in jv
                             where n.Value.TryReadAs<decimal>(out decVal) && decVal > 100
                             select n;
            var jo = jsonResult.ToJsonObject();

            Assert.Equal(expected, jo.ToString());
        }

        /// <summary>
        /// Test for the <see cref="JsonValueLinqExtensions.ToJsonObject"/> method where the origin is a <see cref="JsonArray"/>.
        /// </summary>
        [Fact]
        public void ToJsonObjectFromArrayTest()
        {
            string json = "{\"SearchResponse\":{\"Phonebook\":{\"Results\":[{\"name\":1,\"rating\":1}, {\"name\":2,\"rating\":2}, {\"name\":3,\"rating\":3}]}}}";
            string expected = "{\"1\":{\"name\":2,\"rating\":2},\"2\":{\"name\":3,\"rating\":3}}";

            JsonValue jv = JsonValue.Parse(json);
            double rating = 1;
            var jsonResult = from n in jv.ValueOrDefault("SearchResponse", "Phonebook", "Results")
                             where n.Value.ValueOrDefault("rating").ReadAs<double>(0.0) > rating
                             select n;
            var jo = jsonResult.ToJsonObject();

            Assert.Equal(expected, jo.ToString());
        }
    }
}