Deserialize without generic type-parameter but.. with.. Type parameter? #60

Closed
jbattermann opened this Issue Sep 1, 2014 · 5 comments

Projects

None yet

3 participants

@jbattermann

Good evening,

I was wondering whether there is any existing or a planned way to deserialize a string or stream into an object instance, without providing the target type via a type parameter, but via the "good old" Type parameter in the Json.Deserialize(..) signature, e.g.:

using(var input = new StringReader(myString))
{
var myTargetType = typeof(MyClass);
var result = JSON.Deserialize(input, myTargetType);
}

I know it doesn't exist atm and I would not want to use reflection to call your existing generic method via reflection to achieve that (ick: http://stackoverflow.com/questions/2107845/generics-in-c-using-type-of-a-variable-as-parameter) unless I had to. Right now I am attempting to plug Jil into NancyFx as a custom (De-)Serializer and their IBodySerializer signature only provides the target type as a plain Type parameter value, not as a type parameter.

Is there any chance that might be added to Jil as an alternative to the existing JSON.Deserialize method?

Cheers and thanks,
-J

@kevin-montrose
Owner

This isn't a bad idea, I'll see if I can't slip it into a future release.

@kevin-montrose kevin-montrose self-assigned this Sep 2, 2014
@jbattermann

Awesome, thanks Kevin! (assuming you meant "can" instead of "can't" ;) )

@kevin-montrose kevin-montrose added a commit that closed this issue Sep 3, 2014
@kevin-montrose add a Deserialize method that takes a Type, basically support non-gen…
…eric calls of Deserialize at a small tax; fixes #60
8a4007c
@manofstick manofstick pushed a commit to manofstick/Jil that referenced this issue Sep 12, 2014
@kevin-montrose + Paul Westcott add a Deserialize method that takes a Type, basically support non-gen…
…eric calls of Deserialize at a small tax; fixes #60
df2383d
@dotjosh
dotjosh commented Jun 25, 2015

Is there a way to serialize and pass the type too? I have a collection of objects that are used as their base type, so the generic method isn't serializing any properties defined on the subclass because the type is inferred from the generic signature of JSON.Serialize().

@kevin-montrose
Owner

@dotjosh JSON.SerializeDynamic(object, Options) probably does what you want there. There's a perf hit (since it has to inspect), but if none of your types are DLR-aware then it shouldn't be a big one.

@dotjosh
dotjosh commented Jun 26, 2015

Thank you - yeah I saw that, didn't realize it was applicable. I'll have
to test the performance. The primary reason I'm looking at the library
(over newtonsoft) is the perf..

On Thu, Jun 25, 2015 at 9:44 PM, kevin-montrose notifications@github.com
wrote:

@dotjosh https://github.com/dotjosh JSON.SerializeDynamic(object,
Options) probably does what you want there. There's a perf hit (since it
has to inspect), but if none of your types are DLR-aware then it shouldn't
be a big one.


Reply to this email directly or view it on GitHub
#60 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment