How to Deserialize dynamic types from json
I'm making a game where users will store multiple different types of items. My Question is when deserializing an item from json it will just deserialize to the given
type attribute in the json.
example:
I want to turn Superhero character json into superhero, but instead it becomes character.36 Replies
You're looking for the term "polymorphic", and System.Text.Json has built in support for this via
[JsonDerivedType] and [JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
See the following simple example
so if I have a List<Chart>, I can store LineCharts and BarCharts in it, and they will serialize with a type propertythat is amazing
and the classes can be as different as they need to be
wow
all you need to do is decorate the base class with the correct attributes
I gotcha
thanks man
np
I think you just solved the worlds hardest problem
😄
ðŸ˜
No, I've just done exactly this many times
and I remember when STJ first got these attributes and what a relief it was
oh wow
yeah
I can imagine
JsonPolymorphic isnt strictly needed, but if you skip it, or dont assign a TypeDescriminatorPropertyName, it will default to $type
so its pretty common to override itso in the class that overwrites you'd just add it?
like this:
No, you set the attributes on the base class
so
Character in your case
and JsonDerivedType takes two parameters, the typeof(YourSubclass) and also a string that is its discriminato value, ie the actual value written to the type field
nameof(YourSubClass) will be fine in most cases
just make sure its uniqueis there a trick to if you've got 1000 characters/items? I don't but I know I'll be adding quite a bit.
It would be a little strange adding so many above the class
There might be a source generator that can do it, but I'd recommend looking at your design instead
in your example above, this doesnt need to be a subclass
its just a constructor with values
functionally, a
GMoney isnt any different from a Character, once the values are set up properlycorrect, it is just a basic character right now. This character has a little bit more complexity to it.
But I do want to make my code as best as it can be, so if you have any reccomendations I'm all ears.
Yea that might be a little more complicated to turn into pure data, but its doable. The
CheckWeapon would need to be turned into a class, and you could serialize that too.
Its all serialization 😄ðŸ˜
I'll just add it to Base class
another thing is that you'll likely want to separate your actual "the game is running" classes from your serialization classes
at least if you are to rely on JSON
json doesnt handle reference loops very well, and instead you'd use ID lookups
but thats a bad model for your actual game code, where you certainly want to use references
it is a bit weird, as the game will support online and offline via game saves, and the online database.
the game uses nosql
I have not worked a lot with nosql databases, but I'd imagine you cant just stick a complex C# class with several references in there without configuration?
oh yeah no, the data is serialized then put into the items table.
So that makes me even further suggest you implement a storage model and a game model, and you write code that can convert between the two
hidden bonus, this lets you very easily make the game moddable 🙂
if most of your games data is literally just data files, users can easily make their own characters, levels, items etc
thats kinda what I was going for except in coding sense.
I kinda see that, but if its code then at the very least they need to recompile your game to make a mod
I don't really want modding, just cause it's supposed to be mostly online, and modded items could cause a problem.
For an online game, you cant trust the client at all
so all the client gets to do is tell you stuff like "I want to give item 5123125 in my inventory to player XXXX"
it never ever gets to say "oh I just got a new item, here are its stats"
thats strictly in the server
ah ah ah I see
I miss spoke
the game client handles all the item obtaining and such, but when an item is obtained/modified/removed it tells the server, and it updates the database accordingly and gives players ratings based on their items.
then when the player re logs in another time it will have all the item data
Okay, but what stops me as a malicious player from just sending "I just got item X" packages to the server directly? 🙂
Anyways, thats besides the original point of this thread. I don't know of any good articles on data driven game design in C#, but I do know one for Rust and I imagine the concept should translate very well
this is his example of a healing potion
right, I just added authentication to the server, which hopefully will stop most of it, Cheat engine is still a problem. 😠for now I can restrict users. Since all of it is online with data metrics it will be pretty easy to tell when someone starts cheating.
I gotcha
Fair enough. You'll find that it will become a huge problem if the game ever gets any popularity, as cheating is unfortunately quite rampant if the game allows it in any way, be that exploitable networking or direct memory editing
yes ðŸ˜
I think about it in the shower
I honestly don't know how people will view my game
it's a pretty simple concept
alright, thanks for your advice and fix! Have a great one! 👋