Special values dropped in HTTPService:JSONEncode

During creating a saving-loading system in which JSON is involved, I ran into an error, which was when transmitting special values like Vector3, CFrame, and Color3s into the encoder, I noticed they’d come out as nil after being decoded. How can I fix this issue?

I ran this in the command bar to test my theory, this isn’t the actual script, rather it was a rushed snippet of what I’m concerned with.

local http = game:GetService("HttpService") 
local v = {['m']=Color3.new(0,0,0)}
local nv = http:JSONEncode(v)
print(tostring(http:JSONDecode(nv)['m']))

Color3 is a lua object, so you’d have to convert it to something that can be parsed into JSON.

local c = Color3.new(0, 0, 0)
local v = {['m'] = {c.R, c.G, c.B}}

The JSON string should end up looking something like:

{"m": [0, 0, 0]}

Then you would have to construct a new Color3 object when you convert the string back into a lua table.

2 Likes

You need to serialise it e.g. {0, 0, 0}. However, Roblox utilises r,g,b floats of 0 - 1 instead of 0 - 255 ints, so if you need to save on bytes, use something like this Colours to hex/ints to save bytes when posting to DataStore