JSONEncode increases table length?

I’m trying to save the positions of blocks in 4 values ( id, x, y, z )

My plot has 2466 blocks so the length of the table should be 2466 x 4 = 9864 but when I JSONEncode it jumps up to 33283 is this normal?


function SaveIsland(player)
	local ChunkSaveEncoded = game:GetService("HttpService"):JSONEncode(ChunkSave);
	local SaveChunks = ChunkData("IslandBlocks", player)

	local Save = Data.compress(ChunkSaveEncoded)
	SaveChunks:Set(Save)
	
	print(#ChunkSave)
	print(#ChunkSaveEncoded)
	print(#Save)
end




function BlockTable(player)

	for i,v in pairs(ChunkSave) do
		table.remove(ChunkSave,i)
	end
		
	for _,Block in pairs(workspace.Blocks:GetChildren()) do
		local ID = BlockData[Block.Name].Id
				
		local Position = Block.Position
					
		local Save = {ID,math.ceil(Position.x),math.ceil(Position.Y),math.ceil(Position.z)}
					
		table.insert(ChunkSave,Save)
	end
		
	--- Save
	SaveIsland(player)
end


Players.PlayerRemoving:Connect(BlockTable)

image

1 Like

The value you assume the table’s length should be (9864) also assumes that all 4 of your digits are only one digit and that you don’t have anything other than a single table for data (+2 characters in itself). Naturally, if those numbers are more than a single digit, then your numbers expand as well, so that in itself is a wrong assumption to make. The character size will expand.

Another thing to know is that JSONEncode is turning your table into a string format so that it can be saved. If you have nested tables or whatever, then there needs to be characters to differentiate tables from each other. There again, you have extra characters being added.

This seems completely normal. You can print out the result of a JSONEncoded table to see if any unexpected values are being added or if you just didn’t account for the fact that numbers may be showing up larger and that there’s multiple tables to be packed.

1 Like

You’re actually saving a minimum of 10 characters per block: json [0,1,2,3],
when using JSONEncode (count the characters, this is the best case scenario where the block ID is one digit, as are the positions).

Assuming your block IDs stay below 256 and your plot coordinates stay within 0-15, you could compress this data by compressing the data into a binary string, then saving it as a string.
8+4+4+4=20 bits per block, and 20*2466=4932 bits for your entire region (which only takes up 617 characters when encoded as a raw string). This would bring your save size from 10694 to 617 characters in length!

1 Like

@wow13524 I don’t quite understand is there a good resource on this?

My plot is 120x120x120

1 Like

A 120^3 region is too big for the method I described above (it results in 6,264,000 characters, way too big for a DataStore).
I can get more in-depth once I finish my homework :slight_smile:

1 Like

Will be greatly appreciated :smiley:

1 Like