Exception on


I’m trying to load a valid Json file and then to convert into a JsonItem (Mac, Desktop). Initially I tried to load my own created Json file (UTF8 saved) that throws an exception on converting to JsonItem. So I headed to https://documentation.xojo.com/topics/file_managment/reading_and_writing_data_in_json_format.html to use the sample there:

[{"players":[{"Name":"Bob","position":"1B"},{"Name":"Tom","position":"2B"}],"Team":"Seagulls"}, {"players":[{"Name":"Bill","position":"1B"},{"Name":"Tim","position":"2B"}],"Team":"Pigeons"}, {"players":[{"Name":"Betty","position":"1B"},{"Name":"Tina","position":"2B"}],"Team":"Crows"}]

I load/read the file this way:

[code]Var f As FolderItem = SpecialFolder.Resource(“testfile”)
Var OpCodeList As Dictionary

If f <> Nil Then
If f.Exists Then
// Be aware that TextInputStream.Open could raise an exception
Var t As TextInputStream
t = TextInputStream.Open(f)
t.Encoding = Encodings.UTF8

  // This is working (show json file in textarea)
  OutputField.Value = t.ReadAll

// This throws the exception
OpCodeList = ParseJSON(t.ReadAll)

Catch e As IOException
  MessageBox("Error accessing file.")
End Try

MessageBox(“File could not be found”)
End If

End If[/code]

Thrown Exception:
ErrorNumber: 0
Message: ■■■■ error: premature EOF ck?P>? Offset: 0 Reason: ■■■■ error: premature EOF ck???P>?

( !@#$% stands for a.r.s.e. (w/o dots) and should probably mean “parse”) :o)

I can load my original Json file into any Json reader on my Mac and they don’t show any invalid structure. Initially I had the file content as variable and it was accepted inside Xojo ( OpCodeList = ParseJSON(JsonString) was working ). So I assume something special/wrong with the file.

I also tried different file types (UTF8, RTF, TXT) but all with the same error message.

Do I have to consider something special/additional in order to read and convert a JSON file?

Thanks for making me laugh. You probably need to parse your json the hard way. You need to iterate through it and read the result into a dictionary. Here is a simple example:

DataAsJson = new JSONItem(DataAsJsonString)
catch err as JSONException
'can’t parse
end try
#pragma BreakOnExceptions true
if DataAsJson = nil then Return

for currentJson as integer = 0 to DataAsJson.Count - 1

dim key as String = DataAsJson.NameAt(currentJson)
'do simple values
if key = “ServerName” then
mServerName = DataAsJson.Value(key)
'and so on
end if
Don’t forget to make a Feedback case for the parse problem.

You’re welcome. :o) The Forum does cryptify “bad” words.

Thanks for your answer! I always write any working solution down as others may run into the same problem.

Your code probably will work on my problem. In between I could find the error causing the exception: Xojo seems to be a bit picky with Carriage Returns if it comes to JSONify files. It wants to have the whole JSON file into one line without any RETURNs in between (whitespaces are no problem).
Of course I don’t want to create manually a JSON file in one line what makes it hard to read/enhance. Therefore I made Xojo convert the JSON file as it wanted:

// Replace RETURN by "" (10: Mac/Linux, 13: Windows) SingleLine = t.ReadAll.ReplaceAll(Chr(10), " ").ReplaceAll(Chr(13), " ") OpCodeList = ParseJSON(SingleLine)

This works now like a charm.