Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #15686

Re: String Processing in JavaScript

From Tim Streater <timstreater@greenbee.net>
Newsgroups comp.lang.javascript
Subject Re: String Processing in JavaScript
Date 2012-09-04 10:06 +0100
Organization Personal Software
Message-ID <timstreater-CFC393.10065704092012@news.individual.net> (permalink)
References <rgf248la5ap1dusauc9ek39jasff47mht4@4ax.com> <k22pe2$cmk$1@news.albasani.net> <358a48l9oedo25o1mbqp0nhj0ph9lco16e@4ax.com> <k23c3l$ldn$1@news.albasani.net>

Show all headers | View raw


In article <k23c3l$ldn$1@news.albasani.net>,
 Stefan Weiss <krewecherl@gmail.com> wrote:

> On 2012-09-03 23:27, Gene Wirchenko wrote:
> > On Mon, 03 Sep 2012 19:29:38 +0200, Stefan Weiss <krewecherl@gmail.com> 
> > wrote:
> > > From your other posts, it appears that you're in control of both backend
> > > and client code. If that's the case, you may want to consider one of the
> > > many standardized data transfer formats.
> > 
> >      I am.
> 
> Then I would recommed looking at JSON first. If you know ECMAScript
> object and array literals, you know 95% of JSON already. There are some
> additional syntactic restrictions, but since you're using native/library
> methods on both ends, you don't have to worry about that.
> 
> In the simplest case, server-to-client communication can look like this:
> 
> -- server (using Perl as example) --
>    my $data = {
>       str => "foo",
>       arr => [2, 4, 6],
>       nested => {
>          bar => {
>             value => "baz",
>          }
>       },
>    };
>    print encode_json $data;
> 
> -- client --
>    var data = JSON.parse(xhr.responseText);
>    console.log(data.nested.bar.value);
> 
> If your target clients can't be guaranteed to have native support for
> JSON, you'll want to include a JS library as a fallback. I use a
> minified version of the json2.js script by Douglas Crockford:
> 
>   https://github.com/douglascrockford/JSON-js/blob/master/json2.js
> 
> Further information:
> 
>   Gentle introduction (usual wiki caveats apply)
>   http://en.wikipedia.org/wiki/JSON
> 
>   Author's site (with numerous links and some diagrams)
>   http://www.json.org/
> 
>   The standard
>   http://www.ietf.org/rfc/rfc4627.txt
> 
> >      There is the issue of enough information to use them.  I have not
> > done well trying to find that.
> 
> With all due respect, I find it hard to believe that you had trouble
> finding "enough information" about XML or JSON. Did you even try?

Why fiddle about with this when you can structure the data using simple 
delimiters as the OP has been suggesting. I use hex 0, 1, and 2 as 
delimiters which you can make e.g. as follows:

  var  delim0 = String.fromCharCode(0);
  var  delim1 = String.fromCharCode(1);
  var  delim2 = String.fromCharCode(2);

and then it is simplicity itself to take the results back from your ajax 
call and use e.g.:

  var  items = results.split (delim0);

to give you an array of items, any of which can then be further split:

  var  subItems = items[0].split (delim1);

and so on.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

String Processing in JavaScript Gene Wirchenko <genew@ocis.net> - 2012-08-31 15:56 -0700
  Re: String Processing in JavaScript Mike Winter <usenet@michael-winter.me.invalid> - 2012-09-01 02:30 +0100
    Re: String Processing in JavaScript Gene Wirchenko <genew@ocis.net> - 2012-09-02 17:56 -0700
  Re: String Processing in JavaScript Jim T. <x@y.z> - 2012-08-31 22:30 -0400
    Re: String Processing in JavaScript "Mel Smith" <med_cutout_syntel@aol.com> - 2012-08-31 21:47 -0600
      Re: String Processing in JavaScript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-09-01 08:51 +0000
        Re: String Processing in JavaScript "Mel Smith" <med_cutout_syntel@aol.com> - 2012-09-01 08:38 -0600
      Re: String Processing in JavaScript Gene Wirchenko <genew@ocis.net> - 2012-09-02 18:04 -0700
        Re: String Processing in JavaScript "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-09-03 16:30 +0000
        Re: String Processing in JavaScript Mike Winter <usenet@michael-winter.me.invalid> - 2012-09-03 21:23 +0100
          Re: String Processing in JavaScript Gene Wirchenko <genew@ocis.net> - 2012-09-03 14:26 -0700
  Re: String Processing in JavaScript Stefan Weiss <krewecherl@gmail.com> - 2012-09-03 19:29 +0200
    Re: String Processing in JavaScript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-03 21:45 +0200
    Re: String Processing in JavaScript Gene Wirchenko <genew@ocis.net> - 2012-09-03 14:27 -0700
      Re: String Processing in JavaScript Stefan Weiss <krewecherl@gmail.com> - 2012-09-04 00:48 +0200
        Re: String Processing in JavaScript Gene Wirchenko <genew@ocis.net> - 2012-09-03 17:20 -0700
        Re: String Processing in JavaScript Tim Streater <timstreater@greenbee.net> - 2012-09-04 10:06 +0100
          Re: String Processing in JavaScript Ross McKay <au.org.zeta.at.rosko@invalid.invalid> - 2012-09-04 20:02 +1000
          Re: String Processing in JavaScript Gene Wirchenko <genew@ocis.net> - 2012-09-04 08:47 -0700

csiph-web