-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonString.cls
More file actions
178 lines (145 loc) · 5.77 KB
/
JsonString.cls
File metadata and controls
178 lines (145 loc) · 5.77 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "JsonString"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'@Folder("JSON.Core")
Option Explicit
Implements IJson
Private Const ModuleName As String = "JsonString"
Private Type TData
Value As String
End Type
Private This As TData
Friend Sub Create(ByVal Stream As StringStream)
Const FunctionName As String = "Create"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Parse Stream
End Sub
Private Function IJson_DataType() As JsonDataTypeEnum
Const FunctionName As String = "IJson_DataType"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
IJson_DataType = JsonDataTypeString
End Function
Public Function DataType() As JsonDataTypeEnum
Const FunctionName As String = "DataType"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
DataType = IJson_DataType
End Function
Private Function IJson_ToString() As String
Const FunctionName As String = "IJson_ToString"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
IJson_ToString = """" & Escape(This.Value) & """"
End Function
Public Function ToString() As String
Const FunctionName As String = "ToString"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
ToString = IJson_ToString
End Function
'@DefaultMember
Public Function Value() As String
Attribute Value.VB_UserMemId = 0
Const FunctionName As String = "Value"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Value = This.Value
End Function
Private Sub Parse(ByVal Stream As StringStream)
Const FunctionName As String = "Parse"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Const Pattern As String = "^""(?:\\(?:[""\\\/bfnrt]|u[a-fA-F0-9]{4})|[^""\\\0-\x1F\x7F]+)*"""
If (Stream.Match(Pattern)) Then
Dim Token As String
Token = Stream.PeekString(Pattern)
This.Value = Unescape(Mid$(Token, 2, Len(Token) - 2))
Stream.EatString Token
Else
Err.Raise JsonExceptionUnexpectedToken, ModuleName & "." & FunctionName, "String expected."
End If
End Sub
Private Function Escape(ByVal Value As String) As String
Const FunctionName As String = "Escape"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Dim Rx As VBScript_RegExp_55.RegExp
Set Rx = CreateObject("VBScript.RegExp")
Rx.Global = True
Rx.MultiLine = True
Rx.Pattern = "([""\\\/])"
Dim Data As String
Data = Rx.Replace(Value, "$1") '// Escape double quotes, anti-slash and slah
Data = Replace(Data, vbLf, "\n") '// Escape new line
Data = Replace(Data, vbCr, "\r") '// Escape carriage return
Data = Replace(Data, vbBack, "\b") '// Escape backspace
Data = Replace(Data, vbTab, "\t") '// Escape tab
Data = Replace(Data, vbVerticalTab, "\f") '// Escape Vertical tab
Data = EscapeUnicode(Data) '// Escape unicode characters
Escape = Data
End Function
Private Function EscapeUnicode(ByVal Value As String) As String
Const FunctionName As String = "EscapeUnicode"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Dim Data As String
Dim i As Long
For i = 1 To Len(Value)
Dim Character As String
Character = Mid$(Value, i, 1)
Select Case AscW(Character)
Case 32 To 126
Data = Data & Character
Case Else
Data = Data & "\u" & Right$("0000" & Hex$(AscW(Character)), 4)
End Select
Next
EscapeUnicode = Data
End Function
Private Function Unescape(ByVal Value As String) As String
Const FunctionName As String = "Unescape"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Dim Data As String
Data = UnescapeUnicode(Value) '// Unescape unicode characters
Dim Rx As VBScript_RegExp_55.RegExp
Set Rx = CreateObject("VbScript.RegExp")
Rx.Global = True
Rx.MultiLine = True
Rx.Pattern = "\\([""\\\/])"
Data = Rx.Replace(Data, "$1") '// Unescape double quotes, anti_slash and slash
Data = Replace(Data, "\b", vbBack) '// Unescape backspace
Data = Replace(Data, "\f", vbVerticalTab) '// Unescape vertical tab
Data = Replace(Data, "\n", vbLf) '// Unescape new line
Data = Replace(Data, "\r", vbCr) '// Unescape carriage return
Data = Replace(Data, "\t", vbTab) '// Unescape tab
Unescape = Data
End Function
Private Function UnescapeUnicode(ByVal Value As String) As String
Const FunctionName As String = "UnescapeUnicode"
Dim ErrorLogger As ErrorLogger
Set ErrorLogger = Factory.CreateErrorLogger(ModuleName, FunctionName)
Dim Rx As VBScript_RegExp_55.RegExp
Set Rx = CreateObject("VBScript.RegExp")
Rx.Global = True
Rx.MultiLine = True
Rx.Pattern = "\\u([a-fA-F0-9]{4})"
Dim Matchs As VBScript_RegExp_55.MatchCollection
Dim Data As String
Data = Value
Dim Match As VBScript_RegExp_55.Match
Set Matchs = Rx.Execute(Data)
For Each Match In Matchs
Rx.Pattern = "(\" & Match.Value & ")"
Data = Rx.Replace(Data, ChrW$(Val("&H" & Match.SubMatches(0))))
Next
UnescapeUnicode = Data
End Function