Data Serializer (API + Module)

JSON Data Serializer for data storage

Download it here on Roblox or here on GitHub


API

Serialize (compress for storage) - Typically to save to a DataStore

local serializer = require(path_to_module.DataSerializer)

serializer.Serialize(data)

Deserialize (reconstruct serialized data)

local serializer = require(path_to_module.DataSerializer)

serializer.Deserialize(data)

Example for saving to DataStore

-- Importing the DataSerializer module
local serializer = require(path_to_module.DataSerializer)

-- Getting the DataStore service and initializing a DataStore
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore1")

-- Error handling with pcall to ensure robust DataStore interactions
local function saveData(playerID, data)
    local success, err = pcall(function()
        local serializedData = serializer.Serialize(data)
        DataStore:SetAsync(playerID, serializedData)  -- Using player ID as a key
    end)

    if not success then
        warn("Failed to save data for player " .. playerID .. ": " .. err)
    else
        print("Data saved successfully for player " .. playerID)
    end
end

-- Example player data to be serialized and saved
local playerData = {
    PlayerName = "Player1",
    PlayerCash = 550,
}

-- Save data for a player with error handling
saveData("Player1_ID", playerData)

Example for getting data from DataStore

-- Importing the DataSerializer module
local serializer = require(path_to_module.DataSerializer)

-- Getting the DataStore service and initializing a DataStore
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore1")

-- Function to retrieve and deserialize player data
local function getData(playerID)
    local success, result = pcall(function()
        return DataStore:GetAsync(playerID)  -- Retrieve data from DataStore
    end)

    if success then
        if result then
            local deserializedData = serializer.Deserialize(result)  -- Deserialize the data

            -- Sample output to demonstrate that data was retrieved and deserialized
            print("Player Data Retrieved:")
            for key, value in pairs(deserializedData) do
                print(key .. ": " .. tostring(value))
            end

            return deserializedData
        else
            warn("No data found for player " .. playerID)
        end
    else
        warn("Error retrieving data for player " .. playerID .. ": " .. result)
    end
end

-- Retrieve data for a specific player (e.g., by player ID)
local playerData = getData("Player1_ID")

-- Further operations can be done with the retrieved and deserialized data
if playerData then
    -- Example operation: add some cash to the player's total
    playerData.PlayerCash = playerData.PlayerCash + 50
end

If you have any questions, feel free to contact me on my profile @MochaTheDev

1 Like

Couple of questions:

  • DataSaving already JSON encodes any save-able data set to be saved. It automatically decodes it upon retrieval. This applies only to tables and dictionaries, because those contain more than 1 block of data. Having this in mind, your module already converts the array to a dtring and saves that string…?
  • If it indeed does that, does it come with pre-packed unsave-able values such as Color3 and Vectors/CFrames? Meaning your module automatically converts some string data to the respective datatype? (Vector3 → String → Vector3)
2 Likes

No, it doesn’t as of right now. It’s only intended to be use-as-needed with (expectedly) limited use cases. At some point I do intend to update the release to be able to save things that cannot be traditionally compressed with JSON encoding (like Vector3 and CFrame per your examples.)

This is a wrong concept, serialization is not compression by any means, and in fact, here you are making it bigger by using JSON, you should maybe try and implement a flexible binary system or so but I think that would be too much xd

1 Like