Results 1 to 5 of 5

Thread: JSON to UDT

  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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: JSON to UDT

    1. No.. you need to declare a variable of that type, and assign the values using the individual elements of the variable.

    2. Simply declare one of the elements using your own Type (like Employee) rather than a standard data type (like Long).

    3. No, arrays (of any sort) cannot be populated in a declaration.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    372

    Re: JSON to UDT

    thanks for the info si,true about #3, i was a loose in my description. i guess i meant late, in one line, or something like that ala:

    Code:
    Dim r() As String: r = Split("dan,fred,bob", ",")
    
    msgbox  r(1)  '  fred

    ------


    i need a way to have dynamic properties.

    what would be a the best way to do that?

    i have considered options like xml, htmlDomDoc, etc.
    would the property bag be worth looking into?

    cannot use any forms, so i am limited in that way ( no webbrowsers )
    playing around with the scripting control, i noticed i can evaluate code and output a string. this seems to work in both vbScript and Jscript.

    can these values be 'bubble up' a non-string var into the VB6 app?

    i was also thinking about binary writing a new UDT into the compiled exe just before it ran, but that limits me to a single instance...


    does anyone have any idea/advice/warnings about any of these?

    appreciate all feedback.


    add a new module containing the tr

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: JSON to UDT

    Sorry about the delay, I was away for the holiday season.

    A PropertyBag (or perhaps a Collection) is the right kind of thing to use in this type of situation.

    Hopefully you've worked that out already, but if you need help with it, let us know.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    372

    Re: JSON to UDT

    thanks for the info.
    i was hoping for a more elaborate solution than what i ended up with, but for my needs it works just fine. i gave up on coercion, and use a variant for everything.

    i use the script control to evaluate the json expression everytime it is needed. not the fastest way to do things, but very reliable and simple.


    Code:
    Public Function Eval(code As String, Optional prop As String) As Variant
     Dim x As Variant
    Dim Script2 As New ScriptControl
    Script2.Language = "JScript"
    
    x = (Script2.Eval(code & "['" & prop & "']  "))
    Eval = CStr(x) 
    End Function
    
    'example:
    msgbox Eval( "({a:1,b:2,c:3})", "a") 'displays "1"
    if my project gets more complex, i will be sure to check out the property bag...

    thanks again!
    Last edited by rnd me; Jan 11th, 2008 at 06:09 PM.

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