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
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 placefor sure I tried multiple things with JsonSerializer but it seems to be null at every of my attemps
Show the class, show the json
By default, for example, the naming must match exactly
So a json property
"foo"
will not be serialized into a class property Foo
then there if the json
Right, names seem to match
So just turn those fields into properties
And it will work
they match for sure
i don't suggest using public fields
Rule of thumb is, private fields — public properties
hm private fields instead ?
No, public properties
i mean, yes but then how do you access the data outside the class
Unless you don't want those values accessible from the outside of the class
hmm I guess I can just do like a "public string GetDesc" by exemple
no
C# has properties for this purpose
that's what the { get; set; } does
yeah ik
ok right so
if I do like private string Description {get; set;} it'll be accessible ?
What do you suppose
private
means?uh
im kinda dumb
forget that
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
Definitely shouldn't be doing any i/o
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
hmmm
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
Or a separate class, and keep
Alliance
just a plain ol' modelyeah, depending on how many API endpoints you use it might make more sense to create a service class around the API
did in an old version of that code hm It works but I really tought I could upgrade the code doing this
it was like this
work well
i think that makes a lot more sense than what you have now, personally
Aside from the Newtonsoft usage, yeah, that looks good
but need to remove Task<User> and just put public User
Why?
if it's async you need the Task<> part
$rulesofasync
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:
oh yeah right Http needs to be async
yeah right was wrong
hm I guess that should be better you're right
Just ditch the Newtonsoft
yeah there is a built in json library now (System.Text.Json)
change
JsonConvert.DeserializeObject<T>(str)
into
JsonSerializer.Deserialize<T>(str)
and you're setright ?
LGTM
Though I'd probably just return a
null
if the alliance hasn't been foundhm like just null
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
never returns 404
Ooof
just returns this
i love non-standard apis
that's a silly coded API
it returns messages but no error code
'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
can you $paste the json to the site below?
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!
it's saying the JSON is invalid
because the first character in it is a P
so check what's actually in
response
was actually my fault
was doing dumb thing
providing "" as string for the query
everything good now thx
Unknown User•this hour
Message Not Public
Sign In & Join Server To View