umco / umbraco-ditto

Ditto - the friendly view-model mapper for Umbraco
http://our.umbraco.org/projects/developer-tools/ditto
MIT License
79 stars 33 forks source link

Issue of Custom Type Converter with newtwon Json #124

Closed walleyuan closed 8 years ago

walleyuan commented 9 years ago

I had an issue when using custom type converter to map a JObject with newtwon josn. As newtwon json is using the type converter as well. so the custom type converter will be overwritten (find more details about the issue http://zhenyuan.azurewebsites.net/post/issue-of-custom-type-converter-with-ditto). To walk around the issue, I just changed my serializer to javascriptserializer.

leekelleher commented 9 years ago

Hi @walleyuan,

Thanks for raising this. Although not really sure what Ditto can do about it though.

From your blog post, since the LinkPicker has the LinkPickerTypeConverter associated with it, so both Ditto and JSON.NET's (ToObject) would attempt to use that converter.

For the JavaScriptSerializer replacement, you could go with shorter syntax using: JsonConvert.DeserializeObject<LinkPicker>(value.ToString())

JimBobSquarePants commented 9 years ago

I just got horribly burnt by this trying to serialize a class during processing against webapi. I have no idea how to work around it just now and I can imagine other developers are going to hit this issue.

leekelleher commented 9 years ago

I believe JSON.NET has it's own version type-converter, JsonConverter.

Maybe we have a Ditto one too? e.g. [DittoTypeConverter(typeof(MyCustomConverter))]. In which we use first, then fallback on the standard .NET TypeConverter attribute? (I may need to explain this better?)

JimBobSquarePants commented 9 years ago

I think we may need to. Come to think of that, I think AutoMapper does the same.

walleyuan commented 9 years ago

I found the solution. by dynamically adding the converter to your class. =P @JimBobSquarePants . Just need one line to solve your issue , I will show you tomorrow =P

leekelleher commented 9 years ago

@walleyuan Would it be possible to see the source of your LinkPickerTypeConverter class?

I'm trying to recreate the scenario in a unit-test, so we can figure out the best way to resolve it.

So far I've narrowed it down to overriding the CanConvertTo method to return false;.

// cc @JimBobSquarePants

leekelleher commented 8 years ago

Closing this one. I managed to recreate the issue with a unit-test and found that if my TypeConverter didn't implement CanConvertTo, then Newtonsoft.Json serialization would fail (null reference).

Feel free to re-open if we need further discussion.