asp datatypes converted to JSON

Discussion in 'ASP General' started by michal, Apr 26, 2007.

  1. michal

    michal Guest

    michal, Apr 26, 2007
    #1
    1. Advertisements

  2. http://fabiankoehler.de/wdb/2007/04/26/generate-json-from-asp-datatypes/

    Ummm - ASP is not a language with datatypes. Perhaps you meant
    "vbscript". You also seem to forget that ASP can use other scripting
    languages such as jscript. You might want to update your page.
     
    Bob Barrows [MVP], Apr 26, 2007
    #2
    1. Advertisements

  3. michal

    michal Guest

    thanks ive changed that...
     
    michal, Apr 27, 2007
    #3
  4. michal

    Justin Piper Guest

    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 = 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 = GetRef("Repr::Err")
    End Select

    If IsEmpty(r) Then Repr = TypeName(val) Else Repr = r(val)
    End Function

    Then simple specialized functions for each data type can be defined:

    Function [Repr::Integer] (val)
    [Repr::Integer] = "CInt(" & val & ")"
    End Function

    Function [Repr::Date] (val)
    [Repr::Date] = "#" & val & "#"
    End Function

    Function [Repr::String] (val)
    [Repr::String] = """" _
    & Replace(val, """", """""") _
    & """"
    End Function

    Function [Repr::Boolean] (val)
    [Repr::Boolean] = 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()] = "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] = "New Foo"
    End Function

    You should be able to use a similar strategy to emit JSON-encoded data.
     
    Justin Piper, Apr 27, 2007
    #4
  5. michal

    michal Guest

    michal, Apr 27, 2007
    #5
  6. michal

    michal Guest

    hi justin,
    thanks really for your reply, cause as i can see i can still discover
    something new in Vbscript... i havent seen the getRef function yet..
    its awesome.. will play around and this might be a good refactoring
    suggestion ;)
    thanks!
     
    michal, Apr 27, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.