dcsimg
Results 1 to 6 of 6

Thread: JSON with values array and cCollection

  1. #1

    Thread Starter
    New Member oldVBDev's Avatar
    Join Date
    Aug 2018
    Posts
    12

    JSON with values array and cCollection

    Hi for all.
    I have this class:
    Code:
    Option Explicit
    Public Properties As vbRichClient5.cCollection
    
    Public Property Get FirstProp() As String
        FirstProp = Properties("FirstProp")
    End Property
    
    Public Property Let FirstProp(ByVal strValue As String)
        Properties.Prop("FirstProp") = strValue
    End Property
    
    Public Property Get SecondProp() As String
        SecondProp = Properties("SecondProp")
    End Property
    
    Public Property Let SecondProp(ByVal strValue As String)
        Properties.Prop("SecondProp") = strValue
    End Property
    
    Private Sub Class_Initialize()
        Set Properties = New_c.JSONObject
    End Sub
    With it I must generate a JSON.
    Can I to have how property with a list of value?
    And if I use the method cCoo.Properties.SerializeToJSONString , I will have an array in the JSON?

    Thanks

  2. #2

    Thread Starter
    New Member oldVBDev's Avatar
    Join Date
    Aug 2018
    Posts
    12

    Re: JSON with values array and cCollection

    Hi for all.
    I have solved this way:
    Code:
    Option Explicit
    Public Properties As vbRichClient5.cCollection
    
    Public Property Get FirstProp() As String
        FirstProp = Properties("FirstProp")
    End Property
    
    Public Property Let FirstProp(ByVal strValue As String)
        Properties.Prop("FirstProp") = strValue
    End Property
    
    Public Property Get SecondProp() As String
        SecondProp = Properties("SecondProp")
    End Property
    
    Public Property Let SecondProp(ByVal strValue As String)
        Properties.Prop("SecondProp") = strValue
    End Property
    
    Public Property Get ArrayValue() As String
        SecondProp = Properties("ArrayValue")
    End Property
    
    Public Property Set ArrayValue(objValue As cCollection)
        Set Properties.Prop("ArrayValue") = objValue
    End Property
    
    Private Sub Class_Initialize()
        Set Properties = New_c.JSONObject
    End Sub
    
    Private Sub Form_Load()
        Dim lc_Class  As New cClass
        Dim lc_Coll As New cCollection
        Dim str     As String
    
        With lc_Coll
            .Add "one"
            .Add "two"
        End With
    
        With lc_Class
            Set .ArrayValue = lc_Coll
            .FirstProp = "1"
            .SecondProp = "2"
            str = .Properties.SerializeToJSONString
        End With
    
        Debug.Print str
    End Sub
    and I get:
    {"ArrayValue":["one","two"],"FirstProp":"1","SecondProp":"2"}
    The result is right, but is the method correct?
    Schimdt, jbpro and the others what do they think?
    Thanks

  3. #3
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,093

    Re: JSON with values array and cCollection

    Looks OK, but a couple of thoughts:

    • The Property Get ArrayValue should be returning a cCollection instead of a String and should not be setting "SecondProp"...so:


    Code:
    Public Property Get ArrayValue() As cCollection
        Set ArrayValue = Properties("ArrayValue")
    End Property
    • Maybe just my personal taste, but I don't like exposing the module level "Properties" collection publicly because misbehaving code can modify it directly as opposed to using the public properties. I'd rather have a separate GetJsonString or similarly named method like:


    Code:
    Private Properties As cCollection
    
    Public Function GetJsonString() As String
       GetJsonString = Properties.SerializeToJsonString
    End Function

  4. #4

    Thread Starter
    New Member oldVBDev's Avatar
    Join Date
    Aug 2018
    Posts
    12

    Re: JSON with values array and cCollection

    Quote Originally Posted by jpbro View Post
    Looks OK, but a couple of thoughts:

    • The Property Get ArrayValue should be returning a cCollection instead of a String and should not be setting "SecondProp"...so:


    Code:
    Public Property Get ArrayValue() As cCollection
        Set ArrayValue = Properties("ArrayValue")
    End Property
    • Maybe just my personal taste, but I don't like exposing the module level "Properties" collection publicly because misbehaving code can modify it directly as opposed to using the public properties. I'd rather have a separate GetJsonString or similarly named method like:


    Code:
    Private Properties As cCollection
    
    Public Function GetJsonString() As String
       GetJsonString = Properties.SerializeToJsonString
    End Function
    The first is a mistake of copying and pasting. Sorry!
    The second seems to me a good advice, but when you say "misbehaving code" do you mean a programming error?
    Thank.

  5. #5

    Thread Starter
    New Member oldVBDev's Avatar
    Join Date
    Aug 2018
    Posts
    12

    Re: JSON with values array and cCollection

    Hi for all.
    I changed the class like that:
    Code:
    Option Explicit
    Private Properties As vbRichClient5.cCollection
    Private myArray    As New cCollection
    
    Public Sub ArrayClear()
        myArray.RemoveAll
    End Sub
    
    Public Sub ArrayValueAdd(ByVal Item As Variant, Optional ByVal key As Variant)
        myArray.Add Item, key
        Set Properties.Prop("ArrayValue") = myArray
    End Sub
    
    Public Sub ArrayValueRemove(ByVal key As Variant)
        myArray.Remove key
        Set Properties.Prop("ArrayValue") = myArray
    End Sub
    
    Public Function GetJsonString() As String
        GetJsonString = Properties.SerializeToJSONString
    End Function
    
    Private Sub Class_Initialize()
        Set Properties = New_c.JSONObject
    End Sub
    
    Public Property Get FirstProp() As String
        FirstProp = Properties("FirstProp")
    End Property
    
    Public Property Let FirstProp(ByVal strValue As String)
        Properties.Prop("FirstProp") = strValue
    End Property
    
    Public Property Get SecondProp() As String
        SecondProp = Properties("SecondProp")
    End Property
    
    Public Property Let SecondProp(ByVal strValue As String)
        Properties.Prop("SecondProp") = strValue
    End Property
    and then
    Code:
    Private Sub Form_Load()
        Dim lc_Class As New cClass
        Dim str      As String
    
        With lc_Class
            .FirstProp = "1"
            .SecondProp = "2"
            .ArrayClear
            .ArrayValueAdd "one"
            .ArrayValueAdd "two"
            str = lc_Class.GetJsonString
        End With
    
        Debug.Print str
    End Sub
    I get the right output:
    Code:
    {"FirstProp":"1","SecondProp":"2","ArrayValue":["one","two"]}
    What do you think?

    Thanks

  6. #6
    PowerPoster
    Join Date
    Jun 2013
    Posts
    3,780

    Re: JSON with values array and cCollection

    Quote Originally Posted by oldVBDev View Post
    What do you think?
    I'd take care, that the New_c... constructor is used always (instead of VBs New Keyword),
    to make it easier with switching to regfree-mode (which would only require a drop-in *.bas module later)...

    Second thing is, although your code works (because the cCollection tolerates this) -
    it'd be better to use construct "JSON-flagged" cCollection-instances (New_c.JSONObject and New_c.JSONArray) -
    instead of a "plain cCollection-instance" (e.g. over New_c.Collection)...
    There is Properties in a cCollection, which will later identify such a "flagged instance"
    (.IsJSONArray ... or ... .IsJSONObject) which some "JSON-consuming Objects" might try
    to use, to easier identify "JSON-NodeTypes" - e.g. the cwTree-Widget does that, where you
    can "hang in" a (cCollection-based) JSON-Object, for visualizing in one line of code.

    You also might include some "basic-Properties" into such serializable Classes
    (like e.g. a Public JSON-Prop for reading of / initializing from JSON-String-content)

    Also, JSON-serializing is able to differentiate between these types (in VB, the appropriate Variant-Types are returned):
    - Boolean true/false (mapping to the VB6 vbBoolean-Variant-Type)
    - Numeric-Values (mapping to either Long/Currency/Decimal for Integers - or to a VB-Double for rational numbers)
    - String-Values (mapping to VBs String-Type)
    - null (mapping to the VB6-vbEmpty-VariantType)
    - Object - a Key/Value List (mapping to a cCollection, which has .IsJSONObject = True)
    - Array - a plain Value List without Keys (mapping to a cCollection, which has .IsJSONArray = True)

    So there's no need - to "turn everything into a JSON-String-Type" (Property-wrapping-wise - as shown in "SecondProp" below).

    Code:
    Option Explicit
    
    Private mProps As cCollection
     
    Private Sub Class_Initialize()
        Set mProps = New_c.JSONObject 'Inits of some important Props below
            mProps.Prop("FirstProp") = "Init-String"
            mProps.Prop("SecondProp") = 0
            mProps.Prop("MyArray") = New_c.JSONArray
    End Sub
    
    Public Property Get FirstProp() As String
        FirstProp = mProps("FirstProp")
    End Property
    Public Property Let FirstProp(ByVal strValue As String)
        mProps.Prop("FirstProp") = strValue
    End Property
    
    Public Property Get SecondProp() As Long
        SecondProp = mProps("SecondProp")
    End Property
    Public Property Let SecondProp(ByVal lngValue As Long)
        mProps.Prop("SecondProp") = lngValue
    End Property
    
    Public Property Get MyArray() As cCollection
        Set MyArray = mProps("MyArray")
    End Property
     
    Public Property Get JSON() As String
        JSON = mProps.SerializeToJSONString
    End Property
    Public Property Let JSON(ByVal strJSON As String)
        Set mProps = New_c.JSONDecodeToCollection(strJSON)
    End Property
    Form-TestCode...
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim oClass1 As New cClass
        Dim oClass2 As New cClass
        Dim strJSON As String
     
        oClass1.FirstProp = "String 1" 'assign a String-Value
        oClass1.SecondProp = 2         'assing a Long-Value
        oClass1.MyArray.RemoveAll
        oClass1.MyArray.Add "one"
        oClass1.MyArray.Add "two"
        strJSON = oClass1.JSON
     
        Debug.Print strJSON 'JSON-content of oClass1
        
        oClass2.JSON = strJSON 'now init the oClass2-instance from that content
        Debug.Print oClass2.JSON 'it should now (value-wise) be identical to the oClass1-instance
    End Sub
    HTH

    Olaf

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