Generate JSON from VBScript (ASP) datatypes

-- ASP 2010. 11. 29. 11:36
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

JSON을 ASP에서 사용할 수 있는 방법에 대해 소개한 글이다.
웹서비스 등과 함께 자주 등장하곤 하는 것이 JSON인데, 관련 프로젝트를 할 경우 참고해볼만 하다.


When working with JSON it's nice to have a generator for your language which transforms all the datatypes from your chosen programming language into the JSON grammar so that you can use them within javascript. For a lot of popular languages it's done already (i suppose) but I haven't found one for classic ASP .. so here it comes. The following example quickly demonstrates what the goal is:

 
ASP:
  1. set RS = getRecordset("SELECT * FROM table")
  2. response.write((new JSON).toJSON("rows", RS))

A simple usage of JSON normally is that you create a page which outputs data as JSON as the only response. This page is called later from another page and the returned data is used within a javascript function (known as callback). So the snippet above gets some data from the database and stores it in an adodb.recordset which is passed then to the JSON generator and the result is printed on the page. The consuming page would like to access the data now as it originally was within the recordset. like this:

 
JAVASCRIPT:
  1. function callback(rows){
  2.    for(i = 0; i <rows.length; i++){
  3.       alert(rows[i].columName);
  4.    }
  5. }

read on to get the details...

If you are already impressed by the short example then you will save a lot of time from now on when working with JSON. I have written a class which handles the whole conversation of classic ASP datatypes to JSON grammar. In particular an ASP boolean is recognized as a boolean within javascript, an integer as a number, an array as an array, a recordset as collection, etc. I will come back to the first example later but first another example:

 
ASP:
  1. <script>
  2.    alert(<%= (new JSON).toJSON("foo", array(true, 1, ""), false)%>.foo[0]);
  3. </script>

This short snippet displays an alert with true. As you can see we passed an ASP variable which has been recognized later by javascript. In this very example we even pass an array with 3 different datatypes (boolean, int and string). Those are all accessible within javascript.

Even nesting is fully supported. So array within arrays, dictionaries within arrays and vice versa. Mixing is allowed in every way you imagine. This example demonstrates what i am talking about:

 
ASP:
  1. <%
  2. set d = server.createObject("scripting.dictionary")
  3. d.add"en", "sausage"
  4. d.add"de", array("egal", "wurst")
  5. %>
  6. <script>
  7.    alert(<%= (new JSON).toJSON("foo", array(d), false)%>.foo[0].de[1]);
  8. </script>

We've created a dictionary (which consists of two value pairs - one holds just a string (sausage) and the other an array (egal, wurst)) and we've added this into another array which is the value of "foo". After toJSON has generated the JSON string we can access the whole structure and the alertbox says "wurst".

Now back to the example of the introduction. We can even pass whole recordsets to the generator which will generate a datastructure as followed:

JSON representation for ADODB.recordset

a recordset with two columns ID and LASTNAME will be converted into a javascript array where each field represents a row in the field and the row provides properties which are named the same as the columns within the recordset. That means that iterating through data within javascript is not a mission anymore ... look at this example:

 
ASP:
  1. <script>
  2.    alert(<%= (new JSON).toJSON("data", getRecordset("SELECT id, lastname FROM table"), false)%>.data[0].lastname);
  3. </script>

We transfered the whole adodb.recordset from ASP to javascript using JSON. How cool is that!?

Custom classes

If you create your own classes within VBScript then you might like the automatic conversion of your objects into a JSON representation. As VBScript does not support introspection (reflection) it is necessary to built an own work around. If you want the JSON class to recognize your custom objects it is necessary to implement a reflect() method within the custom type. reflect() must return a dictionary with all properties where each key is the property name and the value is its value. Values can be all kind of types (because its resolved recursively anyway). The following example shows a "Person" class which implements reflect() and therefore can be used within the JSON generator:

 
ASP:
  1. class Person
  2.     public firstname    ''[string] firstname
  3.     public lastname ''[string] lastname
  4.     public favNumbers   ''[array] persons favorite numbers
  5.    
  6.     publicfunction reflect()
  7.         set reflect = server.createObject("scripting.dictionary")
  8.         with reflect
  9.             .add"firstname", firstname
  10.             .add"lastname", lastname
  11.             .add"favNumbers", favNumbers
  12.         endwith
  13.     endfunction
  14. endclass

The following example access the properties of the VBScript Person class within JavaScript (as it would be a JavaScript object).

 
ASP:
  1. <%
  2. set p = new Person
  3. p.firstname = "John"
  4. p.lastname = "Doe"
  5. p.favNumbers = array(2, 7, 234)
  6. %>
  7. <script>
  8.    alert(<%= (new JSON).toJSON("p", p, false)%>.p.favNumbers[0]);
  9. </script>

Update 10.07.2008: as the toJSON() method has been defined as default method it can be used much quicker by leaving the methodname out:

 
ASP:
  1. <script>
  2.    alert(<%= (new JSON)("root", "some value", false)%>.root);
  3. </script>

Those examples are really straight forward and should just demonstrate how to use the generator. Normally you don't really deal with that as a client, you'd rather have some nice classes which do all this stuff for you. In another article I will demonstrate how to combine ASP, JSON and AJAX in a nice and effective way...

The download of the class is at the bottom. Here is a list of the features..

  • Transforms various datatypes from ASP to Javascript using JSON
  • Escapes all characters which needs to be escaped according to JSON's RFC
  • Recursively inspects the values so nesting of values is supported
  • Results can be returned or (for better performance) written directly to the response

Have fun with it. There are other article related to this topic which might be interested for you:

Download latest JSON ASP utility class:
JSON 1.5.1(JSON documentation)

License:
Use it for whatever you want but be sure to leave the credits to the creator(s) within the code. If you don't change it you can get always the latest update here ;)

출처 :
http://www.webdevbros.net/2007/04/26/generate-json-from-asp-datatypes/

posted by 어린왕자악꿍