C
C#9mo ago
Fypur

❔ Json Deserialiser throws NullReferenceException when deserializing an array

No description
No description
36 Replies
Fypur
Fypur9mo ago
cs here has a value of
"[{\"Key\":null,\"MouseButton\":null,\"ControllerButton\":null},{\"Key\":null,\"MouseButton\":null,\"ControllerButton\":null}]"
"[{\"Key\":null,\"MouseButton\":null,\"ControllerButton\":null},{\"Key\":null,\"MouseButton\":null,\"ControllerButton\":null}]"
Im literally serializing something and deserializing it and it throws an error, Im so confused this works fine with an int instead of a Control, or with the c array set to be empty
Angius
Angius9mo ago
I mean, yes it works with an int Because it's a simple type I don't suppose those... Winforms controls or whatever they are will be easily serializeable
Fypur
Fypur9mo ago
They're a custom class It's basically just 3 enums It serializes just fine, it just doesn't work deserializing
Angius
Angius9mo ago
If they're enums, they should serialize fine
Fypur
Fypur9mo ago
that's what Im saying but it ain't working
Angius
Angius9mo ago
How are you serializing it?
Fypur
Fypur9mo ago
Control[] c = new Control[] { new Control(), new Control() };
string cs = JsonSerializer.Serialize(c);
c = JsonSerializer.Deserialize<Control[]>(cs);
Control[] c = new Control[] { new Control(), new Control() };
string cs = JsonSerializer.Serialize(c);
c = JsonSerializer.Deserialize<Control[]>(cs);
that's the whole code no more no less
Angius
Angius9mo ago
Well You're serializing them empty Ah, wait
Fypur
Fypur9mo ago
well it should work still
Angius
Angius9mo ago
Yeah
TheRanger
TheRanger9mo ago
are Keys, MouseButton and Buttons enum?
Fypur
Fypur9mo ago
yes the serializing part goes fine, and then it just doesn't wanna work on deserializing shit you can compile on discord now?
MODiX
MODiX9mo ago
Angius
REPL Result: Success
using System.Text.Json;

enum Foo {A, B, C}
class Control {
public Foo? Foo { get; set; }
}

Control[] c = new Control[] { new Control(), new Control() };
string cs = JsonSerializer.Serialize(c);
c = JsonSerializer.Deserialize<Control[]>(cs);

new { cs, c }
using System.Text.Json;

enum Foo {A, B, C}
class Control {
public Foo? Foo { get; set; }
}

Control[] c = new Control[] { new Control(), new Control() };
string cs = JsonSerializer.Serialize(c);
c = JsonSerializer.Deserialize<Control[]>(cs);

new { cs, c }
Result: <>f__AnonymousType0#1<string, Control[]>
{
"cs": "[{\"Foo\":null},{\"Foo\":null}]",
"c": [
{
"foo": null
},
{
"foo": null
}
]
}
{
"cs": "[{\"Foo\":null},{\"Foo\":null}]",
"c": [
{
"foo": null
},
{
"foo": null
}
]
}
Compile: 621.427ms | Execution: 108.342ms | React with ❌ to remove this embed.
Angius
Angius9mo ago
It works
Fypur
Fypur9mo ago
huhh
Angius
Angius9mo ago
Maybe because it's a struct...?
TheRanger
TheRanger9mo ago
works here
MODiX
MODiX9mo ago
Angius
REPL Result: Success
using System.Text.Json;

enum Foo {A, B, C}
struct Control {
public Foo? Foo { get; set; }
}

Control[] c = new Control[] { new Control(), new Control() };
string cs = JsonSerializer.Serialize(c);
c = JsonSerializer.Deserialize<Control[]>(cs);

new { cs, c }
using System.Text.Json;

enum Foo {A, B, C}
struct Control {
public Foo? Foo { get; set; }
}

Control[] c = new Control[] { new Control(), new Control() };
string cs = JsonSerializer.Serialize(c);
c = JsonSerializer.Deserialize<Control[]>(cs);

new { cs, c }
Result: <>f__AnonymousType0#1<string, Control[]>
{
"cs": "[{\"Foo\":null},{\"Foo\":null}]",
"c": [
{
"foo": null
},
{
"foo": null
}
]
}
{
"cs": "[{\"Foo\":null},{\"Foo\":null}]",
"c": [
{
"foo": null
},
{
"foo": null
}
]
}
Compile: 644.121ms | Execution: 104.912ms | React with ❌ to remove this embed.
Angius
Angius9mo ago
Yeah nah, works with a struct
Fypur
Fypur9mo ago
No description
No description
No description
Fypur
Fypur9mo ago
It's all enums
Fypur
Fypur9mo ago
This is the constructor
No description
TheRanger
TheRanger9mo ago
Monogame?
Fypur
Fypur9mo ago
yup The Control class is a custom one I made
TheRanger
TheRanger9mo ago
i have monogame, i can test
Fypur
Fypur9mo ago
well yeah but it doesn't have anything to do with it it should be basic c#
TheRanger
TheRanger9mo ago
in case whats Fiourp.MouseButton ? probably an enum u made
Fypur
Fypur9mo ago
Custom enum
Fypur
Fypur9mo ago
No description
Fypur
Fypur9mo ago
bruh I just updated dotnet And now it works
Fypur
Fypur9mo ago
No description
TheRanger
TheRanger9mo ago
yeah i just ran it right now and it works
Fypur
Fypur9mo ago
thank you all for the help the problem was me running dotnet 6.2 and i needed dotnet 6.4 yay defnitly not frustrated lol
TheRanger
TheRanger9mo ago
weird
Fypur
Fypur9mo ago
yea Ig the small patches do matter
Accord
Accord9mo 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.