public static void TestGetNode()
        {
            var jsonObject = new JsonObject
            {
                { "text", "property value" },
                { "boolean", true },
                { "number", 15 },
                { "null node", (JsonNode)null },
                { "array", new JsonArray {
                      "value1", "value2"
                  } }
            };

            JsonElement jsonElement = jsonObject.AsJsonElement();
            JsonObject  jsonObjectFromElementGetNode = (JsonObject)JsonNode.GetNode(jsonElement);

            Assert.True(JsonNode.TryGetNode(jsonElement, out JsonNode jsonNodeFromElementTryGetNode));
            var jsonObjectFromElementTryGetNode = (JsonObject)jsonNodeFromElementTryGetNode;

            Assert.Equal("property value", jsonObjectFromElementGetNode["text"]);
            Assert.Equal("property value", jsonObjectFromElementTryGetNode["text"]);

            // Modifying JsonObject will change jsonObjectFromElementGetNode and jsonObjectFromElementTryGetNode:

            jsonObject["text"] = new JsonString("something different");
            Assert.Equal("something different", jsonObjectFromElementGetNode["text"]);
            Assert.Equal("something different", jsonObjectFromElementTryGetNode["text"]);

            // Modifying JsonObjectFromElementGetNode will change JsonObject and jsonObjectFromElementTryGetNode:

            ((JsonBoolean)jsonObjectFromElementGetNode["boolean"]).Value = false;
            Assert.False(((JsonBoolean)jsonObject["boolean"]).Value);
            Assert.False(((JsonBoolean)jsonObjectFromElementTryGetNode["boolean"]).Value);
        }
Пример #2
0
        public static void TestObject()
        {
            var jsonObject = new JsonObject()
            {
                ["existing property"]  = "value",
                ["different property"] = 14
            };

            JsonElement jsonObjectElement = jsonObject.AsJsonElement();

            Assert.True(jsonObjectElement.TryGetProperty("existing property", out JsonElement propertyValue));
            Assert.Equal("value", propertyValue.GetString());
            Assert.True(jsonObjectElement.TryGetProperty(Encoding.UTF8.GetBytes("existing property"), out propertyValue));
            Assert.Equal("value", propertyValue.GetString());

            Assert.Throws <InvalidOperationException>(() => propertyValue.TryGetProperty("property of not JsonObject", out _));
            Assert.Throws <InvalidOperationException>(() => propertyValue.TryGetProperty(Encoding.UTF8.GetBytes("property of not JsonObject"), out _));

            Assert.False(jsonObjectElement.TryGetProperty("not existing property", out propertyValue));
            Assert.Equal(default, propertyValue);
Пример #3
0
        public static void TestCloneJsonNodeInJsonElement()
        {
            var jsonObject = new JsonObject
            {
                { "text", "value" },
                { "boolean", true },
                { "array", new JsonArray {
                      "value1", "value2"
                  } }
            };

            JsonElement jsonElement = jsonObject.AsJsonElement();

            var jsonObjectCloneFromElement = (JsonObject)JsonNode.DeepCopy(jsonElement);

            Assert.Equal(3, jsonObjectCloneFromElement.GetPropertyNames().Count);
            Assert.Equal(3, jsonObjectCloneFromElement.GetPropertyValues().Count);

            Assert.Equal("value", jsonObjectCloneFromElement["text"]);
            Assert.Equal(true, jsonObjectCloneFromElement["boolean"]);

            // Modifying should not change the clone and vice versa:

            jsonObjectCloneFromElement["boolean"] = false;
            Assert.Equal(false, jsonObjectCloneFromElement["boolean"]);
            Assert.Equal(true, jsonObject["boolean"]);

            jsonObjectCloneFromElement.GetJsonArrayPropertyValue("array").Add("value3");
            Assert.Equal(3, jsonObjectCloneFromElement.GetJsonArrayPropertyValue("array").Count);
            Assert.Equal(2, jsonObject.GetJsonArrayPropertyValue("array").Count);

            jsonObject["text"] = "different value";
            Assert.Equal("different value", jsonObject["text"]);
            Assert.Equal("value", jsonObjectCloneFromElement["text"]);

            jsonObject.GetJsonArrayPropertyValue("array").Remove("value2");
            Assert.Equal(1, jsonObject.GetJsonArrayPropertyValue("array").Count);
            Assert.Equal(3, jsonObjectCloneFromElement.GetJsonArrayPropertyValue("array").Count);
        }
        public static void TestAsJsonElement()
        {
            var jsonObject = new JsonObject
            {
                { "text", "property value" },
                { "boolean", true },
                { "number", 15 },
                { "null node", (JsonNode)null },
                { "array", new JsonArray {
                      "value1", "value2"
                  } }
            };

            JsonElement jsonElement = jsonObject.AsJsonElement();

            Assert.False(jsonElement.IsImmutable);

            JsonElement.ObjectEnumerator enumerator = jsonElement.EnumerateObject();

            enumerator.MoveNext();
            Assert.Equal("text", enumerator.Current.Name);
            Assert.Equal(JsonValueKind.String, enumerator.Current.Value.ValueKind);
            Assert.Equal("property value", enumerator.Current.Value.GetString());

            enumerator.MoveNext();
            Assert.Equal("boolean", enumerator.Current.Name);
            Assert.Equal(JsonValueKind.True, enumerator.Current.Value.ValueKind);
            Assert.True(enumerator.Current.Value.GetBoolean());

            enumerator.MoveNext();
            Assert.Equal("number", enumerator.Current.Name);
            Assert.Equal(15, enumerator.Current.Value.GetInt32());
            Assert.Equal(JsonValueKind.Number, enumerator.Current.Value.ValueKind);

            enumerator.MoveNext();
            Assert.Equal("null node", enumerator.Current.Name);
            Assert.Equal(JsonValueKind.Null, enumerator.Current.Value.ValueKind);

            enumerator.MoveNext();
            Assert.Equal("array", enumerator.Current.Name);
            Assert.Equal(2, enumerator.Current.Value.GetArrayLength());
            Assert.Equal(JsonValueKind.Array, enumerator.Current.Value.ValueKind);
            JsonElement.ArrayEnumerator innerEnumerator = enumerator.Current.Value.EnumerateArray();

            innerEnumerator.MoveNext();
            Assert.Equal(JsonValueKind.String, innerEnumerator.Current.ValueKind);
            Assert.Equal("value1", innerEnumerator.Current.GetString());

            innerEnumerator.MoveNext();
            Assert.Equal(JsonValueKind.String, innerEnumerator.Current.ValueKind);
            Assert.Equal("value2", innerEnumerator.Current.GetString());

            Assert.False(innerEnumerator.MoveNext());
            innerEnumerator.Dispose();

            Assert.False(enumerator.MoveNext());
            enumerator.Dispose();

            // Modifying JsonObject will change JsonElement:

            jsonObject["text"] = new JsonNumber("123");
            Assert.Equal(123, jsonElement.GetProperty("text").GetInt32());
        }