JSON nil encoding

If I run this code under pre 2021 I do not get an error… but with 2021 the js.ToString causes a runtime error: “root{“one”}: String value does not have a specified encoding.”

dim s as string
s = “abcd”
s = DefineEncoding(s, nil)
dim js as new JSONItem
js.Value(“one”) = s

Now I am never going to intentionally set the string encoding to nil like I have here… but it would be pretty easy for it to happen if the string came from a socket or file.

Although the new behavior is probably technically correct it seems to me to be a regression… and potentially a nasty problem to find and fix.

Do you think it should be reported and changed to default to utf8 if no encoding is defined?

Please see older threads on the forum.

Basically when data comes in (file, socket, memoryblock), you have to make sure the right encoding is defined.
You may check first with Encoding property.

See also

Tip of the day: StringValueX for database access

I understand that… The issue is that the behavior has changed… It was OK to use an unencoded string in JSON and now it is not.

1 Like

The secondary issue is that the exception is raised far after our ability to handle the problem. This whole situation is a failure on Xojo’s part and nobody seems to want to admit that. We can’t simply open a project from 2020 in 2021 and have it work, as they say their goal often is.

I have a REST API with literally thousands of lines to redundantly change (we tell MySQL we want UTF8 data and apparently that doesn’t happen either), and have been considering the cost of updating the Xojo project vs. moving to a more reliable tool.

Well, I would of course like to have some mitigation in the code like we have in our plugins.

For the old behavior causes strange bugs when you put in code without right encoding.