Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App is freezing when trying to deserialize Json string to object

I'm not sure what is causing this to happen. When debugging, I can step through each line, but when it hits the DeserializeObject line, the program doesn't do anything. I can no longer step over or do anything else with the program. No error messages are being thrown either.

[Command("stats")]
        public async Task LookupMonsterStats(CommandContext ctx, string name)
        {
            string monstersUri = "https://www.dnd5eapi.co/api/monsters/";
            var formatted = name.Replace(" ", "-");
            string apiResponse = string.Empty;

            using (var httpClient = new HttpClient())
            {
                using (var response = await httpClient.GetAsync(monstersUri + formatted))
                {
                    apiResponse = await response.Content.ReadAsStringAsync();
                    var monster = JsonConvert.DeserializeObject<Monster>(apiResponse);
                }
            }

            await ctx.Channel.SendMessageAsync(apiResponse).ConfigureAwait(false);
        }

Edit: Here is what my Monster class looks like

public class Monster
    {
        [JsonProperty("index")]
        public string Index { get; set; }

        [JsonProperty("name")]
        public string Name{ get; set; }

        [JsonProperty("size")]
        public string Size { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("subtype")]
        public string Subtype { get; set; }

        [JsonProperty("alignment")]
        public string Alignment { get; set; }

        [JsonProperty("armor_class")]
        public int? ArmorClass { get; set; }

        [JsonProperty("hit_points")]
        public int? HitPoints { get; set; }

        [JsonProperty("forms")]
        public List<string> Forms { get; set; }

        [JsonProperty("speed")]
        public Object Speed { get; set; }

        [JsonProperty("strength")]
        public int? Strength { get; set; }

        [JsonProperty("dexterity")]
        public int? Dexterity { get; set; }

        [JsonProperty("constitution")]
        public int? Constitution { get; set; }

        [JsonProperty("intelligence")]
        public int? Intelligence { get; set; }

        [JsonProperty("wisdom")]
        public int? Wisdom { get; set; }

        [JsonProperty("charisma")]
        public int? Charisma { get; set; }

        [JsonProperty("proficiencies")]
        public List<string> Proficiencies { get; set; }

        [JsonProperty("damage_vulnerabilities")]
        public List<string> DamageVulnerabilities { get; set; }

        [JsonProperty("damage_resistances")]
        public List<string> DamageResistances { get; set; }

        [JsonProperty("damage_immunities")]
        public List<string> DamageImmunities { get; set; }

        [JsonProperty("condition_immunities")]
        public List<string> ConditionImmunities { get; set; }

        [JsonProperty("senses")]
        public Object Senses { get; set; }

        [JsonProperty("languages")]
        public string Languages { get; set; }

        [JsonProperty("challenge_rating")]
        public decimal? ChallengeRating { get; set; }

        [JsonProperty("special_abilities")]
        public List<string> SpecialAbilities { get; set; }

        [JsonProperty("actions")]
        public List<string> Actions { get; set; }

        [JsonProperty("legendary_actions")]
        public List<string> LegendaryActions { get; set; }

        [JsonProperty("url")]
        public string Url { get; set; }
    }
like image 981
Timothy Sutton Avatar asked Oct 17 '25 17:10

Timothy Sutton


1 Answers

As mentioned in a comment your class definition doesn't seem to match the JSON response, assuming you're using Visual Studio it's probably worth opening Debug / Windows / Exception Settings and make sure "Common Language Runtime Exceptions" is checked. Sometimes I've found stepping over in multi-threaded apps can produce unusual results after an exception.

When I pasted your code into LINQPad I got errors on several members you've defined as List<string> when the underlying type is more complex. What I normally do to avoid those errors is copy the raw json from a browser window to the clipboard and with a CS file open in Visual Studio do an Edit / Paste Special / Paste JSON as classes to automatically create the classes. After doing that I got the following that deserialized the data fine:

async Task Main()
{
    await LookupMonsterStats("aboleth");
}

