Json string to a class object but not {get; set; } !

So I have a class that I did and I did some constructors to avoid the {get; set; } And I have a Json string that I want to use to fill my class, can I like put the Json to a dictionnary and then fill my variables with it ?
56 Replies
Angius
Angius2d ago
JsonSerializer serializes and deserializes properties It can serialize fields with a proper setting, sure But I can't see why would you want to avoid properties in the first place
Alexis 🫐
Alexis 🫐OP2d ago
for sure I tried multiple things with JsonSerializer but it seems to be null at every of my attemps
Angius
Angius2d ago
Show the class, show the json By default, for example, the naming must match exactly
Alexis 🫐
Alexis 🫐OP2d ago
No description
Angius
Angius2d ago
So a json property "foo" will not be serialized into a class property Foo
Alexis 🫐
Alexis 🫐OP2d ago
then there if the json
No description
Angius
Angius2d ago
Right, names seem to match So just turn those fields into properties And it will work
Alexis 🫐
Alexis 🫐OP2d ago
they match for sure
Jimmacle
Jimmacle2d ago
i don't suggest using public fields
Angius
Angius2d ago
Rule of thumb is, private fields — public properties
Alexis 🫐
Alexis 🫐OP2d ago
hm private fields instead ?
Angius
Angius2d ago
No, public properties
Jimmacle
Jimmacle2d ago
i mean, yes but then how do you access the data outside the class
Angius
Angius2d ago
Unless you don't want those values accessible from the outside of the class
Alexis 🫐
Alexis 🫐OP2d ago
hmm I guess I can just do like a "public string GetDesc" by exemple
Jimmacle
Jimmacle2d ago
no C# has properties for this purpose that's what the { get; set; } does
Alexis 🫐
Alexis 🫐OP2d ago
yeah ik ok right so if I do like private string Description {get; set;} it'll be accessible ?
Angius
Angius2d ago
What do you suppose private means?
Alexis 🫐
Alexis 🫐OP2d ago
uh im kinda dumb forget that
Jimmacle
Jimmacle2d ago
also, unrelated but i don't like the HTTP request in a constructor constructors should be very simple and usually just assign arguments to class members your code is also not properly async
Angius
Angius2d ago
Definitely shouldn't be doing any i/o
Jimmacle
Jimmacle2d ago
the constructor will return before the http request has completed and you may access the object while it's still in an "invalid" state as far as your logic is concerned
Alexis 🫐
Alexis 🫐OP2d ago
hmmm
Jimmacle
Jimmacle2d ago
the nice way to do this would be to have a static factory method that does the request then constructs the object using the response then you can properly await it in the calling code
Angius
Angius2d ago
Or a separate class, and keep Alliance just a plain ol' model
Jimmacle
Jimmacle2d ago
yeah, depending on how many API endpoints you use it might make more sense to create a service class around the API
Alexis 🫐
Alexis 🫐OP2d ago
did in an old version of that code hm It works but I really tought I could upgrade the code doing this
Alexis 🫐
Alexis 🫐OP2d ago
it was like this
No description
Alexis 🫐
Alexis 🫐OP2d ago
work well
Jimmacle
Jimmacle2d ago
i think that makes a lot more sense than what you have now, personally
Angius
Angius2d ago
Aside from the Newtonsoft usage, yeah, that looks good
Alexis 🫐
Alexis 🫐OP2d ago
but need to remove Task<User> and just put public User
Angius
Angius2d ago
Why?
Jimmacle
Jimmacle2d ago
if it's async you need the Task<> part
Angius
Angius2d ago
$rulesofasync
MODiX
MODiX2d ago
TLDR on async/await: * every .net API that is suffixed with Async (eg: .Read() and .ReadAsync()) => use the Async version * if the API name ends with Async => await it * if the API returns a Task => await it * if you have to await in a method: * that method must have the async keyword (you could even suffix your function name with Async, up to you) * if it was returning T (eg:string) => it should now return Task<T> (eg: Task<string>) * APIs to ban and associated fix:
var r = t.Result; /* ===> */ var r = await t;
t.Wait(); /* ===> */ await t;
var r = t.GetAwaiter().GetResult(); /* ===> */ var r = await t;
Task.WaitAll(t1, t2); /* ===> */ await Task.WhenAll(t1, t2);
var r = t.Result; /* ===> */ var r = await t;
t.Wait(); /* ===> */ await t;
var r = t.GetAwaiter().GetResult(); /* ===> */ var r = await t;
Task.WaitAll(t1, t2); /* ===> */ await Task.WhenAll(t1, t2);
Alexis 🫐
Alexis 🫐OP2d ago
oh yeah right Http needs to be async yeah right was wrong hm I guess that should be better you're right
Angius
Angius2d ago
Just ditch the Newtonsoft
Jimmacle
Jimmacle2d ago
yeah there is a built in json library now (System.Text.Json)
Angius
Angius2d ago
change JsonConvert.DeserializeObject<T>(str) into JsonSerializer.Deserialize<T>(str) and you're set
Alexis 🫐
Alexis 🫐OP2d ago
right ?
No description
Angius
Angius2d ago
LGTM Though I'd probably just return a null if the alliance hasn't been found
Alexis 🫐
Alexis 🫐OP2d ago
hm like just null
Angius
Angius2d ago
And I'd see if the API maybe returns 404 properly when the alliance hasn't been found If so, I'd check for that, not for a magic string ye
Alexis 🫐
Alexis 🫐OP2d ago
never returns 404
Angius
Angius2d ago
Ooof
Alexis 🫐
Alexis 🫐OP2d ago
just returns this
No description
Jimmacle
Jimmacle2d ago
i love non-standard apis
Alexis 🫐
Alexis 🫐OP2d ago
that's a silly coded API it returns messages but no error code
leowest
leowest2d ago
but does it give u a 404 response status or something? ah u said above it does not yeah that is a weird ass api
Alexis 🫐
Alexis 🫐OP2d ago
'P' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0. hmmmmm alliance = JsonSerializer.Deserialize<Alliance>(response); this line is the problem wtf i don't even understand this silly error I guess I did something wrong bruh
leowest
leowest2d ago
can you $paste the json to the site below?
MODiX
MODiX2d ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Jimmacle
Jimmacle2d ago
it's saying the JSON is invalid because the first character in it is a P so check what's actually in response
Alexis 🫐
Alexis 🫐OP2d ago
was actually my fault was doing dumb thing providing "" as string for the query everything good now thx
Unknown User
Unknown Userthis hour
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server