Your method seems to increase the string length a lot.
Comparing JSONEncode vs BitBuffer on your method: This chart shows the string lengths using Buffer:ToBase91
and Buffer.FromBase91
but it also applies to Buffer:ToString
and Buffer.FromString
.
I kept adding more elements to OriginalTable.upper
like so:
local OriginalTable = {
test = "hello",
test2 = "world",
upper = {
test = {
test = "hello",
test2 = "world",
},
test2 = {
test = "hello",
test2 = "world",
},
test3 = {
test = "hello",
test2 = "world",
},
test4 = {
test = "hello",
test2 = "world",
},
test5 = {
test = "hello",
test2 = "world",
},
}
}
Modifications I made in the testing:
print("==== Compare ====")
local json = game.HttpService:JSONEncode(OriginalTable)
print("Orginal:\n", json, #json)
warn()
-- Serialize Method
local Data = RecursiveStore(OriginalTable)
-- Deserialize Method
local Table = RecursiveRead(Data)
print("Serialized:\n", Data, #Data)
The whole script with my modifications
local BitBuffer = require(game.ServerStorage.BitBuffer)
local BinaryDefintions = {
ValueType = {
-- Conversion
[0x01] = "Table",
[0x02] = "String",
-- NormalIndex values
Table = 0x01, -- Indicator for tables
String = 0x02, -- indicator for our only type.
}
}
-- ValueTypeIndicators
function RecursiveStore(tbl)
local Buffer = BitBuffer.new()
for Index,Value in pairs(tbl) do
if typeof(Value) == "table" then
-- Use our function to store again!
-- This is the idea of recursion.
local ConvertBuffer = RecursiveStore(Value)
--WriteIndexTable
Buffer:WriteUInt(8,BinaryDefintions.ValueType.Table)
Buffer:WriteString(Index)
Buffer:WriteString(ConvertBuffer)
continue
end
-- This is just a basic implimtation that just uses the implicit casting of the tostring() method.
Buffer:WriteUInt(8,BinaryDefintions.ValueType.String)
Buffer:WriteString(Index)
Buffer:WriteString(Value)
end
return Buffer:ToBase91()
end
function RecursiveRead(String)
local Table = {}
local Buffer = BitBuffer.FromBase91(String)
-- This will contuine
repeat
-- Index to retrive the type written
local Type = BinaryDefintions.ValueType[Buffer:ReadUInt(8)]
-- Associate Types with reading methods
if Type == "Table" then
local Index,RawValue = Buffer:ReadString(),Buffer:ReadString()
local Value = RecursiveRead(RawValue)
-- Store values now!
Table[Index] = Value
end
-- String
if Type == "String" then
local Index,Value = Buffer:ReadString(),Buffer:ReadString()
-- Store values now!
Table[Index] = Value
end
-- So we know when were done reading!
until not Type
return Table
end
local OriginalTable = {
test = "hello",
test2 = "world",
upper = {
test = {
test = "hello",
test2 = "world",
},
test2 = {
test = "hello",
test2 = "world",
},
test3 = {
test = "hello",
test2 = "world",
},
test4 = {
test = "hello",
test2 = "world",
},
test5 = {
test = "hello",
test2 = "world",
},
}
}
print("==== Compare ====")
local json = game.HttpService:JSONEncode(OriginalTable)
print("Orginal:\n", json, #json)
warn()
-- Serialize Method
local Data = RecursiveStore(OriginalTable)
-- Deserialize Method
local Table = RecursiveRead(Data)
print("Serialized:\n", Data, #Data)