Exemplo n.º 1
0
    public void TestJsonObject()
    {
        var now  = DateTime.Now;
        var json = CreateModel(now);

        Assert.AreEqual(9, json.Keys.Count());
        Assert.AreEqual("hijklmn", json.GetStringValue("str-a"));
        Assert.AreEqual("hijklmn", json.GetValue <string>("str-a"));
        Assert.AreEqual("rst", json.GetStringValue("str-b"));
        Assert.AreEqual("uvw", json.GetStringValue("str-c"));
        Assert.AreEqual("0123456789", json.GetStringValue("str-d"));
        Assert.AreEqual(123, json.GetInt32Value("num"));
        Assert.AreEqual("123", json.GetStringValue("num"));
        Assert.AreEqual(now.Second, json.GetDateTimeValue("now").Second);
        Assert.AreEqual(now.Second, json.GetValue <DateTime>("now").Second);
        Assert.AreEqual(now.Second, json.GetDateTimeValue("ticks").Second);
        Assert.AreEqual(JsonValueKind.String, json.GetValueKind("now"));
        Assert.AreEqual(JsonValueKind.Number, json.GetValueKind("ticks"));
        Assert.AreEqual(JsonValueKind.Object, json.GetValueKind("props"));
        Assert.AreEqual(JsonValueKind.Array, json.GetValueKind("arr"));
        Assert.AreEqual(json, json.Clone());

        var props = json.GetObjectValue("props");
        var p1    = new JsonObjectNode
        {
            { "p6", "()()" },
            { "p7", 4567 },
            { "p9", "2020W295" },
        };

        p1.SetValue("p2", true);
        p1.SetValue("p3", p1);
        props.SetValue("p1", p1);
        props.SetValue("p4", "p5");
        Assert.AreEqual(props, json.GetValue <JsonObjectNode>("props"));
        Assert.AreEqual(props, json.GetValue <IJsonValueNode>("props"));
        Assert.AreNotEqual(json, p1);
        Assert.IsNotNull(json.GetObjectValue("props", "p1", "p3"));
        Assert.IsTrue(json.GetObjectValue("props", "p1", "p3").GetBooleanValue("p2"));
        Assert.IsFalse(json.TryGetObjectValue("props", "p1").ContainsKey("p8"));
        Assert.AreEqual(17, json.GetObjectValue("props", "p1").GetDateTimeValue("p9").Day);
        Assert.AreEqual(2020, json.GetValue <DateTime>("props", "p1", "p9").Year);
        Assert.AreEqual(0, json.GetValue <DateTime>("props", "p1", "p9").Second);
        Assert.AreEqual(2020, json.TryGetValue <DateTime>("props", "p1", "p9").Year);
        Assert.IsNull(json.TryGetObjectValue("props", "p1", "p3", "p6"));
        Assert.AreEqual(4567, p1.GetInt32Value("p7"));
        Assert.AreEqual(4567, p1.GetValue <int>("p7"));
        p1.SetValue("p9", "Wed, 21 Oct 2015 07:28:00 GMT");
        Assert.AreEqual(28, json.GetObjectValue("props", "p1").GetDateTimeValue("p9").Minute);
        Assert.AreEqual(10, json.GetValue <DateTime>("props", "p1", "p9").Month);
        Assert.AreEqual(2015, json.GetValue <DateTime>("props", "p1", "p9").Year);
        p1.SetValue("p9", "2022-01-03T04:56:00");
        Assert.AreEqual(56, json.GetObjectValue("props", "p1").GetDateTimeValue("p9").Minute);
        Assert.AreEqual(1, json.GetValue <DateTime>("props", "p1", "p9").Month);
        Assert.AreEqual(2022, json.GetValue <DateTime>("props", "p1", "p9").Year);
        p1.SetValue("p9", "2010-1-2");
        Assert.AreEqual(2010, json.GetValue <DateTime>("props", "p1", "p9").Year);
        Assert.AreEqual(2, json.GetValue <DateTime>("props", "p1", "p9").Day);
        p1.SetValue("p9", "7月17日");
        Assert.AreEqual(DateTime.UtcNow.Year, json.GetValue <DateTime>("props", "p1", "p9").Year);
        Assert.AreEqual(17, json.GetValue <DateTime>("props", "p1", "p9").Day);
        p1.SetValue("p9", "сегодня");
        Assert.AreEqual(DateTime.UtcNow.Day, json.GetValue <DateTime>("props", "p1", "p9").Day);
        Assert.AreEqual(0, json.GetValue <DateTime>("props", "p1", "p9").Minute);
        p1.SetValue("p9", "сейчас");
        Assert.AreEqual(DateTime.UtcNow.Day, json.GetValue <DateTime>("props", "p1", "p9").Day);
        p1.SetValue("p9", "어제");
        Assert.AreEqual((DateTime.UtcNow - TimeSpan.FromDays(1)).Day, json.GetValue <DateTime>("props", "p1", "p9").Day);
        p1.SetValue("p9", "mañana");
        Assert.AreEqual((DateTime.UtcNow + TimeSpan.FromDays(1)).Day, json.GetValue <DateTime>("props", "p1", "p9").Day);
        p1.SetValue("p9", "2020W295");
        Assert.AreEqual(7, json.GetValue <DateTime>("props", "p1", "p9").Month);
        Assert.AreEqual("2020W295", json.GetValue <string>("props", "p1", "p9"));

        json.EnableThreadSafeMode(3);
        Assert.IsTrue(json.GetObjectValue("props", "p1", "p3").GetBooleanValue("p2"));
        Assert.IsTrue(json["props", "p1", "p3", "p2"].GetBoolean());
        Assert.IsTrue(json.TryGetObjectValue("props", "p1", "p3").GetBooleanValue("p2"));
        Assert.IsTrue(json.TryGetValue("props.p1.p3.p2", true).GetBoolean());
        Assert.IsTrue(json.TryGetValue("props. 'p1'.'p3'.p2", true).GetBoolean());
        Assert.IsTrue(json.TryGetValue("[props]['p1'][p3][ 'p2' ]", true).GetBoolean());
        Assert.IsTrue(json.GetValue <bool>("props", "p1", "p3", "p2"));
        try
        {
            var testError = json["props", "p1", "q", "p3"];
            Assert.Fail();
        }
        catch (InvalidOperationException)
        {
        }
        catch (ArgumentException)
        {
        }

        try
        {
            var testError = json["props", "p1", "p3", "q"];
            Assert.Fail();
        }
        catch (InvalidOperationException)
        {
        }
        catch (ArgumentException)
        {
        }

        json["dot.net"] = new JsonArrayNode
        {
            "Text",
            new JsonObjectNode
            {
                { "name", "value" },
                { ".[][][]....\"\'\\.", "test" }
            }
        };
        Assert.AreEqual(4567, p1.GetInt32Value("p7"));
        Assert.IsTrue(json.ContainsKey("dot.net"));
        Assert.AreEqual("Text", json["dot.net", 0].GetString());
        Assert.AreEqual(JsonValueKind.Array, json["dot.net"].ValueKind);
        Assert.AreEqual("e", json.TryGetValue("[dot.net][1]['.[][][]....\\\"\\\'\\\\.'][1]", true).GetString());
        Assert.AreEqual(4, json.TryGetValue("'dot.net'.1.\".[][][]....\\\"\\\'\\\\.\".length", true).GetInt32());
        json.Remove("dot.net");
        Assert.IsFalse(json.ContainsKey("dot.net"));
        Assert.IsNull(json.TryGetValue("dot.net"));

        var jsonArray = json.GetArrayValue("arr");

        Assert.AreEqual(0, jsonArray.Count);
        jsonArray.Add("*+-\"\'\\");
        jsonArray.Add(456);
        jsonArray.AddRange(jsonArray);
        jsonArray.Remove(2);
        Assert.AreEqual(456, jsonArray.GetInt32Value(1));
        jsonArray.InsertRange(1, jsonArray);
        Assert.AreEqual(6, jsonArray.Count);
        Assert.AreEqual("*+-\"\'\\", jsonArray.GetStringValue(0));
        Assert.AreEqual(jsonArray.GetStringValue(0), jsonArray.GetStringValue(1));
        Assert.AreEqual(JsonValueKind.Number, jsonArray.GetValueKind(2));
        Assert.AreEqual(jsonArray.GetStringValue(2, true), jsonArray.GetStringValue(3, true));
        Assert.AreEqual(jsonArray.GetInt32Value(2), jsonArray.GetInt32Value(4));
        Assert.AreEqual(jsonArray.GetInt32Value(2), jsonArray.GetInt32Value(5));
        jsonArray.SetValue(0, 7);
        jsonArray.SetValue(1, 0);
        jsonArray.AddRange(new[] { 8, 9 });
        jsonArray.AddNull();
        Assert.AreEqual(9, jsonArray.Count);
        Assert.AreEqual(7, jsonArray.GetInt32Value(0));
        Assert.AreNotEqual(jsonArray.GetInt32Value(0), jsonArray.GetInt32Value(1));
        Assert.IsTrue(json.TryGetValue(new List <string> {
            "arr"
        }, out _));
        Assert.IsFalse(json.TryGetValue(new List <string> {
            "arr", "some"
        }, out _));
        Assert.AreEqual(7, json.GetValue("arr", "0").GetInt32());
        Assert.AreEqual(7, json.GetValue("arr", null, null, "0", string.Empty).GetInt32());
        Assert.IsNull(json.TryGetValue(new[] { "arr", "0", "x" }));
        Assert.AreEqual(9, jsonArray.GetStringCollection().ToList().Count);

        jsonArray.Remove(jsonArray.Count - 1);
        var m = json.Deserialize <JsonModel>();

        Assert.AreEqual(json.GetStringValue("str-a"), m.A);
        Assert.AreEqual(json.GetStringValue("str-b"), m.B);
        Assert.AreEqual(json.GetStringValue("str-c"), m.C);
        Assert.AreEqual(json.GetInt32Value("num"), m.Num);
        Assert.IsInstanceOfType(m.Col, typeof(IEnumerable <int>));
        var list = m.Col.ToList();

        Assert.AreEqual(jsonArray.Count, list.Count);
        Assert.AreEqual(0, list[1]);
        Assert.AreEqual(456, list[2]);
        Assert.AreEqual(8, list[6]);
        Assert.AreEqual(9, list[7]);
        jsonArray.AddNull();
        Assert.AreEqual(JsonValues.Null, jsonArray[jsonArray.Count - 1]);
        jsonArray.Add(new JsonArrayNode
        {
            8, 9, 0
        });
        jsonArray.Add(null as string);
        jsonArray.AddNull();
        jsonArray.RemoveNull();
        Assert.AreEqual(JsonValueKind.Array, jsonArray[jsonArray.Count - 1].ValueKind);

        var jsonDoc = (JsonDocument)json;
        var jsonStr = json.ToString();

        Assert.IsTrue(jsonStr.StartsWith("{") && jsonStr.Length > 20);
        Assert.AreEqual(jsonStr, jsonDoc.RootElement.ToString());
        using var stream = new MemoryStream();
        using var writer = new Utf8JsonWriter(stream);
        json.WriteTo(writer);
        writer.Flush();
        Assert.AreEqual(jsonStr, Encoding.UTF8.GetString(stream.ToArray()));
        stream.Position = 0;
        json            = JsonObjectNode.Parse(stream);
        Assert.AreEqual(9, json.Keys.Count);
        Assert.AreEqual(jsonStr, json.ToString());
        var jsonNode = (System.Text.Json.Nodes.JsonNode)json;

        Assert.IsNotNull(jsonNode);
        json = jsonNode;
        Assert.IsNotNull(json);
        Assert.AreEqual(9, json.Keys.Count);
        Assert.AreEqual(jsonStr, json.ToString());

        Assert.IsFalse(jsonArray.IsNull(0));
        Assert.IsTrue(jsonArray.IsNullOrUndefined(100));
        Assert.IsTrue(jsonArray.EnsureCount(20) > 0);
        jsonArray.SetValue(20, "str");
        Assert.AreEqual("str", jsonArray.GetStringValue(20));
        jsonArray.SetValue(20, DateTime.Now);
        jsonArray.SetValue(20, Guid.NewGuid());
        jsonArray.SetValue(20, 1);
        Assert.AreEqual(1, jsonArray.GetInt32Value(20));
        jsonArray.SetValue(20, 1L);
        Assert.AreEqual(1L, jsonArray.GetInt64Value(20));
        jsonArray.SetValue(20, 1.0);
        Assert.AreEqual(1.0, jsonArray.GetDoubleValue(20));
        jsonArray.SetValue(20, 1.0F);
        Assert.AreEqual(1.0F, jsonArray.GetSingleValue(20));
        jsonArray.SetValue(20, true);
        Assert.IsTrue(jsonArray.GetBooleanValue(20));
        jsonArray.SetValue(20, false);
        Assert.IsFalse(jsonArray.GetBooleanValue(20));
        Assert.AreEqual(JsonValueKind.False, (jsonArray as IJsonContainerNode).GetValue("20").ValueKind);
        jsonArray.Remove(20);

        Assert.IsFalse(json.IsNull("props"));
        Assert.IsTrue(json.IsNullOrUndefined("test"));
        json.SetValue("test", "str");
        Assert.AreEqual("str", json.GetStringValue("test"));
        json.SetValue("test", Guid.NewGuid());
        json.SetValue("test", DateTime.Now);
        json.SetUnixTimestampValue("test", DateTime.Now);
        json.SetJavaScriptDateTicksValue("test", DateTime.Now);
        json.SetDateTimeStringValue("test", DateTime.Now);
        json.SetValue("test", 1);
        Assert.AreEqual(1, json.GetInt32Value("test"));
        json.SetValue("test", 1L);
        Assert.AreEqual(1L, json.GetInt64Value("test"));
        json.SetValue("test", 1.0);
        Assert.AreEqual(1.0, json.GetDoubleValue("test"));
        json.SetValue("test", 1.0F);
        Assert.AreEqual(1.0F, json.GetSingleValue("test"));
        json.SetValue("test", true);
        Assert.IsTrue(json.GetBooleanValue("test"));
        json.SetValue("test", false);
        Assert.IsFalse(json.GetBooleanValue("test"));
        json.Remove("test");
        Assert.IsTrue(json.IsNullOrUndefined("test"));
        json.Add("1", 70);
        json.Add("2", "Greetings!");
        json.Add("3", true);
        json.Add("4", 1.0);
        json.Add("5", 600L);
        json.Add("6", DateTime.Now);
        Assert.IsTrue(json.Count > 6);

        json.Clear();
        Assert.AreEqual(0, json.Count);
        json.SetRange(new Dictionary <string, int>
        {
            { "number", 100 }
        });
        json.SetRange(new Dictionary <string, int>
        {
            { "number", 200 },
            { "another", 300 }
        }, true);
        json.SetRange(new Dictionary <string, JsonObjectNode>
        {
            { "duplicate", json }
        });
        json.SetRange(new Dictionary <string, JsonObjectNode>
        {
            { "another", new JsonObjectNode() }
        }, true);
        json.SetRange(new Dictionary <string, JsonArrayNode>
        {
            { "arr", new JsonArrayNode() }
        });
        json.SetRange(new Dictionary <string, JsonArrayNode>
        {
            { "arr", null }
        }, true);
        Assert.AreEqual(4, json.Count);
        json.IncreaseValue("number");
        json.IncreaseValue("number", 7L);
        Assert.AreEqual(108, json.GetValue <int>("number"));
        json.DecreaseValue("number");
        json.DecreaseValue("number", 7L);
        Assert.AreEqual(100L, json.GetInt64Value("number"));
        json.IncreaseValue("number", 1.2);
        json.DecreaseValue("number", 0.3);
        Assert.IsTrue(json.GetDoubleValue("number") > 100);

        var j1 = Serialize <System.Text.Json.Nodes.JsonObject>("{ \"a\": \"bcdefg\", \"h\": \"ijklmn\" }");

        Assert.AreEqual(2, j1.Count);
        var j2 = Serialize <System.Text.Json.Nodes.JsonArray>("[ 123, 456 ]");

        Assert.AreEqual(2, j2.Count);
        var j3 = Serialize <System.Text.Json.Nodes.JsonNode>("{ \"a\": \"bcdefg\", \"h\": \"ijklmn\" }");

        Assert.AreEqual(2, j3.AsObject().Count);
        j3 = Serialize <System.Text.Json.Nodes.JsonNode>("[ 123, 456 ]");
        Assert.AreEqual(2, j3.AsArray().Count);
    }