C
C#14mo ago
Vedrit

✅ Questions about casting

Hi all, I'm looking to simplify some network code I've got. Networking doesn't really play too large a role in this particular case, but it does add a level of separation between scripts sending information and scripts receiving information. I have this one class that is used to serialize/deserialize the data for transmission. Currently, I have separate Dictionaries and counters for each data type it has to manage, so that when the data is read, I know exactly how much to cast as which type, which I do in order it was written. My question is, do I have to use separate Dictionaries for each type, or could I use one generic Dictionary? Is there any ambiguation in the typical data types? int, string, float, etc, that would be a problem if I try to cast the data on-the-fly?
8 Replies
Omnissiah
Omnissiah14mo ago
why would you need a dictionary? i guess we need some code how is the type recognized, is there a discriminator? also the counters things i don't understand
Vedrit
VedritOP14mo ago
I used dictionaries mainly for organization. The keys and values are read from the dictionary I don't know the details behind the deserialization, but to my understanding it's a byte array. You iterate through, telling it to cast the bytes as a specific type, then move to the next index. The counter is so that when I'm iterating through, I know exactly how many are of which type, and since they were written in groups, I can read them in groups. I guess, long story short, if I serialize an int, a string, and a float to a byte array, do I have to know the correct type when casting?
Angius
Angius14mo ago
Well, ideally you'd serialize an object with an int, a string, and a float property... and deserialize to that same type
Jimmacle
Jimmacle14mo ago
do you have code examples? normally your messages should include a header that provides necessary information like what type of message it is, how long it is, etc
Omnissiah
Omnissiah14mo ago
so wait instead of having a straight (isolated) deserializer, with a sequence of fields types and order, you "cycle" a dictionary to rebuild an object? because that would be probably the more difficult option i don't know if we are talking about nested types or what
Jimmacle
Jimmacle14mo ago
also, before rolling your own binary serializer/deserializer can you use something more standard like json, protobuf, etc?
Vedrit
VedritOP14mo ago
It's not mine, it's part of a networking library. That's why I said I didn't know the details. I'll get a code example in a bit, just woke up. Sorry, after putting my thoughts together, I've realized that this is a question better suited to the community for the network library I'm using. Thanks for trying, everyone.
Accord
Accord14mo 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