C
C#2y ago
Fypur

❔ Json Deserialiser throws NullReferenceException when deserializing an array

No description
No description
36 Replies
Fypur
FypurOP2y 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
Angius2y 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
FypurOP2y ago
They're a custom class It's basically just 3 enums It serializes just fine, it just doesn't work deserializing
Angius
Angius2y ago
If they're enums, they should serialize fine
Fypur
FypurOP2y ago
that's what Im saying but it ain't working
Angius
Angius2y ago
How are you serializing it?
Fypur
FypurOP2y 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
Angius2y ago
Well You're serializing them empty Ah, wait
Fypur
FypurOP2y ago
well it should work still
Angius
Angius2y ago
Yeah
TheRanger
TheRanger2y ago
are Keys, MouseButton and Buttons enum?
Fypur
FypurOP2y 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
MODiX2y 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
Angius2y ago
It works
Fypur
FypurOP2y ago
huhh
Angius
Angius2y ago
Maybe because it's a struct...?
TheRanger
TheRanger2y ago
works here
MODiX
MODiX2y 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
Angius2y ago
Yeah nah, works with a struct
Fypur
FypurOP2y ago
No description
No description
No description
Fypur
FypurOP2y ago
It's all enums
Fypur
FypurOP2y ago
This is the constructor
No description
TheRanger
TheRanger2y ago
Monogame?
Fypur
FypurOP2y ago
yup The Control class is a custom one I made
TheRanger
TheRanger2y ago
i have monogame, i can test
Fypur
FypurOP2y ago
well yeah but it doesn't have anything to do with it it should be basic c#
TheRanger
TheRanger2y ago
in case whats Fiourp.MouseButton ? probably an enum u made
Fypur
FypurOP2y ago
Custom enum
Fypur
FypurOP2y ago
No description
Fypur
FypurOP2y ago
bruh I just updated dotnet And now it works
Fypur
FypurOP2y ago
No description
TheRanger
TheRanger2y ago
yeah i just ran it right now and it works
Fypur
FypurOP2y 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
TheRanger2y ago
weird
Fypur
FypurOP2y ago
yea Ig the small patches do matter
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.

Did you find this page helpful?