TypeMismatchException with JSONItem.Count using Xojo 2023r4 and 2024r1

The following code runs with no issue in my app using Xojo 2023r3

Public Function ToStringArray(Extends json As JSONItem) As String()
  Dim result() As String
  
  If json Is Nil Then Return result
  If Not json.IsArray Then Return result
  
  Dim LastItemIndex As Integer = json.Count - 1  // Get exception here with 2023r4 and 2024r1
  
  For i As Integer = 0 To LastItemIndex
    result.Append json(i).StringValue
  Next
  
  Return result
  
End Function

However, when my app runs this code using Xojo 2023r4 or 2024r1, I get a TypeMismatchException on the line noted. This occurs when the method’s JSONItem is passed ["Test"]

Screenshot 2024-04-03 at 9.10.25 pm

If I continue and let my app’s Unhandled Exception window catch this I get the following stack info:

RuntimeRaiseException
_Z19RaiseExceptionClassRK13ClassDeclBase
VariantArrayAssignmentWithTypeCheck
JSONItem.LastRowIndex%i8%o<JSONItem>
JSONItem.Count%i8%o<JSONItem>
DBReportShared.$ToStringArray%A1s%o<JSONItem>
DBReportChart.DrawChk%%o<DBReportChart>

If I create a test app using 2023r4 or 2024r1 containing just this method and pass it ["Test"] there is no issue. Can anyone see why this is happening with my main app or suggest a way to isolate what’s causing the issue?

Testing on macOS 13.6. Thanks

The only way I could find to trigger a TypeMismatchException in that method was with value type mixing for something that can’t be converted to a string, and that happened on the Append line. I used this JSON data to get that result:

["Test", {"a":"b"}]

I’d double-check that the data you’re converting to a JSONItem is what you expect it to be in your main project since it works in a simpler test project. That’s the only cause I can think of.

Thanks @Anthony_G_Cyphers for looking into this. Yes it’s odd. The TypeMismatchException in my main app though is happing on this line:

Screenshot 2024-04-03 at 10.04.30 pm

And above I’ve provided a screen shot of the ‘json’ value ["Test"] from the main app, showing the binary data to make sure there isn’t some obscure hidden characters in there. Looks identical to what is in my test app that works.

There’s nothing intermittent about it, happens every time I try.

Unfortunately, your only recourse may be to provide the project where you see this happening to the Xojo team in a private feedback case.

1 Like

Check if a modern one works

Public Function ToStringArray(Extends json As JSONItem) As String()

  Var result() As String
  
  If json Is Nil Or (Not json.IsArray) Then Return result
  
  For i As Integer = 0 To json.LastRowIndex
    result.Append json.ValueAt(i).StringValue
  Next
  
  Return result
  
End Function

Thanks @Rick_Araujo. Tried this but getting the same exception:

Screenshot 2024-04-04 at 7.19.18 am

To share this project privately with Xojo I’d have to separate this section of the app out as it’s a large app that also calls custom plugins.

Break there and inspect “json” contents. I guess there’s something broken in that data.

By the way, I missed one new syntax change. Append() was deprecated in favor of Add()

Public Function ToStringArray(Extends json As JSONItem) As String()

  Var result() As String
  
  If json Is Nil Or (Not json.IsArray) Then Return result
  
  For i As Integer = 0 To json.LastRowIndex
    result.Add json.ValueAt(i).StringValue
  Next
  
  Return result
  
End Function

Also, the JSON exceptions sometimes carry useful messages, check the exception’s Message property.

1 Like

Thanks @Rick_Araujo. A breakpoint on the line shows the same contents of ‘json’:

Screenshot 2024-04-04 at 8.20.13 am

The same line of code that has the exception:

Screenshot 2024-04-04 at 8.20.53 am

Did you click on the exception to see its message property?

Thanks @Tim_Parnell. I’m getting no text in the exception’s Message property or any other property:

Here, your supposed data works…

Yes I get that if I place this ‘ToStringArray’ method in a new project it works with the data that is seen in my production app. The issue is it doesn’t work in my actual app when using Xojo 2023r4 or 2024r1.

I’ve been able to create a cut-down project that illustrates this and have posted a confidential issue on https://tracker.xojo.com.

Many thanks all for your help.

2 Likes

Would be great if Xojo, once fixing the bug, could remove the attachment and make the report and findings public, under your request/authorization.

Yes I’ll ask, and also report here. Thanks

2 Likes