Results 1 to 5 of 5

Thread: JSON to UDT

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    372

    JSON to UDT

    hey everyone-
    i havent been on much, as i have been working with javascript a lot lately.

    i have the problem of converting it's json objects into the UDTs.

    i wrote the following js snippit to help out.

    it converts any javascript object into a UDT template.

    Code:
    //   JAVASCRIPT CODE
    Object.prototype.vbType= function (typeName) {var jsOb = [Date, String, Array, Object, RegExp, Boolean, Number, Error];
    function obMap(ob){ var r=[]; var i=0; for(var z in ob){if(ob.hasOwnProperty(z))r[i++]=[z,ob[z]]};return r};
    function getVB(v) {var bType = typeof v;var vbTypes = {number:"Double", 'function':"", object:""};if (parseInt(v) === v) {return "Long";}
    if ("undefined string date number boolean ".indexOf(bType)) {if (vbTypes[bType]) {var j = vbTypes[bType];return j[0].toUpperCase() + j.substr(1);}if (v.getTime) {return "Date";}
    if (bType === "object") {return "Variant";}}return bType[0].toUpperCase() + bType.substr(1);}
    t = obMap(this).map(function (a) {tofu = typeof a[1];if (a[0] == "prototype") {return "";}
    if (tofu == "function") {return "";}if (tofu == "object") {if (a[1].getTime) {tofu = "Date";}
    if (a[1].pop) {return [a[0] + "( " + (a[1].length + 1) + " ) ", "as", "Variant"].join("  ");}}if (tofu == "object") {tuu = [a[0], "as", "Variant"].join("  ");} else {tuu = [a[0], "as", getVB(a[1])].join("  ");}return tuu;}).sort().join("\n	").replace(/\n\t\n\t/gm, "");
    var tpName = typeName || String(this.type);var hd = "\nPublic Type " + tpName + "\n	";var buf = hd + t + "\nEnd Type	'" + tpName + "\n";return buf || new Error(buf);}
    
    // for example:
    myJSON= {    office: 'local', 
                    age: 21, 
                 format: 'text',
                 joined: (new Date()),
                  flags: [2,7,4,9,"A4"],
                   dept: 'Sales' }
    
    alert(  myJSON.vbType("Employee") )
    
    /*  msgbox's : 
    Public Type Employee
    	age  as  Long
    	dept  as  String
    	flags( 6 )   as  Variant
    	format  as  String
    	joined  as  Date
    	office  as  String
    End Type	'Employee                          */
    as you can see, it seems to work ok, but i was wondering about a few thing about VB6:

    1. is there any way to assign values within the UDT?
    2. how do i made nested data structures?
    -do i need to 'flatten' the UDTs and define the top-down?
    3. can an array be populated with anything besides strings at time of dim?


    thanks for any advice!

    oh, if anyone can use the code above, help yourself to it (tested in firefox)
    Last edited by rnd me; Dec 22nd, 2007 at 09:16 AM. Reason: clicked wrong button

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width