JSON_2.0.4.asp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <%
  2. '
  3. ' VBS JSON 2.0.3
  4. ' Copyright (c) 2009 Tuðrul Topuz
  5. ' Under the MIT (MIT-LICENSE.txt) license.
  6. '
  7. Const JSON_OBJECT = 0
  8. Const JSON_ARRAY = 1
  9. Class jsCore
  10. Public Collection
  11. Public Count
  12. Public QuotedVars
  13. Public Kind ' 0 = object, 1 = array
  14. Private Sub Class_Initialize
  15. Set Collection = CreateObject("Scripting.Dictionary")
  16. QuotedVars = True
  17. Count = 0
  18. End Sub
  19. Private Sub Class_Terminate
  20. Set Collection = Nothing
  21. End Sub
  22. ' counter
  23. Private Property Get Counter
  24. Counter = Count
  25. Count = Count + 1
  26. End Property
  27. ' - data maluplation
  28. ' -- pair
  29. Public Property Let Pair(p, v)
  30. If IsNull(p) Then p = Counter
  31. Collection(p) = v
  32. End Property
  33. Public Property Set Pair(p, v)
  34. If IsNull(p) Then p = Counter
  35. If TypeName(v) <> "jsCore" Then
  36. Err.Raise &hD, "class: class", "Incompatible types: '" & TypeName(v) & "'"
  37. End If
  38. Set Collection(p) = v
  39. End Property
  40. Public Default Property Get Pair(p)
  41. If IsNull(p) Then p = Count - 1
  42. If IsObject(Collection(p)) Then
  43. Set Pair = Collection(p)
  44. Else
  45. Pair = Collection(p)
  46. End If
  47. End Property
  48. ' -- pair
  49. Public Sub Clean
  50. Collection.RemoveAll
  51. End Sub
  52. Public Sub Remove(vProp)
  53. Collection.Remove vProp
  54. End Sub
  55. ' data maluplation
  56. ' encoding
  57. Function jsEncode(str)
  58. Dim charmap(127), haystack()
  59. charmap(8) = "\b"
  60. charmap(9) = "\t"
  61. charmap(10) = "\n"
  62. charmap(12) = "\f"
  63. charmap(13) = "\r"
  64. charmap(34) = "\"""
  65. charmap(47) = "\/"
  66. charmap(92) = "\\"
  67. Dim strlen : strlen = Len(str) - 1
  68. ReDim haystack(strlen)
  69. Dim i, charcode
  70. For i = 0 To strlen
  71. haystack(i) = Mid(str, i + 1, 1)
  72. charcode = AscW(haystack(i)) And 65535
  73. If charcode < 127 Then
  74. If Not IsEmpty(charmap(charcode)) Then
  75. haystack(i) = charmap(charcode)
  76. ElseIf charcode < 32 Then
  77. haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
  78. End If
  79. Else
  80. haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
  81. End If
  82. Next
  83. jsEncode = Join(haystack, "")
  84. End Function
  85. ' converting
  86. Public Function toJSON(vPair)
  87. Select Case VarType(vPair)
  88. Case 0 ' Empty
  89. toJSON = "null"
  90. Case 1 ' Null
  91. toJSON = "null"
  92. Case 7 ' Date
  93. ' toJSON = "new Date(" & (vPair - CDate(25569)) * 86400000 & ")" ' let in only utc time
  94. toJSON = """" & CStr(vPair) & """"
  95. Case 8 ' String
  96. toJSON = """" & jsEncode(vPair) & """"
  97. Case 9 ' Object
  98. Dim bFI,i
  99. bFI = True
  100. If vPair.Kind Then toJSON = toJSON & "[" Else toJSON = toJSON & "{"
  101. For Each i In vPair.Collection
  102. If bFI Then bFI = False Else toJSON = toJSON & ","
  103. If vPair.Kind Then
  104. toJSON = toJSON & toJSON(vPair(i))
  105. Else
  106. If QuotedVars Then
  107. toJSON = toJSON & """" & i & """:" & toJSON(vPair(i))
  108. Else
  109. toJSON = toJSON & i & ":" & toJSON(vPair(i))
  110. End If
  111. End If
  112. Next
  113. If vPair.Kind Then toJSON = toJSON & "]" Else toJSON = toJSON & "}"
  114. Case 11
  115. If vPair Then toJSON = "true" Else toJSON = "false"
  116. Case 12, 8192, 8204
  117. toJSON = RenderArray(vPair, 1, "")
  118. Case Else
  119. toJSON = Replace(vPair, ",", ".")
  120. End select
  121. End Function
  122. Function RenderArray(arr, depth, parent)
  123. Dim first : first = LBound(arr, depth)
  124. Dim last : last = UBound(arr, depth)
  125. Dim index, rendered
  126. Dim limiter : limiter = ","
  127. RenderArray = "["
  128. For index = first To last
  129. If index = last Then
  130. limiter = ""
  131. End If
  132. On Error Resume Next
  133. rendered = RenderArray(arr, depth + 1, parent & index & "," )
  134. If Err = 9 Then
  135. On Error GoTo 0
  136. RenderArray = RenderArray & toJSON(Eval("arr(" & parent & index & ")")) & limiter
  137. Else
  138. RenderArray = RenderArray & rendered & "" & limiter
  139. End If
  140. Next
  141. RenderArray = RenderArray & "]"
  142. End Function
  143. Public Property Get jsString
  144. jsString = toJSON(Me)
  145. End Property
  146. Sub Flush
  147. If TypeName(Response) <> "Empty" Then
  148. Response.Write(jsString)
  149. ElseIf WScript <> Empty Then
  150. WScript.Echo(jsString)
  151. End If
  152. End Sub
  153. Public Function Clone
  154. Set Clone = ColClone(Me)
  155. End Function
  156. Private Function ColClone(core)
  157. Dim jsc, i
  158. Set jsc = new jsCore
  159. jsc.Kind = core.Kind
  160. For Each i In core.Collection
  161. If IsObject(core(i)) Then
  162. Set jsc(i) = ColClone(core(i))
  163. Else
  164. jsc(i) = core(i)
  165. End If
  166. Next
  167. Set ColClone = jsc
  168. End Function
  169. End Class
  170. Function jsObject
  171. Set jsObject = new jsCore
  172. jsObject.Kind = JSON_OBJECT
  173. End Function
  174. Function jsArray
  175. Set jsArray = new jsCore
  176. jsArray.Kind = JSON_ARRAY
  177. End Function
  178. Function toJSON(val)
  179. toJSON = (new jsCore).toJSON(val)
  180. End Function
  181. %>