Class JSON ^
Comes up with functionality for JSON ( to use within ASP. Correct escaping of characters, generating JSON Grammer out of ASP datatypes and structures Some examples (all use the toJSON() method but as it is the class' default method it can be left out): <%
'simple number
output = (new JSON)("myNum", 2, false)
'generates {"myNum": 2}

'array with different datatypes
output = (new JSON)("anArray", array(2, "x", null), true)
'generates "anArray": [2, "x", null]
'(note: the last parameter was true, thus no surrounding brackets in the result)
Version: 1.5
Author: Michal Gabrukiewicz (gabru at, Michael Rebec on 2007-04-26 12:46
Last modified: 10.07.2008 09:30:44

Properties ^

Name Type Description
recordsetPaging bool indicates if only the current page should be processed on paged recordsets. e.g. would return only 10 records if RS.pagesize is set to 10. default = false (means that always all records are processed) 
toResponse bool should the generated representation be written directly to the response (using response.write)? default = false 

Methods ^

takes a given string and makes it JSON valid
all characters which needs to be escaped are beeing replaced by their unicode representation according to the RFC4627#2.5 -

val [string]
value which should be escaped
Returns [string]
JSON valid string
generates a representation of a name value pair in JSON grammer
It generates a name value pair which is represented as {"name": value} in JSON. the generation is fully recursive. Thus the value can also be a complex datatype (array in dictionary, etc.) e.g. set j = new JSON
j.toJSON "n", array(RS, dict, false), false
j.toJSON "n", array(array(), 2, true), false

name [string]
name of the value (accessible with javascript afterwards). leave empty to get just the value
val [variant, int, float, array, object, dictionary, recordset]
value which needs to be generated. Conversation of the data types is as follows:
  • ASP datatype -> JavaScript datatype
  • nothing, null -> null
  • int, double -> number
  • string -> string
  • boolean -> bool
  • array -> array
  • dictionary -> Represents it as name value pairs. Each key is accessible as property afterwards. json will look like"name": {"key1": "some value", "key2": "other value"}
  • multidimensional array -> Generates a 1-dimensional array (flat) with all values of the multidimensional array
  • recordset -> array where each row of the recordset represents a field of the array. Each array field has properties according to the column names of the recordset (lowercase!) e.g toJSON("r", RS, false) can be accessed afterwards with r[0].id
  • request object -> every property and collection (cookies, form, querystring, etc) of the asp request object is exposed as an item of a dictionary. Property names are lowercase. e.g. servervariables.
  • object -> name of the type (if unknown type) or all its properties (if class implements reflect() method)
  • Implement a reflect() function if you want your custom classes to be recognized. The function must return a dictionary where the key holds the property name and the value its value. Example of a reflect function within a User class which has firstname and lastname properties function reflect()
        set reflect = server.createObject("scripting.dictionary")
        reflect.add "firstname", firstname
        reflect.add "lastname", lastname
    end function
    Example of how to generate a JSON representation of the asp request object and access the HTTP_HOST server variable in Javascript: <script>alert(<%= (new JSON)(empty, request, false) %>.servervariables.HTTP_HOST);</script>
    nested [bool]
    indicates if the name value pair is already nested within another? if yes then the {} are left out.
    Returns [string]
    returns a JSON representation of the given name value pair (if toResponse is on then the return is written directly to the response and nothing is returned)