Loading ...
Sorry, an error occurred while loading the content.

Re: VB implementation.

Expand Messages
  • Atif Aziz
    I am not aware of any VB6 implementation, but one of the interesting things you can do is use JavaScript directly from within VB6 without having to add any
    Message 1 of 5 , Nov 18, 2005
    • 0 Attachment
      I am not aware of any VB6 implementation, but one of the interesting things you can do is use JavaScript directly from within VB6 without having to add any external dependencies. You also have to bear in mind that due to lack of reflection and dynamic objects in VB6, it will be hard to have a native implementation anyhow. Your best bet would be to go with a dictionary-like object. By hosting JavaScript in VB6, however, you gain a whole bunch of benefits, first of which is that you can directly use the JavaScript JSON implementation from Douglas Crockford or rely on eval (when deserializing) to do the trick. Here's some VB6 code to illustrate my point:

      Sub Main()

      Dim Script: Set Script = CreateObject("MSScriptControl.ScriptControl.1")
      Script.Language = "JavaScript"

      Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
      Script.AddCode FSO.OpenTextFile("json.js").ReadAll()

      Dim Point
      Set Point = Script.CodeObject.JSON.parse("{ ""x"" : 1, ""y"" : 2 }")

      Debug.Print Point.x
      Debug.Print Point.y

      Debug.Print CallByName(Point, "x", VbGet)
      Debug.Print CallByName(Point, "y", VbGet)

      End Sub

      The MS ScriptControl object that is created at the beginning is an automation wrapper for scripting engines that support ActiveX Scripting, which covers JavaScript and VBScript. Note that this does not really add a dependency because the MS ScriptControl has been shipping with Windows since a while now (e.g., msscript.ocx ships and installs with Windows 2000). You should be able to package your application without it. Next, setting the Language property to "JavaScript" causes the control to fetch and host the JavaScript engine. I then assume you have json.js lying around somewhere on disk and load its contents into the scripting engine. At this point, the script is parsed and the JSON is ready to be used. All you need to do to call JavaScript functions is go over the CodeObject property. As you can see above, I get JSON.parse to return a JSON-ified point object with an x and y property. You can access those properties using the normal property-accesor syntax in VB. If case-sensitivity is an issue, as it can be in a case-insensitive language like VB and the IDE that can sometimes change casing behind your back, I suggest using the CallByName function where the exact case of the property you wish to read/write can be specified as a string. I have also shown how to do this above.

      Going the other way is harder, that is getting the JSON format for a VB object is much harder. Again, this is due to the limited way in which a VB object exposes itself to JavaScript since the languages are based on different capabilities. In other words, you won't get the expected results if you simply pass a VB object to JSON.stringify. But perhaps you can use the script control to bridge the gap. In other words, when it comes to JSON, just use JavaScript from VB. Create the objects you want to JSON-ify via JavaScript as script code.

      - Atif


      --- In json@yahoogroups.com, "malcontent123" <malcontent@u...> wrote:
      >
      > I am looking for a VB implementation of the JSON data format. I am
      > talking about VB 6 not .NET preferably in native VB code so I don't
      > have another DLL dependency in my project.
      >
      > If anybody knows of one I would very much appreciate hearing about it.
      >
      > Thank you very much.
      >




      [Non-text portions of this message have been removed]
    • MPCM
      I just tried that on my machine and it threw a lovely warning from Norton about Possible Malicious scripts running (standard error, not something actually
      Message 2 of 5 , Nov 18, 2005
      • 0 Attachment
        I just tried that on my machine and it threw a lovely warning from
        Norton about Possible Malicious scripts running (standard error, not
        something actually malicious). I'm not thinking that this would be a
        viable option if in fact this happens for others as well.

        Although you would not be able to pass native vb objects into a json
        format easily, it still would be very useful to be able to decode json
        into native variable types, and at at least be able to convert viable
        data formats into json.

        I'm sure for most of us, it's meant to make getting data from json
        speaking platforms (usually web based) to our vb apps more easily, and
        sending light messages back.

        Otherwise we are just doing some type of delimiting ourselves anyway,
        so a standard format, even if it doesn't match fully, would still be
        useful. I'll post back sometime next week, as I think I will be able
        to put something together to do this.

        --
        Matt
      • Atif Aziz
        Hi Matt, Ah, yes, I don t have Norton software on my PC so I did not get that warning on my machine when running the code I posted. I can see how this would a
        Message 3 of 5 , Nov 21, 2005
        • 0 Attachment
          Hi Matt,

          Ah, yes, I don't have Norton software on my PC so I did not get that
          warning on my machine when running the code I posted. I can see how this
          would a problem for some, and frankly, it was just to show how you can
          get up and running immediately with JSON in VB6 without investing too
          much time upfront. With proper encapsulation, you could always revisit
          the code and port it to a native implementation once you had the upper
          bits done, but I'm sure you are the better judge of how to approach the
          problem in your case. This could get a little off-topic, but my personal
          belief is that scripting security warnings are completely useless for
          most users and even administrators because they cannot make an informed
          decision about most of those warnings. The responsibility to inform,
          therefore, falls upon the application provider in the shape of README
          files (which are not really read that much anyhow) or making early tests
          at start-up. The latter approach allows one to tell the user or
          administrator that your software requires a particular technology that
          appears to have been blocked. This is happening today with ports and
          pop-ups in browser and so scripting in desktop applications does not
          appear to be an exception.

          >>
          Although you would not be able to pass native vb objects into a json
          format easily, it still would be very useful to be able to decode json
          into native variable types, and at at least be able to convert viable
          data formats into json.
          <<

          I totally agree. I just think that the lack of reflection and expando
          objects make it difficult to have such an implementation in VB6, but not
          impossible. I can imagine that CallByName for dynamic invocation of
          property accessors and TLBINF32.DLL for reflection would be your best
          friends here in a native implementation.

          - Atif

          -----Original Message-----
          From: json@yahoogroups.com [mailto:json@yahoogroups.com] On Behalf Of
          MPCM
          Sent: Friday, November 18, 2005 4:39 PM
          To: json@yahoogroups.com
          Subject: Re: [json] Re: VB implementation.

          I just tried that on my machine and it threw a lovely warning from
          Norton about Possible Malicious scripts running (standard error, not
          something actually malicious). I'm not thinking that this would be a
          viable option if in fact this happens for others as well.

          Although you would not be able to pass native vb objects into a json
          format easily, it still would be very useful to be able to decode json
          into native variable types, and at at least be able to convert viable
          data formats into json.

          I'm sure for most of us, it's meant to make getting data from json
          speaking platforms (usually web based) to our vb apps more easily, and
          sending light messages back.

          Otherwise we are just doing some type of delimiting ourselves anyway,
          so a standard format, even if it doesn't match fully, would still be
          useful. I'll post back sometime next week, as I think I will be able
          to put something together to do this.

          --
          Matt




          Yahoo! Groups Links
        Your message has been successfully submitted and would be delivered to recipients shortly.