ASP JSON Object

ASP JSON Object

Coding Archive

Coding Archive

ASP JSON Object

You can pull and send json data with the following json object code.

<%

Const JSON_OBJECT    = 0
Const JSON_ARRAY    = 1

Class jsCore
    Public Collection
    Public Count
    Public QuotedVars
    Public Kind ' 0 = object, 1 = array

    Private Sub Class_Initialize
        Set Collection = CreateObject("Scripting.Dictionary")
        QuotedVars = True
        Count = 0
    End Sub

    Private Sub Class_Terminate
        Set Collection = Nothing
    End Sub

    ' counter
    Private Property Get Counter 
        Counter = Count
        Count = Count + 1
    End Property

    ' - data maluplation
    ' -- pair
    Public Property Let Pair(p, v)
        If IsNull(p) Then p = Counter
        Collection(p) = v
    End Property

    Public Property Set Pair(p, v)
        If IsNull(p) Then p = Counter
        If TypeName(v) <> "jsCore" Then
            Err.Raise &hD, "class: class", "Incompatible types: '" & TypeName(v) & "'"
        End If
        Set Collection(p) = v
    End Property

    Public Default Property Get Pair(p)
        If IsNull(p) Then p = Count - 1
        If IsObject(Collection(p)) Then
            Set Pair = Collection(p)
        Else
            Pair = Collection(p)
        End If
    End Property
    ' -- pair
    Public Sub Clean
        Collection.RemoveAll
    End Sub

    Public Sub Remove(vProp)
        Collection.Remove vProp
    End Sub
    ' data maluplation

    ' encoding
    Function jsEncode(str)
        Dim charmap(127), haystack()
        charmap(8)  = "\b"
        charmap(9)  = "\t"
        charmap(10) = "\n"
        charmap(12) = "\f"
        charmap(13) = "\r"
        charmap(34) = "\"""
        charmap(47) = "\/"
        charmap(92) = "\\"

        Dim strlen : strlen = Len(str) - 1
        ReDim haystack(strlen)

        Dim i, charcode
        For i = 0 To strlen
            haystack(i) = Mid(str, i + 1, 1)

            charcode = AscW(haystack(i)) And 65535
            If charcode < 127 Then
                If Not IsEmpty(charmap(charcode)) Then
                    haystack(i) = charmap(charcode)
                ElseIf charcode < 32 Then
                    haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
                End If
            Else
                haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
            End If
        Next

        jsEncode = Join(haystack, "")
    End Function

    ' converting
    Public Function toJSON(vPair)
        Select Case VarType(vPair)
            Case 0    ' Empty
                toJSON = "null"
            Case 1    ' Null
                toJSON = "null"
            Case 7    ' Date
                ' toJSON = "new Date(" & (vPair - CDate(25569)) * 86400000 & ")"    ' let in only utc time
                toJSON = """" & CStr(vPair) & """"
            Case 8    ' String
                toJSON = """" & jsEncode(vPair) & """"
            Case 9    ' Object
                Dim bFI,i 
                bFI = True
                If vPair.Kind Then toJSON = toJSON & "[" Else toJSON = toJSON & "{"
                For Each i In vPair.Collection
                    If bFI Then bFI = False Else toJSON = toJSON & ","

                    If vPair.Kind Then 
                        toJSON = toJSON & toJSON(vPair(i))
                    Else
                        If QuotedVars Then
                            toJSON = toJSON & """" & i & """:" & toJSON(vPair(i))
                        Else
                            toJSON = toJSON & i & ":" & toJSON(vPair(i))
                        End If
                    End If
                Next
                If vPair.Kind Then toJSON = toJSON & "]" Else toJSON = toJSON & "}"
            Case 11
                If vPair Then toJSON = "true" Else toJSON = "false"
            Case 12, 8192, 8204
                toJSON = RenderArray(vPair, 1, "")
            Case Else
                toJSON = Replace(vPair, ",", ".")
        End select
    End Function

    Function RenderArray(arr, depth, parent)
        Dim first : first = LBound(arr, depth)
        Dim last : last = UBound(arr, depth)

        Dim index, rendered
        Dim limiter : limiter = ","

        RenderArray = "["
        For index = first To last
            If index = last Then
                limiter = ""
            End If 

            On Error Resume Next
            rendered = RenderArray(arr, depth + 1, parent & index & "," )

            If Err = 9 Then
                On Error GoTo 0
                RenderArray = RenderArray & toJSON(Eval("arr(" & parent & index & ")")) & limiter
            Else
                RenderArray = RenderArray & rendered & "" & limiter
            End If
        Next
        RenderArray = RenderArray & "]"
    End Function

    Public Property Get jsString
        jsString = toJSON(Me)
    End Property

    Sub Flush
        If TypeName(Response) <> "Empty" Then 
            Response.Write(jsString)
        ElseIf WScript <> Empty Then 
            WScript.Echo(jsString)
        End If
    End Sub

    Public Function Clone
        Set Clone = ColClone(Me)
    End Function

    Private Function ColClone(core)
        Dim jsc, i
        Set jsc = new jsCore
        jsc.Kind = core.Kind
        For Each i In core.Collection
            If IsObject(core(i)) Then
                Set jsc(i) = ColClone(core(i))
            Else
                jsc(i) = core(i)
            End If
        Next
        Set ColClone = jsc
    End Function

