C
C#10mo ago
shibboleet

✅ JsonConvert.DeserializeObject return contains all null elements

Using Newtonsoft.Json, I have the following JSON (attached) with the following deserializer code:
public class ObjectDB
{
public struct DBFile
{
long Timestamp;
List<Class> Classes;
List<Object> Objects;
List<Category> Categories;
}
struct Class
{
string InternalName;
string Name;
string Notes;
int Games;
int Progress;
Dictionary<string, Parameter> Parameters;
}

struct ArgValue
{
string Value;
string Notes;
}

struct Object
{
string InternalName;
string ClassNameSMG1;
string ClassNameSMG2;
string Name;
string Notes;
string Category;
string AreaShape;
string ListSMG1;
string ListSMG2;
string File;
int Games;
int Progress;
bool IsUnused;
bool IsLeftover;
}

struct Category
{
string Key;
string Description;
}

struct Parameter
{
string Name;
string Type;
int Games;
bool Needed;
string Description;
List<ArgValue> Values;
List<string> Exclusives;
}

public static void LoadDB()
{
JsonSerializerSettings settings = new();
sDatabase = JsonConvert.DeserializeObject<DBFile>(File.ReadAllText("res/objectdb.json"), settings);
}

static DBFile sDatabase;
}
public class ObjectDB
{
public struct DBFile
{
long Timestamp;
List<Class> Classes;
List<Object> Objects;
List<Category> Categories;
}
struct Class
{
string InternalName;
string Name;
string Notes;
int Games;
int Progress;
Dictionary<string, Parameter> Parameters;
}

struct ArgValue
{
string Value;
string Notes;
}

struct Object
{
string InternalName;
string ClassNameSMG1;
string ClassNameSMG2;
string Name;
string Notes;
string Category;
string AreaShape;
string ListSMG1;
string ListSMG2;
string File;
int Games;
int Progress;
bool IsUnused;
bool IsLeftover;
}

struct Category
{
string Key;
string Description;
}

struct Parameter
{
string Name;
string Type;
int Games;
bool Needed;
string Description;
List<ArgValue> Values;
List<string> Exclusives;
}

public static void LoadDB()
{
JsonSerializerSettings settings = new();
sDatabase = JsonConvert.DeserializeObject<DBFile>(File.ReadAllText("res/objectdb.json"), settings);
}

static DBFile sDatabase;
}
However, the deserializer is not throwing an error but every element of DBFile is null. https://cdn.discordapp.com/attachments/1177777512525340752/1179410186717364324/image.png?ex=6579ae54&is=65673954&hm=c0ab82bfb39fd7785f407e64f07aaba100ba3cad3f1326bf4a59fb3b4a3935f4&
24 Replies
cap5lut
cap5lut10mo ago
that is the default behaviour, if u want to change that u will have to pass some settings: https://www.newtonsoft.com/json/help/html/DeserializeMissingMemberHandling.htm
shibboleet
shibboleet10mo ago
interesting
No description
shibboleet
shibboleet10mo ago
ah, I think I see what happens
shibboleet
shibboleet10mo ago
No description
shibboleet
shibboleet10mo ago
good 'ol formatting and string escapes not exactly sure how to approach that
cap5lut
cap5lut10mo ago
thats actually the wrong thingy. this configures what happens if the JSON has some property that is not mapped to the POCO to set if it throws or sets to null, it seems u need a custom ContractResolver: https://stackoverflow.com/questions/57673787/how-to-throw-a-jsonserializationexception-when-a-property-is-missing-in-the-json#answer-57680436
shibboleet
shibboleet10mo ago
the issue moreso lies in the inability of it to properly parse the JSON due to string formatting
cap5lut
cap5lut10mo ago
do u mean because the \r\ns in there?
shibboleet
shibboleet10mo ago
¯\_(ツ)_/¯ it can't find an obvious key that is clearly there, so something is blocking it from doing so
cap5lut
cap5lut10mo ago
because they dont cause an issue, these are whitespaces (a windows line break to be exactly)
shibboleet
shibboleet10mo ago
removed the \r\n formatting strings but it still fails perhaps it's the \"Timestamp\"
cap5lut
cap5lut10mo ago
nope, thats just how visual studio displays the string
shibboleet
shibboleet10mo ago
then I can't tell you what the issue is here it can't find the key "Timestamp" even though it's there
cap5lut
cap5lut10mo ago
aaah, i think i see the issue its the typical property vs field thingy i guess
shibboleet
shibboleet10mo ago
hm?
Want results from more Discord servers?
Add your server