Skip to content

Make SerializeElement work with range().value output multidimensional array #16

@bmclean-nexamp

Description

@bmclean-nexamp

The current code only handles single dimensional, 0 based arrays. The code below that I'm too lazy to PR ( and might want some cleanup ) makes it correctly encode json from a structure with one of the two dimensional 1 based arrays that range returns. Test cases would be nice, etc, but I'm on deadline and can't be bothered :(

        Case Is >= vbArray
            If UBound(vElement) = -1 Then
                .item(.Count) = "[]"
            Else
                .item(.Count) = "[" & vbCrLf
                dimensions = Bounds(vElement).Count
                For i = LBound(vElement) To UBound(vElement)
                    If dimensions > 1 Then ' Note that this works only for 2 dimensions, which is what range returns
                        .item(.Count) = sIndent & "["
                       For j = LBound(vElement, 2) To UBound(vElement, 2)
                            .item(.Count) = sIndent & vbTab
                            SerializeElement vElement(i, j), sIndent & vbTab
                            If Not (j = UBound(vElement, 2)) Then .item(.Count) = ","
                            .item(.Count) = vbCrLf
                        Next
                        .item(.Count) = sIndent & "]"
                        If Not (i = UBound(vElement)) Then .item(.Count) = ","
                    Else
                        .item(.Count) = sIndent & vbTab
                        SerializeElement vElement(i), sIndent & vbTab
                        If Not (i = UBound(vElement)) Then .item(.Count) = ","
                        .item(.Count) = vbCrLf
                    End If
                Next
                .item(.Count) = sIndent & "]"
            End If

and from a StackOverflow answer:

Function Bounds(A As Variant) As Collection
Dim c As New Collection
Dim v As Variant, i As Long

  On Error GoTo exit_function
  i = 1
  Do While True
      v = Array(LBound(A, i), UBound(A, i))
      c.Add v
      i = i + 1
  Loop

exit_function:
Set Bounds = c
End Function

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions