Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
joergbattermann opened this issue Sep 1, 2014 · 5 comments
Assignees

Comments

@joergbattermann
Copy link

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
Copy link
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
@joergbattermann
Copy link
Author

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

manofstick pushed a commit to manofstick/Jil that referenced this issue Sep 12, 2014
@dotjosh
Copy link

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
Copy link
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
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants