✅ 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
Aurumaker722y 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
Aurumaker722y 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
Aurumaker722y ago
so the backing field is required
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Aurumaker72
Aurumaker722y 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
Aurumaker722y 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
Aurumaker722y 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
Aurumaker722y 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
Aurumaker722y ago
/close
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server
More Posts
❔ IAsyncEnumerable and IQueriable unit testingI have an interface of the repository, which has a method Query<T>. This method should return IQuery❔ Generics with different input and output typeI'm writing a generic at the moment which it something like this: ```csharp T GetSomething<T, U>(U I❔ Can I use slice operator with loop index (remainder operator%)?I want to slice an array from start to end index but if end index exceeds the array length, loop it ❔ My ASP.NET project isn't launchingI just made templates for a Standalone Angular project and a ASP.NET 6 project but it seems like onl❔ Making properties accessible by both static and non-static methodsI had to make a static duplicate of a property, because I wanted it to be accessible by both non-sta❔ Windows Forms different controls but same event doing different code?```cs private void OnSecondsChange(object sender, EventArgs e) { textBoxUpTime.Text += $"{sender✅ What is better, in this case, Replace() or Substring()In Unity it seems that text input comes with a 'Zero Width Space Character' at the end, and that mes❔ How to pass anchor tag values to controller to render view based on selected valueHI, how do I pass the values assigned to an anchor tag to the controller so that the controller can ❔ How to force method call regardless of type?```cs public class TempBaseClass { } public class TempGenericClass<T> : TempBaseClass { public ❔ Double property with [Required] data annotation does not validateHow can I make it so that the req body has to have a DiscountPercentage field?