You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JsonExtensionData should not use the dictionary name when serializing #29983
However, if I attempt to serialize the type, it includes the dictionary name as the property key:
varserialized=JsonSerializer.ToString(poco);Console.WriteLine(serialized);// Prints {"Id":10,"Extensions":{"Hello":"world"}}
While this does round-trip, the expectation is keys in JsonExtensionData should alongside other properties of the Poco object. Here's the same routine performed using Json.NET:
varjson="{\"Id\": 10, \"Hello\": \"world\" }";varpoco=JsonConvert.DeserializeObject<MyPoco>(json);Console.WriteLine(poco.Extensions["Hello"]);varserialized=JsonConvert.SerializeObject(poco);Console.WriteLine(serialized);// Prints {"Id":10,"Hello":"world"}
More interestingly, Json.NET does not allow using the dictionary name for deserialization:
Can you make the Extensions dictionary property private? Edit: Nevermind, that doesn't work
I am curious to see what would happen if you added JsonIgnore attribute on the Extensions dictionary along with the JsonExtensionData? Edit:
We throw an exception:
System.InvalidOperationException : The data extension property 'System.Text.Json.Serialization.Tests.ValueTests+MyPoco.Extensions' does not match the required signature of IDictionary<string, JsonElement> or IDictionary<string, object>.
Similarly, what happens if a custom camel casing policy (or JsonPropertyName attribute) caused a property to collide with the overflow capturing dictionary that has JsonExtensionData on it. Edit:
As expected, this throws the right error message:
System.InvalidOperationException : The JSON property name for 'System.Text.Json.Serialization.Tests.ValueTests+MyPoco.MyType' collides with another property.
Yep we discussed this update here
My first idea was emit properties with Dictionary<string,JsonElement> and standard dictionary for Dictionary<string,object>dotnet/corefx#38306 (comment) but at the end we decided to emit simple dictionary for every pairs.
Seem that alg should be more clever and "reflect" on how serialize object value.
Just to know @JamesNK your serialized has got some limit on value type?Or it serialize/deserialize also complex struct as property like dic of dic etc...?
This issue is primarily talking about serialization (not deserialization). On serialization, we don't want to emit the name of the dictionary property that has [JsonExtensionData] on it since that dictionary is only meant for capturing overflow and round-tripping.
@ahsonkhan some more infos, I found time to do some tests dotnet/corefx@master...MarcoRossignoli:json_extensions and if I'm not mistaken code always emitted dic name also before that PR, I reverted the code(with WriteExtensionData inline) and tried to serialize <string,JsonElement> and get same result {"Id":0,"Extensions":{"test":"world"}}
I try to help more with my thought, hope help...the problem seem that the check for state.Current.JsonClassInfo.DataExtensionProperty was done(before we removed it) inside HandleDictionary too late, after we write start(object or array)
Maybe we should check for extension and handle every item of dic like a value before outside HandleDictionary or push items inside it but it's more complex because we need to skip open/close object.
Seem not trivial.
I'll pre-empt someone bringing this up as an issue: it is valid to have duplicate names in a JSON object.
vart=newTypeWithExtensionData();t.Name="A name!";t.ExtensionData["Name"]="Extension data name!";
Would result in:
{
"Name": "A name!",
"Name": "Extension data name!"
}
This is valid JSON.
Note that if you deserialize it, it won't exactly roundtrip. The extension data dictionary will be empty and the Name property will be "Extension data name!" (because it is last, and the deserializer doesn't track that a property has already been set). IMO this is fine. This whole situation is an edge-case.
Is this issue resolved? I still get the extension data nested under the name of the dictionary, this doesn’t happen when using newtonsoft in .net Core 2.0
ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Activity
pranavkm commentedon Jun 22, 2019
cc @JamesNK
JamesNK commentedon Jun 22, 2019
This should be changed.
My point of view is the extension property itself is ignored. Only the content is serialized and deserialized.
ahsonkhan commentedon Jun 22, 2019
Can you make the
Extensionsdictionary property private?Edit: Nevermind, that doesn't work
I am curious to see what would happen if you added
JsonIgnoreattribute on theExtensionsdictionary along with theJsonExtensionData?Edit:
We throw an exception:
Similarly, what happens if a custom camel casing policy (or
JsonPropertyNameattribute) caused a property to collide with the overflow capturing dictionary that hasJsonExtensionDataon it.Edit:
As expected, this throws the right error message:
ahsonkhan commentedon Jun 22, 2019
cc @steveharter
ahsonkhan commentedon Jun 25, 2019
Looks like this regression occurred because of a change to resolve the other issue:
https://github.com/dotnet/corefx/pull/38306/files#diff-cf3c98b9ccd0b5614511b8adf262f8e9L41
https://github.com/dotnet/corefx/issues/38238
cc @MarcoRossignoli
MarcoRossignoli commentedon Jun 26, 2019
Yep we discussed this update here
My first idea was emit properties with
Dictionary<string,JsonElement>and standard dictionary forDictionary<string,object>dotnet/corefx#38306 (comment) but at the end we decided to emit simple dictionary for every pairs.Seem that alg should be more clever and "reflect" on how serialize object value.
Just to know @JamesNK your serialized has got some limit on value type?Or it serialize/deserialize also complex struct as property like dic of dic etc...?
ahsonkhan commentedon Jun 26, 2019
This issue is primarily talking about serialization (not deserialization). On serialization, we don't want to emit the name of the dictionary property that has
[JsonExtensionData]on it since that dictionary is only meant for capturing overflow and round-tripping.MarcoRossignoli commentedon Jun 26, 2019
@ahsonkhan some more infos, I found time to do some tests dotnet/corefx@master...MarcoRossignoli:json_extensions and if I'm not mistaken code always emitted dic name also before that PR, I reverted the code(with
WriteExtensionDatainline) and tried to serialize <string,JsonElement> and get same result{"Id":0,"Extensions":{"test":"world"}}ahsonkhan commentedon Jun 26, 2019
That's good info. Thanks for sharing @MarcoRossignoli
@steveharter, looks like our previous assumption was incorrect.
MarcoRossignoli commentedon Jun 26, 2019
I try to help more with my thought, hope help...the problem seem that the check for
state.Current.JsonClassInfo.DataExtensionPropertywas done(before we removed it) insideHandleDictionarytoo late, after we write start(object or array)Maybe we should check for extension and handle every item of dic like a value before outside
HandleDictionaryor push items inside it but it's more complex because we need to skip open/close object.Seem not trivial.
JamesNK commentedon Jun 26, 2019
I'll pre-empt someone bringing this up as an issue: it is valid to have duplicate names in a JSON object.
Would result in:
{ "Name": "A name!", "Name": "Extension data name!" }This is valid JSON.
Note that if you deserialize it, it won't exactly roundtrip. The extension data dictionary will be empty and the Name property will be "Extension data name!" (because it is last, and the deserializer doesn't track that a property has already been set). IMO this is fine. This whole situation is an edge-case.
kkatoch commentedon Nov 3, 2020
Is this issue resolved? I still get the extension data nested under the name of the dictionary, this doesn’t happen when using newtonsoft in .net Core 2.0