I am working on an ASP classic project where I implemented the JScript JSON class found here . It is capable of interacting with both VBScript and JScript, and this is almost certainly the code presented in json.org . I have to use VBScript for this project by my team manager.
It works great on primitives and classes defined in ASP. But I need vocabulary objects, which, as far as I know, are accessible only through COM interaction. (via Server.CreateObject("Scripting.Dictionary") ) I have the following class, which is a product: (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 is a dictionary of words: pairs of meanings. This is how I serialize it: (product.asp)
<% dim oProd set oProd = new ProductInfo ' ... fill in properties ' ... output appropriate headers and stuff Response.write( JSON.stringify( oProd ) ) %>
When I pass the ProductInfo instance to JSON.Stringify (as shown above), I get the following:
{ "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": {} }
As you can see, the Specs property is an empty object. I believe the string JSON method knows that the Specs property is an object, so it adds {} to the JSON string around the string output. In this case, this is an empty string. What I expect is to show, however, not an empty object. See below:
"Specs": { "foo":"bar", "baz":1, "etc":"..." }
I believe the problem area of โโthe JSON library is here: (json2.asp)
// Otherwise, iterate through all of the keys in the object. for (k in value) { if (Object.hasOwnProperty.call(value, k)) { v = str(k, value); if (v) { partial.push(quote(k) + (gap ? ': ' : ':') + v); } } }
I maintain that the problem with the above code is that it assumes that all objects inherit from the Object class. (The one that hasOwnProperty provides). However, I believe that the likelihood that COM objects do not inherit from the Object class - or at least the same Object class. Or at least do not implement any interface necessary to execute for ... in on them.
Update: So far I feel that the question is impossible to answer; - I expect some web client to request (via http) a JSON representation of this object or a collection of this object.
tl; dr Question: What should I do so that Scripting.Dictionary can be correctly output as JSON instead of rejecting and returning only an empty string? Do I need to โreinvent the wheelโ and write my own Dictionary class in VBScript that acts like a regular object in ASP?