michaelneu / webxcel

🤔 A REST backend built with plain VBA Microsoft Excel macros. Yes. Macros.
https://dev.to/michaelneu/to-vba-and-beyond---building-a-restful-backend-using-plain-microsoft-excel-macros-76n
MIT License
438 stars 70 forks source link

Suggestion: JSON Implementation #27

Open sancarn opened 4 years ago

sancarn commented 4 years ago

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