-
Notifications
You must be signed in to change notification settings - Fork 6
/
JsonScript.bas
129 lines (92 loc) · 3.31 KB
/
JsonScript.bas
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
Attribute VB_Name = "JsonScript"
' JsonScript v1.2.1
' (c) Gustav Brock, Cactus Data ApS, CPH
' https://github.com/CactusData/VBA.CVRAPI
'
' Low-level wrapper functions to retrieve and encode/decode Json data by JavaScript.
'
' License: MIT (http://opensource.org/licenses/mit-license.php)
'
' 2018-05-03: Binding of Script Control changed to late binding for simplicity.
' Added option for 64-bit script control (third-party)
'
' Requires:
' 32-bit VBA: Presence of "Microsoft Script Control 1.0"
' 64-bit VBA: Install of third-party script control "Tablacus Script Control 64"
' https://tablacus.github.io/scriptcontrol_en.html
'
Option Compare Text
Option Explicit
' Script engine to run JavaScript (Microsoft JScript).
Private ScriptEngine As Object
' Initialize the engine.
'
Public Sub InitiateScriptEngine()
Dim Prompt As String
Dim Buttons As VbMsgBoxStyle
Dim Title As String
On Error GoTo Err_InitiateScriptEngine
If ScriptEngine Is Nothing Then
Set ScriptEngine = CreateObject("ScriptControl")
ScriptEngine.Language = "JScript"
ScriptEngine.AddCode "function encode(plainString) {return encodeURIComponent(plainString);}"
ScriptEngine.AddCode "function getProperty(jsonObj, propertyName) {return jsonObj[propertyName];}"
ScriptEngine.AddCode "function getKeys(jsonObj) {var keys = new Array(); for (var i in jsonObj) {keys.push(i);} return keys;}"
End If
Exit_InitiateScriptEngine:
Exit Sub
Err_InitiateScriptEngine:
Prompt = "Error " & Err.Number & ":" & vbCrLf & Err.Description
Buttons = vbCritical + vbOKOnly
Title = "Script Control Objcet Error"
MsgBox Prompt, Buttons, Title
Resume Exit_InitiateScriptEngine
End Sub
' Terminate the engine.
'
Public Sub TerminateScriptEngine()
Set ScriptEngine = Nothing
End Sub
' Get the keys of a Json object.
'
Public Function GetKeys( _
ByVal JsonObject As Object) _
As String()
Dim KeysObject As Object
Dim Keys() As String
Dim Length As Integer
Set KeysObject = ScriptEngine.Run("getKeys", JsonObject)
Length = GetProperty(KeysObject, "length")
If Length > 0 Then
ReDim Keys(Length - 1)
End If
' KeysObject is just a comma separated string ...
Keys = Split(KeysObject, ",")
GetKeys = Keys
End Function
' Get a property by name.
'
Public Function GetProperty(ByVal JsonObject As Object, ByVal PropertyName As String) As Variant
GetProperty = ScriptEngine.Run("getProperty", JsonObject, PropertyName)
End Function
' Get a property as an object by name.
'
Public Function GetObjectProperty(ByVal JsonObject As Object, ByVal PropertyName As String) As Object
Set GetObjectProperty = ScriptEngine.Run("getProperty", JsonObject, PropertyName)
End Function
' URL Encode a string.
'
Public Function EncodeUrl( _
ByVal PlainString As String) _
As String
Dim EncodedString As String
Call InitiateScriptEngine
EncodedString = ScriptEngine.Run("encode", PlainString)
EncodeUrl = EncodedString
End Function
' URL decode a Json string.
'
Public Function DecodeJsonString(ByVal JSonString As String) As Object
Call InitiateScriptEngine
Set DecodeJsonString = ScriptEngine.Eval("(" + JSonString + ")")
End Function