Exemple #1
0
        private static void AddToParent(
            KeyValuePair <string, JsonNode> nodePair,
            ref Stack <KeyValuePair <string, JsonNode> > currentNodes,
            ref JsonNode toReturn,
            DuplicatePropertyNameHandlingStrategy duplicatePropertyNameHandling = DuplicatePropertyNameHandlingStrategy.Replace)
        {
            if (currentNodes.Any())
            {
                KeyValuePair <string, JsonNode> parentPair = currentNodes.Peek();

                // Parent needs to be JsonObject or JsonArray
                Debug.Assert(parentPair.Value is JsonObject || parentPair.Value is JsonArray);

                if (parentPair.Value is JsonObject jsonObject)
                {
                    Debug.Assert(nodePair.Key != null);

                    // Handle duplicate properties accordingly to duplicatePropertyNameHandling:

                    if (duplicatePropertyNameHandling == DuplicatePropertyNameHandlingStrategy.Replace)
                    {
                        jsonObject[nodePair.Key] = nodePair.Value;
                    }
                    else if (jsonObject._dictionary.ContainsKey(nodePair.Key))
                    {
                        if (duplicatePropertyNameHandling == DuplicatePropertyNameHandlingStrategy.Error)
                        {
                            throw new ArgumentException(SR.JsonObjectDuplicateKey);
                        }

                        Debug.Assert(duplicatePropertyNameHandling == DuplicatePropertyNameHandlingStrategy.Ignore);
                    }
                    else
                    {
                        jsonObject.Add(nodePair);
                    }
                }
                else if (parentPair.Value is JsonArray jsonArray)
                {
                    Debug.Assert(nodePair.Key == null);
                    jsonArray.Add(nodePair.Value);
                }
            }
            else
            {
                toReturn = nodePair.Value;
            }
        }
Exemple #2
0
        private static void AddToParent(
            KeyValuePair <string?, JsonNode?> nodePair,
            ref Stack <KeyValuePair <string?, JsonNode?> > currentNodes,
            ref JsonNode?toReturn,
            DuplicatePropertyNameHandlingStrategy duplicatePropertyNameHandling = DuplicatePropertyNameHandlingStrategy.Replace)
        {
            if (currentNodes.TryPeek(out KeyValuePair <string?, JsonNode?> parentPair))
            {
                // Parent needs to be JsonObject or JsonArray
                Debug.Assert(parentPair.Value is JsonObject || parentPair.Value is JsonArray);

                if (parentPair.Value is JsonObject jsonObject)
                {
                    Debug.Assert(nodePair.Key != null);

                    // Handle duplicate properties accordingly to duplicatePropertyNameHandling:

                    if (duplicatePropertyNameHandling == DuplicatePropertyNameHandlingStrategy.Replace)
                    {
                        jsonObject[nodePair.Key] = nodePair.Value;
                    }
                    else if (jsonObject._dictionary.ContainsKey(nodePair.Key))
                    {
                        if (duplicatePropertyNameHandling == DuplicatePropertyNameHandlingStrategy.Error)
                        {
                            throw new ArgumentException(SR.JsonObjectDuplicateKey);
                        }

                        Debug.Assert(duplicatePropertyNameHandling == DuplicatePropertyNameHandlingStrategy.Ignore);
                    }
                    else
                    {
                        jsonObject.Add(nodePair !);
                    }
                }
                else if (parentPair.Value is JsonArray jsonArray)
                {
                    Debug.Assert(nodePair.Key == null);
                    jsonArray.Add(nodePair.Value);
                }
            }
            else
            {
                // We are at the top level, so adding node to parent means setting it as returned one

                toReturn = nodePair.Value;
            }
        }
Exemple #3
0
        public static void TestParseWithNestedDuplicates(DuplicatePropertyNameHandlingStrategy duplicatePropertyNameHandling)
        {
            var options = new JsonNodeOptions
            {
                DuplicatePropertyNameHandling = duplicatePropertyNameHandling
            };

            var stringWithDuplicates = @"
            {
                ""property"": ""first value"",
                ""nested object"": 
                {
                    ""property"": ""duplicate value"",
                    ""more nested object"": 
                    {
                        ""property"": ""last duplicate value""
                    }
                },
                ""array"" : [ ""property"" ]
            }";

            var jsonObject = (JsonObject)JsonNode.Parse(stringWithDuplicates, options);

            Assert.Equal(3, jsonObject.GetPropertyNames().Count);
            Assert.Equal(3, jsonObject.GetPropertyValues().Count);
            Assert.Equal("first value", jsonObject["property"]);
            CheckNestedValues(jsonObject);

            jsonObject.Remove("property");

            Assert.Equal(2, jsonObject.GetPropertyNames().Count);
            Assert.Equal(2, jsonObject.GetPropertyValues().Count);
            CheckNestedValues(jsonObject);

            void CheckNestedValues(JsonObject jsonObject)
            {
                var nestedObject = (JsonObject)jsonObject["nested object"];

                Assert.Equal(2, nestedObject.GetPropertyNames().Count);
                Assert.Equal(2, nestedObject.GetPropertyValues().Count);
                Assert.Equal("duplicate value", nestedObject["property"]);

                var moreNestedObject = (JsonObject)nestedObject["more nested object"];

                Assert.Equal(1, moreNestedObject.GetPropertyNames().Count);
                Assert.Equal(1, moreNestedObject.GetPropertyValues().Count);
                Assert.Equal("last duplicate value", moreNestedObject["property"]);

                var array = (JsonArray)jsonObject["array"];

                Assert.Equal(1, array.Count);
                Assert.True(array.Contains("property"));
            }

            var nestedObject = (JsonObject)jsonObject["nested object"];

            nestedObject.Add("array", new JsonNumber());

            Assert.IsType <JsonArray>(jsonObject["array"]);
            Assert.IsType <JsonNumber>(nestedObject["array"]);
        }