public async Task LookupMonsterStats(string name)
{
    string monstersUri = "https://www.dnd5eapi.co/api/monsters/";
    var formatted = name.Replace(" ", "-");
    string apiResponse = string.Empty;

    using (var httpClient = new HttpClient())
    {
        using (var response = await httpClient.GetAsync(monstersUri + formatted))
        {
            apiResponse = await response.Content.ReadAsStringAsync();
            var monster = JsonConvert.DeserializeObject<Monster>(apiResponse);
            monster.Dump();
        }
    }
}

public class Monster
{
    public string index { get; set; }
    public string name { get; set; }
    public string size { get; set; }
    public string type { get; set; }
    public object subtype { get; set; }
    public string alignment { get; set; }
    public int armor_class { get; set; }
    public int hit_points { get; set; }
    public string hit_dice { get; set; }
    public Speed speed { get; set; }
    public int strength { get; set; }
    public int dexterity { get; set; }
    public int constitution { get; set; }
    public int intelligence { get; set; }
    public int wisdom { get; set; }
    public int charisma { get; set; }
    public Proficiency[] proficiencies { get; set; }
    public object[] damage_vulnerabilities { get; set; }
    public object[] damage_resistances { get; set; }
    public object[] damage_immunities { get; set; }
    public object[] condition_immunities { get; set; }
    public Senses senses { get; set; }
    public string languages { get; set; }
    public int challenge_rating { get; set; }
    public int xp { get; set; }
    public Special_Abilities[] special_abilities { get; set; }
    public Action[] actions { get; set; }
    public Legendary_Actions[] legendary_actions { get; set; }
    public string url { get; set; }
}

public class Speed
{
    public string walk { get; set; }
    public string swim { get; set; }
}

public class Senses
{
    public string darkvision { get; set; }
    public int passive_perception { get; set; }
}

public class Proficiency
{
    public Proficiency1 proficiency { get; set; }
    public int value { get; set; }
}

public class Proficiency1
{
    public string index { get; set; }
    public string name { get; set; }
    public string url { get; set; }
}

public class Special_Abilities
{
    public string name { get; set; }
    public string desc { get; set; }
    public Dc dc { get; set; }
}

public class Dc
{
    public Dc_Type dc_type { get; set; }
    public int dc_value { get; set; }
    public string success_type { get; set; }
}

public class Dc_Type
{
    public string index { get; set; }
    public string name { get; set; }
    public string url { get; set; }
}

public class Action
{
    public string name { get; set; }
    public string desc { get; set; }
    public Options options { get; set; }
    public Damage[] damage { get; set; }
    public int attack_bonus { get; set; }
    public Dc1 dc { get; set; }
    public Usage usage { get; set; }
}

public class Options
{
    public int choose { get; set; }
    public From[][] from { get; set; }
}

public class From
{
    public string name { get; set; }
    public int count { get; set; }
    public string type { get; set; }
}

public class Dc1
{
    public Dc_Type1 dc_type { get; set; }
    public int dc_value { get; set; }
    public string success_type { get; set; }
}

public class Dc_Type1
{
    public string index { get; set; }
    public string name { get; set; }
    public string url { get; set; }
}

public class Usage
{
    public string type { get; set; }
    public int times { get; set; }
}

public class Damage
{
    public Damage_Type damage_type { get; set; }
    public string damage_dice { get; set; }
}

public class Damage_Type
{
    public string index { get; set; }
    public string name { get; set; }
    public string url { get; set; }
}

public class Legendary_Actions
{
    public string name { get; set; }
    public string desc { get; set; }
    public int attack_bonus { get; set; }
    public Damage1[] damage { get; set; }
}

public class Damage1
{
    public Damage_Type1 damage_type { get; set; }
    public string damage_dice { get; set; }
}

public class Damage_Type1
{
    public string index { get; set; }
    public string name { get; set; }
    public string url { get; set; }
}
like image 111
PeterJ Avatar answered Oct 19 '25 09:10

PeterJ