Closed cubed-it closed 4 years ago
It is really weird.
that is really weird indeed. What version are you using and what .net framework is this?
Thanks,
I checked the code with the latest release obtained from NuGet and ran the code with .NET Framework 4.5, if we set the ID to another value, like the following:
new SimpleObject2() { ID = 1, Name = "Test", Value = "Value" }
It was deserialized properly.
It seems that there is something wrong when deserializing default values, since ID=0 happens to be the default value.
@rpgmaker Look at this place: https://github.com/rpgmaker/NetJSON/blob/master/NetJSON/NetJSON.cs#L4472
It may be the problem.
It loads the default value of the member and compare it against the deserialized value, if they equal each other, goto propNullLabel
. Thus the value 0 was not set and the default -5 was remained there.
I tried to use GenerateTypesInto
to save the assembly, however, I got this:
System.TypeLoadException: Access is denied: 'NetJSON.NetJSONSerializer`1[ConsoleApp1.SimpleObject2]'.
Use GenerateAssembly flag instead
By analyzing the generated assembly, I confirmed the problem was caused by the aforementioned comparison against the default value of the member type. Why there was such kind of comparison? It seemed to be unnecessary and removable.
The comparison is only generated if the skip default is enabled which is by default. What happens if you set it to false?
It had already been false
in the posted code.
Before calling the Deserialize
method, I could verify that value was still false
in the settings instance.
Sorry I missed that 😔. Will try to change the code to avoid the compare and see what happens.
@cubed-it, Will try to work on a solution this weekend. Thanks.
@cubed-it, Will try to work on a solution this weekend. Thanks.
hey, i've been following your conversation in silence ;) thanks a lot in advance!
Ended up generated the code as the following
@cubed-it, can you pull the latest from master and test your code again? I also added a unit test for your scenario which is passing for me at the moment.
I will push to nuget soon if you rather wait for that.
@cubed-it , i just published to nuget a moment ago - https://www.nuget.org/packages/NetJSON/
my test looks good, thanks for the quick fix!
Ended up generated the code as the following
The code logic could be optimized to the following, which may save some comparisons:
if (P_4.SkipDefaultValue == false || n != 0) {
P_2.ID = num;
}
I can flip the condition and check if skip default first on my next code change. Thanks
The following linqpad example...
...produces the following result. SimpleObject2.Id should actually be 0, but the result is -5?