VBA-tools / VBA-JSON

JSON conversion and parsing for VBA
MIT License
1.74k stars 565 forks source link

Add support for System.Collections in JsonConverter.ConvertToJson #267

Open sshankar-rks opened 5 months ago

sshankar-rks commented 5 months ago

Supported collection types are Hashtable, SortedList, ArrayList, Stack, Queue. BitArray is not supported. Don't think it will receive much usage.

Why: I use this library for logging data structures in VBA. It's very easy to dump nested tree/list/dictionary structures using ConvertToJson rather than writing code to iterate over each collection. Since System.Collection collections are often used in VBA, I thought this was a good idea.

How: Use existing JSON serialization code as much as possible: Map Hashtable and SortedList to Dictionary Map ArrayList, Stack and Queue to Array.

Some bugs in .Net/COM/VBA had to be hacked around. See comment on Stack and Queue types in JsonConverter.bas

Deserialization: JSON doesn't support any of these types, so no code to change there.

sshankar-rks commented 5 months ago

Forgot to mention: Tests were also updated.

Nick-vanGemeren commented 2 months ago

Those aren't bugs, they're features :{ See Stack Overflow

I suggest applying a more general solution:

    Dim json_VarType As Integer:     json_VarType = VBA.VarType(JsonValue)
    Dim json_TypeName As String:     json_TypeName = VBA.TypeName(JsonValue)
...
    If json_VarType = vbString Then         ' (separate conditions for faster test)
        If json_TypeName <> "String" Then
            ' See https://stackoverflow.com/questions/55659056/why-is-vbas-vartype-function-saying-this-com-object-is-a-string-object-is-ins
            json_VarType = vbObject
        End If
    End If