JSONEncode deleting parts of array

I’m using Terrain:ReadVoxels() to save terrain chunks to an external realtime database. Saving the occupancies works great, but as soon as I try to encode the materials using JSONEncode(), it deletes a large portion of the array.

Simply running the line:

print(HttpService:JSONDecode(HttpService:JSONEncode(Materials)))

Destroys the array. It essentially takes all information that is too deep in the table and just converts it to an empty table.

BEFORE JSONEncode/Decode:
image
AFTER JSONEncode/Decode:
image
Notice how it just stops saving new data after a certain point.

Is this just a limitation of JSONEncode/Decode? If it is, how can I convert this information into a string to save it. Thank you.

1 Like

If your table has numeric indexes and dictionaries, then this must be the problem. At least in the datastore it does not accept numeric indexes and dictionaries, and from what I know when saving in the datastore the tables are converted to json.

1 Like

Datastores aside, simply running the line:

print(HttpService:JSONDecode(HttpService:JSONEncode(Materials)))

converts the table into a problem.
Apologies if I misunderstand what you mean. I’m just trying to find a solution where I can encode the array into a string and decode it back into an array. Does that make sense?

Thanks for your help.

1 Like

So, what I meant about the datastore is that when you save a table in the datastore, they are converted to json, so the problem of the datastore not saving numeric indexes and dictionaries may be a limitation of the conversion of the table to json.

In short, check if your table has a numeric index and a dictionary, this may be the problem. Use only one, either a numeric index or a dictionary.

1 Like

If I were to guess, it’s probably because Enums aren’t JSON serializable (you’re trying to serialize an Enum.Material in this case). You should iterate over the array and serialize it in some way (I would save the Material.Value as ints are generally shorter than strings), then to deserialize, use Enum.Material:FromValue(storedValue)

2 Likes

RBLXSerialize.rbxm (117.7 KB)

this useful module was made by Whim, it should solve your problem. It converts types into strings and vice versa

1 Like

I much prefer Data-Oriented-House’s Squash library. It’s well-researched and space-optimized:
https://data-oriented-house.github.io/Squash/api/Squash/#EnumItem

1 Like

This module is throwing tons of errors for me.
“attempt to get length of a boolean value”

So this doesnt seem to work for my use case.

1 Like

I’m interested in using this, but for the love of God I can’t find a download link. What am I missing :rofl:

1 Like

You could use moon wave / wally to import it. Although if you’d like, you can just paste all the code into a module script

1 Like

Not sure if this is related to your problem at any way but from my personal testing Roblox’s JSONEncode and Decode do have a max to the amount of data they can handle. It’s around 200,000 characters iirc, and you can try paginating data if you want to use more than that.

Okay, I imported it but I’m super confused on how to actually use it. This seems like a super high-profile module and I’m just not even sure where to start.
Do you think it would be possible for you to give me some code snippets of what I should do?

Thanks so much for all the help, seriously awesome :smile:

Nevermind, created my own serialization system. Here’s the code for those who need it in the future:

local function SerializeMaterials(Materials)
	for _, D1 in Materials do
		if type(D1) == "table" then
			for _, D2 in D1 do
				for Iteration, D3 in D2 do
					D2[Iteration] = D3.Value
				end
			end
		end
	end
	return Materials
end
local function DeserializeMaterials(Materials)
	for _, D1 in Materials do
		if type(D1) == "table" then
			for _, D2 in D1 do
				for Iteration, D3 in D2 do
					D2[Iteration] = Enum.Material:FromValue(D3)
				end
			end
		end
	end
	return Materials
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.