JSON?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

JSON?

Joe Armstrong (AL/EAB)
Re quoting and unquoting - I ran into this problem recently
so I have made two changes to the standard libraries (both changes will not break old code :-)

Change 1 - to io_lib.erl  print binaries containing strings as <<"abc">> as <<"abc">> and NOT
           <<97,98,99>>

             I posted a (buggy) version of this earlier - but now it appears to be correct

Change 2 - erl_scan.erl.  Accept <<' ...   '>> as an alternative way of entering binaries containing      
           strings.

          With this change I can write

              func() ->
                   <<'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                  "http://www.w3.org/TR/html4/strict.dtd">
                  <html>
                  <head>
                  <meta http-equiv="Content-Type"

                        ... dozens of lines with single and double quotes ...

                   '>>

            '>> is the binary delimiter - And there is NO quoting convention *within* the body
          of the binary. So if you want to write '>> within the binary you can't (horrors)
            (but who would want to anyway? - '>>)

<aside>

        I've also written a little parse transform. So that

           foo() ->
                {'$template', <<'<h1>${title}</h1>
                              <p>Written by: ${author} on ${date}
                              ..
                            '>>}

      Gets transformed into

                foo() -> [<<"<h1>">>,
                            "title",
                            <<"</h1>
                               <p>written by: ">>,
                            "author"
                          ...
                            ]

      Which is actually rather useful for mixing and embedding Erlang in HTML

</aside>

In my case a certain reluctance to using binaries has a lot to do with how they are printed and
how they are inputted. The above two conventions make life a *lot* easier.

<aside>
I had pondered the

        <<#flunkYStuff# ... #flunkyStuff#>>

Convention (old Unix people will understand this :-) - so I could enter *any* text within the
body of the binary without quoting. So you could write:

        <<#glurk# to quote a binary use <<' ... '>> or use the
                    <<#flunkyStuff# ... #flunkyStuff#>> convention
          #glurk#>>

Eureka - having written this down it actually looks less repulsive than I had imagined -
I shall add this tonight :-)

</aside>

 

 /Joe


           


> -----Original Message-----
> From: Jim Larson [mailto:jim]
> Sent: den 17 november 2005 09:29
> To: Joe Armstrong (AL/EAB); Bob.Smart;
> erlang-questions
> Cc: jim
> Subject: Re: JSON?
>
>
> I've recieved approval to release the JSON library for Erlang!
> I'll get it cleaned up and released tomorrow.
>
> In message
> <EF4121B4EBC4E045BDE1273F9D0A87FF011209AA
> se> Joe Armstrong writes:
> >
> >IMHO you might get a nicer mapping if you changed strings to
> binaries.
>
> I've pondered that.  It's a little awkward with the quoting and
> unquoting you need to do.  Binaries also imply that you'd need to
> choose a Unicode format for the internal representation that would
> be good for all applications.
>
>
> >I'd let the parser return an association list (not a dict) -
> since (probably)
> >the object is small
> >and it's nice to be able to print it and pattern match it directly.
>
> Yeah - I'd be glad to trade the asymptotic efficiency of dicts for
> compactness and nicer printing.  I liked dicts until they started
> showing up in error reports.  I'll probably change this in a future
> release.
>
> Pattern matching?  This means you'd have to:
>
> * have the JSON decoder always sort the assoc-list;
> * remember to maintain your pattern assoc-list in sorted order;
> * have optional fields sort after mandatory fields.
>
> Am I forgetting some new language feature?
>
> Thanks for your thoughts!
>
> Jim Larson
> jim
>