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


Groups > comp.lang.forth > #55012

Re: Moving JSON around.

Newsgroups comp.lang.forth
Date 2017-02-19 16:08 -0800
References <9520efbe-f7e6-46b9-a052-7c0b99b0a352@googlegroups.com> <87bmtymty0.fsf@nightsong.com> <7b0d720e-787a-4e81-93df-b386c9e7f487@googlegroups.com> <877f4mmsfs.fsf@nightsong.com>
Message-ID <1ec246a3-4b87-40af-8024-e3bc5230912f@googlegroups.com> (permalink)
Subject Re: Moving JSON around.
From franck.bensusan@gmail.com

Show all headers | View raw


Le dimanche 19 février 2017 10:38:16 UTC+1, Paul Rubin a écrit :
> I wonder if a Forth 2000x recognizer could handle JSON.  { } [ ] " and ,
> would all be separate words that pushed items to and popped them from
> a parsing stack, calling user functions at each event.

Hello,

Json objects (and arrays) could be handled directly by the outer 
interpreter with the data stack, but with changes of some words behavior : 

1) { and } words should be dedicated to Json objects and not locals.
{ creates a json frame on the control stack.
} creates a json object with Pairs on the data stack.
Locals could be handled by ( and ) and commentaries only handled by \

2) : checks if it is the beginning of a definition or just a json 
separator. If Json separator, do nothing.

3) [ and ] words are dedicated to arrays definition.
Entering/ending runtime mode is done by other words 
(<% and %> for instance).

4) , word is dedicated to array and Json separator.
For Json, it creates a Pair with the two objects on the stack.

5) true, false and null are a defined as constants.

6) The outer interpreter is hooked to handle some words without a need 
for a following space (or using a recogniser ? ) : { } [ ] and ,

7) Dot is used for Floats.

With those changes, the outer interpreter can parse Json objects and arrays 
as any other code and EVALUATE can evaluate strings containing those objects.

Is it worth it to make those changes and break the behavior of those
Forth words ? Can it be a possible evolution of the standard ? 
Or is it possible to do this with recognisers ? I don't know.

Oforth implements this behavoir, so its outer interpreter handles 
arrays and json without the need of a specific parser.

For instance : 
{ "aaa": 12, "bbb" : 13.2, "ccc" : [ 1, 2, 3 ], "ddd" : "eee" } .s
[1] (Json) {"aaa" : 12, "bbb" : 13.2, "ccc" : [1, 2, 3], "ddd" : "eee" }
ok
asString .s
[1] (String) {"aaa" : 12, "bbb" : 13.2, "ccc" : [1, 2, 3], "ddd" : "eee" }
ok
evaluate .s
[1] (Json) {"aaa" : 12, "bbb" : 13.2, "ccc" : [1, 2, 3], "ddd" : "eee" }
ok

: testCompile -- aJson 
   { "aaa": 12, "bbb" : 13.2, "ccc" : [ 1, 2, 3 ], "ddd" : "eee" } 
;

testCompile .s
[1] (Json) {"aaa" : 12, "bbb" : 13.2, "ccc" : [1, 2, 3], "ddd" : "eee" }


For the big JSON provided in this post, the outer interpreter failed because 
all elements are kept on the data stack before creating a Json (or an array).
Json (or array) are created only when ] or } is evaluated ie when
the number of items is available.

Into this json, a least one json (or array) had much more than 1000 
elements and this overflows the data stack.

I updated { and [ to create arrays and jsons at the beginning and
add each element when , or } or ] is evaluated.
 
This is much less performant as the number of elements is unknown at the
beginning and arrays will have be reallocated if needed (and here, it 
is needed a lot). But it works with any number of items.

Using Oforth version under development : 

: slurp	  	\ s -- sb   
   File new File.READ over open
   dup size swap read
;

: test
  "citylots.json" slurp evaluate ;

On my laptop (Core I7), it takes about 24 seconds for the outer interpreter to 
evaluate this string.

But I am not sure that handling declared arrays/jsons with more than 1000 
items worth the reduction of performances. So I don't think I will keep this 
update.

Franck

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


Thread

Moving JSON around. Julian Fondren <julian.fondren@gmail.com> - 2017-02-18 23:37 -0800
  Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-19 10:07 +0200
    Re: Moving JSON around. Julian Fondren <julian.fondren@gmail.com> - 2017-02-19 00:35 -0800
      Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-19 12:12 +0200
        Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-19 13:17 +0200
          Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-19 13:33 +0200
            Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-19 16:25 +0200
              Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-20 11:57 +0200
                Re: Moving JSON around. Paul Rubin <no.email@nospam.invalid> - 2017-02-20 02:03 -0800
                Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-20 12:56 +0200
                Re: Moving JSON around. Paul Rubin <no.email@nospam.invalid> - 2017-02-21 16:18 -0800
                Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-22 06:39 +0200
                Re: Moving JSON around. Julian Fondren <julian.fondren@gmail.com> - 2017-02-24 22:36 -0800
                Re: Moving JSON around. Paul Rubin <no.email@nospam.invalid> - 2017-02-25 01:50 -0800
                Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-25 20:02 +0200
                Re: Moving JSON around. Julian Fondren <julian.fondren@gmail.com> - 2017-02-25 23:13 -0800
                Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-26 10:13 +0200
                Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-26 13:01 +0200
                Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-26 16:05 +0200
  Re: Moving JSON around. Paul Rubin <no.email@nospam.invalid> - 2017-02-19 01:05 -0800
    Re: Moving JSON around. Julian Fondren <julian.fondren@gmail.com> - 2017-02-19 01:17 -0800
      Re: Moving JSON around. Paul Rubin <no.email@nospam.invalid> - 2017-02-19 01:38 -0800
        Re: Moving JSON around. franck.bensusan@gmail.com - 2017-02-19 16:08 -0800
          Re: Moving JSON around. Paul Rubin <no.email@nospam.invalid> - 2017-02-19 17:16 -0800
          Re: Moving JSON around. Julian Fondren <julian.fondren@gmail.com> - 2017-02-19 17:52 -0800
            Re: Moving JSON around. Ron Aaron <rambamist@gmail.com> - 2017-02-20 07:26 +0200
            Re: Moving JSON around. franck.bensusan@gmail.com - 2017-02-20 03:01 -0800

csiph-web