Xojo.Core.InvalidArgumentException in Text = GenerateJSON( .Dictionary )

Any clues on the root cause of the following exception ?

dim d as Xojo.Data.Dictionary
... populate dictionary ...
dim t as Text = Xojo.Data.GenerateJSON( d )   // <-- throws the following ...

Exception: Xojo.Core.InvalidArgumentException
Message:   root{?}{?}{?}{?}: String value is not convertible to UTF-8.

I fear the fix will be obviously simple !

Did you add an empty String to the dictionary, as opposed to empty Text? If so, you’ve exposed a bug and the solution is to make sure it’s added as Text, not String.

Otherwise, make sure any Strings you’ve added have valid encodings.

I’m still getting my head around Strings vs Text. Feels like Strings are going out of fashion however, Text is NOT documented anywhere in the Xojo Help system.

How would I enforce all Strings to be UTF-8 including empty strings ?

Start here.

Encoding tells the system how to interpret the bytes that are contained in the String. But an empty string has no bytes, so there is nothing to interpret, so there is no encoding.

The simplest way to handle this is to add all of your strings as Text with the .ToText method. Instead of d.Value(key) = someString, use d.Value(key) = someString.ToText.

[quote=195370:@Kem Tekinay]Start here.

Encoding tells the system how to interpret the bytes that are contained in the String. But an empty string has no bytes, so there is nothing to interpret, so there is no encoding.
[/quote]

Actually a Xojo empty string is encodings.ASCII. As soon as one character is in there, it becomes UTF-8.

Excellent … Problem solved.

Not a single String to be found anywhere now …

Without knowing for sure, I think that’s a convenient fiction to differentiate from “nil encoded”. If it’s empty, what is there to interpret?

I just checked the encoding with the example at TextEncoding
http://documentation.xojo.com/index.php/TextEncoding

Dim t as TextEncoding t=Encoding("q") If t <> Nil then Label1.text="Base="+Str(t.base) Label2.text="Format="+Str(t.format) Label3.text="Variant="+Str(t.variant) end if

With “” it gives base = 1536, and when anything else, “256”.

All that remaining purely academic, since getting the encoding of an empty string is futile.

I ran across a new one today that could cause some people issues. Using an old framework Date was causing an Xojo.Core.InvalidArgumentException exception. Converted them to SQLdateTime and they worked just fine. Very weird.