C
C#15mo ago
Fypur

❔ Json Deserialiser throws NullReferenceException when deserializing an array

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