VBA code: How Strictly The JSON Grammar Must Be Used


The JSON grammar is well defined, and, strictly speaking, leaves no room for interpretation. However, sometimes, it can come in handy to be a little forgiving. The alternative is that deserialization fails, and, that in order to make it work, you carefully need to correct all grammatical errors in the JSON text. The class offers you the option to be forgiving by means of the method SetHowToConformToJSONGrammar.

The method SetHowToConformToJSONGrammar

Description

The sub SetHowToConformToJSONGrammar sets the level of strictness of the JSON grammar that will be used when deserializing.

Image showing mapping the typical flow of the VBA code

The only thing that the sub SetHowToConvertJSONNullToVBA does is set a parameter in the class cJSON. The parameter is used when deserializing the argument JSONText of Deserialize. Of course, the default value is a strict interpretation of the JSON grammar as described in the standard The JSON Data Interchange Format, Standard ECMA-404, 1st Edition / October 2013.

However, sometimes, this might be too much. With a bit of good will, some errors can be forgiven. The kind of forgivable errors are deviations from the standard that probably would not bother a (flexible) human interpreter. More precise, we are talking about the following errors. The list is exhaustive. No other errors are forgiven.

We forgivewhile it should beRule
NULL, Null, nuLL, ...nullUppercase characters are allowed.
FALSE, False, faLSE, ...falseUppercase characters are allowed.
TRUE, True, trUE, ...trueUppercase characters are allowed.
+1.2341.1234Plus sign in front of a number is allowed.
000000.12340.1234Leading zeroes are allowed.
00000012341234Leading zeroes are allowed.
.12340.1234Number of digits before decimal sign can be zero.
-.1234-0.1234Number of digits before decimal sign can be zero.
{"Name":"Value"}abc{"Name":"Value"}Non-white space characters after the last closing brace are allowed.
{"Name":"Value"}}} }}{"Name":"Value"}Non-white space characters after the last closing brace are allowed.
["Value1","Value2"]abc["Value1","Value2"]Non-white space characters after the last closing bracket are allowed.
["Value1","Value2"] ]]]["Value1","Value2"]Non-white space characters after the last closing bracket are allowed.

JSON is everywhere. However, not everybody is super strict when building a text in JSON format. Often, when you receive a JSON text, you are not 100% sure whether the IT component that has built the JSON text was super strict about the JSON grammar. Also, if it doesn't, there may be no obvious way to have the errors corrected at the source. It is quite annoying when you have to "correct" the JSON text each time before using it, especially when the errors are minor. For this reason, we have added the option to forgive some (minor) errors when deserializing.

Syntax

Public Sub SetHowToConformToJSONGrammar(ByVal HowToConformToJSONGrammar As eHowToConformToJSONGrammar)

Arguments

Name of argumentType of argument
HowToConformToJSONGrammarEnumeration eHowToConformToJSONGrammarRequired

The argument HowToConformToJSONGrammar can take the following values.

Value
BeStrictDefault
BeForgiving

Type of function

The sub is a public method of the class cJSON.

Example 1: Forgive uppercase in false, true or null.

Here is the text to deserialize. Strictly speaking, it is not a JSON text. {"Name":false} would be a JSON text.

{"Name":False}

Here is the VBA code.

The line If (D Is Nothing) Then can be replaced by If JSON.IsOk() Then and the error can be obtained by calling the function ShowWhyNotOk.

Example 2: Forgive slightly different number formats.

Here is the text to deserialize. Strictly speaking, it is not a JSON text. [123, 0, 0.12345] would be a JSON text.

[+123, 0000.0, .12345]

Here is the VBA code.

Example 3: Forgive extra characters after last closing character.

Here is the text to deserialize. Strictly speaking, it is not a JSON text. {"Name":"Value"}} would be a JSON text.

{"Name":"Value"}}

Here is the VBA code.