-
Notifications
You must be signed in to change notification settings - Fork 0
/
Application.cfm
362 lines (318 loc) · 14 KB
/
Application.cfm
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<cfapplication name="pointAPI" clientmanagement="yes" sessionmanagement="yes" loginstorage="session">
<cfset request.APIToken = "---Your API Token --->">
<cfset request.APIUsername = "<1--- Your API Password Token --->">
<cffunction name="jsonencode" access="remote" returntype="string" output="No"
hint="Converts data from CF to JSON format">
<cfargument name="data" type="any" required="Yes" />
<cfargument name="queryFormat" type="string" required="No" default="query" /> <!-- query or array -->
<cfargument name="queryKeyCase" type="string" required="No" default="lower" /> <!-- lower or upper -->
<cfargument name="stringNumbers" type="boolean" required="No" default=false >
<cfargument name="formatDates" type="boolean" required="No" default=false >
<cfargument name="columnListFormat" type="string" required="No" default="string" > <!-- string or array -->
<!--- VARIABLE DECLARATION --->
<cfset var jsonString = "" />
<cfset var tempVal = "" />
<cfset var arKeys = "" />
<cfset var colPos = 1 />
<cfset var i = 1 />
<cfset var column = ""/>
<cfset var datakey = ""/>
<cfset var recordcountkey = ""/>
<cfset var columnlist = ""/>
<cfset var columnlistkey = ""/>
<cfset var dJSONString = "" />
<cfset var escapeToVals = "\\,\"",\/,\b,\t,\n,\f,\r" />
<cfset var escapeVals = "\,"",/,#Chr(8)#,#Chr(9)#,#Chr(10)#,#Chr(12)#,#Chr(13)#" />
<cfset var _data = arguments.data />
<!--- BOOLEAN --->
<cfif IsBoolean(_data) AND NOT IsNumeric(_data) AND NOT ListFindNoCase("Yes,No", _data)>
<cfreturn LCase(ToString(_data)) />
<!--- NUMBER --->
<cfelseif NOT stringNumbers AND IsNumeric(_data) AND NOT REFind("^0+[^\.]",_data)>
<cfreturn ToString(_data) />
<!--- DATE --->
<cfelseif IsDate(_data) AND arguments.formatDates>
<cfreturn '"#DateFormat(_data, "medium")# #TimeFormat(_data, "medium")#"' />
<!--- STRING --->
<cfelseif IsSimpleValue(_data)>
<cfreturn '"' & ReplaceList(_data, escapeVals, escapeToVals) & '"' />
<!--- ARRAY --->
<cfelseif IsArray(_data)>
<cfset dJSONString = createObject('java','java.lang.StringBuffer').init("") />
<cfloop from="1" to="#ArrayLen(_data)#" index="i">
<cfset tempVal = jsonencode( _data[i], arguments.queryFormat, arguments.queryKeyCase, arguments.stringNumbers, arguments.formatDates, arguments.columnListFormat ) />
<cfif dJSONString.toString() EQ "">
<cfset dJSONString.append(tempVal) />
<cfelse>
<cfset dJSONString.append("," & tempVal) />
</cfif>
</cfloop>
<cfreturn "[" & dJSONString.toString() & "]" />
<!--- STRUCT --->
<cfelseif IsStruct(_data)>
<cfset dJSONString = createObject('java','java.lang.StringBuffer').init("") />
<cfset arKeys = StructKeyArray(_data) />
<cfloop from="1" to="#ArrayLen(arKeys)#" index="i">
<cfset tempVal = jsonencode( _data[ arKeys[i] ], arguments.queryFormat, arguments.queryKeyCase, arguments.stringNumbers, arguments.formatDates, arguments.columnListFormat ) />
<cfif dJSONString.toString() EQ "">
<cfset dJSONString.append('"' & arKeys[i] & '":' & tempVal) />
<cfelse>
<cfset dJSONString.append("," & '"' & arKeys[i] & '":' & tempVal) />
</cfif>
</cfloop>
<cfreturn "{" & dJSONString.toString() & "}" />
<!--- QUERY --->
<cfelseif IsQuery(_data)>
<cfset dJSONString = createObject('java','java.lang.StringBuffer').init("") />
<!--- Add query meta data --->
<cfif arguments.queryKeyCase EQ "lower">
<cfset recordcountKey = "recordcount" />
<cfset columnlistKey = "columnlist" />
<cfset columnlist = LCase(_data.columnlist) />
<cfset dataKey = "data" />
<cfelse>
<cfset recordcountKey = "RECORDCOUNT" />
<cfset columnlistKey = "COLUMNLIST" />
<cfset columnlist = _data.columnlist />
<cfset dataKey = "data" />
</cfif>
<cfset dJSONString.append('"#recordcountKey#":' & _data.recordcount) />
<cfif arguments.columnListFormat EQ "array">
<cfset columnlist = "[" & ListQualify(columnlist, '"') & "]" />
<cfset dJSONString.append(',"#columnlistKey#":' & columnlist) />
<cfelse>
<cfset dJSONString.append(',"#columnlistKey#":"' & columnlist & '"') />
</cfif>
<cfset dJSONString.append(',"#dataKey#":') />
<!--- Make query a structure of arrays --->
<cfif arguments.queryFormat EQ "query">
<cfset dJSONString.append("{") />
<cfset colPos = 1 />
<cfloop list="#_data.columnlist#" delimiters="," index="column">
<cfif colPos GT 1>
<cfset dJSONString.append(",") />
</cfif>
<cfif arguments.queryKeyCase EQ "lower">
<cfset column = LCase(column) />
</cfif>
<cfset dJSONString.append('"' & column & '":[') />
<cfloop from="1" to="#_data.recordcount#" index="i">
<!--- Get cell value; recurse to get proper format depending on string/number/boolean data type --->
<cfset tempVal = jsonencode( _data[column][i], arguments.queryFormat, arguments.queryKeyCase, arguments.stringNumbers, arguments.formatDates, arguments.columnListFormat ) />
<cfif i GT 1>
<cfset dJSONString.append(",") />
</cfif>
<cfset dJSONString.append(tempVal) />
</cfloop>
<cfset dJSONString.append("]") />
<cfset colPos = colPos + 1 />
</cfloop>
<cfset dJSONString.append("}") />
<!--- Make query an array of structures --->
<cfelse>
<cfset dJSONString.append("[") />
<cfloop query="_data">
<cfif CurrentRow GT 1>
<cfset dJSONString.append(",") />
</cfif>
<cfset dJSONString.append("{") />
<cfset colPos = 1 />
<cfloop list="#columnlist#" delimiters="," index="column">
<cfset tempVal = jsonencode( _data[column][CurrentRow], arguments.queryFormat, arguments.queryKeyCase, arguments.stringNumbers, arguments.formatDates, arguments.columnListFormat ) />
<cfif colPos GT 1>
<cfset dJSONString.append(",") />
</cfif>
<cfif arguments.queryKeyCase EQ "lower">
<cfset column = LCase(column) />
</cfif>
<cfset dJSONString.append('"' & column & '":' & tempVal) />
<cfset colPos = colPos + 1 />
</cfloop>
<cfset dJSONString.append("}") />
</cfloop>
<cfset dJSONString.append("]") />
</cfif>
<!--- Wrap all query data into an object --->
<cfreturn "{" & dJSONString.toString() & "}" />
<!--- UNKNOWN OBJECT TYPE --->
<cfelse>
<cfreturn '"' & "unknown-obj" & '"' />
</cfif>
</cffunction>
<cffunction name="jsondecode" access="remote" returntype="any" output="no" hint="Converts data frm JSON to CF format">
<cfargument name="data" type="string" required="Yes" />
<!--- DECLARE VARIABLES --->
<cfset var ar = ArrayNew(1) />
<cfset var st = StructNew() />
<cfset var dataType = "" />
<cfset var inQuotes = false />
<cfset var startPos = 1 />
<cfset var nestingLevel = 0 />
<cfset var dataSize = 0 />
<cfset var i = 1 />
<cfset var skipIncrement = false />
<cfset var j = 0 />
<cfset var char = "" />
<cfset var dataStr = "" />
<cfset var structVal = "" />
<cfset var structKey = "" />
<cfset var colonPos = "" />
<cfset var qRows = 0 />
<cfset var qCol = "" />
<cfset var qData = "" />
<cfset var curCharIndex = "" />
<cfset var curChar = "" />
<cfset var result = "" />
<cfset var unescapeVals = "\\,\"",\/,\b,\t,\n,\f,\r" />
<cfset var unescapeToVals = "\,"",/,#Chr(8)#,#Chr(9)#,#Chr(10)#,#Chr(12)#,#Chr(13)#" />
<cfset var unescapeVals2 = '\,",/,b,t,n,f,r' />
<cfset var unescapetoVals2 = '\,",/,#Chr(8)#,#Chr(9)#,#Chr(10)#,#Chr(12)#,#Chr(13)#' />
<cfset var dJSONString = "" />
<cfset var _data = Trim(arguments.data) />
<!--- NUMBER --->
<cfif IsNumeric(_data)>
<cfreturn _data />
<!--- NULL --->
<cfelseif _data EQ "null">
<cfreturn "" />
<!--- BOOLEAN --->
<cfelseif ListFindNoCase("true,false", _data)>
<cfreturn _data />
<!--- EMPTY STRING --->
<cfelseif _data EQ "'" OR _data EQ '""'>
<cfreturn "" />
<!--- STRING --->
<cfelseif ReFind('^"[^\\"]*(?:\\.[^\\"]*)*"$', _data) EQ 1 OR ReFind("^'[^\\']*(?:\\.[^\\']*)*'$", _data) EQ 1>
<cfset _data = mid(_data, 2, Len(_data)-2) />
<!--- If there are any \b, \t, \n, \f, and \r, do extra processing
(required because ReplaceList() won't work with those) --->
<cfif Find("\b", _data) OR Find("\t", _data) OR Find("\n", _data) OR Find("\f", _data) OR Find("\r", _data)>
<cfset curCharIndex = 0 />
<cfset curChar = ""/>
<cfset dJSONString = createObject("java", "java.lang.StringBuffer").init("") />
<cfloop condition="true">
<cfset curCharIndex = curCharIndex + 1 />
<cfif curCharIndex GT len(_data)>
<cfbreak />
<cfelse>
<cfset curChar = mid(_data, curCharIndex, 1) />
<cfif curChar EQ "\">
<cfset curCharIndex = curCharIndex + 1 />
<cfset curChar = mid(_data, curCharIndex,1) />
<cfset pos = listFind(unescapeVals2, curChar) />
<cfif pos>
<cfset dJSONString.append(ListGetAt(unescapetoVals2, pos)) />
<cfelse>
<cfset dJSONString.append("\" & curChar) />
</cfif>
<cfelse>
<cfset dJSONString.append(curChar) />
</cfif>
</cfif>
</cfloop>
<cfreturn dJSONString.toString() />
<cfelse>
<cfreturn ReplaceList(_data, unescapeVals, unescapeToVals) />
</cfif>
<!--- ARRAY, STRUCT, OR QUERY --->
<cfelseif ( Left(_data, 1) EQ "[" AND Right(_data, 1) EQ "]" )
OR ( Left(_data, 1) EQ "{" AND Right(_data, 1) EQ "}" )>
<!--- Store the data type we're dealing with --->
<cfif Left(_data, 1) EQ "[" AND Right(_data, 1) EQ "]">
<cfset dataType = "array" />
<cfelseif ReFindNoCase('^\{"recordcount":[0-9]+,"columnlist":"[^"]+","data":\{("[^"]+":\[[^]]*\],?)+\}\}$', _data, 0) EQ 1>
<cfset dataType = "query" />
<cfelse>
<cfset dataType = "struct" />
</cfif>
<!--- Remove the brackets --->
<cfset _data = Trim( Mid(_data, 2, Len(_data)-2) ) />
<!--- Deal with empty array/struct --->
<cfif Len(_data) EQ 0>
<cfif dataType EQ "array">
<cfreturn ar />
<cfelse>
<cfreturn st />
</cfif>
</cfif>
<!--- Loop through the string characters --->
<cfset dataSize = Len(_data) + 1 />
<cfloop condition="#i# LTE #dataSize#">
<cfset skipIncrement = false />
<!--- Save current character --->
<cfset char = Mid(_data, i, 1) />
<!--- If char is a quote, switch the quote status --->
<cfif char EQ '"'>
<cfset inQuotes = NOT inQuotes />
<!--- If char is escape character, skip the next character --->
<cfelseif char EQ "\" AND inQuotes>
<cfset i = i + 2 />
<cfset skipIncrement = true />
<!--- If char is a comma and is not in quotes, or if end of string, deal with data --->
<cfelseif (char EQ "," AND NOT inQuotes AND nestingLevel EQ 0) OR i EQ Len(_data)+1>
<cfset dataStr = Mid(_data, startPos, i-startPos) />
<!--- If data type is array, append data to the array --->
<cfif dataType EQ "array">
<cfset arrayappend( ar, jsondecode(dataStr) ) />
<!--- If data type is struct or query... --->
<cfelseif dataType EQ "struct" OR dataType EQ "query">
<cfset dataStr = Mid(_data, startPos, i-startPos) />
<cfset colonPos = Find('":', dataStr) />
<cfif colonPos>
<cfset colonPos = colonPos + 1 />
<cfelse>
<cfset colonPos = Find(":", dataStr) />
</cfif>
<cfset structKey = Trim( Mid(dataStr, 1, colonPos-1) ) />
<!--- If needed, remove quotes from keys --->
<cfif Left(structKey, 1) EQ "'" OR Left(structKey, 1) EQ '"'>
<cfset structKey = Mid( structKey, 2, Len(structKey)-2 ) />
</cfif>
<cfset structVal = Mid( dataStr, colonPos+1, Len(dataStr)-colonPos ) />
<!--- If struct, add to the structure --->
<cfif dataType EQ "struct">
<cfset StructInsert( st, structKey, jsondecode(structVal) ) />
<!--- If query, build the query --->
<cfelse>
<cfif structKey EQ "recordcount">
<cfset qRows = jsondecode(structVal) />
<cfelseif structKey EQ "columnlist">
<cfset st = QueryNew( jsondecode(structVal) ) />
<cfif qRows>
<cfset QueryAddRow(st, qRows) />
</cfif>
<cfelseif structKey EQ "data">
<cfset qData = jsondecode(structVal) />
<cfset ar = StructKeyArray(qData) />
<cfloop from="1" to="#ArrayLen(ar)#" index="j">
<cfloop from="1" to="#st.recordcount#" index="qRows">
<cfset qCol = ar[j] />
<cfset QuerySetCell(st, qCol, qData[qCol][qRows], qRows) />
</cfloop>
</cfloop>
</cfif>
</cfif>
</cfif>
<cfset startPos = i + 1 />
<!--- If starting a new array or struct, add to nesting level --->
<cfelseif "{[" CONTAINS char AND NOT inQuotes>
<cfset nestingLevel = nestingLevel + 1 />
<!--- If ending an array or struct, subtract from nesting level --->
<cfelseif "]}" CONTAINS char AND NOT inQuotes>
<cfset nestingLevel = nestingLevel - 1 />
</cfif>
<cfif NOT skipIncrement>
<cfset i = i + 1 />
</cfif>
</cfloop>
<!--- Return appropriate value based on data type --->
<cfif dataType EQ "array">
<cfreturn ar />
<cfelse>
<cfreturn st />
</cfif>
<!--- INVALID JSON --->
<cfelse>
<cfthrow message="Invalid JSON" detail="The document you are trying to jsondecode is not in valid JSON format" />
</cfif>
</cffunction>