Wednesday, October 1, 2014

Importance of unit test, some code are unpredictable

This doesn't work:

[WebMethod]        
public static object SwitchLeaders(string jsonString)
{
    dynamic json = JsonConvert.DeserializeObject(jsonString);

    return new { Autobot = json.Decepticon, Decepticon = json.Autobot };
}
Returns wrong data: json: {"d":{"Autobot":[],"Decepticon":[]}}



This works:

[WebMethod]        
public static object SwitchLeaders(string jsonString)
{
    dynamic json = JsonConvert.DeserializeObject(jsonString);

    string a = json.Decepticon;
    string b = json.Autobot;

    return new { Autobot = a, Decepticon = b };
}
Returns correct json {"d":{"Autobot":"Megatron”,"Decepticon":"Optimus Prime"}}



Happy Coding!

No comments:

Post a Comment