JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.73k stars 3.25k forks source link

Newtonsoft adding extra square brackets when deserializing a JSON value with forward slash #2824

Open dhu5432 opened 1 year ago

dhu5432 commented 1 year ago

Source/destination types

JSON -> Dictionary<string, string>

Source/destination JSON

{"name":"Premium"}

{"nam/e":"Premium"}

Expected behavior

The JSON path of the first entry is "name". The JSON path of the second entry is "na/me"

Actual behavior

The JSON path of the first entry is "name". The JSON path of the second entry is "['nam/e']"

Steps to reproduce

string object1 = @"{""name"" : ""Premium""}";
var object1Deserialized = JsonConvert.DeserializeObject<Dictionary<string,string>>(object1);
var object1JToken = JToken.FromObject(object1Deserialized);
Assert.AreEqual("name", object1JToken.Children().First().Path);

string object2 = @"{""nam/e"" : ""Premium""}";
var object2Deserialized = JsonConvert.DeserializeObject<Dictionary<string, string>>(object2);
var object2JToken = JToken.FromObject(object2Deserialized);
Assert.AreEqual("['nam/e']", object2JToken.Children().First().Path);

The code/test case above passes, even though I would expect object2's path to be na/me Using .NET 7.0 and Newtonsoft.Json 13.0.2