Closed walleyuan closed 8 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())
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.
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?)
I think we may need to. Come to think of that, I think AutoMapper does the same.
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
@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
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.
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.