dcsimg
Results 1 to 40 of 125

Thread: VB6 - JsonBag, Another JSON Parser/Generator

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,712

    VB6 - JsonBag, Another JSON Parser/Generator

    With SOAP basically a dead duck and XML itself fading in importance, JSON is becoming more and more important as a serialization format today.

    I've seen a number of VB6 JSON implementations around, including a couple posted right here in the CodeBank. Sadly none of them are very good, with little quirks and latent bugs (like improperly handling numeric data). Most of these ignore and flout the JSON standards at JSON too!

    In any case I have my own implementation designed to conform as closely as possible to the standard, and now at version 1.6 it seems mature enough to share. I rewrote my notes into six pages of documentation which should make it a bit easier for others to use.


    Bug reports are welcome. Though it seems to be working fine it is hard to prove code to be correct and there are probably edge cases I've never encountered.

    Performance seems more than adequate for its purpose, which is mainly access to cloud computing services. Local config files are probably best still handled as plain old INI format files, though one could use JSON for that as well I suppose.


    There is just one Class involved: JsonBag.cls, and as the documentation suggests it should be easy enough to import into a VBA host (Excel, etc.) as long as you tweak the API calls for 64-bit hosts when required.


    The attachment includes this Class along with the documentation in RTF format, packaged up as a testbed Project JsonBagTest.vbp:

    Name:  sshot.png
Views: 23158
Size:  20.5 KB

    As you can see JsonBag supports a Whitespace property to format JSON for readability. By default compact JSON is generated.


    Accessing the "document model" and creating JSON documents in code is easy enough. This is illustrated by a fragment from the test Project:

    Code:
    Private Sub cmdGenSerialize_Click()
        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.Text = .JSON
        End With
    End Sub

    Newst version here.
    Attached Files Attached Files
    Last edited by dilettante; Jan 21st, 2017 at 10:56 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width