Hello! I sorta understand Serializing and Deserializing… i’d like to understand more about what they are for and especially WHAT ARE THE BENEFITS? Are they good if you are storing mountains and mountains of data? I can’t find clear articles anywhere… i appreciate it it! You guys are the best!
As I understand it when you serialize an object you take all the properties (or the state of a object) and save it to a format that you can put in a datastore. So if you had like a player made character with custom meshes then you could serialize the character and then deserialize it to load it again from a datastore. (I may be wrong though this is just how I understand it)
Read here, it may help you understand the concept.
The main benefit is compression. Once data is compressed, it can be saved without failing. Unlike having the data in extremely long strings that can fill it up pretty much quickly.
If you use HttpService:JSONDecode()
and check the length of it, you’ll get the data size. That’s how you can see how much data it contains.
To really simplify it (I assume you refer to compression in some form):
Pros:
- Smaller data amount (saves space, network bandwidth, etc - generally all good)
- You can introduce things like checksums and parity bits to ensure data integrity
Cons:
- If you mess it up, it goes wrong quick (data corruption)
- It can be more intensive / complex to process when you actually receive the data
Okay, yeah i have huge amounts of data, especially string data, in 5 different basic stores [Character info store] which is all the character appearance stuff, name, description, etc. which contains like 20 pieces of data, then [Magic Values Store] which is all the basic leveling values that contribute to the primary core functions of the game, theres like 20 pieces of data there, then [Spells Store] this is all the data pertaining to the spells themselves, theres like 40 to 50 pieces rn, but it will grow, [Inventory Store] all inventory items which each item has a dictionary within a dictionary, theres a lot of pieces in here and especially a lot of strings, and finally [NPC info store] which contains all the npc inventories and information like the number of times you met the NPC and such. That contains a lot of info too.
So basically, you’re saying, “MANELIN SERIALIZE THAT STUPID DATA”? XD
i guess i should.
JSONDecode will give you the length of an array (if valid) and compression doesn’t guarantee non-failing data saving, even with respect to the amount of characters you use in your final data set.
Unlike what others have said serialization does not mean compression, these are different things. It’s a way of converting to a byte stream. This allows you to send it to a destinations outside of the program (e.g. to a database or over a network). Compression on the other hand is good for storing mountains of data by making the data smaller in size.
If serialization does not mean compression, then what are you talking about for compression? How would you do that then?
Serialized data can be compressed. Serialization is about representing data differently, like as a string. Compression is about encoding patterns the data has and making it smaller. Deserializing is about turning the serialized data back into the original form.
here’s an example of serializing a Color3
object into a hexadecimal string:
local function serializeColor3(color)
return ("%02x%02x%02x"):format(color.r*255, color.g*255, color.b*255)
end
local function deserializeColor3(serial)
local r, g, b = serial:match("(..)(..)(..)")
r, g, b = tonumber(r, 16), tonumber(g, 16), tonumber(b, 16)
return Color3.fromRGB(r, g, b)
end
print(serializeColor3(Color3.fromRGB(255, 128, 64)))
> ff8040
this may also be considered encoding. Serialization in particular may just store that color in a list, assign it a label like “1” to refer to the first element in the list, and now the label “1” is the serial.
The oopsie when I realized it’s JSONEncode
and not JSONDecode
. Someone please remind me to not read things while tired.