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.
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 forgive | while it should be | Rule |
---|---|---|
NULL, Null, nuLL, ... | null | Uppercase characters are allowed. |
FALSE, False, faLSE, ... | false | Uppercase characters are allowed. |
TRUE, True, trUE, ... | true | Uppercase characters are allowed. |
+1.234 | 1.1234 | Plus sign in front of a number is allowed. |
000000.1234 | 0.1234 | Leading zeroes are allowed. |
0000001234 | 1234 | Leading zeroes are allowed. |
.1234 | 0.1234 | Number of digits before decimal sign can be zero. |
-.1234 | -0.1234 | Number 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 argument | Type of argument | |
---|---|---|
HowToConformToJSONGrammar | Enumeration eHowToConformToJSONGrammar | Required |
The argument HowToConformToJSONGrammar
can take the following values.
Value | |
---|---|
BeStrict | Default |
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.
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 33 | Public Sub Example1() Dim JSONText As String JSONText = "{""Name"":False}" Dim JSON As cJSON Set JSON = New cJSON Dim D As Dictionary Set D = JSON.Deserialize(JSONText) If (D Is Nothing) Then MsgBox "D is nothing" 'shows D is nothing Else MsgBox "D contains data" End If JSON.SetHowToConformToJSONGrammar BeForgiving Set D = JSON.Deserialize(JSONText) If (D Is Nothing) Then MsgBox "D is nothing" Else MsgBox "D contains data" 'shows D contains data End If Set D = Nothing Set JSON = Nothing End Sub |
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.
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 33 34 35 36 | Public Sub Example2() Dim JSONText As String JSONText = "[+123, 0000.0, .12345]" Dim JSON As cJSON Set JSON = New cJSON Dim D As Dictionary Set D = JSON.Deserialize(JSONText) If (D Is Nothing) Then MsgBox "D is nothing" 'shows D is nothing Else MsgBox "D contains data" End If JSON.SetHowToConformToJSONGrammar BeForgiving Set D = JSON.Deserialize(JSONText) If (D Is Nothing) Then MsgBox "D is nothing" Else MsgBox "D contains data" 'shows D contains data MsgBox D.Item("0") 'shows 123 MsgBox D.Item("1") 'shows 0 MsgBox D.Item("2") 'shows 0,12345 End If Set D = Nothing Set JSON = Nothing End Sub |
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.
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 33 | Public Sub Example3() Dim JSONText As String JSONText = "{""Name"":""Value""}}" Dim JSON As cJSON Set JSON = New cJSON Dim D As Dictionary Set D = JSON.Deserialize(JSONText) If (D Is Nothing) Then MsgBox "D is nothing" 'shows D is nothing Else MsgBox "D contains data" End If JSON.SetHowToConformToJSONGrammar BeForgiving Set D = JSON.Deserialize(JSONText) If (D Is Nothing) Then MsgBox "D is nothing" Else MsgBox "D contains data" 'shows D contains data End If Set D = Nothing Set JSON = Nothing End Sub |