In my current ASP classic project, I have integrated the JScript JSON class available at this link. This class can interact with both VBScript and JScript, closely resembling the code provided on json.org. However, due to team manager's requirement, I must utilize VBScript for this project.
The JSON parsing works well for primitives and classes defined within ASP. But now, I need to incorporate Dictionary objects, which as far as I know, are only accessible through COM interop (using
Server.CreateObject("Scripting.Dictionary")
). I have a class named ProductInfo that represents a product in the following structure: (ProductInfo.class.asp)
<%
Class ProductInfo
Public ID
Public Category
Public PriceUS
Public PriceCA
Public Name
Public SKU
Public Overview
Public Features
Public Specs
End Class
%>
The Specs
property within ProductInfo is a Dictionary of key-value pairs. Here's how I attempt to serialize it: (product.asp)
<%
dim oProd
set oProd = new ProductInfo
' ... fill in properties
' ... output appropriate headers and stuff
Response.write( JSON.stringify( oProd ) )
%>
When passing an instance of ProductInfo to JSON.Stringify, the resulting JSON contains an empty object for the Specs property:
{
"id": "1547",
"Category": {
"id": 101,
"Name": "Category Name",
"AlternateName": "",
"URL": "/category_name/",
"ParentCategoryID": 21
},
"PriceUS": 9.99,
"PriceCA": 11.99,
"Name": "Product Name",
"SKU": 3454536,
"Overview": "Lorem Ipsum dolor sit amet..",
"Features": "Lorem Ipsum dolor sit amet..",
"Specs": {}
}
Instead of an empty object, I would like the JSON to display actual key-value pairs for Specs. My assumption is that the issue lies within the JSON library implementation (json2.asp).
After analyzing the code snippet where the problem might occur, it seems to me that the library assumes all objects inherit from the Object class, which may not be true for COM objects used in ASP.
Update: Although the specifics might not impact the answer, I anticipate a web client fetching the JSON representation of this object or a collection thereof via HTTP.
tl;dr The question: How can I ensure that Scripting.Dictionary outputs correctly as JSON without returning an empty string? Would creating a custom Dictionary class in VBScript be necessary for it to behave like a standard object in ASP?