[quoted text, click to view] On Thu, 26 Apr 2007 11:30:49 -0500, michal <mgabru@gmail.com> wrote:
> hi guys,
> i thought you might be interested in a nice JSON class which converts
> ASP datatypes (basic datatypes, dictionaries, recordsets, ...) into
> JSON so that javascript can easily understand it ...
> you'll find the demonstration and the download here
>
http://fabiankoehler.de/wdb/2007/04/26/generate-json-from-asp-datatype= s/
You might find refactoring that generateValue function into several
smaller functions worthwhile. I have a function I use for debugging
which looks like this:
Function Repr(val)
Dim r
Select Case False
' For Scalars and objects other than Err, the name of the Repr=
' function can be derived directly from the value's type
Case IsObject(val), val Is Err
On Error Resume Next
Set r =3D GetRef("Repr::" & TypeName(val))
On Error Goto 0
' The type of the Err object is "Object", so its Repr function=
' must be manually chosen
Case Else
Set r =3D GetRef("Repr::Err")
End Select
If IsEmpty(r) Then Repr =3D TypeName(val) Else Repr =3D r(val)
End Function
Then simple specialized functions for each data type can be defined:
Function [Repr::Integer] (val)
[Repr::Integer] =3D "CInt(" & val & ")"
End Function
Function [Repr::Date] (val)
[Repr::Date] =3D "#" & val & "#"
End Function
Function [Repr::String] (val)
[Repr::String] =3D """" _
& Replace(val, """", """""") _
& """"
End Function
Function [Repr::Boolean] (val)
[Repr::Boolean] =3D CStr(val)
End Function
Function [Repr::Variant()] (val)
With CreateObject("Scripting.Dictionary")
Dim elm: For Each elm In val : .Add .Count, Repr(elm) : Next
[Repr::Variant()] =3D "Array(" & Join(.Items, ", ") & ")"
End With
End Function
This allows you to easily define Repr functions for user-defined types:
Class Foo : End Class
Function [Repr::Foo] (val)
[Repr::Foo] =3D "New Foo"
End Function
You should be able to use a similar strategy to emit JSON-encoded data.
-- =
Justin Piper
Bizco Technologies