[erlang-questions] json_to_term EEP

Willem de Jong w.a.de.jong@REDACTED
Thu Jul 31 08:06:28 CEST 2008


On 7/30/08, Richard A. O'Keefe <ok@REDACTED> wrote:
>
> On 29 Jul 2008, at 6:10 pm, Willem de Jong wrote:
>
>> How about a SAX-like API?
>>
>
> (1) Anyone who wants such a design can produce their own design,
>    AND their own code.  The EEP I am concerned with is a DVM-
>    like design (Document *Value* Model).


Of course, but if the Erlang team creates special, fast support in C it
would be good if it could be used by as many people as possible.

(3) In the functional programming world, SAX is less attractive,
>    because the usual techniques for using an ESIS/SAX-like interface
>    are heavily stateful.
>
>    Once I had my Document Value Model kit, I found doing things the
>    "functional" way over documents as trees was so much easier than
>    doing things the ESIS/SAX-like way that now work with entire
>    forms whenever I can, and this is *C* programming I'm talking
>    about, where stateful is supposed to be easy.


I personally like working with a SAX parser. See the example below - I quite
enjoyed writing it.



> The question is whether the things that an ESIS/SAX-like interface
> let you do are things that people particularly *want* to do with JSON.
> I have no idea.


The point is, that the Erlang team would probably like to implement only 1
very
fast JSON parser in C. In my opinion, that should be a SAX-like parser,
because
it is easy to create DVM output based on SAX output, but pointless to do it
the
other way around.

To give an example:

A sax parser may create the following events (that is: call its callback
function with the following arguments, while parsing):

E = [startDocument,startObject, {key,"menu"}, startObject, {key,"id"},
 {value,"file"}, {key,"popup"}, startObject, {key,"menuitem"},
 startArray,startObject, {key,"value"}, {value,"New"}, {key,"onclick"},
 {value,"CreateNewDoc()"}, endObject,startObject, {key,"value"},
 {value,"Close"}, {key,"onclick"}, {value,"CloseDoc()"}, endObject,
 endArray,endObject,endObject,endObject, endDocument].

(This corresponds to a slightly shortened version of the second example
found on json.org).

Below an example of a callback function to process these events - this
function would be called by the SAX parser when it has processed another
relevant part of the JSON document. The parser passes the value
returned by the function to the next invocation (second argument of the
function, the first argument is the SAX event).

dvm(startDocument, _) ->
  start;
dvm(startObject, Stack) ->
  [[]| Stack];
dvm(startArray, Stack) ->
  [[]| Stack];
dvm({key, _} = Event, Stack) ->
  [Event|Stack];
dvm({value, Value}, start) ->
  {value, Value};
dvm({value, Value}, [{key, Key}, List | T]) ->
  [[{Key, Value} | List] | T];
dvm({value, Value}, [List | T]) ->
  [[Value | List] | T];
dvm(endObject, [List | T]) ->
  dvm({value, {lists:reverse(List)}}, T);
dvm(endArray, [List | T]) ->
  dvm({value, lists:reverse(List)}, T);
dvm(endDocument, {value, R}) ->
  R.

With the events given above this gives the following output:
(you can use lists:foldl(fun dvm/2, [], E). to try this).

{[{"menu",
   {[{"id","file"},
     {"popup",
      {[{"menuitem",
         [{[{"value","New"},{"onclick","CreateNewDoc()"}]},
          {[{"value","Close"},{"onclick","CloseDoc()"}]}]}]}}]}}]}

Regards,

Willem
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080731/1ed25ede/attachment.htm>


More information about the erlang-questions mailing list