Plugin:SetSetting does not escape strings

Plugin:SetSetting() does not escape strings when writing them to the settings file which is unexpected. While this would be workable as we can just escape strings ourselves, GetSetting() does correctly handle escaped strings in the expected way. The reason this is such a problem is if you write an escaped string, for example \"hello\", the next time you set any setting it will reparse the whole file unescaping that string and then rewrite it unescaped as "hello" which is now invalid json. The only solution I am left with is to base64 encode any data which may contain an invalid json character which needs to be escaped.

The repro I am using is

plugin:SetSetting("some_setting", {["\\\"hello\\\""] = true })
print(plugin:GetSetting("some_setting")) -- Works but the backslashes are missing (correctly unescaped)
plugin:SetSetting("another_setting", 1) -- Rewrites some_setting without escaping
print(plugin:GetSetting("some_setting")) -- Errors and returns nil

Expected behavior

I expect SetSetting and GetSetting to handle escaping strings for us. Or at least both not escape strings and give us stable writes which can’t affect other settings.

4 Likes