Results 1 to 3 of 3

Thread: About RC6.Collection.Prop (JSONObject.Prop)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    About RC6.Collection.Prop (JSONObject.Prop)

    RC6.Collection is somewhat inconvenient when dealing with the null/empty value of JSON objects, for example:

    Code:
        Dim oID As Object
        Dim oJSON As cCollection
        
        Set oJSON = New_c.JSONObject
        Set oJSON = New_c.JSONDecodeToCollection("{""id"": null}")
        
        Set oID = oJSON.Prop("id")    '--- Run-time error: Object required ---
    I need to do the following processing:

    Code:
        If oJSON.Exists("id") = False Then
            Set oID = Nothing
        ElseIf IsEmpty(oJSON.Prop("id")) = True Then
            Set oID = Nothing
        Else
            Set oID = oJSON.Prop("id")
        End If
    It's recommended to add an optional parameters to RC6.Collection.Prop:
    Code:
    Property Prop(Key, Optional ByVal IsObject As Boolean)
    
    'OR
    
    Property Prop(Key, Optional ByVal NullIsNothing As Boolean)
    When id does not exist or is null, return Nothing:
    Code:
    Set oID = oJSON.Prop("id", True)    '--- When id does not exist or is null, return Nothing ---
    Last edited by SearchingDataOnly; Jun 19th, 2021 at 02:46 PM.

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: About RC6.Collection.Prop (JSONObject.Prop)

    The example does not look very realistic to me.

    Why does an ID-Property has to be an Object (it usually is a Value-Type, like a String or an Integer).

    Furthermore, when the ID-Prop (for some strange reason) *has* to contain an Object,
    why do you work LateBound there...?

    The only kind of Object, which is allowed in a JSON-Hierarchy, is either:
    - another JSON-Object (represented via "just another cCollection")
    - or a JSON-Array (represented as well, via a cCollection... "just Items, without Keys")

    I'd write your Test this way:
    Code:
    Private Sub Form_Load()
      Dim oJSON As cCollection
      Set oJSON = New_c.JSONObject
          oJSON.Prop("o") = Empty
      Debug.Print oJSON.SerializeToJSONString 'just to check, what oJSON currently contains
     
      Dim Obj As cCollection
      If oJSON.Exists("o") Then If IsObject(oJSON("o")) Then Set Obj = oJSON("o")
      'after the line above, Obj is either Nothing, or a valid cCollection-instance
    End Sub
    Or, when you ensure better defaults in your JSON-String in the first place,
    then you will not need any checks:
    Code:
    Private Sub Form_Load()
      Dim oJSON As cCollection
      Set oJSON = New_c.JSONDecodeToCollection("{""o"":{}") '<- ensure better defaults
     
      Dim Obj As cCollection
      Set Obj = oJSON("o")  'if you do, then you can rely on "stuff already existing, where it should"
      Debug.Print Obj.Count 'this gives a Prop-Count of 0
    End Sub

    Olaf

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2020
    Posts
    1,421

    Re: About RC6.Collection.Prop (JSONObject.Prop)

    Quote Originally Posted by Schmidt View Post
    The example does not look very realistic to me.

    Why does an ID-Property has to be an Object (it usually is a Value-Type, like a String or an Integer).
    ...
    ...
    In the abstract syntax tree, id is a node object. Under normal circumstances, id is a JSON-Object (another cCollection), but when id does not exist, the JSON string returned by almost all third-party JS-libs is "null" instead of "{}"

    Please run the following example in astExplorer.net:
    Code:
    var myPlugin;
    (function (myPlugin) {
        
    })();
    Edit:
    In addition, perhaps the following optional parameter would be better:
    Code:
    Property Prop(Key, Optional ByVal DefaultNullValue As Variant)
    Last edited by SearchingDataOnly; Jun 20th, 2021 at 08:57 AM.

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