'Please read disclaimer before using this code.
Option Explicit
Dim s As String ' Used for Storing JSON String
Dim jsep As String ' Used for Comma
Const jencl = """" ' Used for Double Quotes
Public Enum JDataTypes
JString
JNumber
JObject
End Enum
Public Sub addJArray(code As String, ar As Collection, JType As JDataTypes)
Dim obj As Variant
Dim Count As Integer
If s <> "" Then jsep = ","
s = s & jsep & jencl & code & jencl & ":["
Count = 0
For Each obj In ar
If s <> "" Then jsep = ","
If Count = 0 Then
Count = 1
Else
s = s & jsep
End If
If JType = JString Then
s = s & jencl & obj & jencl
ElseIf JType = JObject Then
s = s & obj.generateJSON
Else
s = s & obj
End If
Next
s = s & "]"
End Sub
Public Sub addJString(code As String, value As String)
If s <> "" Then jsep = ","
s = s & jsep & jencl & code & jencl & ":" & jencl & Replace(value, """", "\""") & jencl
End Sub
Public Sub addJInteger(code As String, value As Integer)
If s <> "" Then jsep = ","
s = s & jsep & jencl & code & jencl & ":" & value
End Sub
Public Sub addJDouble(code As String, value As Double)
If s <> "" Then jsep = ","
s = s & jsep & jencl & code & jencl & ":" & value
End Sub
Public Sub addJObject(code As String, value As JSONWriter)
If s <> "" Then jsep = ","
s = s & jsep & jencl & code & jencl & ":" & value.generateJSON
End Sub
Public Function generateJSON() As String
generateJSON = "{" & s & "}"
End Function
Option Explicit
Dim s As String ' Used for Storing JSON String
Dim jsep As String ' Used for Comma
Const jencl = """" ' Used for Double Quotes
Public Enum JDataTypes
JString
JNumber
JObject
End Enum
Public Sub addJArray(code As String, ar As Collection, JType As JDataTypes)
Dim obj As Variant
Dim Count As Integer
If s <> "" Then jsep = ","
s = s & jsep & jencl & code & jencl & ":["
Count = 0
For Each obj In ar
If s <> "" Then jsep = ","
If Count = 0 Then
Count = 1
Else
s = s & jsep
End If
If JType = JString Then
s = s & jencl & obj & jencl
ElseIf JType = JObject Then
s = s & obj.generateJSON
Else
s = s & obj
End If
Next
s = s & "]"
End Sub
Public Sub addJString(code As String, value As String)
If s <> "" Then jsep = ","
s = s & jsep & jencl & code & jencl & ":" & jencl & Replace(value, """", "\""") & jencl
End Sub
Public Sub addJInteger(code As String, value As Integer)
If s <> "" Then jsep = ","
s = s & jsep & jencl & code & jencl & ":" & value
End Sub
Public Sub addJDouble(code As String, value As Double)
If s <> "" Then jsep = ","
s = s & jsep & jencl & code & jencl & ":" & value
End Sub
Public Sub addJObject(code As String, value As JSONWriter)
If s <> "" Then jsep = ","
s = s & jsep & jencl & code & jencl & ":" & value.generateJSON
End Sub
Public Function generateJSON() As String
generateJSON = "{" & s & "}"
End Function