String 2,000,000 Characters long!

Hello!
Recently I’ve been trying to convert my whole map into a JSON. So I can try making a map builder that you only need a single string for. But with this comes a problem; when I convert the Table Data to a json, THE STRING IS 2,000,000 MILLION CHARACTERS LONG! I can’t put this string anywhere or studio crashes/errors! When it doesn’t crash, I get an error that reads: string too long

So far, I’ve tried to shorten the RequiredToAdd table to remove some properties, and it still comes out as a VERY high number! I’ve searched through the dev-forum too to see if I could paste it anywhere, and even tried PostAsync to see if that would work. It turns out the text is about 2 whole megabytes! If you can improve my code to fix this, please let me know, thanks!

local Parts = {}

local DATA = {}

local RequiredToAdd = {
	'Shape';
	'ClassName';
	'CFrame';
	'Size';
	'CanCollide';
	'Transparency';
	'Color';
	'Material';
	'Anchored';
	'CastShadow';
	'Massless';
	'Reflectance';
}

for ppp, instance in ipairs(workspace:GetDescendants()) do
	if instance:IsA('BasePart') and not (instance:IsA('Terrain') or instance:IsA('UnionOperation')) then
		if not (instance:IsA('Terrain')) then
			local CurrentItemInList = #DATA+1

			DATA[CurrentItemInList] = {}
			local NewTable = DATA[CurrentItemInList]

			if instance:IsA('MeshPart') then
				NewTable.MeshId = instance.MeshId
			end

			for _, Variation in ipairs(RequiredToAdd) do
				if not (Variation == 'Shape' and (instance:IsA('MeshPart') or instance:IsA('WedgePart') or instance:IsA('CornerWedgePart') or instance:IsA('TrussPart') or instance:IsA('TriangleMeshPart'))) then
					NewTable[Variation] = instance[Variation]
				end
			end
		end
	end
	warn('step done : ', ppp)
end

warn('nani???')

local inf = game.HttpService:JSONEncode(DATA)

warn('DONE :D', inf:len())

If you can help me, I would be very grateful!
Thank you!

2 Likes

All I can say is…Good luck finding help. lol

1 Like

Haha, Im gonna need it. :sweat_smile:

1 Like

You could represent each property as a single character for storage and only convert it back to the full name when you’re reading/writing directly to the properties of an instance. That should shorten it a good amount, but I’m not sure it’ll completely solve your issue. Some kind of compression module might help, though I’m not sure what would be the best for JSON.

Also, make sure you tostring properties before JSONEncoding them, since I’m pretty sure Roblox’s custom datatypes like Vector3 will end up as null if you don’t.

2 Likes

oh my goodness, i feel kinda dumb O_O
thanks, il try it

You can save even more room by using a list instead of a table. JSONs support arrays, right?

local RequiredToAdd = {
	'ClassName';
	'Shape';
	'Transparency';
    -- etc
}
local exampleObject = {"Part",1,0}
local obj = Instance.new(exampleObject[1])
for i, v in next, RequiredToAdd, 1 do
    obj[v] = exampleObject[i]
end

Of course it won’t be so simple when you get into things besides BaseParts (or CFrames) but I’m sure you can work it out. Doing this, your arrays will be a lot shorter since they don’t have keys.
Also some data types (such as Shape) can be compressed more than you might think. Shape is an enum and can be represented by a single number - 0 for a Ball, 1 for a Block, and 2 for a Cylinder. You can do this with things even if they aren’t enums, so long as you don’t mind a slight performance hit when loading/saving and you hard-code it in. You can also round or truncate the transparency and stuff since Transparency doesn’t usually need more than two digits behind the decimal.

2 Likes