Module description

jis -- JSON input stream
The jis module implements a validating JSON parser. Feeding of the parser can be done with two words: jis-set-string for feeding the parser with strings and jis-set-reader for feeding the parser through an execution token. This token should have the following stack behavior:
                                                                      
   x -- c-addr u | 0                                                       
x is a word indicating the context of the reader and is identical to first parameter of jis-set-reader. For example this value is the file descriptor during reading of a file. The execution token returns, if successful, the read data in c-addr u. The input is parsed by calling the jis-read word repeatedly until it returns jis.error or jis.done. The word returns one of the following results with its stack parameters:
                                                                      
jis.error          --             = Error                                  
jis.done           --             = Stream is correctly parsed             
jis.start-object   --             = Start of new object                    
jis.end-object     --             = End of object                          
jis.start-array    --             = Start of new array                     
jis.end-array      --             = End of array                           
jis.name           -- c-addr u    = Name as string                         
jis.string         -- c-addr u    = String value                           
jis.number         -- n           = Number value                           
jis.double         -- d           = Double value                           
jis.float          -- r           = Float value                            
jis.boolean        -- flag        = Boolean value                          
jis.null           --             = Null value                             

json reader constants

jis.error ( -- n )
Error
jis.done ( -- n )
Done reading
jis.start-object ( -- n )
Start object
jis.start-array ( -- n )
Start array
jis.name ( -- n )
Name
jis.string ( -- n )
String value
jis.number ( -- n )
Number value
jis.double ( -- n )
Double value
jis.float ( -- n )
Float value
jis.boolean ( -- n )
Boolean value
jis.null ( -- n )
Null value
jis.end-object ( -- n )
End object
jis.end-array ( -- n )
End array

json reader structure

jis% ( -- n )
Get the required space for a jis reader variable

json reader variable creation, initialisation and destruction

jis-init ( jis -- )
Initialise the json reader variable
jis-(free) ( jis -- )
Free the internal, private variables from the heap
jis-create ( "<spaces>name" -- ; -- jis )
Create a named json reader variable in the dictionary
jis-new ( -- jis )
Create a new json reader variable on the heap
jis-free ( jis -- )
Free the jis reader variable from the heap

json reader init words

jis-set-reader ( x xt jis -- )
Init the json parser for reading using the reader callback xt with its data x
jis-set-string ( c-addr u jis -- )
Init the json parser for for reading from the string c-addr u

json reader word

jis-read ( jis -- i*x n )
Read the next json token n with its parameters from the source &lb;see json reader constants&rb;
jis+remove-read-parameters ( i*x n -- )
Remove the various parameters of a json token after calling jis-read &lb;see json reader constants&rb;

Inspection

jis-dump ( jis -- )
Dump the json input stream

Examples

\ ==============================================================================
\
\          jos_expl - the json output stream example in the ffl
\
\               Copyright (C) 2010  Dick van Oudheusden
\  
\ This library is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public
\ License as published by the Free Software Foundation; either
\ version 2 of the License, or (at your option) any later version.
\
\ This library is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
\ General Public License for more details.
\
\ You should have received a copy of the GNU General Public
\ License along with this library; if not, write to the Free
\ Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
\
\ ==============================================================================
\ 
\  $Date: 2008-01-13 08:09:33 $ $Revision: 1.3 $
\
\ ==============================================================================

include ffl/jis.fs


\ Example : Parse a json string


\ Create the json input stream in the dictionary

jis-create jis1

\ Feed the parser with a string

s\" {\"value\":10,\"flag\":false,\"array\":[1,2,3.1],\"string\":\"hello\",\"empty\":\"\"}" jis1 jis-set-string

\ Parse the string and print the parsed tokens

: json-parse  ( -- = Parse the string )
  BEGIN
    jis1 jis-read
    dup  jis.error <> over jis.done <> AND
  WHILE
    CASE
      jis.start-object OF ." Start Object"     ENDOF
      jis.start-array  OF ." Start array"      ENDOF
      jis.name         OF ." Name : "     type ENDOF
      jis.string       OF ." String : "   type ENDOF
      jis.number       OF ." Number : "   .    ENDOF 
      jis.double       OF ." Double : "   d.   ENDOF
      jis.float        OF ." Float : "    f.   ENDOF
      jis.boolean      OF ." Boolean : "  .    ENDOF
      jis.null         OF ." Null"             ENDOF
      jis.end-object   OF ." End Object"       ENDOF
      jis.end-array    OF ." End Array"        ENDOF
      ." Token:" dup .
    ENDCASE
    cr
  REPEAT
  drop
;

json-parse

Generated by fsdocgen 0.1.0