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)

1local dss = game:GetService("DataStoreService")
2local http = game:GetService("HttpService")
3local store = dss:GetDataStore("whatever")
4local data = http:JSONDecode(store)
5 
6--load data and do whatever
7 
8local encodedData = http:JSONEncode(newData)
9store:SetAsync(tableOfData, encodedData)

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

or the common way:

1local dss = game:GetService("DataStoreService")
2local store = dss:GetDataStore("whatever")
3local data = store:GetAsync(tableOfData)
4 
5--load data
6 
7store: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...

1DataStore:SetAsync(key, table)

and

1DataStore: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...

01local HttpService = game:GetService("HttpService")
02 
03local table = {}
04local JSON_table = HttpService:JSONEncode(table)
05 
06-- check size limit of the data being saved
07if (#JSON_table >= 2^16) then
08    -- don't save
09else
10    -- save
11end

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