I’m writing a custom json deserializer and I’m writing this:
public class MyObjectToJson : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
MyObject TheObject = new MyObject;
TheObject.TheValue = serializer.ConvertToType<string>(dictionary["TheValue"]);
What happens if the json object doesn’t contain a key TheValue? Does this code crash or just continues with TheObject.TheValue = “”
It will throw an exception. You can avoid by checking whether
dictionary.ContainsKey("TheValue")before callingdictionary["TheValue"].