Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sshankar-rks
Copy link

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.

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.
@sshankar-rks
Copy link
Author

Forgot to mention: Tests were also updated.

@Nick-vanGemeren
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants