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

Just providing an update on this. Iā€™ve supplied a project to Xojo via a confidential issue that demonstrates the bug. As of two weeks ago it has had a status of reproducible. Hopefully it wonā€™t take too long for this to get looked at.

1 Like