JsonUtil.Deserialize throws a NullReferenceException when deserializing a POCO with constructor that has parameters but has no parameterless constructor declared.
var json = @"""property"": 0";
var obj = JsonUtil.Deserialize
### Findings
Adding a parameterless constructor to the class seems to make it work. This error throws even if the json string has no related properties of the class to deserialize.
## Stack trace
Uncaught (in promise) Error: System.NullReferenceException: Object reference not set to an instance of an object.
at SimpleJson.PocoJsonSerializerStrategy.DeserializeObject (:5000/System.Object value, System.Type type) <0x1b1f848 + 0x00892> in <2735edf83ce145e396aa31dd360e42a4>:0
at SimpleJson.SimpleJson.DeserializeObject (:5000/System.String json, System.Type type, SimpleJson.IJsonSerializerStrategy jsonSerializerStrategy) <0x1af8430 + 0x00066> in <2735edf83ce145e396aa31dd360e42a4>:0
at SimpleJson.SimpleJson.DeserializeObject[T] (:5000/System.String json) <0x1af8198 + 0x0001c> in <2735edf83ce145e396aa31dd360e42a4>:0
at Microsoft.AspNetCore.Blazor.JsonUtil.Deserialize[T] (:5000/System.String json) <0x1af7b58 + 0x00008> in <2735edf83ce145e396aa31dd360e42a4>:0
at StandaloneApp.Program.Main (:5000/System.String[] args) <0x18c9ee8 + 0x0007a> in <77121b2fe73d460ab08b56bfcda7f591>:0
at SimpleJson.PocoJsonSerializerStrategy.DeserializeObject (:5000/System.Object value, System.Type type) <0x1b1f848 + 0x00892> in <2735edf83ce145e396aa31dd360e42a4>:0
at SimpleJson.SimpleJson.DeserializeObject (:5000/System.String json, System.Type type, SimpleJson.IJsonSerializerStrategy jsonSerializerStrategy) <0x1af8430 + 0x00066> in <2735edf83ce145e396aa31dd360e42a4>:0
at SimpleJson.SimpleJson.DeserializeObject[T] (:5000/System.String json) <0x1af8198 + 0x0001c> in <2735edf83ce145e396aa31dd360e42a4>:0
at Microsoft.AspNetCore.Blazor.JsonUtil.Deserialize[T] (:5000/System.String json) <0x1af7b58 + 0x00008> in <2735edf83ce145e396aa31dd360e42a4>:0
at StandaloneApp.Program.Main (:5000/System.String[] args) <0x18c9ee8 + 0x0007a> in <77121b2fe73d460ab08b56bfcda7f591>:0
at Object.callMethod (MonoPlatform.ts:70)
at Object.callEntryPoint (MonoPlatform.ts:47)
at Boot.ts:38
at step (UriHelper.ts:89)
at Object.next (UriHelper.ts:89)
at fulfilled (UriHelper.ts:89)
```
This is as expected. If your constructor requires a parameter, there's no way for SimpleJson to know what value is legal to pass for it. You should deserialize into types with parameterless constructors.
Even so, it ought to throw a useful error rather than an NRE. That's never correct behavior.
Also, it might be worth looking into how Dapper manages to work around this problem...
@masonwheeler I agree that a more useful error should be thrown, but that's more of a matter for https://github.com/facebook-csharp-sdk/simple-json.
Edit: That being said, there's not really any activity on Simple JSON so that's a good explanation of the NRE in the first place.
Oh, this doesn't actually come from Microsoft? My mistake. :(
We are maintaining a fork of it here, so opening this issue here is fine.
@danroth27 Makes sense! I might take a look at this to see if I can give a friendlier exception message.
I think Json.Net also is able to handle these cases as well, though the implementation might be more complex. So I'd agree a friendlier error message should be at least easier to implement.
Most helpful comment
This is as expected. If your constructor requires a parameter, there's no way for SimpleJson to know what value is legal to pass for it. You should deserialize into types with parameterless constructors.