Ejemplo n.º 1
0
    public void TestJsonAttribute()
    {
        var jArr = new JsonArrayNode
        {
            "abc",
            true,
            "defg"
        };

        jArr.AddNull();
        jArr.Add(1234);
        var jObj = new JsonObjectNode();

        jObj.SetValue("hijk", jArr);
        jObj.SetValue("lmn", "opq");
        jObj.SetValue("rst", "56789K");
        Assert.AreEqual(56789000, jObj.TryGetInt32Value("rst"));
        Assert.AreEqual(56789000, jObj["rst"].GetInt64());
        jObj.SetValue("rst", 56789);
        jObj.SetValue("uvw", false);
        jObj.SetNullValue("x");
        jObj.SetValue("y", new JsonObjectNode());
        jObj.SetValue("z", 0);

        var model = new JsonAttributeTestModel
        {
            A = new DateTime(2020, 1, 1),
            B = new DateTime(2020, 1, 2),
            C = new DateTime(2020, 1, 3),
            D = new DateTime(2020, 1, 4),
            E = new DateTime(2020, 1, 5),
            F = jObj,
            G = jArr,
            H = new List <string>
            {
                "!@#$%^",
                null,
                "&()_+-="
            },
            I = new HashSet <string>
            {
                "***",
                string.Empty,
                "|||||||"
            }
        };
        var str    = JsonSerializer.Serialize(model);
        var model2 = JsonSerializer.Deserialize <JsonAttributeTestModel>(str);

        Assert.AreEqual(model.A, model2.A);
        Assert.AreEqual(model.B, model2.B);
        Assert.AreEqual(model.C, model2.C);
        Assert.AreEqual(model.D, model2.D);
        Assert.AreEqual(model.E, model2.E);
        Assert.AreEqual(model.F.Count, model2.F.Count);
        Assert.AreEqual(model.G.Count, model2.G.Count);
        Assert.AreEqual(model.H.Count, model2.H.Count);
        Assert.AreEqual(model.I.Count - 1, model2.I.Count);

        model = new JsonAttributeTestModel
        {
            A = new DateTime(2020, 1, 1),
            B = null,
            C = new DateTime(2020, 1, 3),
            D = null,
            E = new DateTime(2020, 1, 5)
        };
        str    = JsonSerializer.Serialize(model);
        model2 = JsonSerializer.Deserialize <JsonAttributeTestModel>(str);
        Assert.AreEqual(model.A, model2.A);
        Assert.AreEqual(model.B, model2.B);
        Assert.AreEqual(model.C, model2.C);
        Assert.AreEqual(model.D, model2.D);
        Assert.AreEqual(model.E, model2.E);
        Assert.AreEqual(null, model2.F);
        Assert.AreEqual(null, model2.G);
        Assert.AreEqual(null, model2.H);
        Assert.AreEqual(null, model2.I);

        str    = @"{
""H"": "":,.;/| "",
""I"": ""abcdefg hijklmn    opq\trst\n\nuvw\rxyz"",
""J"": ""123456"",
""K"": ""[11.1, 76.9)"",
""L"": ""[, 999999999999)"",
""M"": ""[3.1415926, " + Numbers.InfiniteSymbol + @"]"",
""N"": ""[3.6.0, 5.0.0)"",
""O"": 2,
""P"": ""unauthorized"",
""R"": ""#CC3333"",
""S"": ""rgba(240, 240, 16, 0.8)"",
""T"": { ""b"": 255 },
""V"": ""unknown"",
""W"": 17.24
}";
        model2 = JsonSerializer.Deserialize <JsonAttributeTestModel>(str);
        Assert.AreEqual(1, model2.H.Count);
        Assert.AreEqual(":,.;/| ", model2.H[0]);
        Assert.AreEqual(6, model2.I.Count);
        Assert.AreEqual((uint)123456, model2.J);
        Assert.IsNotNull(model2.K);
        Assert.AreEqual(12, model2.K.MinValue);
        Assert.IsFalse(model2.K.LeftOpen);
        Assert.AreEqual(76, model2.K.MaxValue);
        Assert.IsFalse(model2.K.RightOpen);
        Assert.IsFalse(model2.K.IsInInterval(11));
        Assert.IsTrue(model2.K.IsInInterval(12));
        Assert.IsTrue(model2.K.IsInInterval(76));
        Assert.IsFalse(model2.K.IsInInterval(77));
        Assert.IsNotNull(model2.L);
        Assert.IsNull(model2.L.MinValue);
        Assert.IsTrue(model2.L.LeftOpen);
        Assert.IsFalse(model2.L.LeftBounded);
        Assert.AreEqual(999999999999, model2.L.MaxValue);
        Assert.IsTrue(model2.L.RightOpen);
        Assert.IsTrue(model2.L.RightBounded);
        Assert.IsNotNull(model2.M);
        Assert.AreEqual(3.1415926, model2.M.MinValue);
        Assert.IsFalse(model2.M.LeftOpen);
        Assert.IsTrue(model2.M.LeftBounded);
        Assert.IsTrue(model2.M.RightOpen);
        Assert.IsFalse(model2.M.RightBounded);
        Assert.IsFalse(model2.M.IsInInterval(3));
        Assert.IsTrue(model2.M.IsInInterval(3.1415926));
        Assert.IsTrue(model2.M.IsInInterval(200000000000000));
        Assert.IsNotNull(model2.N);
        Assert.AreEqual("3.6.0", model2.N.MinValue);
        Assert.IsFalse(model2.N.LeftOpen);
        Assert.IsTrue(model2.N.LeftBounded);
        Assert.AreEqual("5.0.0", model2.N.MaxValue);
        Assert.IsTrue(model2.N.RightOpen);
        Assert.IsTrue(model2.N.RightBounded);
        Assert.IsFalse(model2.N.IsInInterval("3.0.0.0"));
        Assert.IsTrue(model2.N.IsInInterval("3.6.0.0"));
        Assert.IsTrue(model2.N.IsInInterval("4.2.0.0"));
        Assert.IsFalse(model2.N.IsInInterval("5.0.0.0"));
        Assert.IsFalse(model2.N.IsInInterval("6.0.0.0"));
        Assert.IsNotNull(model2.N.MinVersion);
        Assert.AreEqual(6, model2.N.MinVersion.Minor);
        Assert.AreEqual(Data.ChangeErrorKinds.Unauthorized, model2.O);
        Assert.AreEqual(Data.ChangeErrorKinds.Unauthorized, model2.P);
        Assert.AreEqual(Data.ChangeErrorKinds.None, model2.Q);
        Assert.AreEqual(0xCC, model2.R.R);
        Assert.AreEqual(240, model2.S.G);
        Assert.IsTrue(model.S.A <0.9 && model2.S.A> 0.7);
        Assert.AreEqual(255, model2.T.B);
        Assert.IsNull(model2.U);
        Assert.AreEqual(Data.ChangeMethods.Unknown, model2.V);
        Assert.AreEqual(17, model2.W.Degree);
        Assert.IsTrue(model2.W.Arcminute > 0);
        str = JsonSerializer.Serialize(model2);
        Assert.IsNotNull(str);
    }