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

MetaTable to JSON?

Asked by 9 years ago

Simply put I have a script similar to this-->

01local maintable = {}
02local addtable = {"Stuff","MoreStuff"}
03local metatable = {"MoreMoreStuff","ExtraMoreStuff"}
04setmetatable(addtable,metatable)
05table.insert(maintable,1,addtable)
06 
07 
08local DataStore = game:GetService("DataStoreService"):GetDataStore("FakeDataStore")
09local http = game:GetService("HttpService")
10local JSON = http:JSONEncode(maintable)
11DataStore:SetAsync("JsonSave",JSON)

The whole thing works fine and when I load the JSON again I have everything... except the meta table. I try-->

01--Of course not in the same script
02local DataStore = game:GetService("DataStoreService"):GetDataStore("FakeDataStore")
03local LoadedMainTable = DataStore:GetAsync("JsonSave")
04local Decodedtable = http:JSONDecode(LoadedMainTable)
05local addtable = Decodedtable[1]
06 if addtable then
07    local metatable = getmetatable(addtable)
08    if metatable then
09        print("GotMetaTable")
10    else
11        print("--MetaTable = nil")
12    end
13end

This script results in this in the output-->

1--MetaTable = nil

So my question is: Does JSONEncode/Decode get ride of all meta tables? If not then how would you successfully get a meta table from a decoded JSON format?

0
This looks like a job for, BlueTaslem! Validark 1580 — 9y
0
Updated with code -- apologies, I've been without internet BlueTaslem 18071 — 9y

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Metatables can't be meaningfully serialized (turned into text) because they have functions attached to them. (This is documented)

This is impossible because functions can be closures--they can refer to particular variables ruining in the program, which doesn't make sense to give to someone on another computer.

I would suggest making an extra property recording the "type" of the thing, and looking up the appropriate metatable and then applying it.


Here is an example of the kind of thing I am suggesting:

01function setupHouse(data)
02    setmetatable(data, houseMeta)
03end
04 
05function setupLine(data)
06    setmetatable(data, lineMeta)
07end
08 
09-- `data` is the result of DecodeJSON.
10-- It attaches the appropriate metatable
11function receiveData(data)
12    if data.type == "house" then
13        setupHouse(data)
14    elseif data.type == "line" then
15        setupLine(data)
16    elseif ... etc then
17 
18    end
19end

Then you have to remember that when you serialize to JSON, you have to include the proper .type property.

You can shorten all of the elseifs using a dictionary:

01local metas = {
02    house = houseMeta,
03    line = lineMeta,
04    dog = dogMeta,
05    -- etc
06}
07 
08function receiveData(data)
09    local meta = metas[data.type]
10    if meta then
11        setmetatable(data, meta)
12    end
13end
14 
15-- adds the `.type` property to `.data`, which is an object with (maybe) a metatable
View all 24 lines...
0
Could you please give me some example code to go off of. ClassicTheBlue 65 — 9y
Ad

Answer this question