-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsonencode.asp
164 lines (151 loc) · 5.45 KB
/
jsonencode.asp
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
<script language="javascript" runat="server">
/**
* jsonencode.asp
*
* JavaScript function for encoding strings for JSON
* using fast JavaScript callbacks on a single regular expression search/replace per string
*
* @version 1.00.01 2011-03-16
* @package asp
* @author Ross McKay <rmckay@webaware.com.au>
* @link https://github.com/webaware/ASP-JSONEncode
* @copyright copyright © 2011 WebAware Pty Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Full license: {@link http://www.webaware.com.au/free/license.htm}
*/
/**
* encode a string for embedding as a value in a JSON document
*
* will encode control characters as Unicode hex, and LF CR TAB FF BS / \ " as readable escaped characters
*
* ref: http://www.ietf.org/rfc/rfc4627.txt?number=4627
*
* @return string
* @param string src the value to encode
*/
function JSONEncodeString(src) {
if (null === src || typeof src == "undefined")
return "";
var s = String(src);
// encode control characters as well as \ / "
return s.replace(/[\\\/"\x00-\x1f\x7f-\xa0\u2000-\u200f\u2028-\u202f]/g, function(match) {
switch (match) {
case "\\":
return "\\\\";
case "/":
return "\\/";
case '"':
return '\\"';
case "\r":
return "\\r";
case "\n":
return "\\n";
case "\t":
return "\\t";
case "\f":
return "\\f";
case "\b":
return "\\b";
default:
// return as \uNNNN
var c = match.charCodeAt(0);
return "\\u" + ("0000" + c.toString(16)).slice(-4);
}
});
}
</script>
<script language="vbscript" runat="server">
'-----------------------------------------------------------------------
' format a date object into ISO-8601 so that JavaScript will parse it
'
' @param Date d
' @return string
'-----------------------------------------------------------------------
Function JSONEncodeDate(d)
JSONEncodeDate = Right("000" & Year(d), 4) & "-" & Right("0" & Month(d), 2) & "-" & Right("0" & Day(d), 2) _
& "T" & Right("0" & Hour(d), 2) & "-" & Right("0" & Minute(d), 2) & "-" & Right("0" & Second(d), 2)
End Function
'-----------------------------------------------------------------------
' convert a dictionary object into a collection of JSON elements
'
' @param string elementName the name of the JSON element wrapping the collection
' @param Scripting.Dictionary dict a name/value pair collection of mixed data types
' @return string
'-----------------------------------------------------------------------
Function JSONEncodeDict(ByVal elementName, ByVal dict)
Dim i, delim
JSONEncodeDict = """" & JSONEncodeString(elementName) & """:{"
delim = ""
For Each i In dict
JSONEncodeDict = JSONEncodeDict & delim
Select Case VarType(dict(i))
Case vbObject
JSONEncodeDict = JSONEncodeDict & JSONEncodeDict(i, dict(i))
Case vbNull
JSONEncodeDict = JSONEncodeDict & """" & JSONEncodeString(i) & """:null"
Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbByte
JSONEncodeDict = JSONEncodeDict & """" & JSONEncodeString(i) & """:" & dict(i)
Case vbDate
JSONEncodeDict = JSONEncodeDict & """" & JSONEncodeString(i) & """:" & JSONEncodeDate(dict(i))
Case vbBoolean
JSONEncodeDict = JSONEncodeDict & """" & JSONEncodeString(i) & """:" & LCase(dict(i))
Case Else
If IsArray(dict(i)) Then
JSONEncodeDict = JSONEncodeDict & """" & JSONEncodeString(i) & """:" & JSONEncodeArray(dict(i))
Else
JSONEncodeDict = JSONEncodeDict & """" & JSONEncodeString(i) & """:""" & JSONEncodeString(dict(i)) & """"
End If
End Select
delim = ","
Next
JSONEncodeDict = JSONEncodeDict & "}"
End Function
'-----------------------------------------------------------------------
' convert an array into a collection of JSON elements,
'
' @param Array arr an array of mixed data types
' @return string
'-----------------------------------------------------------------------
Function JSONEncodeArray(ByVal arr)
Dim i, delim
JSONEncodeArray = "["
delim = ""
For i = LBound(arr) To UBound(arr)
JSONEncodeArray = JSONEncodeArray & delim
Select Case VarType(arr(i))
Case vbObject
JSONEncodeArray = JSONEncodeArray & JSONEncodeDict(i, arr(i))
Case vbNull
JSONEncodeArray = JSONEncodeArray & "null"
Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbByte
JSONEncodeArray = JSONEncodeArray & arr(i)
Case vbDate
JSONEncodeArray = JSONEncodeArray & JSONEncodeDate(arr(i))
Case vbBoolean
JSONEncodeArray = JSONEncodeArray & LCase(arr(i))
Case Else
If IsArray(arr(i)) Then
JSONEncodeArray = JSONEncodeArray & JSONEncodeArray(arr(i))
Else
JSONEncodeArray = JSONEncodeArray & """" & JSONEncodeString(arr(i)) & """"
End If
End Select
delim = ","
Next
JSONEncodeArray = JSONEncodeArray & "]"
End Function
</script>