Results 1 to 9 of 9

Thread: [RESOLVED] [VB6 RC6 JSON] Lesson learned on Immediate view and JSON parsing

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Resolved [RESOLVED] [VB6 RC6 JSON] Lesson learned on Immediate view and JSON parsing

    It would appear that the Immediate view window has an automatic break for line length.

    Case in point:

    Working with Olaf's RC6 JSON MenuAndToolbar Demo:

    https://www.vbforums.com/showthread....=1#post4681121

    I wanted to study the JSON that was produced from adding menu and sub-menu items so I dropped a Debug.Print to see this:

    Code:
    'this function returns a dynamically created Menu as a JSON-String (which could be stored in a DB, or elsewhere)
    Public Function ExampleMenuAsJSONString() As String
    Dim Root As cMenuItem
      Set Root = Cairo.CreateMenuItemRoot("MenuBar", "MenuBar") 'Key, Caption
     
      'Create the cMenuItem object and pass to specific Menu item (eg. File, Edit, Extr, Help)
      AddFileMenuEntriesTo Root.AddSubItem("File", "&File") 'Key, Caption, [IconKey], [Enabled = True], [Checked], [BeforeIndexZeroBased] returns cMenuItem
      AddEditMenuEntriesTo Root.AddSubItem("Edit", "&Edit")
      AddEditMenuEntriesTo Root.AddSubItem("Disabled", "&Disabled", , False)  'just to demonstrate a disabled entry
      AddExtrMenuEntriesTo Root.AddSubItem("Extras", "E&xtras")
      AddHelpMenuEntriesTo Root.AddSubItem("Help", "&Help")
    
    
      ExampleMenuAsJSONString = Root.ToJSONString
      
      Debug.Print ExampleMenuAsJSONString
      
    End Function
    From the Immediate window I did a Cntrl-A (select all) and Cntrl-C (copy):

    Code:
    {"Key":"MenuBar","Caption":"MenuBar","IconKey":"","Enabled":true,"Items":{"File":{"Key":"File","Caption":"&File","IconKey":"","Enabled":true,"Items":{"New":{"Key":"New","Caption":"&New","IconKey":"Document-New","Enabled":true},"Sep":{"Key":"Sep","Caption":"-","IconKey":"","Enabled":true},"Open":{"Key":"Open","Caption":"&Open...","IconKey":"Document-Open","Enabled":true},"Save":{"Key":"Save","Caption":"&Save|Shift+V","IconKey":"Document-Save","Enabled":true},"SaveAs":{"Key":"SaveAs","Caption":"S&ave as...","IconKey":"Document-Save-As","Enabled":true},"Sep2":{"Key":"Sep2","Caption":"-","IconKey":"","Enabled":true},"ExitApp":{"Key":"ExitApp","Caption":"E&xit Application","IconKey":"Application-Exit","Enabled":true}}},"Edit":{"Key":"Edit","Caption":"&Edit","IconKey":"","Enabled":true,"Items":{"Cut":{"Key":"Cut","Caption":"C&ut","IconKey":"Edit-Cut","Enabled":true},"Copy":{"Key":"Copy","Caption":"&Copy","IconKey":"Edit-Copy","Enabled":true},"Paste":{"Key":"Paste","Caption":"&Paste|Ctrl+V","IconKey":"Edit-Paste","
    Enabled":true},"Delete":{"Key":"Delete","Caption":"&Delete","IconKey":"Edit-Delete","Enabled":true},"Sep":{"Key":"Sep","Caption":"-","IconKey":"","Enabled":true},"Select all":{"Key":"Select all","Caption":"&Select all|F6","IconKey":"Edit-Select-All","Enabled":true}}},"Disabled":{"Key":"Disabled","Caption":"&Disabled","IconKey":"","Enabled":false,"Items":{"Cut":{"Key":"Cut","Caption":"C&ut","IconKey":"Edit-Cut","Enabled":true},"Copy":{"Key":"Copy","Caption":"&Copy","IconKey":"Edit-Copy","Enabled":true},"Paste":{"Key":"Paste","Caption":"&Paste|Ctrl+V","IconKey":"Edit-Paste","Enabled":true},"Delete":{"Key":"Delete","Caption":"&Delete","IconKey":"Edit-Delete","Enabled":true},"Sep":{"Key":"Sep","Caption":"-","IconKey":"","Enabled":true},"Select all":{"Key":"Select all","Caption":"&Select all|F6","IconKey":"Edit-Select-All","Enabled":true}}},"Extras":{"Key":"Extras","Caption":"E&xtras","IconKey":"","Enabled":true,"Items":{"Item1":{"Key":"Item1","Caption":"Menu-Item&1","IconKey":"MenuIconKey1","Enabled":true},"Item
    2":{"Key":"Item2","Caption":"Menu-Item&2","IconKey":"MenuIconKey3","Enabled":false},"Item3":{"Key":"Item3","Caption":"-","IconKey":"","Enabled":true},"Item4":{"Key":"Item4","Caption":"&Menu-Item2 disabled","IconKey":"MenuIconKey1","Enabled":true,"Checked":true},"Item5":{"Key":"Item5","Caption":"This pops up a &SubMenu","IconKey":"MenuIconKey2","Enabled":true,"Items":{"SubItem1":{"Key":"SubItem1","Caption":"Caption SubItem1","IconKey":"MenuIconKey1","Enabled":true},"SubItem2":{"Key":"SubItem2","Caption":"Caption SubItem2","IconKey":"MenuIconKey2","Enabled":true,"Items":{"SubSubItem1":{"Key":"SubSubItem1","Caption":"Caption SubSubItem1","IconKey":"MenuIconKey1","Enabled":true}}}}}}},"Help":{"Key":"Help","Caption":"&Help","IconKey":"","Enabled":true,"Items":{"About":{"Key":"About","Caption":"&About|Ctrl+Shift+A","IconKey":"About-Hint","Enabled":true},"Sep":{"Key":"Sep","Caption":"-","IconKey":"","Enabled":true},"Index":{"Key":"Index","Caption":"&Index...","IconKey":"Help-Contents","Enabled":true},"Find":{"Key":
    "Find","Caption":"&Find...","IconKey":"Edit-Find","Enabled":true}}}}}
    The online parsers (more than one) choked on a couple of places throwing errors.


    • Error: Invalid character found at position 2. [Code 180, Structure 261]
      Error: Invalid character found at position 6. [Code 180, Structure 514]


    Well, this was frustrating. I decided it would be good practice to help get familiar with the JSON format by doing the formatting BY HAND (and eye coordination...lol!)

    {"Key":"MenuBar",
    "Caption":"MenuBar",
    "IconKey":"",
    "Enabled":true,
    "Items":{
    "File":{
    "Key":"File",
    "Caption":"&File",
    "IconKey":"",
    "Enabled":true,
    "Items":{
    "New":{
    "Key":"New",
    "Caption":"&New",
    "IconKey":"Document-New",
    "Enabled":true
    },
    "Sep":{
    "Key":"Sep",
    "Caption":"-",
    "IconKey":"",
    "Enabled":true
    },
    "Open":{
    "Key":"Open",
    "Caption":"&Open...",
    "IconKey":"Document-Open",
    "Enabled":true},
    "Save":{
    "Key":"Save",
    "Caption":"&Save|Shift+V",
    "IconKey":"Document-Save","Enabled":true
    },
    "SaveAs":{
    "Key":"SaveAs",
    "Caption":"S&ave as...",
    "IconKey":"Document-Save-As",
    "Enabled":true
    },
    "Sep2":{
    "Key":"Sep2",
    "Caption":"-",
    "IconKey":"",
    "Enabled":true},
    "ExitApp":{
    "Key":"ExitApp",
    "Caption":"E&xit Application",
    "IconKey":"Application-Exit",
    "Enabled":true
    }
    }
    },
    "Edit":{
    "Key":"Edit",
    "Caption":"&Edit",
    "IconKey":"",
    "Enabled":true,
    "Items":{
    "Cut":{
    "Key":"Cut",
    "Caption":"C&ut",
    "IconKey":"Edit-Cut",
    "Enabled":true
    },
    "Copy":{
    "Key":"Copy",
    "Caption":"&Copy",
    "IconKey":"Edit-Copy",
    "Enabled":true
    },
    "Paste":{
    "Key":"Paste",
    "Caption":"&Paste|Ctrl+V",
    "IconKey":"Edit-Paste",
    "Enabled":true
    },
    "Delete":{
    "Key":"Delete",
    "Caption":"&Delete",
    "IconKey":"Edit-Delete",
    "Enabled":true
    },
    "Sep":{
    "Key":"Sep",
    "Caption":"-",
    "IconKey":"",
    "Enabled":true
    },
    "Select all":{
    "Key":"Select all",
    "Caption":"&Select all|F6",
    "IconKey":"Edit-Select-All",
    "Enabled":true
    }
    }
    },
    "Disabled":{
    "Key":"Disabled",
    "Caption":"&Disabled",
    "IconKey":"",
    "Enabled":false,
    "Items":{
    "Cut":{
    "Key":"Cut",
    "Caption":"C&ut",
    "IconKey":"Edit-Cut",
    "Enabled":true},
    "Copy":{
    "Key":"Copy",
    "Caption":"&Copy",
    "IconKey":"Edit-Copy",
    "Enabled":true
    },
    "Paste":{
    "Key":"Paste",
    "Caption":"&Paste|Ctrl+V",
    "IconKey":"Edit-Paste",
    "Enabled":true
    },
    "Delete":{
    "Key":"Delete",
    "Caption":"&Delete",
    "IconKey":"Edit-Delete",
    "Enabled":true
    },
    "Sep":{
    "Key":"Sep",
    "Caption":"-",
    "IconKey":"",
    "Enabled":true
    },
    "Select all":{
    "Key":"Select all",
    "Caption":"&Select all|F6",
    "IconKey":"Edit-Select-All",
    "Enabled":true
    }
    }
    },
    "Extras":{
    "Key":"Extras",
    "Caption":"E&xtras",
    "IconKey":"",
    "Enabled":true,
    "Items":{
    "Item1":{
    "Key":"Item1",
    "Caption":"Menu-Item&1",
    "IconKey":"MenuIconKey1",
    "Enabled":true
    },
    "Item2":{
    "Key":"Item2",
    "Caption":"Menu-Item&2",
    "IconKey":"MenuIconKey3",
    "Enabled":false
    },
    "Item3":{
    "Key":"Item3",
    "Caption":"-",
    "IconKey":"",
    "Enabled":true
    },
    "Item4":{
    "Key":"Item4",
    "Caption":"&Menu-Item2 disabled",
    "IconKey":"MenuIconKey1",
    "Enabled":true,"Checked":true
    },
    "Item5":{
    "Key":"Item5",
    "Caption":"This pops up a &SubMenu",
    "IconKey":"MenuIconKey2",
    "Enabled":true,
    "Items":{
    "SubItem1":{
    "Key":"SubItem1",
    "Caption":"Caption SubItem1",
    "IconKey":"MenuIconKey1",
    "Enabled":true
    },
    "SubItem2":{
    "Key":"SubItem2",
    "Caption":"Caption SubItem2",
    "IconKey":"MenuIconKey2",
    "Enabled":true,
    "Items":{
    "SubSubItem1":{
    "Key":"SubSubItem1",
    "Caption":"Caption SubSubItem1",
    "IconKey":"MenuIconKey1",
    "Enabled":true
    }
    }
    }
    }
    }
    }
    },
    "Help":{
    "Key":"Help",
    "Caption":"&Help",
    "IconKey":"",
    "Enabled":true,
    "Items":{
    "About":{
    "Key":"About",
    "Caption":"&About|Ctrl+Shift+A",
    "IconKey":"About-Hint",
    "Enabled":true
    },
    "Sep":{
    "Key":"Sep",
    "Caption":"-",
    "IconKey":"",
    "Enabled":true
    },
    "Index":{
    "Key":"Index",
    "Caption":"&Index...",
    "IconKey":"Help-Contents",
    "Enabled":true
    },
    "Find":{
    "Key":"Find",
    "Caption":"&Find...",
    "IconKey":"Edit-Find","Enabled":true
    }
    }
    }
    }
    }


    And while doing it I came across the two problem areas by virtue of the spacing was 'off' as I had a rhythm going with hitting the ENTER key, the TAB key and BACKSPACE key at the various sections.

    This made me look at the Immediate window again as to why I had these two 'BREAKS' in the JSON.

    The Immediate window displayed 4 lines total. So it broke up the serial string in 3 places. Why did it not have 3 errors then?

    Well, because 2 of the 3 breaks occurred in the middle of a string. (eg. "string" - between the ""). Had the break been outside the "", the JSON parser would not have choked on it.

    Lesson learned #1.

    If you send your JSON string out to the Immediate window and it extends to 2 or more lines, be aware that the break is not in the middle of a string between the ".." quotes if you plan to copy to a parser.

    Lesson learned #2.

    Parse a large JSON string manually into a nicely formatted view (be the parser!) and you'll soon be very familiar with how the sections are divided and structured. Was worth the effort. (I won't do it again though

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,384

    Re: [VB6 RC6 JSON] Lesson learned on Immediate view and JSON parsing

    I've never formatted a JSON string ... I let tools do that form me. Whether it's a plugin in VSC, or the built-in parser in IntelliJ ... OR the formatter in Notepad++ .... same with XML, unless it's a small chunk, I don't bother with it myself. I'll use a tool, or some kind of parser/formatter to do that for me.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: [VB6 RC6 JSON] Lesson learned on Immediate view and JSON parsing

    It's been a few days now and I'm still working out the JSON mindset using RC6 methods along with SQLite. It's the JSON part that is still a bit disjointed in my head and need to get it sorted out.

    This is the example code I've been studying the last few days.
    https://www.vbforums.com/showthread....=1#post5556037

    What I gather from this example code is that for every drawing 'tool' that I need for the app, each should have its own class. That seems logical enough.

    Therefore, in my own project, I will create a class for each tool that can be saved to DB (some tools are temporary and are never saved on the chart so not included in this list below):

    Actual names (note I'm not using cTextTools name or cTrendTools name as in the example code).

    cTexts
    cTrendLines
    cChannels
    cPitchforks
    cSquares
    cArrows

    (...there are more, but these are enough for this discussion.)


    In EACH of these tool classes, I need to DECLARE a Private mJSON As cCollection in the (General Declarations).

    In EACH of these tool classes, I need to INSTANTIATE a mJSON object in the class INIT.
    Set mJSON = New_c.JSONObject
    If there are any default values I wish to set, here in INIT is where I should set those properties.

    So whenever a 'tool' is 'created', it will have internally a JSON object that should allow me to take take the tool-objects properties and format them into JSON formatted strings so I can stash them away in a DB column. Am I on track so far?

    These 'tool' objects contain ALL the properties it will need to describe it. So for cTrendLines, there would be a X1, Y1, X2, Y2, LineColor and LineWidth, for example. These properties can be set with values either internally (via INIT) and/or using Get/Let (or Set if an object). These properties (values) are then added applied to the mJSON.Prop("Property_Name as Key").

    mJSON.Prop("Y1") = RHS
    BTW, anyone have an idea what RHS stands for? It's used as a parameter variable for multiple types so 'S' can't mean 'string'. Just curious.


    I note that each 'Tools-Class' also includes a JSON property. So I did a test to see what kind of string this would produce.

    Code:
    Private Sub Form_Load()
      'DrawChartToPicBox picChart, "Portfolio_1", "MSFT" 'Chart-rendering "by PortfolioName and StockName" (the creation-code of this Demo-Chart-Record sits in modMain.bas)
      
      Dim TestObj As cTrendLines
      Set TestObj = New cTrendLines
    
      TestObj.X1 = 49
      TestObj.Y1 = 78
      TestObj.X2 = 190
      TestObj.Y2 = 203
      TestObj.LineColor = vbYellow
      TestObj.LineWidth = 2
    
      Dim sRet As String
    
      sRet = TestObj.JSON
    
      Dim FileNum As Integer
      Dim FilePath As String
    
      FilePath = Environ("USERPROFILE") & "\Desktop\JSON.txt"
    
      FileNum = FreeFile
      Open FilePath For Output As FileNum
    
      Print #FileNum, sRet
      Close FileNum
      
      
    End Sub
    The resulting string:

    {"LineColor":65535,"LineWidth":2,"X1":49,"Y1":78,"X2":190,"Y2":203}
    Formatted:
    {
    "LineColor": 65535,
    "LineWidth": 2,
    "X1": 49,
    "Y1": 78,
    "X2": 190,
    "Y2": 203
    }
    So it seems that the JSONObject.Prop() adds a key-pair within a 'collection' that creates this COLLECTION OF KEY PAIRS enclosed in curly braces. This would be ONE RECORD in JSON. Right?

    So how are these COLLECTIONS all grouped together into a COLLECTION OF ORDERED RECORDS in the event that I have more than one 'TrendLine'?

    The answer appears to be within the cChart class and the use of the cCollection object's Add() method.

    Sub Add(Item, [Key], [Before], [After])

    From what I can determine, you need to state 'what collection' you wish to Add a Key-Pair to.

    cCollection Object (mJSON) .Prop("Tool Object Name").Add Item (cConstructor.JSONDecodeToCollection(JSON as string))

    **cConstructor.JSONDecodeToCollection(JSON as string) returns a cCollection.


    So let me see if I have this straight.

    1. For a given tool (obj) I create one or more COLLECTION KEY PAIRS using mJSON.Prop().

    2. To ADD additional COLLECTION KEY PAIRS for a 'single' tool (obj), I would use mJSON.Prop("Tool_obj").Add New_c.JSONDecodeToCollection(Tool_Obj.JSON)

    In this example app, #2 is a method of another class (cChart). This tells me that a TOOL_OBJ collection is being added to a CHART_OBJ collection.

    Is this how the Hierarchy is formed? (one group is sub to another group, etc.)

    Or could I simply use .Add within a ToolClass method?

    Let's see what happens if I do that.

    cTrendLines class
    Code:
    Public Sub TrendLinesAdd()
      mJSON.Prop("TrendLines").Add New_c.JSONDecodeToCollection(Me.JSON)
    End Sub
    Test code
    Code:
    Private Sub Form_Load()
      'DrawChartToPicBox picChart, "Portfolio_1", "MSFT" 'Chart-rendering "by PortfolioName and StockName" (the creation-code of this Demo-Chart-Record sits in modMain.bas)
      
      Dim TestObj As cTrendLines
      Set TestObj = New cTrendLines
      
      With TestObj
        .X1 = 49
        .Y1 = 78
        .X2 = 190
        .Y2 = 203
        .LineColor = vbYellow
        .LineWidth = 2
      End With
      
      Dim TestObj2 As cTrendLines
      Set TestObj2 = New cTrendLines
    
      With TestObj2
        .X1 = 23
        .Y1 = 44
        .X2 = 144
        .Y2 = 288
        .LineColor = vbGreen
        .LineWidth = 1
      End With
    
      TestObj.TrendLinesAdd
      
      
      Dim sRet As String
      
      sRet = TestObj.JSON
      
      Dim FileNum As Integer
      Dim FilePath As String
      
      FilePath = Environ("USERPROFILE") & "\Desktop\JSON.txt"
      
      FileNum = FreeFile
      Open FilePath For Output As FileNum
      
      Print #FileNum, sRet
      Close FileNum
      
      
    End Sub



    The first problem I noted is that in order to create another COLLECTION OF KEY-PAIRS that I would have to create another Tool-Obj. Both of these tool-objects contain the SAME method I added TrendLinesAdd().

    What would happen if I tried to use this method from just the first Tool-Obj?

    ERROR!! Run-time error '424': Object required

    Why? TrendLinesAdd() is being run from an object (TestObj) and it's JSON.Prop("TrendLines") is specifically indicated. Also, when I step through the code, "Me.JSON" shows it has a valid JSON string being passed to New_c.JSONDecodeToCollection().

    So what object is it looking for?

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,384

    Re: [VB6 RC6 JSON] Lesson learned on Immediate view and JSON parsing

    I can't address RC6 stuff, but I can chime in on the JSON portion... to denote a collection of stuff in JSON, use [] around it... like so:

    Code:
    {
      names:['tg','dill','eduardo'.'webbiz']
    }
    That creates an array of strings... but they don't need to be strings. they can also be objects:
    Code:
    {
      "users": [
        {"name": "tg", "memberSince": "May 2002", "postCount": 34320 },
        {"name": "webbiz", "memberSince": "Nov 2010", "postCount": 1022 }
        ]
    }
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: [VB6 RC6 JSON] Lesson learned on Immediate view and JSON parsing

    Quote Originally Posted by techgnome View Post
    I can't address RC6 stuff, but I can chime in on the JSON portion... to denote a collection of stuff in JSON, use [] around it... like so:

    Code:
    {
      names:['tg','dill','eduardo'.'webbiz']
    }
    That creates an array of strings... but they don't need to be strings. they can also be objects:
    Code:
    {
      "users": [
        {"name": "tg", "memberSince": "May 2002", "postCount": 34320 },
        {"name": "webbiz", "memberSince": "Nov 2010", "postCount": 1022 }
        ]
    }
    -tg
    @techgnome

    Thanks TG,

    The thing is, the RC6 stuff does all the bracketing and formatting. I'm trying to sort out the incremental procedure of using the JSON methods required to 'construct' the complete JSON structure string in order to store it in a SQLite DB field/record, AND how to pull it back out and deconstruct it for use in the apps drawing methods.

    Because the ChartJSON app example uses a Chart class in order to draw, I'm trying to learn how these JSON methods work together because in my project drawing is NOT done in a Chart class but in a UserControl.

    If I can just get it in my head what I need to do to apply this code logic to my own project then I'd be able to figure out where the necessary methods need to live. Do I put it in my UserControl? Do I need a separate class like the Chart class as a 'collector' of collections? I'm just not sure right now.

    As to 'how' to create a JSON string 'by hand' using braces and brackets, no problem. But that is something I'm not suppose to do using the RC6 methods.

    Appreciate the comment.

    PS: And I'd like to know why I got that error. What object is it looking for?

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: [VB6 RC6 JSON] Lesson learned on Immediate view and JSON parsing

    Well, (believe it or not) I actually figured out what the "object" is that it is crying about.

    Olaf calls this a "Mother-Object".

    It's not a VB class object it wants, it is a JSON object.

    More specifically, it wants a JSON 'array'!

    Unfortunately this error dialog needs work. Is this dialog coming from inside RC6?

    So I need to create a "TrendLines" array (thus here comes the "[brackets]").

    Set mJSON.Prop("TrendLines") = New_c.JSONArray


    And just like that, I can add these Key-Pair Collections into a JSON Array.

    This...

    Code:
      Dim TestObj As cTrendLines
      Set TestObj = New cTrendLines
      Dim ToolCase As cChart
      Set ToolCase = New cChart
      
      With TestObj
        .X1 = 49
        .Y1 = 78
        .X2 = 190
        .Y2 = 203
        .LineColor = vbYellow
        .LineWidth = 2
      End With
      
      Dim TestObj2 As cTrendLines
      Set TestObj2 = New cTrendLines
      
      With TestObj2
        .X1 = 23
        .Y1 = 44
        .X2 = 144
        .Y2 = 288
        .LineColor = vbGreen
        .LineWidth = 1
      End With
      
      ToolCase.TrendLinesAdd TestObj
      ToolCase.TrendLinesAdd TestObj2
    ...now produces this...

    {
    "TrendLines": [
    {
    "LineColor": 65535,
    "LineWidth": 2,
    "X1": 49,
    "Y1": 78,
    "X2": 190,
    "Y2": 203
    },
    {
    "LineColor": 65280,
    "LineWidth": 1,
    "X1": 23,
    "Y1": 44,
    "X2": 144,
    "Y2": 288
    }
    ]
    }

    PPPS: And just in case you are wondering, no, I do not intend to create multiple tool-objects of the same tool. Once a particular tool has been added to the JSON collection, if the user wishes to use that tool again they will just be reusing the same tool-object. No need for TestObj, TestObj2, TestObj3, etc. That was just for testing purposes.
    Last edited by webbiz; Sep 26th, 2023 at 02:21 PM.

  7. #7
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,069

    Re: [VB6 RC6 JSON] Lesson learned on Immediate view and JSON parsing

    Quote Originally Posted by webbiz View Post
    PS: And I'd like to know why I got that error. What object is it looking for?
    It is looking for a cCollection of type JSONArray, which you forgot to priorily provide in .Prop("TrendLines")
    (in the original Demo, this initialization with a JSONArray happens in cChart Class_Initialize).

    Perhaps it is easier for you, when you avoid any "Extra-Wrapper-Classes" for each JSON-Hierarchy-Level -
    and work directly with the cCollection-Type instead:

    Code:
    Private Sub Form_Load() 'bottom-up construction of a JSON-Hierarchy
      'bottom-level of the hierarchy
      Dim L1 As cCollection
      Set L1 = New_c.JSONObject
          L1.Prop("X") = 1
          L1.Prop("Y") = 11
      Debug.Print L1.SerializeToJSONString
          
      Dim L2 As cCollection
      Set L2 = New_c.JSONObject
          L2.Prop("X") = 2
          L2.Prop("Y") = 22
      Debug.Print L2.SerializeToJSONString
      
      'intermediate level of the hierarchy
      Dim Lines As cCollection
      Set Lines = New_c.JSONArray 'Plural in a JSON-Hierarchy implies a JSON-Array
          Lines.Add L1 'Array-ColTypes add their Values directly (here L1, as a Value of type JSONObject)
          Lines.Add L2 'dito here
      Debug.Print Lines.SerializeToJSONString
      
      'highest level of the hierarchy (the root-object)
      Dim ChartExtraData As cCollection
      Set ChartExtraData = New_c.JSONObject 'singular implies a JSON-Object and not a JSON-Array
          ChartExtraData.Prop("Title") = "Some Titel" 'assignment of a "Leaf-Value" (an Endpoint you cannot "drill-down-further")
          ChartExtraData.Prop("LastChanged") = Now    'assignment of another leaf
          ChartExtraData.Prop("TrendLines") = Lines   'assignment of a "Branch-Value" (a JSON-Array, one can use to "drill-down-further")
      Debug.Print ChartExtraData.SerializeToJSONString
      
      Dim ChartClone As cCollection
      Set ChartClone = New_c.JSONDecodeToCollection(ChartExtraData.SerializeToJSONString)
          ChartClone.Prop("Title") = "Changed Title"
          ChartClone("TrendLines")(0)("X") = 111111
          ChartClone("TrendLines")(1)("Y") = 222222
      Debug.Print ChartClone.SerializeToJSONString
    End Sub
    HTH

    P.S. "RHS" when used in a Property Let Method, usually means "Right-Hand-Side"
    ... of an assignment like: SomeObject.SomeProp = "I'm the Right-Hand-Side"

    Olaf
    Last edited by Schmidt; Sep 26th, 2023 at 02:28 PM.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: [VB6 RC6 JSON] Lesson learned on Immediate view and JSON parsing

    Quote Originally Posted by Schmidt View Post
    It is looking for a cCollection of type JSONArray, which you forgot to priorily provide in .Prop("TrendLines")
    (in the original Demo, this initialization with a JSONArray happens in cChart Class_Initialize).

    Perhaps it is easier for you, when you avoid any "Extra-Wrapper-Classes" for each JSON-Hierarchy-Level -
    and work directly with the cCollection-Type instead:

    Code:
    Private Sub Form_Load() 'bottom-up construction of a JSON-Hierarchy
      'bottom-level of the hierarchy
      Dim L1 As cCollection
      Set L1 = New_c.JSONObject
          L1.Prop("X") = 1
          L1.Prop("Y") = 11
      Debug.Print L1.SerializeToJSONString
          
      Dim L2 As cCollection
      Set L2 = New_c.JSONObject
          L2.Prop("X") = 2
          L2.Prop("Y") = 22
      Debug.Print L2.SerializeToJSONString
      
      'intermediate level of the hierarchy
      Dim Lines As cCollection
      Set Lines = New_c.JSONArray 'Plural in a JSON-Hierarchy implies a JSON-Array
          Lines.Add L1 'Array-ColTypes add their Values directly (here L1, as a Value of type JSONObject)
          Lines.Add L2 'dito here
      Debug.Print Lines.SerializeToJSONString
      
      'highest level of the hierarchy (the root-object)
      Dim ChartExtraData As cCollection
      Set ChartExtraData = New_c.JSONObject 'singular implies a JSON-Object and not a JSON-Array
          ChartExtraData.Prop("Title") = "Some Titel" 'assignment of a "Leaf-Value" (an Endpoint you cannot "drill-down-further")
          ChartExtraData.Prop("LastChanged") = Now    'assignment of another leaf
          ChartExtraData.Prop("TrendLines") = Lines   'assignment of a "Branch-Value" (a JSON-Array, one can use to "drill-down-further")
      Debug.Print ChartExtraData.SerializeToJSONString
      
      Dim ChartClone As cCollection
      Set ChartClone = New_c.JSONDecodeToCollection(ChartExtraData.SerializeToJSONString)
          ChartClone.Prop("Title") = "Changed Title"
          ChartClone("TrendLines")(0)("X") = 111111
          ChartClone("TrendLines")(1)("Y") = 222222
      Debug.Print ChartClone.SerializeToJSONString
    End Sub
    HTH

    P.S. "RHS" when used in a Property Let Method, usually means "Right-Hand-Side"
    ... of an assignment like: SomeObject.SomeProp = "I'm the Right-Hand-Side"

    Olaf
    Thank you. I may have been typing at the same time you posted. Did you notice that I did figure that out?

    Thanks for clarifying some things including the RHS. I was using it just because it was in your example, but every time I typed it I kept thinking "what does it abbreviate?". Now I can sleep without that nagging thought.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: [VB6 RC6 JSON] Lesson learned on Immediate view and JSON parsing

    Okay, so I believe I'm ready to SUMMARIZE the whole thing into one neat little write-up here. This may prove useful later when I might need to refresh my memory, or if someone happens to be wanting to create RC6 JSON structures from a beginners position.

    =========================

    For every KEY-PAIR Collection within the total JSON structure, including those to be placed inside of JSON arrays, the following applies:

    Every 'tool' in my project has a CLASS created for it. This will allow the creation of a TOOL OBJECT which contain the tool's PROPERTIES and the methods to format these into JSON collections.

    1. Create a CLASS for each tool. (cTrendLines, cTexts, cChannels, etc.)

    A. (DECLARATIONS)
    Private mJSON As cCollection
    B. (INIT)
    Set mJSON = New_c.JSONObject
    *Include any default property values here

    C. Create a GET/LET JSON property to allow access to the collection.
    Get JSON() As String
    JSON = mJSON.SerializeToString
    Let JSON(ByVal RHS As String)
    Set mJSON = New_c.JSONDecodeToCollection(RHS)

    2. Create the Properties for the Tool (this also in the Tool class)

    A. Get PropertyName() as Double
    PropertyName = mJSON.Prop("PropertyName")
    B. Let PropertyName(ByVal RHS as Double)
    mJSON.Prop("PropertyName") = RHS


    3. Create a CLASS (cToolChest) that will be used to collect all these JSON collections and group them into their respective section names (JSON objects and arrays).
    "ToolSectionTitle" is the name given for a JSON object or array.

    A. (DECLARATIONS)
    Private mJSON As cCollection
    (INIT)
    Set mJSON = New_c.JSONObject
    Set mJSON.Prop("ToolSectionTitle") = New_c.JSONArray


    4. Create method in this CLASS to add the individual tool-JSON-objects to the main collection.

    A. ToolNameAdd(ToolName As cToolClass)
    mJSON.Prop("ToolSectionTitle").Add New_c.JSONDecodeToCollection(ToolName.JSON)


    5. Create methods in this CLASS to retrieve information from the JSON collection

    A. How many of a specific type of tool is there in the collection? (GET)
    ToolNameCount()
    ToolNameCount = mJSON.Prop("ToolName").Count

    B. Information from a specific tool in the collection. (GET)
    ToolItem(ByVal IndexZeroBased As Long) as cToolClass
    Set ToolItem = New cToolClass
    ToolItem.JSON = mJSON.Prop("ToolName").ItemByIndex(IndexZeroBased).SerializeToJSONString




    I believe that should do it. To get the complete JSON collection in order to store in the SQLite DB, this can be done by accessing ToolChest.JSON if I'm not mistaken.


    EDIT: I knew I forgot something. In ToolChest class, be sure to add a GET/LET for JSON as well.

    Code:
    
    Public Property Get JSON() As String
      JSON = mJSON.SerializeToJSONString
    End Property
    
    
    Public Property Let JSON(ByVal RHS As String)
      Set mJSON = New_c.JSONDecodeToCollection(RHS)
    End Property
    Last edited by webbiz; Sep 26th, 2023 at 06:29 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width