2021 R1 JSON Encoding default error

Just upgraded IDE, now I get an error when compiling:

root{"my_string"}: String value does not have a specified encoding.

All my strings are declared w/out specifying encoding, which I assumed means UTF-8 by default (the standard for JSON)

Thanks for any tips!

1 Like

Where does this string appear? Is it from a line of code, a constant, something else?

When I receive a string from “the outside”, like a web endpoint via https, I need to set its encoding to utf8 in Xojo before processing it, because it gets in set as an “unknown” encoding (nil encoding, if I do recall correctly).

Hi Kem, it’s internal to my project, not brought in from another file or internet URL.

Hi Rick,

I never changed any of the default encodings on my strings, so my assumption is they’re all UTF8 (as JSON desires).

I set a breakpoint and examined my Var and it shows NIL for the encoding, so I went ahead and tried to change that with this line…

Var mystr as String = I400_END_POINT_SN.ConvertEncoding(Encodings.UTF8)

…however the encoding still shows at NIL for ‘mystr’.

Don’t convert it, define it.

Var mystr as String = I400_END_POINT_SN.DefineEncoding(Encodings.UTF8)

You can’t convert unknown to something.

1 Like

Your constants, string literals, and so forth are nearly always stored internally using UTF-8 encoding.
https://documentation.xojo.com/api/data_types/string.html

I rely on that and will not be pleased if Xojo took another “Oh let’s just remove KeyDown” approach to fixing some problem.

1 Like

I don’t believe he defined a string and did not set it to another content. The new content came as unknown encoding, besides he probably knowing that it was originally coded as UTF8, but is shown as unknown (been there before), so we need to define it.

Such name gives me a clue that something could be being read from some acquisition point.

You are correct - this var is local string that identifies this device to a Cloud service I’m writing. It’s written and stored on the device locally, and on startup it’s read back into central module for the system to use, including the ‘Cloud’ service (JSON) interface, which is what started malfunctioning after the IDE upgrade.

I’ve revised the binary stream read/write to now force encoding when handling these string Vars and now anything JSON seems to be fine - for whatever reason that process of reading/writing to the file system has a nuance that no longer assumes UTF8. Perhaps it was my inexperience that should have been forcing an encoding from the very beginning.

1 Like

I’ve made this and it works for me

Var dt As String = Request.PathComponents(4)
dt = dt.DefineEncoding(Encodings.UTF8)

And no more error

I have to fall on my sword and say that perhaps I should explicitly set the Encodings of my strings that are used with objects that care deeply about proper encoding.

1 Like

Read() on Binarystream takes an encoding parameter IIRC.
So no need for DefineEncoding.

And before you do DefineEncoding, check encoding property of the string to see whether it’s nil.