I’m writing a custom javascript converter and I’m receiving a string that should contain an int.
This is what I’m doing:
public class MyObjectToJson : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
MyObject TheObject = new MyObject;
if (serializer.ConvertToType<int>(dictionary["TheInt"]) == true)
{
MyObject.TheInt = serializer.ConvertToType<int>(dictionary["TheInt"]);
}
However, it’s not working on the conditional statement. What do I need to change? I want to test that I’m getting an int.
Thanks.
Change your code to use this condition:
This is a better solution than relying on an exception to be thrown, as catching exceptions is computationally expensive.