VBA code: Check if the deserialization is OK


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.

Image showing mapping the typical flow of the VBA code

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 function Deserialize has been called. If you do so, the function will return true.
  • If the function IsOk returns true, the dictionary that is returned by Deserialize will be different from Nothing.
  • If the function IsOk returns false, the reason why the deserialization was not successfull can be obtained from the function ShowWhyNotOk.
  • A call to the function IsOk is equivalent to testing if the dictionary returned by the function Deserialize is Nothing.
  • A call to the function IsOk is equivalent to testing if the length of the string returned by ShowWhyNotOk is zero.

Example 1: Basic behavior

Here is the JSON text to deserialize.

{"Name":"Value"}

Here is the VBA code.

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.

{"Name":"Value"

Here is the VBA code.

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.