public static void TestTrailingCommas() { string jsonStringWithTrailingCommas = @" { ""firstProperty"": ""first value"", ""secondProperty"" : ""second value"", }"; JsonNodeOptions options = new JsonNodeOptions { AllowTrailingCommas = default }; Assert.ThrowsAny <JsonException>(() => JsonNode.Parse(jsonStringWithTrailingCommas, options)); options = new JsonNodeOptions { AllowTrailingCommas = false }; Assert.ThrowsAny <JsonException>(() => JsonNode.Parse(jsonStringWithTrailingCommas, options)); options = new JsonNodeOptions { AllowTrailingCommas = true }; JsonObject jsonObject = (JsonObject)JsonNode.Parse(jsonStringWithTrailingCommas, options); Assert.Equal("first value", jsonObject["firstProperty"]); Assert.Equal("second value", jsonObject["secondProperty"]); Assert.Equal(2, jsonObject.GetPropertyNames().Count); Assert.Equal(2, jsonObject.GetPropertyValues().Count); }
public static void CaseSensitivity_EditMode() { var jArray = new JsonArray(); var jObject = new JsonObject(); jObject.Add("MyProperty", 42); jObject.Add("myproperty", 42); // No exception // Options on direct node. var options = new JsonNodeOptions { PropertyNameCaseInsensitive = true }; jArray = new JsonArray(); jObject = new JsonObject(options); jObject.Add("MyProperty", 42); jArray.Add(jObject); Assert.Throws <ArgumentException>(() => jObject.Add("myproperty", 42)); // Options on parent node (deferred creation of options until Add). jArray = new JsonArray(options); jObject = new JsonObject(); jArray.Add(jObject); jObject.Add("MyProperty", 42); Assert.Throws <ArgumentException>(() => jObject.Add("myproperty", 42)); // Dictionary is created when Add is called for the first time, so we need to be added first. jArray = new JsonArray(options); jObject = new JsonObject(); jObject.Add("MyProperty", 42); jArray.Add(jObject); jObject.Add("myproperty", 42); // No exception since options were not set in time. }
public static void TestJsonCommentHandling() { string jsonStringWithComments = @" { // First comment ""firstProperty"": ""first value"", //Second comment ""secondProperty"" : ""second value""// Third comment // Last comment }"; JsonNodeOptions options = new JsonNodeOptions { CommentHandling = default }; Assert.ThrowsAny <JsonException>(() => JsonNode.Parse(jsonStringWithComments, options)); options = new JsonNodeOptions { CommentHandling = JsonCommentHandling.Disallow }; Assert.ThrowsAny <JsonException>(() => JsonNode.Parse(jsonStringWithComments, options)); options = new JsonNodeOptions { CommentHandling = JsonCommentHandling.Skip }; JsonObject jsonObject = (JsonObject)JsonNode.Parse(jsonStringWithComments, options); Assert.Equal("first value", jsonObject["firstProperty"]); Assert.Equal("second value", jsonObject["secondProperty"]); Assert.Equal(2, jsonObject.GetPropertyNames().Count); Assert.Equal(2, jsonObject.GetPropertyValues().Count); }
public static void TestParseFailsWhenExceedsMaxDepth() { var builder = new StringBuilder(); for (int i = 0; i < 100; i++) { builder.Append("["); } for (int i = 0; i < 100; i++) { builder.Append("]"); } // Test for default MaxDepth Assert.ThrowsAny <JsonException>(() => JsonNode.Parse(builder.ToString())); JsonNodeOptions options = new JsonNodeOptions { MaxDepth = default }; Assert.ThrowsAny <JsonException>(() => JsonNode.Parse(builder.ToString(), options)); // Test for MaxDepth of 5 options = new JsonNodeOptions { MaxDepth = 5 }; Assert.ThrowsAny <JsonException>(() => JsonNode.Parse(builder.ToString(), options)); }
public static void TestJsonNodeOptionsSet() { var options = new JsonNodeOptions() { PropertyNameCaseInsensitive = true }; // Ctor that takes just options var obj1 = new JsonObject(options); obj1["Hello"] = "World"; // Ctor that takes props IEnumerable + options IEnumerable <KeyValuePair <string, JsonNode?> > props = new List <KeyValuePair <string, JsonNode?> > { new KeyValuePair <string, JsonNode?>("Hello", "World") }; var obj2 = new JsonObject(props, options); // Create method using JsonDocument doc = JsonDocument.Parse(@"{""Hello"":""World""}"); var obj3 = JsonObject.Create(doc.RootElement, options); Test(obj1); Test(obj2); Test(obj3);
private void AddChild(JsonObject node, string propertyName, JsonElement element) { var options = new JsonNodeOptions { PropertyNameCaseInsensitive = Options.PropertyNameCaseInsensitive }; JsonNode?childNode = element.ValueKind == JsonValueKind.Object ? JsonObject.Create(element, options) : element.ValueKind == JsonValueKind.Array ? JsonArray.Create(element, options) : JsonValue.Create(element, options); node[propertyName] = childNode; }
public static void TestDefaultJsonNodeOptions() { JsonNodeOptions defaultOptions = default; JsonNodeOptions newOptions = new JsonNodeOptions(); Assert.Equal(defaultOptions.AllowTrailingCommas, newOptions.AllowTrailingCommas); Assert.Equal(defaultOptions.CommentHandling, newOptions.CommentHandling); Assert.Equal(defaultOptions.DuplicatePropertyNameHandling, newOptions.DuplicatePropertyNameHandling); Assert.Equal(defaultOptions.MaxDepth, newOptions.MaxDepth); Assert.False(defaultOptions.AllowTrailingCommas); Assert.Equal(JsonCommentHandling.Disallow, defaultOptions.CommentHandling); Assert.Equal(DuplicatePropertyNameHandlingStrategy.Replace, defaultOptions.DuplicatePropertyNameHandling); Assert.Equal(0, defaultOptions.MaxDepth); }
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"]); }