webxcel icon indicating copy to clipboard operation
webxcel copied to clipboard

Suggestion: JSON Implementation

Open sancarn opened this issue 4 years ago • 0 comments

Hello,

I noticed you made a custom implementation of JSON. Just wanted to let you know that there is an implementation of JSON here:

https://github.com/sancarn/VBA-STD-Library/blob/master/src/WIP/stdJSON/JSONBag%20Licensed/JsonBag%202.0/JsonBag.cls with license: https://github.com/sancarn/VBA-STD-Library/blob/master/src/WIP/stdJSON/JSONBag%20Licensed/license.txt (Apache2)

Which has several advantages. Mainly it's kept in a single file, but also it's got fairly nice usage syntax:

Example:

json = "{""web-app"":{""servlet"":[{""init-param"":{""templateProcessorClass"":""MyClass""}}]}"

Debug.Print myBag![web-app]![servlet](1)![init-param]![templateProcessorClass] 'Prints MyClass

And construction of JSON is pretty nice also:

With JB
    .Clear
    .IsArray = False 'Actually the default after Clear.

    ![First] = 1
    ![Second] = Null
    With .AddNewArray("Third")
        .Item = "These"
        .Item = "Add"
        .Item = "One"
        .Item = "After"
        .Item = "The"
        .Item = "Next"
        .Item(1) = "*These*" 'Should overwrite 1st Item, without moving it.

        'Add a JSON "object" to this "array" (thus no name supplied):
        With .AddNewObject()
            .Item("A") = True
            !B = False
            !C = 3.14E+16
        End With
    End With
    With .AddNewObject("Fourth")
        .Item("Force Case") = 1 'Use quoted String form to force case of names.
        .Item("force Case") = 2
        .Item("force case") = 3

        'This syntax can be risky with case-sensitive JSON since the text is
        'treated like any other VB identifier, i.e. if such a symbol ("Force"
        'or "Case" here) is already defined in the language (VB) or in your
        'code the casing of that symbol will be enforced by the IDE:

        ![Force Case] = 666 'Should overwrite matching-case named item, which
                            'also moves it to the end.
        'Safer:
        .Item("Force Case") = 666
    End With
    'Can also use implied (default) property:
    JB("Fifth") = Null

    txtSerialized= .JSON
End With

I didn't make the library but it looks pretty damn good in general. Haven't done extensive testing on this though

sancarn avatar May 30 '20 11:05 sancarn