Reputation: 21
Hoping someone can catch what I'm doing wrong. CardCollection.cs just contains a List called cards, with a public methods Cards to act as getter/setter.
I should point out that I'm using Visual Studio 2017 for development, and when I inspect the string "jsonString" in debug mode using the built-in JSON Visualizer it displays properly in that window. Unfortunately JsonConvert.DeserializeObject does not properly set my CardCollection object and results in an empty list of cards
From "Form.cs"
private async void SelectFileButton_ClickAsync(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "JSON Files|*.json";
dialog.Title = "Select a JSON File";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string line = "";
string jsonString = "";
string fileName = dialog.FileName;
StringBuilder builder = new StringBuilder("");
using (StreamReader reader = File.OpenText(dialog.FileName))
{
while ((line = await reader.ReadLineAsync()) != null)
{
line = line.Replace("\t", "");
line = line.Replace("\n", "");
builder.Append(line);
}
}
jsonString = builder.ToString();
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
CardCollection cardList = JsonConvert.DeserializeObject<CardCollection>(jsonString, settings);
}
}
Card.cs
public class Card
{
string[] colorIdentity;
string[] colors;
double convertedManaCost;
ForeignData[] foreignData;
bool isReserved;
string layout;
Legality[] legalities;
string loyalty;
string manaCost;
string name;
string power;
string[] printings;
Ruling[] rulings;
string[] subtypes;
string[] supertypes;
string text;
string toughness;
string type;
string[] types;
string uuid;
public string[] ColorIdentity { get => colorIdentity; set => colorIdentity = value; }
public string[] Colors { get => colors; set => colors = value; }
public double ConvertedManaCost { get => convertedManaCost; set => convertedManaCost = value; }
public bool IsReserved { get => isReserved; set => isReserved = value; }
public string Layout { get => layout; set => layout = value; }
public string Loyalty { get => loyalty; set => loyalty = value; }
public string ManaCost { get => manaCost; set => manaCost = value; }
public string Name { get => name; set => name = value; }
public string Power { get => power; set => power = value; }
public string[] Printings { get => printings; set => printings = value; }
public string[] Subtypes { get => subtypes; set => subtypes = value; }
public string[] Supertypes { get => supertypes; set => supertypes = value; }
public string Text { get => text; set => text = value; }
public string Toughness { get => toughness; set => toughness = value; }
public string Type { get => type; set => type = value; }
public string[] Types { get => types; set => types = value; }
public string Uuid { get => uuid; set => uuid = value; }
internal ForeignData[] ForeignData { get => foreignData; set => foreignData = value; }
internal Legality[] Legalities { get => legalities; set => legalities = value; }
internal Ruling[] Rulings { get => rulings; set => rulings = value; }
}
{
"1999 World Championships Ad": {
"colorIdentity": [],
"colors": [],
"convertedManaCost": 0.0,
"foreignData": [],
"isReserved": false,
"layout": "token",
"legalities": {},
"name": "1999 World Championships Ad",
"printings": [
"WC99"
],
"rulings": [],
"starter": true,
"subtypes": [],
"supertypes": [],
"text": "",
"type": "Card",
"types": [
"Card"
],
"uuid": "8635dea0-985a-4c02-a02b-8b08d96fb2dd"
},
"2004 World Championships Ad": {
"colorIdentity": [],
"colors": [],
"convertedManaCost": 0.0,
"foreignData": [],
"isReserved": false,
"layout": "token",
"legalities": {},
"name": "2004 World Championships Ad",
"printings": [
"WC04"
],
"rulings": [],
"starter": true,
"subtypes": [],
"supertypes": [],
"text": "",
"type": "Card",
"types": [
"Card"
],
"uuid": "cbd6a762-f3c2-4b29-a07e-f0c9c81cf1ec"
},
"A Display of My Dark Power": {
"colorIdentity": [],
"colors": [],
"convertedManaCost": 0.0,
"foreignData": [],
"isReserved": false,
"layout": "scheme",
"legalities": {},
"name": "A Display of My Dark Power",
"printings": [
"ARC"
],
"rulings": [
{
"date": "2010-06-15",
"text": "The ability affects all players, not just you."
},
{
"date": "2010-06-15",
"text": "The effect doesn’t wear off until just before your next untap step (even if an effect will cause that untap step to be skipped)."
},
{
"date": "2010-06-15",
"text": "The types of mana are white, blue, black, red, green, and colorless."
},
{
"date": "2010-06-15",
"text": "If a land produces more than one type of mana at a single time (as Boros Garrison does, for example), the land’s controller chooses which one of those types of mana is produced by the delayed triggered ability."
},
{
"date": "2010-06-15",
"text": "If a land is tapped for mana but doesn’t produce any (for example, if you tap Gaea’s Cradle for mana while you control no creatures), the delayed triggered ability won’t trigger."
}
],
"starter": true,
"subtypes": [],
"supertypes": [],
"text": "When you set this scheme in motion, until your next turn, whenever a player taps a land for mana, that player adds one mana of any type that land produced.",
"type": "Scheme",
"types": [
"Scheme"
],
"uuid": "c5287e77-890d-41bd-acc6-7a8f1866426d"
}
}
Upvotes: 0
Views: 3007
Reputation: 499
Your class Card
does not match your json in several ways. There are several good ways to create classes for serializing/deserializing json directly.
Method 1:
This will create the classes you need to deserialize your data.
Method 2: Use Quicktype.io. Paste you json and get the classes needed in your favorite language.
One extra problem with you class Card
is that the property names are case sensitive! ColorIdentity != colorIdentity.
If you want to keep PascalCasing in your code, but have camelCasing in your Json you need to decorate each property like this:
[JsonProperty("colorIdentity")]
public object[] ColorIdentity { get; set; }
Also the names of the competitions in your json are objects that are not present in your classes. If you follow method 1 or 2 you will see what I mean.
Upvotes: 2
Reputation: 14477
"1999 World Championships Ad": {
What you have here is an object, not an array. You should deserialize it as a dictionary:
JsonConvert.DeserializeObject<Dictionary<string, Card>>(...);
Upvotes: 2