✅ Newtonsoft.JSON correctly serializes data but erroneously leaves out arrays during deserialization

My simplified code is as follows:
public class Building
{
public string Key;

public Building(string key)
{
Key = key;
}
}
public class Building
{
public string Key;

public Building(string key)
{
Key = key;
}
}
public class Game
{
public Game(object _)
{
buildings = new Building[]
{
new Building("extractor")
};
}
private Building[] buildings;

public Building[] Buildings => buildings;
}
public class Game
{
public Game(object _)
{
buildings = new Building[]
{
new Building("extractor")
};
}
private Building[] buildings;

public Building[] Buildings => buildings;
}
After serializing the Game class, I end up with:
{
"Buildings": [
{
"Key": "extractor"
}
]
}
{
"Buildings": [
{
"Key": "extractor"
}
]
}
This is correct, but deserializing the Game class from this json results in this data:
Game
-> buildings: null
Game
-> buildings: null
The buildings array has been lost during deserialization. This same behaviour happens with the built-in JsonSerializer.
17 Replies
Aurumaker72
Aurumaker72OP2y ago
after changing
private Building[] buildings;
public Building[] Buildings => buildings;
private Building[] buildings;
public Building[] Buildings => buildings;
to
[JsonProperty(nameof(Buildings))]
public Building[] Buildings { get; set; }
[JsonProperty(nameof(Buildings))]
public Building[] Buildings { get; set; }
it works .. i think
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Aurumaker72
Aurumaker72OP2y ago
yeah i cant do that i need to fire events when my properties change
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Aurumaker72
Aurumaker72OP2y ago
so the backing field is required
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Aurumaker72
Aurumaker72OP2y ago
yeah im doing mvvm and i am using mvvm toolkit, but im in model layer so no real use for it there
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Aurumaker72
Aurumaker72OP2y ago
no i dont want inpc inpc is for viewmodel layer i just want to send events you aren't supposed to use mvvmtoolkit in the model layer
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Aurumaker72
Aurumaker72OP2y ago
what would be the proper usage of full props? this?
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
class Game {
[JsonProperty]
private double number;

public double Number { get => number; set { number = value; NumberChanged.Invoke(); } }
}
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
class Game {
[JsonProperty]
private double number;

public double Number { get => number; set { number = value; NumberChanged.Invoke(); } }
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Aurumaker72
Aurumaker72OP2y ago
because i opt out of implicit serialisation so i need to manually specify what to serialize
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Aurumaker72
Aurumaker72OP2y ago
/close
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server