DomMa
DomMa

Reputation: 151

Losing data when deserializing bool

I´m trying to deserialize a JSON into a selfmade object A, containing 2 strings and a List of another object B. Object B consists of another string and a bool.

Sample JSON:

{  
   "ButtonList":[  
      {  
         "Fast":false,
         "Name":"TableOverview"
      },
      {  
         "Fast":true,
         "Name":"Evaluation"
      }
   ],
   "FavoritGraphic":"PDFreport",
   "FavoritText":"Findings"
}

The syntax of the JSON is double-checked and correct. Also a test without the bool works just fine. But when I try to deserialize the object with the bool, the List is null (favorit-strings still have correct values). I can´t figure out what I´m doing wrong here ...

public class ButtonSettingsModel
{
  public readonly string FavoritText;
  public readonly string FavoritGraphic;
  public readonly List<ButtonInfo> ButtonList;

  public ButtonSettingsModel(string favoritText, string favoritGraphic, List<ButtonInfo> fastButtons)
  {
    FavoritText = favoritText;
    FavoritGraphic = favoritGraphic;
    ButtonList = fastButtons;
  }
}

public class ButtonInfo
{
  public readonly string Name;
  public readonly bool Fast;

  public ButtonInfo(string name, bool fast)
  {
    Name = name;
    Fast = fast;
  }
}

ButtonSettingsModel ButtonSettings = GetObjectFromJson<ButtonSettingsModel>(jsonString);

public T GetObjectFromJson<T>(string jsonString) // correct json
{
  var foo = JsonConvert.DeserializeObject<T>(jsonString); // List == null
  return foo;
}

Upvotes: 1

Views: 512

Answers (1)

Crowcoder
Crowcoder

Reputation: 11514

It's about the naming of the constructor arguments. While not case-sensitive, JSON.Net does need to match them up.

Either change the json array name to match the constructor or change the constructor to match the json:

void Main()
{
    string jsonString = @"{  
   ""buttonList"":[
      {  
         ""Name"":""TableOverview"",
         ""Fast"":false         
      },
      {
         ""Name"":""Evaluation""
      }
   ],
   ""FavoritGraphic"":""PDFreport"",
   ""FavoritText"":""Findings""
}";


    ButtonSettingsModel ButtonSettings = GetObjectFromJson<ButtonSettingsModel>(jsonString);
    //ButtonSettings.Dump();
}

// Define other methods and classes here
public class ButtonSettingsModel
{
    public readonly string FavoritText;
    public readonly string FavoritGraphic;
    public readonly List<ButtonInfo> ButtonList;

    public ButtonSettingsModel(string favoritText, string favoritGraphic, List<ButtonInfo> buttonList)
    {
        FavoritText = favoritText;
        FavoritGraphic = favoritGraphic;
        ButtonList = buttonList;
    }
}

public class ButtonInfo
{
    public readonly string Name;
    public readonly bool Fast;

    public ButtonInfo(string name, bool fast)
    {
        Name = name;
        Fast = fast;
    }
}

public T GetObjectFromJson<T>(string jsonString) // correct json
{
    var foo = JsonConvert.DeserializeObject<T>(jsonString); // List != null
    return foo;
}

Upvotes: 2

Related Questions