Objects from streams

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RebelGeekz

    Objects from streams

    I am trying to create objects sent as streams over the wire but I am
    no expert in javascript, can anybody lend me a hand please?

    // this works, no problem at all
    client = {name:'John Doe', age:35};
    alert(client.na me);

    // this doesn't
    stream = "{name:'Jan e Doe', age:32}";
    person = eval(stream);
    alert(person.na me);

    The whole idea is to request data to a back-end, then the server will
    produce streams in the javascript format that will be used to create
    objects on the client side.

    The question is: how to use the stream as object initializer? I tried
    'eval' but didn't work, maybe I'm doing it wrong. Any other ideas?

    Thanks in advance
  • Martin Honnen

    #2
    Re: Objects from streams



    RebelGeekz wrote:
    [color=blue]
    > I am trying to create objects sent as streams over the wire but I am
    > no expert in javascript, can anybody lend me a hand please?
    >
    > // this works, no problem at all
    > client = {name:'John Doe', age:35};
    > alert(client.na me);
    >
    > // this doesn't
    > stream = "{name:'Jan e Doe', age:32}";
    > person = eval(stream);
    > alert(person.na me);[/color]

    If you use

    eval('person = ' + stream);

    it should work. The problem is that {} is used both to delimit object
    literals as well as statement blocks thus if you have some code starting
    with { it is considered the start of a block and not the start of an
    object literal.
    --

    Martin Honnen


    Comment

    • Rebel Geekz

      #3
      Re: Objects from streams

      Thanks Martin!


      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Objects from streams

        rebelgeekz@hotm ail.com (RebelGeekz) writes:
        [color=blue]
        > I am trying to create objects sent as streams over the wire but I am
        > no expert in javascript, can anybody lend me a hand please?
        >
        > // this works, no problem at all
        > client = {name:'John Doe', age:35};
        > alert(client.na me);
        >
        > // this doesn't
        > stream = "{name:'Jan e Doe', age:32}";
        > person = eval(stream);
        > alert(person.na me);[/color]

        You have found a use for eval, that isn't bad. Good job :) Anyway, the
        content of the string sent to eval, is assumed to be Javascript
        statements, not a single expression. When it sees a "{" in that
        context, it assumes that it is the start of a block. To avoid this,
        just wrap the expression in parentheses:
        person = eval("("+stream +")");
        or use the Function constructor:
        person = Function("retur n "+stream+";")() ;
        [color=blue]
        > The question is: how to use the stream as object initializer? I tried
        > 'eval' but didn't work, maybe I'm doing it wrong.[/color]

        Yep. :)

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Rebel Geekz

          #5
          Re: Objects from streams

          Thanks for the tip Lasse,

          I guess I would use it like this:

          // stream will be sent from the server
          stream = "Client = {name:'Jane Doe', age:32}";
          eval(stream);

          this way it would be easier for me to produce the streams from the
          server and use them in the client.

          I also found some code to convert the object to a stream like netscape
          object.ToSource (), really cool stuff.

          Thanks all



          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Rebel Geekz

            #6
            Re: Objects from streams

            Thanks again Martin,

            The whole idea is to make httpRequests to my data server which is going
            to produce javascript objects (jso), trying to implement an easier
            architecture for sharing objects accross boundaries.

            That way it would be easier to get/set objects from/to the data server
            and use them in my web forms. No bloated xml parsing at all ;-)

            With a simple call like this I could get any object I need:

            xmlhttp = new ActiveXObject(" Microsoft.XMLHT TP");
            xmlhttp.open("G ET",
            "http://myDataServer.co m/GetObject.asp?i nvoice=123456", true);
            xmlhttp.onready statechange=fun ction() {
            if (xmlhttp.readyS tate==4) {
            invoice = eval(xmlhttp.re sponseText);
            // do something with the invoice
            alert(invoice.t otal);
            }
            }
            xmlhttp.send(nu ll)

            then binding the object to the web form and sending it back to the
            server without having to submit/reload the page

            Sort of interactive web application with simple web services, if we can
            call it that way ;-)

            Don't know yet, just playing


            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Rebel Geekz

              #7
              Re: Objects from streams

              Here is a sample for those who've been following:

              Premium303 adalah wadah permainan dengan modal depo 10k via bank dan ewallet terlengkap hari ini, semua mamber dapat melakukan metode depo ini


              Feel free to share ideas ;-)

              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: Objects from streams

                Rebel Geekz <rebelgeekz@hot mail.com> writes:
                [color=blue]
                > Here is a sample for those who've been following:
                >
                > http://georgenava.com/samples/sodascript.html
                >
                > Feel free to share ideas ;-)[/color]

                It appears to be IE only, so I haven't tested it.

                Looking at the ToStream function, it has a problem with objects and
                strings.

                For objects, the properties can be any string, not only those that
                are valid identifiers, for the object
                {"foo bar":42}
                you will generate
                {foo bar:42}
                which is a syntax error. Even worse is the object
                {"foo:42,bar":4 3}
                which has only one property with the name "foo:42,bar ". You write
                it as
                {foo:42,bar:43}
                a completely different, but syntactically legal object.

                I recommend treating the key as a string and use the same transformation
                as on strings. Perhaps test whether the key is a valid identifier,
                /^[$_\w]+$/.test(key)&&!/^\d/.test(key), and only treat it as a string
                otherwise.

                That brings me to strings. You only replace backspace and quotes. You
                alos need to escape the non-printable characters, newlines etc. Try
                making this string into a stream: "hello\nwor ld". The newline is sent
                verbatim, and newlines are not allowed inside string literals.

                I made this recently:
                ---
                var escapes = ["b","t","n","v" ,"f","r"]

                function LZ(str,n) {
                n = n||2;
                str = String(str);
                while(str.lengt h<n) {str = "0"+str;}
                return str;
                }

                function stringToSource( string) {
                var chars = string.split("" );
                for (var i=0;i<chars.len gth;i++) {
                var code = chars[i].charCodeAt(0);
                if (code<32) {
                if (code >=8 && code <=13) {
                chars[i]="\\"+escape s[code-8];
                } else {
                chars[i]="\\x"+LZ(code. toString(16));
                }
                } else if (chars[i]=="\"" || chars[i]=="\\") {
                chars[i]="\\"+chars[i];
                } else if (code>=128 && code < 192) {
                chars[i]="\\x"+LZ(code. toString(16));
                } else if (code >= 256) {
                chars[i]="\\u"+LZ(code. toString(16),4) ;
                }
                }
                return "\""+chars.join ("")+"\"";
                }
                ---

                I guess there could be problems with RegExps too, but I haven't looked
                into it.

                Good luck.

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Steve van Dongen

                  #9
                  Re: Objects from streams

                  On 12 Jan 2004 08:05:21 -0800, rebelgeekz@hotm ail.com (RebelGeekz)
                  wrote:
                  [color=blue]
                  >I am trying to create objects sent as streams over the wire but I am
                  >no expert in javascript, can anybody lend me a hand please?
                  >
                  >// this works, no problem at all
                  >client = {name:'John Doe', age:35};
                  >alert(client.n ame);
                  >
                  >// this doesn't
                  >stream = "{name:'Jan e Doe', age:32}";
                  >person = eval(stream);
                  >alert(person.n ame);
                  >
                  >The whole idea is to request data to a back-end, then the server will
                  >produce streams in the javascript format that will be used to create
                  >objects on the client side.
                  >
                  >The question is: how to use the stream as object initializer? I tried
                  >'eval' but didn't work, maybe I'm doing it wrong. Any other ideas?[/color]

                  XML parsing isn't that bad...

                  Anyways, I wrote this quite a while ago and for some reason never used
                  it. I'm pretty sure it correctly handles all native Javascript
                  datatypes. (My test code is commented out at the bottom.) It might
                  help.

                  Regards,
                  Steve

                  function Serialize(obj)
                  {
                  if (typeof navigator != "undefined" && navigator.appNa me ==
                  'Netscape')
                  {
                  return( obj.toSource() );
                  }

                  var strSource = "";

                  if (typeof(obj) == "undefined" )
                  {
                  strSource = "undefined" ;
                  }

                  else if (obj == null)
                  {
                  strSource = "null";
                  }

                  else if (typeof(obj) == "boolean" || typeof(obj) == "number" ||
                  typeof(obj) == "function")
                  {
                  strSource = obj;
                  }

                  else if (typeof(obj) == "string")
                  {
                  strSource = "'" + obj.replace(/([\\'"])/g, "\\$1") + "'";
                  }

                  else if (typeof(obj) == "object")
                  {
                  if (obj.constructo r == Boolean)
                  {
                  strSource = "new Boolean(" + obj.valueOf() + ")";
                  }

                  else if (obj.constructo r == Number)
                  {
                  strSource = "new Number(" + obj.valueOf() + ")";
                  }

                  else if (obj.constructo r == String)
                  {
                  strSource = "new String(" + Serialize(obj.t oString()) +
                  ")";
                  }

                  else if (obj.constructo r == Date)
                  {
                  strSource = "new Date(" + obj.valueOf() + ")";
                  }

                  else if (obj.constructo r == Array)
                  {
                  strSource = "[";
                  for (var i=0; i<obj.length; i++)
                  strSource += Serialize(obj[i]) + ",";
                  if (i>0)
                  strSource = strSource.subst ring(0,
                  strSource.lengt h-1);
                  strSource += "]";
                  }

                  else if (obj.constructo r == RegExp)
                  {
                  strSource = obj.toString();
                  }

                  else if (obj.constructo r == Object)
                  {
                  var strSource = "{"
                  for (var key in obj)
                  strSource = strSource + "" + key + ":" +
                  Serialize(obj[key]) + ","
                  if (strSource.leng th > 1)
                  strSource = strSource.subst ring(0,
                  strSource.lengt h-1);
                  strSource += "}";
                  }
                  }

                  else
                  {
                  throw new Error(0, "Serialize encountered unexpected object
                  type: " + typeof(obj));
                  }

                  return strSource;
                  }

                  function Deserialize(str Source)
                  {
                  if (typeof strSource != "string")
                  throw new Error(0, "Deserializ e expects a string
                  argument");

                  var x;
                  return eval("x = " + strSource);
                  }

                  /*
                  var anObject = {
                  a: null,
                  b: 3442,
                  c: 'aks\\\'ldf8',
                  d: /te\st/,
                  e: true,
                  f: function () { return 't\est' },
                  g: [],
                  h: ['asdf','sdf',23 43],
                  i: new Boolean(),
                  j: new Number(-23),
                  k: new String('foo\\ba r'),
                  l: new Date(),
                  m: undefined,
                  n: { a: 'abc' }
                  }

                  var x = Deserialize(Ser ialize(anObject ));
                  var s = "";
                  for (var p in anObject)
                  {
                  s += p + ": " + anObject[p] + " == " + x[p] + "\n";
                  }
                  WScript.Echo(s) ;
                  */

                  Comment

                  • Willie Lau

                    #10
                    Re: Objects from streams

                    Please take a look at Douglas Crockford's JavaScript Object Notation
                    (JSON) at http://www.json.org

                    The following function may help with your needs.

                    parseJSON = function(jsonSt ring)
                    {
                    eval('function parseJSON(){ret urn ' + jsonString + ';}');
                    return parseJSON();
                    };

                    In your case, it allows your stream to remain as "{name:'Jan e Doe',
                    age:32}".
                    You can then assign to Client using:

                    Client = parseJSON(strea m);

                    Regards,
                    Willie Lau

                    Rebel Geekz <rebelgeekz@hot mail.com> wrote in message news:<4003060a$ 0$70302$7586835 5@news.frii.net >...[color=blue]
                    > Thanks for the tip Lasse,
                    >
                    > I guess I would use it like this:
                    >
                    > // stream will be sent from the server
                    > stream = "Client = {name:'Jane Doe', age:32}";
                    > eval(stream);
                    >
                    > this way it would be easier for me to produce the streams from the
                    > server and use them in the client.
                    >
                    > I also found some code to convert the object to a stream like netscape
                    > object.ToSource (), really cool stuff.
                    >
                    > Thanks all
                    >
                    >
                    >
                    > *** Sent via Developersdex http://www.developersdex.com ***
                    > Don't just participate in USENET...get rewarded for it![/color]

                    Comment

                    Working...