❔ HTTP request object creation / ignoring fields when deserializing JSON
Hey there. I am writing a program using the Deck of Cards API which can be found here: https://deckofcardsapi.com/. The API returns a response which contains JSON.
I have a class named "Deck", I first wanted "Deck" to load and deserialize itself in the constructor using the API but I don't really know if that's possible or a good idea. I am wondering if there are better alternatives or best practices for this problem.
I also have a second problem. Whenever I want to request a card using the API, the response contains many properties I don't need. Is there a way of only deserializing certain properties? Above you can see a class called "Card" and I only need "Value", "Suit", and "Image" but the JSON in the response contains much more as you can see below.
Response when drawing card(s).
Deck of Cards
Deck of Cards - an API for playing cards.
24 Replies
Regarding the first question, constructors cannot be asynchronous, and HTTP calls should be
The usual way would be having some
DeckService
class that has an async Task<Deck> GetDeck()
method
For the latter, if the class you deserialize into has only selected properties... it will have only thoseOkay thank you both for the responses!
generally i would say u need a bit more, the suit should be an enum, not a string, afaik stj can automatically translate strings to enums, but if not u might need a little custom converter for that enum
STJ should be able to convert strings to enums, yeah
never touched it myself for enums so i want sure ;p
i rarely use json nowadays but binary (de)serialization using source generates
Yes, I read about custom converters but didn't know it could do that automatically? does it work when the enum name is not case-sensitive equal to the json value?
angius#0000
REPL Result: Failure
Exception: JsonException
Compile: 605.900ms | Execution: 84.837ms | React with ❌ to remove this embed.
Doesn't work by default, alas
angius#0000
REPL Result: Failure
Exception: CompilationErrorException
Compile: 681.999ms | Execution: 0.000ms | React with ❌ to remove this embed.
Huh, thought it's an existing converter?
i think in this case u would even want a custom serializer if i understand it correctly, cuz in ur enum u would use the singular
(cant remenber the other one in english :'D)
but the api spits it out as plurar (hearts, clubs, spades, blahs)
cap5lut#0000
REPL Result: Failure
Exception: JsonException
Compile: 689.527ms | Execution: 103.512ms | React with ❌ to remove this embed.
hmm
._. we dont pass the options xD
cap5lut#0000
REPL Result: Success
Result: Foo
Compile: 719.373ms | Execution: 95.137ms | React with ❌ to remove this embed.
lmao
Yeah, it'll work now
it is, but iirc its in a different namespace
yep
using System.Text.Json.Serialization;
as above 😄
angius#0000
REPL Result: Success
Result: bool
Compile: 735.518ms | Execution: 107.051ms | React with ❌ to remove this embed.
Yeah, deserializes to an enum just fine
that will probably still apply, or am i wrong here?
oh wait wrong referenced message
this one ⤴️
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.