If you try to put the content of the JSON text into a variable of the type Dictionary
, several things could go wrong. There might be errors in the JSON text. Or, the data in the JSON text cannot be translated perfectly to VBA. Or, there might be a bug in the code that is doing the deserialization. In other words, after a deserialization, it is good practice to check if everything is still ok. This is why the function IsOk
has been added to the class.
The method IsOk
Description
The function IsOk
will tell you whether or not the deserialization was succesfull or not.
Syntax
Function IsOk() As Boolean
Arguments
The function has no arguments.
Return
If the deserialization was succesfull, the return of the function will be true
. Otherwise, it will be false
.
Type of function
The function is a public method of the class cJSON
.
Remarks
- It makes no sense to call the function
IsOk
before the functionDeserialize
has been called. If you do so, the function will returntrue
. - If the function
IsOk
returnstrue
, the dictionary that is returned byDeserialize
will be different fromNothing
. - If the function
IsOk
returnsfalse
, the reason why the deserialization was not successfull can be obtained from the functionShowWhyNotOk
. - A call to the function
IsOk
is equivalent to testing if the dictionary returned by the functionDeserialize
isNothing
. - A call to the function
IsOk
is equivalent to testing if the length of the string returned byShowWhyNotOk
is zero.
Example 1: Basic behavior
Here is the JSON text to deserialize.
Here is the VBA code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Public Sub Example1() Dim JSONString As String JSONString = "{""Name"":""Value""}" Dim JSON As cJSON Set JSON = New cJSON Dim D As Dictionary Set D = JSON.Deserialize(JSONString) If (JSON.IsOk()) Then MsgBox D.Item("Name") 'shows Value MsgBox TypeName(D.Item("Name")) 'shows String Else MsgBox JSON.ShowWhyNotOk() End If Set D = Nothing Set JSON = Nothing End Sub |
If the deserialization was not succesfull, you need to handle the error properly.
Example 2: Equivalent conditions
Here is the text to deserialize. Note that it is not a JSON text. The closing brace is missing.
Here is the VBA code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | Public Sub Example2() Dim JSONString As String JSONString = "{""Name"":""Value""" Dim JSON As cJSON Set JSON = New cJSON Dim D As Dictionary Set D = JSON.Deserialize(JSONString) If (JSON.IsOk()) Then MsgBox "D contains data" Else MsgBox "D is nothing" 'shows D is nothing End If If (Not (D Is Nothing)) Then MsgBox "D contains data" Else MsgBox "D is nothing" 'shows D is nothing End If If (Len(JSON.ShowWhyNotOk()) = 0) Then MsgBox "D contains data" Else MsgBox "D is nothing" 'shows D is nothing End If Set D = Nothing Set JSON = Nothing End Sub |
The function ShowWhyNotOk
is cosmetical. It was our design choice to add it, so that the user of the class doesn't have to remember any of the alternative conditions. However, you can do without it.