Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Is JSON encoding more useful for saving data?

Asked by 6 years ago

I've been hearing from some people that JSON is better, but I'd like to hear other people's opinions and maybe some advantages or disadvantages.

For example, should I use this code:

(mock-up)

local dss = game:GetService("DataStoreService")
local http = game:GetService("HttpService")
local store = dss:GetDataStore("whatever")
local data = http:JSONDecode(store)

--load data and do whatever

local encodedData = http:JSONEncode(newData)
store:SetAsync(tableOfData, encodedData)

(may not be correct as I did it loosely in the way another user did)

or the common way:

local dss = game:GetService("DataStoreService")
local store = dss:GetDataStore("whatever")
local data = store:GetAsync(tableOfData)

--load data

store:SetAsync(tableOfData, newData)

1 answer

Log in to vote
4
Answered by 6 years ago
Edited 6 years ago

Data structures (such as tables) are automatically encoded to JSON upon set request to ROBLOX's data store service. For instance, there is no difference between...

DataStore:SetAsync(key, table)

and

DataStore:SetAsync(key, JSONEncode(table))

Encoding a table with JSON for the purpose of saving is redundant.

However

Although you don't need to encode a table in JSON to save it, you may want to. There is a character limit to the JSON-formatted data that you can save under one key (I think it's 2^16 characters or something like that). So, perhaps one might want to encode a table before saving it to check the length of the encoded data to see if it will save successfully. For example...

local HttpService = game:GetService("HttpService")

local table = {}
local JSON_table = HttpService:JSONEncode(table)

-- check size limit of the data being saved
if (#JSON_table >= 2^16) then
    -- don't save
else
    -- save
end

This is just one out of other potential uses of encoding data in JSON before saving it. I hope this helped. If you have any questions, just let me know.

Disclaimer:

This is based off of my old knowledge of DataStoreService. Although I don't think any of this has changed, I will update this answer accordingly if I find out any new information.

0
Ah, that makes sense. Thanks a lot! ihatecars100 502 — 6y
Ad

Answer this question