End Class

Function jsObject
    Set jsObject = new jsCore
    jsObject.Kind = JSON_OBJECT
End Function

Function jsArray
    Set jsArray = new jsCore
    jsArray.Kind = JSON_ARRAY
End Function

Function toJSON(val)
    toJSON = (new jsCore).toJSON(val)
End Function
%>

ASP JSON Object video tutorial, Coding Archive video tutorial, ASP JSON Object Coding Archive tutorial, ASP JSON Object Coding Archive watch video, ASP JSON Object Coding Archive HD Video, You can pull and send json data with the following json object code. Coding Archive sample video, You can pull and send json data with the following json object code. Coding Archive video code , ASP JSON Object learning video, ASP JSON Object coding video

Coding Archive

In this area, I show advanced ASP - Active Server Pages coding and working styles and coding with MySQL, MSSQL, Oracle and other databases. - See All
Classic ASP XMLHTTP Url Html Code Viewer
You can do your transactions quickly with the Classic ASP code, where you can pull the html code structure from the url you connect with XMLHTTP.
ASP MySql Database Random Records
For Classic Asp MySql database random data this code is for you! You can use it in all your projects for Mysql database random data that works smoothly and fast.
Classic ASP CDO Message Mail Component Code
You can use the code I have given to send mail without any problems with the CDO component. Have peace of mind with this CDO code that works smoothly in your projects :)
Classic ASP XMLHTTP Set Timeout Code
With Classic ASP you can set the connection time limit when retrieving xml data. With the code I gave you, you can end long-term connection problems.
ASPJpeg Draw Image
You can examine the example of placing a logo on a photo with the Classic Asp ASPJpeg component with this code. You can use the Classic ASP AspJpeg component code piece that will always work for you in all your works.
Formatnumber ASP Classic Function
You can examine the Classic ASP formatnumber function in detail with the code I will give you.
Classic ASP ASPJpeg Component Full Code
I am giving all relevant code structure for writing on a photo with aspjpeg. I'm sure this code structure will be very useful for you.
Classic ASP Include File Code
For Classic ASP, you can benefit from the code structure I have given to call another asp file into the page.
Classic ASP Mysql Record Limit
If you want to set limits with classic asp in mysql table listing, you can use the code structure I have given.
Jquery Load Div From Another Page
This code will be useful for calling an external page with jquery inside the div tag.
Total Number of Records in Mysql Database with a Single Code
Asp Mysql Recordcount. This code is for you to quickly query the total number of data in the Mysql database table.
First Day and Last Day of the Month Date Function
With this asp vbscript function you can find the first and last day of a month. It is a very fast function that will work in your database filtering operations.