This repository has been archived by the owner on Apr 26, 2020. It is now read-only.
Updated JsonNetBodyDeserializer.cs to support array types #69
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
After encountering errors doing model binding to array types, I investigated the root cause and decided to fix it. It looks like JsonNetBodyDeserializer.cs creates a separate instance of the target type from that created during deserialization, and then manually copies properties over in order to support blacklisting of properties (a Nancy feature not directly integrated with Newtonsoft.Json). As part of this, JsonNetBodyDeserializer.cs assumes that it can use
Activator.CreateInstance
on the destination type. This does not work for arrays, as they do not have a parameterless constructor. The approach I took, since the deserialization is going to require the use of aList<T>
anyway, is to leverage the existingList<T>
deserialization, and then if the destination type is actuallyT[]
, callList<T>
'sToArray
method before returning the final value.Prerequisites
Description
ToArray
, I generate dynamic method specializations to perform the call toList<T>.ToArray
, and I set up a simple cache of delegates referring to these methods.CreateObjectWithBlacklistExcluded
detects an array typeT[]
, a new methodConvertArray
is called which retrieves this converter from the cache (constructing it if necessary) and applies it to the result ofConvertCollection
onList<T>
.CreateObjectWithBlacklistExcluded
is altered so thatActivator.CreateInstance
is not called on the destination type in this circumstance.