SlapperAutoMapper / Slapper.AutoMapper

Slapper.AutoMapper maps dynamic data to static types. Slap your data into submission!
MIT License
287 stars 76 forks source link

Type Casting from Int64 to Int32 #3

Closed BStrat77 closed 11 years ago

BStrat77 commented 11 years ago

Hi Randy,

I just downloaded your code and so far I have to say it's pretty slick. I did run into one issue though and wanted to run it by you. I'm using the NewtonSoft Json deserializer to take a json result from a RESTful service and generate Expando objects from it. From there I'm using Slapper to map these to an internally published NuGet of the model used for the services, so essentially I can rebuild them back into what they are sent over the wire as but allow the ability to additional fields to be added to the services without breaking what a tightly coupled contract that would be created if I just deserialized to the objects directly...the additional fields can be grabbed next time the subscribing project is updated.

One catch, by default it appears numerics added to the Expando objects come back as Int64, and the contract model classes typically use Int32, so the following line of code in SetMemberValue is crashing due to the inability to cast from Int64 to Int32.

propertyInfo.SetValue( obj, value, null );

I was able to fix this by changing the line of code to the following:

propertyInfo.SetValue(obj, Convert.ChangeType(value, propertyInfo.PropertyType), null);

I assume this will blow up where the casting is invalid, but this appears to offer a bit more flexibility without hopefully a significant performance hit. Let me know your thoughts and if this is inline with your original intentions.

Here's how i'm deserializing the JSON string

JsonConvert.DeserializeObject(reader.ReadToEnd(), new ExpandoObjectConverter());

Thanks for your time.

randyburden commented 11 years ago

Makes perfect sense and a good suggestion. I'm glad you are able to make use of the library. This has been resolved with commit 7d4d8d31341c18cadd7994c511be1205468f3ea5

BStrat77 commented 11 years ago

Thanks so much Randy. It's getting used pretty heavily at the moment and so far it seems to be holding up great.

joantune commented 6 years ago

@BStrat77: I was trying to do the exact same thing - but I'm getting issues with the serialization/deserialization - at first I thought it was because I was doing that to/from an EF object and he was complaining about the ICollection on the object from which he cannot do an instance of (as it's an Interface) but then even when using a DTO I had a casting issue.

Can you please submit the code that you used for this? i.e. the serialization and deserialization?

Thanks in advance

BStrat77 commented 6 years ago

@joantune I'm no longer with that client so unfortunately I don't have an example to share and with it being over 4 years ago I just can't remember much of the details. I'd try changing your DTO object types to the kind that Slapper is trying to convert it to and confirm it's a type casting issue. It looks like the code I suggested is committed so you might be running into a different problem.