JSON/json_module [ Modules ]

[ Top ] [ Modules ]

NAME

    json_module

DESCRIPTION

    JSON-FORTRAN: A Fortran 2008 JSON (JavaScript Object Notation) API.

NOTES

    -Based on fson by Joseph A. Levin (see License below)
        -The original F95 code was split into four files:
            fson_path_m.f95, fson_string_m.f95, fson_value_m.f95, fson.f95
        -The code has been extensively modified and combined into this 
            one module (json_module.f90).
        -Some Fortran 2003/2008 features are now used 
            (e.g., allocatable strings, newunit, generic, class, abstract interface)
    -The headers in this file follow the ROBODoc conventions.
            Compile with: robodoc --src ./ --doc ./doc --multidoc --html 
                                  --tabsize 4 --ignore_case_when_linking 
                                  --syntaxcolors --source_line_numbers --index

HISTORY

    Joseph A. Levin : March 2012
    Jacob Williams : 2/8/2013 : Extensive modifications to the original code.

SEE ALSO

    [1] http://github.com/jacobwilliams/json-fortran [json-fortran development site]
    [2] http://jacobwilliams.github.io/json-fortran [json-fortran online documentation]
    [3] http://www.json.org/ [JSON website]
    [4] http://jsonlint.com/ [JSON validator]

COPYRIGHT

    -------------------------------------------------------------------------------------
    json-fortran License:

    JSON-FORTRAN: A Fortran 2008 JSON API
    http://github.com/jacobwilliams/json-fortran

    Copyright (c) 2014, Jacob Williams
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, this
      list of conditions and the following disclaimer in the documentation and/or
      other materials provided with the distribution.

    * The names of its contributors may not be used to endorse or promote products 
      derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
    ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
    ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    -------------------------------------------------------------------------------------
    Original FSON License:

    http://github.com/josephalevin/fson [FSON code retrieved on 12/2/2013]

    Copyright (c) 2012 Joseph A. Levin

    Permission is hereby granted, free of charge, to any person obtaining a copy of this
    software and associated documentation files (the "Software"), to deal in the Software
    without restriction, including without limitation the rights to use, copy, modify, merge,
    publish, distribute, sublicense, and/or sell copies of the Software, and to permit
    persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or
    substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
    INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
    PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
    OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.

json_module/json_add [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_add

DESCRIPTION

    Add objects to a linked list of json_values.

NOTES

    Formerly, this was called json_value_add

SOURCE

363     interface json_add
364         module procedure :: json_value_add_member
365         module procedure :: json_value_add_integer, json_value_add_integer_vec
366         module procedure :: json_value_add_double,  json_value_add_double_vec
367         module procedure :: json_value_add_logical, json_value_add_logical_vec
368         module procedure :: json_value_add_string,  json_value_add_string_vec
369     end interface json_add

json_module/json_check_for_errors [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_check_for_errors

DESCRIPTION

    Retrieve error code from the module.
    This should be called after json_parse to check for errors.
    If an error is thrown, before using the module again, json_initialize
    should be called to clean up before it is used again.

EXAMPLE

    type(json_file) :: json
    logical :: status_ok
    character(len=:),allocatable :: error_msg
    call json%load_file(filename='myfile.json')
    call json_check_for_errors(status_ok, error_msg)
    if (.not. status_ok) then
        write(*,*) 'Error: '//error_msg
        call json_clear_exceptions()
        call json%destroy()
    end if

SEE ALSO

    json_failed

AUTHOR

    Jacob Williams : 12/4/2013

SOURCE

1349     subroutine json_check_for_errors(status_ok, error_msg)
1350 
1351     implicit none
1352 
1353     logical(LK),intent(out) :: status_ok
1354     character(kind=CK,len=:),allocatable,intent(out) :: error_msg
1355 
1356     status_ok = .not. exception_thrown
1357 
1358     if (.not. status_ok) then
1359         if (allocated(err_message)) then
1360             error_msg = err_message
1361         else
1362             error_msg = 'Unknown Error'
1363         end if
1364     else
1365         error_msg = ''
1366     end if
1367 
1368     end subroutine json_check_for_errors

json_module/json_clear_exceptions [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_clear_exceptions

DESCRIPTION

    Clear exceptions in the JSON module.

AUTHOR

    Jacob Williams : 12/4/2013

SOURCE

1270     subroutine json_clear_exceptions()
1271 
1272     implicit none
1273 
1274     !clear the flag and message:
1275     exception_thrown = .false.
1276     err_message = ''
1277 
1278     end subroutine json_clear_exceptions

json_module/json_count [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_count

DESCRIPTION

    Count the number of children.

HISTORY

    JW : 1/4/2014 : Original routine removed.  
                    Now using n_children variable.
                    Renamed from json_value_count.

SOURCE

2437     function json_count(me) result(count)
2438 
2439     implicit none
2440 
2441     integer(IK)                         :: count
2442     type(json_value),pointer,intent(in) :: me
2443     
2444     count = me%n_children
2445 
2446     end function json_count

json_module/json_create_array [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_create_array

DESCRIPTION

    Allocate a json_value pointer and make it an array variable.
    The pointer should not already be allocated.

EXAMPLE

    type(json_value),pointer :: p
    call json_create(p,'arrayname')

AUTHOR

    Jacob Williams

SOURCE

4474     subroutine json_create_array(me,name)
4475     
4476     implicit none
4477     
4478     type(json_value),pointer            :: me
4479     character(kind=CK,len=*),intent(in) :: name
4480     
4481     call json_value_create(me)
4482     call to_array(me,name)    
4483     
4484     end subroutine json_create_array

json_module/json_create_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_create_double

DESCRIPTION

    Allocate a json_value pointer and make it a double variable.
    The pointer should not already be allocated.

EXAMPLE

    type(json_value),pointer :: p
    call json_create(p,'value',1.0d0)

AUTHOR

    Jacob Williams

SOURCE

4344     subroutine json_create_double(me,val,name)
4345     
4346     implicit none
4347     
4348     type(json_value),pointer            :: me
4349     character(kind=CK,len=*),intent(in) :: name
4350     real(RK),intent(in)                 :: val
4351     
4352     call json_value_create(me)
4353     call to_double(me,val,name)    
4354     
4355     end subroutine json_create_double

json_module/json_create_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_create_integer

DESCRIPTION

    Allocate a json_value pointer and make it an integer variable.
    The pointer should not already be allocated.

EXAMPLE

    type(json_value),pointer :: p
    call json_create(p,'value',1)

AUTHOR

    Jacob Williams

SOURCE

4311     subroutine json_create_integer(me,val,name)
4312     
4313     implicit none
4314     
4315     type(json_value),pointer            :: me
4316     character(kind=CK,len=*),intent(in) :: name
4317     integer(IK),intent(in)              :: val
4318     
4319     call json_value_create(me)
4320     call to_integer(me,val,name)    
4321     
4322     end subroutine json_create_integer

json_module/json_create_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_create_logical

DESCRIPTION

    Allocate a json_value pointer and make it a logical variable.
    The pointer should not already be allocated.

EXAMPLE

    type(json_value),pointer :: p
    call json_create(p,'value',.true.)

AUTHOR

    Jacob Williams

SOURCE

4278     subroutine json_create_logical(me,val,name)
4279     
4280     implicit none
4281     
4282     type(json_value),pointer            :: me
4283     character(kind=CK,len=*),intent(in) :: name
4284     logical(LK),intent(in)              :: val
4285     
4286     call json_value_create(me)
4287     call to_logical(me,val,name)    
4288     
4289     end subroutine json_create_logical

json_module/json_create_null [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_create_null

DESCRIPTION

    Allocate a json_value pointer and make it a null variable.
    The pointer should not already be allocated.

EXAMPLE

    type(json_value),pointer :: p
    call json_create(p,'value')

AUTHOR

    Jacob Williams

SOURCE

4410     subroutine json_create_null(me,name)
4411     
4412     implicit none
4413     
4414     type(json_value),pointer            :: me
4415     character(kind=CK,len=*),intent(in) :: name
4416     
4417     call json_value_create(me)
4418     call to_null(me,name)    
4419     
4420     end subroutine json_create_null

json_module/json_create_object [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_create_object

DESCRIPTION

    Allocate a json_value pointer and make it an object variable.
    The pointer should not already be allocated.

EXAMPLE

    type(json_value),pointer :: p
    call json_create(p,'objectname')

AUTHOR

    Jacob Williams

SOURCE

4442     subroutine json_create_object(me,name)
4443     
4444     implicit none
4445     
4446     type(json_value),pointer            :: me
4447     character(kind=CK,len=*),intent(in) :: name
4448     
4449     call json_value_create(me)
4450     call to_object(me,name)    
4451     
4452     end subroutine json_create_object

json_module/json_create_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_create_string

DESCRIPTION

    Allocate a json_value pointer and make it a string variable.
    The pointer should not already be allocated.

EXAMPLE

    type(json_value),pointer :: p
    call json_create(p,'value','hello')

AUTHOR

    Jacob Williams

SOURCE

4377     subroutine json_create_string(me,val,name)
4378     
4379     implicit none
4380     
4381     type(json_value),pointer            :: me
4382     character(kind=CK,len=*),intent(in) :: name
4383     character(kind=CK,len=*),intent(in) :: val
4384     
4385     call json_value_create(me)
4386     call to_string(me,val,name)    
4387     
4388     end subroutine json_create_string

json_module/json_destroy [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_destroy

DESCRIPTION

    Destructor routine for a json_value pointer.
    This must be called explicitly if it is no longer needed, 
    before it goes out of scope.  Otherwise, a memory leak will result.

USAGE

    type(json_value) :: p
    ...
    call json_destroy(p)

SOURCE

471     interface json_destroy
472         module procedure :: json_value_destroy
473     end interface

json_module/json_failed [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_failed

DESCRIPTION

    Logical function to indicate if an exception has been thrown.

EXAMPLE

    type(json_file) :: json
    logical :: status_ok
    character(len=:),allocatable :: error_msg
    call json%load_file(filename='myfile.json')
    if (json_failed()) then
        call json_check_for_errors(status_ok, error_msg)
        write(*,*) 'Error: '//error_msg
        call json_clear_exceptions()
        call json%destroy()
    end if

SEE ALSO

    json_check_for_errors

AUTHOR

    Jacob Williams : 12/5/2013

SOURCE

1400     function json_failed() result(failed)
1401 
1402     implicit none
1403 
1404     logical(LK) :: failed
1405 
1406     failed = exception_thrown
1407 
1408     end function json_failed

json_module/json_file [ Classes ]

[ Top ] [ json_module ] [ Classes ]

NAME

    json_file

DESCRIPTION

    The json_file is the main public class that is
    used to open a file and get data from it.

EXAMPLE

    type(json_file) :: json
    integer :: ival
    real(real64) :: rval
    character(len=:),allocatable :: cval
    logical :: found
    call json%load_file(filename='myfile.json')
    call json%print_file() !print to the console
    call json%get('var.i',ival,found)
    call json%get('var.r(3)',rval,found)
    call json%get('var.c',cval,found)
    call json%destroy()

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

258         private
259 
260         !the JSON structure read from the file:
261         type(json_value),pointer :: p => null()
262 
263         contains
264 
265         procedure,public :: load_file        => json_file_load
266         procedure,public :: load_from_string => json_file_load_from_string
267         
268         procedure,public :: destroy     => json_file_destroy
269         procedure,public :: move        => json_file_move_pointer
270         procedure,public :: info        => json_file_variable_info
271         
272         procedure,public :: print_to_string => json_file_print_to_string
273 
274         generic,public :: print_file => json_file_print_to_console, &
275                                         json_file_print_1, &
276                                         json_file_print_2
277         
278         generic,public :: get => json_file_get_object,      &
279                                  json_file_get_integer,     &
280                                  json_file_get_double,      &
281                                  json_file_get_logical,     &
282                                  json_file_get_string,      &
283                                  json_file_get_integer_vec, &
284                                  json_file_get_double_vec,  &
285                                  json_file_get_logical_vec, &
286                                  json_file_get_string_vec
287                                  
288         generic,public :: update =>  json_file_update_integer,  &
289                                      json_file_update_logical,  &
290                                      json_file_update_real,     &
291                                      json_file_update_string
292 
293         !get:
294         procedure :: json_file_get_object
295         procedure :: json_file_get_integer
296         procedure :: json_file_get_double
297         procedure :: json_file_get_logical
298         procedure :: json_file_get_string
299         procedure :: json_file_get_integer_vec
300         procedure :: json_file_get_double_vec
301         procedure :: json_file_get_logical_vec
302         procedure :: json_file_get_string_vec
303         
304         !update:
305         procedure :: json_file_update_integer
306         procedure :: json_file_update_logical
307         procedure :: json_file_update_real
308         procedure :: json_file_update_string
309         
310         !print_file:
311         procedure :: json_file_print_to_console
312         procedure :: json_file_print_1
313         procedure :: json_file_print_2

json_module/json_file_destroy [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_destroy

USAGE

    call me%destroy()

DESCRIPTION

    Destroy the JSON file.

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

595     subroutine json_file_destroy(me)
596     
597     implicit none
598 
599     class(json_file),intent(inout) :: me
600 
601     if (associated(me%p)) call json_value_destroy(me%p)
602 
603     end subroutine json_file_destroy

json_module/json_file_get_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_get_double

USAGE

    call me%get(path,val,found)

DESCRIPTION

    Get a double from a JSON file.

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

1049     subroutine json_file_get_double (me, path, val, found)
1050 
1051     implicit none
1052 
1053     class(json_file),intent(inout)      :: me
1054     character(kind=CK,len=*),intent(in) :: path
1055     real(RK),intent(out)                :: val
1056     logical(LK),intent(out),optional    :: found
1057 
1058     call json_get(me%p, path=path, value=val, found=found)
1059 
1060     end subroutine json_file_get_double

json_module/json_file_get_double_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_get_double_vec

USAGE

    call me%get(path,vec,found)

DESCRIPTION

    Get a double vector from a JSON file.

AUTHOR

    Jacob Williams : 1/19/2014

SOURCE

1080     subroutine json_file_get_double_vec(me, path, vec, found)
1081     
1082     implicit none
1083 
1084     class(json_file),intent(inout)                :: me
1085     character(kind=CK,len=*),intent(in)           :: path
1086     real(RK),dimension(:),allocatable,intent(out) :: vec
1087     logical(LK),intent(out),optional              :: found
1088 
1089     call json_get(me%p, path, vec, found)
1090 
1091     end subroutine json_file_get_double_vec

json_module/json_file_get_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_get_integer

USAGE

    call me%get(path,val)

DESCRIPTION

    Get an integer from a JSON file.

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

987     subroutine json_file_get_integer(me, path, val, found)
988 
989     implicit none
990 
991     class(json_file),intent(inout)      :: me
992     character(kind=CK,len=*),intent(in) :: path
993     integer(IK),intent(out)             :: val
994     logical(LK),intent(out),optional    :: found
995 
996     call json_get(me%p, path=path, value=val, found=found)
997 
998     end subroutine json_file_get_integer

json_module/json_file_get_integer_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_get_integer_vec

USAGE

    call me%get(path,vec)

DESCRIPTION

    Get an integer vector from a JSON file.

AUTHOR

    Jacob Williams : 1/20/2014

SOURCE

1018     subroutine json_file_get_integer_vec(me, path, vec, found)
1019 
1020     implicit none
1021 
1022     class(json_file),intent(inout)                   :: me
1023     character(kind=CK,len=*),intent(in)              :: path
1024     integer(IK),dimension(:),allocatable,intent(out) :: vec
1025     logical(LK),intent(out),optional                 :: found
1026 
1027     call json_get(me%p, path, vec, found)
1028 
1029     end subroutine json_file_get_integer_vec

json_module/json_file_get_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_get_logical

USAGE

    call me%get(path,val,found)

DESCRIPTION

    Get a logical from a JSON file.

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

1111     subroutine json_file_get_logical(me,path,val,found)
1112 
1113     implicit none
1114 
1115     class(json_file),intent(inout)       :: me
1116     character(kind=CK,len=*),intent(in)  :: path
1117     logical(LK),intent(out)              :: val
1118     logical(LK),intent(out),optional     :: found
1119 
1120     call json_get(me%p, path=path, value=val, found=found)
1121 
1122     end subroutine json_file_get_logical

json_module/json_file_get_logical_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_get_logical_vec

USAGE

    call me%get(path,vec)

DESCRIPTION

    Get a logical vector from a JSON file.

AUTHOR

    Jacob Williams : 1/20/2014

SOURCE

1142     subroutine json_file_get_logical_vec(me, path, vec, found)
1143 
1144     implicit none
1145 
1146     class(json_file),intent(inout)                   :: me
1147     character(kind=CK,len=*),intent(in)              :: path
1148     logical(LK),dimension(:),allocatable,intent(out) :: vec
1149     logical(LK),intent(out),optional                 :: found
1150     
1151     call json_get(me%p, path, vec, found)
1152 
1153     end subroutine json_file_get_logical_vec

json_module/json_file_get_object [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_get_object

USAGE

    call me%get(path,p,found)

DESCRIPTION

    Get a pointer to an object from a JSON file.

AUTHOR

    Jacob Williams : 2/3/2014

SOURCE

956     subroutine json_file_get_object(me, path, p, found)
957     
958     implicit none
959 
960     class(json_file),intent(inout)       :: me
961     character(kind=CK,len=*),intent(in)  :: path
962     type(json_value),pointer,intent(out) :: p
963     logical(LK),intent(out),optional     :: found
964 
965     call json_get_by_path(me%p, path=path, p=p, found=found)
966 
967     end subroutine json_file_get_object

json_module/json_file_get_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_get_string

USAGE

    call me%get(path,val)

DESCRIPTION

    Get a character string from a json file.
    Note: val is an allocatable character string.

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

1174     subroutine json_file_get_string(me, path, val, found)
1175 
1176     implicit none
1177 
1178     class(json_file),intent(inout)                   :: me
1179     character(kind=CK,len=*),intent(in)              :: path
1180     character(kind=CK,len=:),allocatable,intent(out) :: val
1181     logical(LK),intent(out),optional                 :: found
1182 
1183     call json_get(me%p, path=path, value=val, found=found)
1184 
1185     end subroutine json_file_get_string

json_module/json_file_get_string_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_get_string_vec

USAGE

    call me%get(path,vec)

DESCRIPTION

    Get a string vector from a JSON file.

AUTHOR

    Jacob Williams : 1/19/2014

SOURCE

1205     subroutine json_file_get_string_vec(me, path, vec, found)
1206 
1207     implicit none
1208 
1209     class(json_file),intent(inout)                                :: me
1210     character(kind=CK,len=*),intent(in)                           :: path
1211     character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec
1212     logical(LK),intent(out),optional                              :: found
1213 
1214     call json_get(me%p, path, vec, found)
1215 
1216     end subroutine json_file_get_string_vec

json_module/json_file_load [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_load

DESCRIPTION

    Load a JSON file.

EXAMPLE

    type(json_file) :: f
    call f%load_file('my_file.json')

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

660     subroutine json_file_load(me, filename, unit)
661 
662     implicit none
663 
664     class(json_file),intent(inout)      :: me
665     character(kind=CK,len=*),intent(in) :: filename
666     integer(IK),intent(in),optional     :: unit
667 
668     call json_parse(file=filename, p=me%p, unit=unit)
669 
670     end subroutine json_file_load

json_module/json_file_load_from_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_load_from_string

DESCRIPTION

    Load the JSON data from a string.

EXAMPLE

    type(json_file) :: f
    call f%load_from_string('{ "name": "Leonidas" }')

AUTHOR

    Jacob Williams : 1/13/2015

SOURCE

691     subroutine json_file_load_from_string(me, str)
692 
693     implicit none
694 
695     class(json_file),intent(inout)      :: me
696     character(kind=CK,len=*),intent(in) :: str
697 
698     call json_parse(str=str, p=me%p)
699 
700     end subroutine json_file_load_from_string

json_module/json_file_move_pointer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_move_pointer

USAGE

    call to%move(from)

DESCRIPTION

    Move the json_value pointer from one json_file to another.
    "from" is then nullified, but not destroyed.
    Note: if "from%p" is not associated, then an error is thrown.

AUTHOR

    Jacob Williams : 12/5/2014

SOURCE

625     subroutine json_file_move_pointer(to,from)
626     
627     implicit none
628 
629     class(json_file),intent(inout) :: to
630     class(json_file),intent(inout) :: from
631 
632     if (associated(from%p)) then
633         to%p => from%p
634         nullify(from%p)
635     else
636         call throw_exception('Error in json_file_move_pointer: '//&
637                              'pointer is not associated.')
638     end if
639 
640     end subroutine json_file_move_pointer

json_module/json_file_print_1 [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_print_1

USAGE

    call me%print_file(iunit)

DESCRIPTION

    Print the JSON file.
    Prints to the specified file unit (if not present, then prints to the console)

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

751     subroutine json_file_print_1(me, iunit)
752 
753     implicit none
754 
755     class(json_file),intent(inout)  :: me
756     integer(IK),intent(in)          :: iunit  !must be non-zero
757 
758     integer(IK) :: i
759     character(kind=CK,len=:),allocatable :: dummy
760 
761     if (iunit/=0) then
762         i = iunit
763     else
764         call throw_exception('Error in json_file_print_1: iunit must be nonzero.')
765         return
766     end if
767 
768     call json_value_print(me%p,iunit=i,str=dummy,indent=1,colon=.true.)
769 
770     end subroutine json_file_print_1

json_module/json_file_print_2 [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_print_2

USAGE

    call me%print_file(filename)

DESCRIPTION

    Print the JSON file.
    Input is the filename.  The file is opened, printed, and then closed.

EXAMPLE

    type(json_file) :: f
    logical :: found
    call f%load_file('my_file.json')    !open the original file
    call f%update('version',4,found)    !change the value of a variable
    call f%print_file('my_file_2.json') !save file as new name

AUTHOR

    Jacob Williams : 1/11/2015

SOURCE

798     subroutine json_file_print_2(me,filename)
799 
800     implicit none
801 
802     class(json_file),intent(inout)      :: me
803     character(kind=CK,len=*),intent(in) :: filename
804 
805     integer(IK) :: iunit,istat
806 
807     open(newunit=iunit,file=filename,status='REPLACE',iostat=istat)
808     if (istat==0) then
809         call me%print_file(iunit)    !call the other routine
810         close(iunit,iostat=istat)
811     else
812         call throw_exception('Error in json_file_print_2: could not open file: '//&
813                               trim(filename))
814     end if
815     
816     end subroutine json_file_print_2

json_module/json_file_print_to_console [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_print_to_console

USAGE

    call me%print_file()

DESCRIPTION

    Print the JSON file to the console.

AUTHOR

    Jacob Williams : 1/11/2015

SOURCE

720     subroutine json_file_print_to_console(me)
721 
722     implicit none
723 
724     class(json_file),intent(inout)  :: me
725 
726     character(kind=CK,len=:),allocatable :: dummy
727 
728     call json_value_print(me%p,iunit=output_unit,str=dummy,indent=1,colon=.true.)
729 
730     end subroutine json_file_print_to_console

json_module/json_file_print_to_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_print_to_string

USAGE

    call me%print_to_string(str)

DESCRIPTION

    Print the JSON file to a string.

EXAMPLE

    type(json_file) :: f
    character(kind=CK,len=:),allocatable :: str
    call f%load_file('my_file.json')
    call f%print_file(str)

AUTHOR

    Jacob Williams : 1/11/2015

SOURCE

842     subroutine json_file_print_to_string(me,str)
843 
844     implicit none
845 
846     class(json_file),intent(inout)                   :: me
847     character(kind=CK,len=:),allocatable,intent(out) :: str
848 
849     call json_value_to_string(me%p,str)
850     
851     end subroutine json_file_print_to_string

json_module/json_file_update_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_update_integer

DESCRIPTION

    Given the path string, if the variable is present in the file, 
    and is a scalar, then update its value.
    If it is not present, then create it and set its value.

SEE ALSO

    json_update_integer

AUTHOR

    Jacob Williams : 1/10/2015

SOURCE

1648     subroutine json_file_update_integer(me,name,val,found)
1649     implicit none
1650     
1651     class(json_file),intent(inout)      :: me
1652     character(kind=CK,len=*),intent(in) :: name
1653     integer(IK),intent(in)              :: val
1654     logical(LK),intent(out)             :: found
1655     
1656     if (.not. exception_thrown) call json_update(me%p,name,val,found)
1657     
1658     end subroutine json_file_update_integer

json_module/json_file_update_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_update_logical

DESCRIPTION

    Given the path string, if the variable is present in the file, 
    and is a scalar, then update its value.
    If it is not present, then create it and set its value.

SEE ALSO

    json_update_logical

AUTHOR

    Jacob Williams : 1/10/2015

SOURCE

1680     subroutine json_file_update_logical(me,name,val,found)
1681     implicit none
1682     
1683     class(json_file),intent(inout)      :: me
1684     character(kind=CK,len=*),intent(in) :: name
1685     logical(LK),intent(in)              :: val
1686     logical(LK),intent(out)             :: found
1687 
1688     if (.not. exception_thrown) call json_update(me%p,name,val,found)
1689 
1690     end subroutine json_file_update_logical

json_module/json_file_update_real [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_update_real

DESCRIPTION

    Given the path string, if the variable is present in the file, 
    and is a scalar, then update its value.
    If it is not present, then create it and set its value.

SEE ALSO

    json_update_real

AUTHOR

    Jacob Williams : 1/10/2015

SOURCE

1712     subroutine json_file_update_real(me,name,val,found)
1713     implicit none
1714     
1715     class(json_file),intent(inout)      :: me
1716     character(kind=CK,len=*),intent(in) :: name
1717     real(RK),intent(in)                 :: val
1718     logical(LK),intent(out)             :: found
1719 
1720     if (.not. exception_thrown) call json_update(me%p,name,val,found)
1721 
1722     end subroutine json_file_update_real

json_module/json_file_update_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_update_string

DESCRIPTION

    Given the path string, if the variable is present in the file, 
    and is a scalar, then update its value.
    If it is not present, then create it and set its value.

SEE ALSO

    json_update_string

AUTHOR

    Jacob Williams : 1/10/2015

SOURCE

1744     subroutine json_file_update_string(me,name,val,found)
1745     implicit none
1746     
1747     class(json_file),intent(inout)      :: me
1748     character(kind=CK,len=*),intent(in) :: name
1749     character(kind=CK,len=*),intent(in) :: val
1750     logical(LK),intent(out)             :: found
1751 
1752     if (.not. exception_thrown) call json_update(me%p,name,val,found)
1753 
1754     end subroutine json_file_update_string

json_module/json_file_variable_info [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_file_variable_info

USAGE

    call me%info(path,found,var_type,n_children)

DESCRIPTION

    Returns information about a variable in a file.

AUTHOR

    Jacob Williams : 2/3/2014

SOURCE

871     subroutine json_file_variable_info(me,path,found,var_type,n_children)
872 
873     implicit none
874 
875     class(json_file),intent(inout)      :: me
876     character(kind=CK,len=*),intent(in) :: path
877     logical(LK),intent(out)             :: found
878     integer(IK),intent(out)             :: var_type
879     integer(IK),intent(out)             :: n_children
880 
881     type(json_value),pointer :: p
882 
883     !initialize:
884     nullify(p)
885 
886     !get a pointer to the variable (if it is there):
887     call me%get(path,p,found)
888 
889     if (found) then
890 
891         !get info:
892         call json_info(p,var_type,n_children)
893 
894     else
895 
896         !set to dummy values:
897         var_type = json_unknown
898         n_children = 0
899 
900     end if
901 
902     !cleanup:
903     nullify(p)
904 
905     end subroutine json_file_variable_info

json_module/json_get [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get

DESCRIPTION

    Get data from a json_value linked list.

SOURCE

408     interface json_get
409         module procedure :: json_get_by_path
410         module procedure :: json_get_integer, json_get_integer_vec
411         module procedure :: json_get_double,  json_get_double_vec
412         module procedure :: json_get_logical, json_get_logical_vec
413         module procedure :: json_get_string,  json_get_string_vec
414         module procedure :: json_get_array
415     end interface json_get

json_module/json_get_array [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_array

DESCRIPTION

    This routine calls the user-supplied array_callback subroutine
        for each element in the array.
    Note: for integer, double, logical, and character arrays,
        a higher-level routine is provided (see json_get), so
        this routine does not have to be used for those cases.

SOURCE

3867     subroutine json_get_array(this, path, array_callback, found)
3868 
3869     implicit none
3870 
3871     type(json_value),pointer,intent(in)          :: this
3872     character(kind=CK,len=*),intent(in),optional :: path
3873     procedure(array_callback_func)               :: array_callback
3874     logical(LK),intent(out),optional             :: found
3875 
3876     type(json_value),pointer :: element,p
3877     integer(IK) :: i, count
3878 
3879     if (.not. exception_thrown) then
3880 
3881         nullify(p)
3882 
3883         ! resolve the path to the value
3884         if (present(path)) then
3885             call json_get_by_path(this=this, path=path, p=p)
3886         else
3887             p => this
3888         end if
3889 
3890         if (.not. associated(p)) then
3891 
3892             call throw_exception('Error in json_get_array:'//&
3893                                  ' Unable to resolve path: '//trim(path))
3894 
3895         else
3896 
3897             select case (p%var_type)
3898             case (json_array)
3899                 count = json_count(p)
3900                 element => p%children
3901                 do i = 1, count ! callback for each child
3902                     call array_callback(element, i, count)
3903                     element => element%next
3904                 end do
3905             case default
3906                 call throw_exception('Error in json_get_array:'//&
3907                                      ' Resolved value is not an array. '//&
3908                                      trim(path))
3909             end select
3910 
3911             !cleanup:
3912             if (associated(p))       nullify(p)
3913             if (associated(element)) nullify(element)
3914 
3915         end if
3916 
3917         if (exception_thrown) then
3918             if (present(found)) then
3919                 found = .false.
3920                 call json_clear_exceptions()
3921             end if
3922         else
3923             if (present(found)) found = .true.
3924         end if
3925 
3926     else
3927         if (present(found)) found = .false.
3928     end if
3929 
3930     end subroutine json_get_array

json_module/json_get_by_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_by_path

USAGE

   call json_get(me,path,p,found)

DESCRIPTION

    Returns the json_value pointer given the path string.

EXAMPLE

    type(json_value),pointer :: dat,p
    logical :: found
    ...
    call json_get(dat,'data(2).version',p,found)

NOTES

    The following special characters are used to denote paths:
     $         - root
     @         - this
     .         - child object member
     [] or ()  - child array element

    Thus, if any of these characters are present in the name key,
        this routine cannot be used to get the value.
    In that case, the json_get_child routines would need to be used.

SOURCE

2949     subroutine json_get_by_path(this, path, p, found)
2950 
2951     implicit none
2952 
2953     type(json_value),pointer,intent(in)  :: this
2954     character(kind=CK,len=*),intent(in)  :: path
2955     type(json_value),pointer,intent(out) :: p
2956     logical(LK),intent(out),optional     :: found
2957     
2958     character(kind=CK,len=1),parameter :: start_array_alt = '('
2959     character(kind=CK,len=1),parameter :: end_array_alt   = ')'
2960 
2961     integer(IK) :: i, length, child_i
2962     character(kind=CK,len=1) :: c
2963     logical(LK) :: array
2964     type(json_value),pointer :: tmp
2965 
2966     if (.not. exception_thrown) then
2967 
2968         nullify(p)
2969 
2970         ! default to assuming relative to this
2971         p => this
2972 
2973         child_i = 1
2974 
2975         array = .false.
2976 
2977         length = len_trim(path)
2978 
2979         do i=1, length
2980 
2981             c = path(i:i)
2982 
2983             select case (c)
2984             case ('$')
2985 
2986                 ! root
2987                 do while (associated (p%parent))
2988                     p => p%parent
2989                 end do
2990                 child_i = i + 1
2991 
2992             case ('@')
2993 
2994                 ! this
2995                 p => this
2996                 child_i = i + 1
2997 
2998             case ('.')
2999 
3000                 ! get child member from p
3001                 if (child_i < i) then
3002                     nullify(tmp)
3003                     call json_get_child(p, path(child_i:i-1), tmp)
3004                     p => tmp
3005                     nullify(tmp)
3006                 else
3007                     child_i = i + 1
3008                     cycle
3009                 end if
3010 
3011                 if (.not. associated(p)) then
3012                     call throw_exception('Error in json_get_by_path:'//&
3013                                          ' Error getting child member.')
3014                     exit
3015                 end if
3016 
3017                 child_i = i+1
3018 
3019             case (start_array,start_array_alt)
3020 
3021                 !....Modified to allow for 'var[3]' style syntax
3022                 !Note: jmozmoz/fson has a slightly different version of this...
3023 
3024                 ! start looking for the array element index
3025                 array = .true.
3026 
3027                 ! get child member from p
3028                 if (child_i < i) then
3029                     nullify(tmp)
3030                     call json_get_child(p, path(child_i:i-1), tmp)
3031                     p => tmp
3032                     nullify(tmp)
3033                 else
3034                     child_i = i + 1
3035                     cycle
3036                 end if
3037                 if (.not. associated(p)) then
3038                     call throw_exception('Error in json_get_by_path:'//&
3039                                          ' Error getting array element')
3040                     exit
3041                 end if
3042                 child_i = i + 1
3043 
3044             case (end_array,end_array_alt)
3045 
3046                 if (.not.array) then
3047                     call throw_exception('Error in json_get_by_path: Unexpected ]')
3048                     exit
3049                 end if
3050                 array = .false.
3051                 child_i = string_to_integer(path(child_i:i-1))
3052 
3053                 nullify(tmp)
3054                 call json_get_child(p, child_i, tmp)
3055                 p => tmp
3056                 nullify(tmp)
3057 
3058                 child_i= i + 1
3059 
3060             end select
3061 
3062         end do
3063 
3064         if (exception_thrown) then
3065 
3066             if (present(found)) then
3067                 found = .false.
3068                 call json_clear_exceptions()
3069             end if
3070 
3071         else
3072 
3073             ! grab the last child if present in the path
3074             if (child_i <= length) then
3075                 nullify(tmp)
3076                 call json_get_child(p, path(child_i:i-1), tmp)
3077                 p => tmp
3078                 nullify(tmp)
3079             end if
3080             if (associated(p)) then
3081                 if (present(found)) found = .true.    !everything seems to be ok
3082             else
3083                 call throw_exception('Error in json_get_by_path:'//&
3084                                      ' variable not found: '//trim(path))
3085                 if (present(found)) then
3086                     found = .false.
3087                     call json_clear_exceptions()
3088                 end if
3089             end if
3090             
3091         end if
3092 
3093     else
3094         if (present(found)) found = .false.
3095     end if
3096 
3097     end subroutine json_get_by_path

json_module/json_get_child [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_child

DESCRIPTION

    Get a child, either by index or name string.
    Both of these return a json_value pointer.

NOTES

    Formerly, this was called json_value_get_child

SOURCE

344     interface json_get_child
345         module procedure json_value_get_by_index
346         module procedure json_value_get_by_name_chars
347     end interface json_get_child

json_module/json_get_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_double

DESCRIPTION

    Get a double value from a json_value.

SOURCE

3324     subroutine json_get_double(this, path, value, found)
3325 
3326     implicit none
3327 
3328     type(json_value),pointer           :: this
3329     character(kind=CK,len=*), optional :: path
3330     real(RK),intent(out)               :: value
3331     logical(LK),intent(out),optional   :: found
3332 
3333     type(json_value),pointer :: p
3334 
3335     if (.not. exception_thrown) then
3336 
3337         nullify(p)
3338 
3339         if (present(path)) then
3340             call json_get_by_path(this=this, path=path, p=p)
3341         else
3342             p => this
3343         end if
3344 
3345         if (.not. associated(p)) then
3346 
3347             call throw_exception('Error in json_get_double:'//&
3348                                  ' Unable to resolve path: '//trim(path))
3349 
3350         else
3351 
3352             select case (p%var_type)
3353             case (json_integer)
3354                 value = p%int_value
3355             case (json_double)
3356                 value = p%dbl_value
3357             case (json_logical)
3358                 if (p%log_value) then
3359                     value = 1.0_RK
3360                 else
3361                     value = 0.0_RK
3362                 end if
3363             case default
3364                 call throw_exception('Error in json_get_double:'//&
3365                                      ' Unable to resolve value to double: '//&
3366                                      trim(path))
3367             end select
3368 
3369             nullify(p)
3370 
3371         end if
3372 
3373         if (exception_thrown) then
3374             if (present(found)) then
3375                 found = .false.
3376                 call json_clear_exceptions()
3377             end if
3378         else
3379             if (present(found)) found = .true.
3380         end if
3381 
3382     else
3383 
3384         value = 0.0_RK
3385         if (present(found)) found = .false.
3386 
3387     end if
3388 
3389     end subroutine json_get_double

json_module/json_get_double_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_double_vec

DESCRIPTION

    Get a double vector from a JSON value.

AUTHOR

    Jacob Williams : 5/14/2014

SOURCE

3406     subroutine json_get_double_vec(me, path, vec, found)
3407 
3408     implicit none
3409 
3410     type(json_value),pointer                      :: me
3411     character(kind=CK,len=*),intent(in)           :: path
3412     real(RK),dimension(:),allocatable,intent(out) :: vec
3413     logical(LK),intent(out),optional              :: found
3414 
3415     logical(LK) :: initialized
3416 
3417     initialized = .false.
3418 
3419     if (allocated(vec)) deallocate(vec)
3420 
3421     !the callback function is called for each element of the array:
3422     call json_get(me, path=path, array_callback=get_double_from_array, found=found)
3423 
3424     contains
3425 
3426         ! callback function for double
3427         subroutine get_double_from_array(element, i, count)
3428         implicit none
3429 
3430         type(json_value),pointer,intent(in) :: element
3431         integer(IK),intent(in)              :: i        !index
3432         integer(IK),intent(in)              :: count    !size of array
3433 
3434         !size the output array:
3435         if (.not. initialized) then
3436             allocate(vec(count))
3437             initialized = .true.
3438         end if
3439 
3440         !populate the elements:
3441         call json_get(element, value=vec(i))
3442 
3443         end subroutine get_double_from_array
3444 
3445     end subroutine json_get_double_vec

json_module/json_get_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_integer

DESCRIPTION

    Get an integer value from a json_value.

SOURCE

3190     subroutine json_get_integer(this, path, value, found)
3191 
3192     implicit none
3193 
3194     type(json_value),pointer,intent(in) :: this
3195     character(kind=CK,len=*),optional   :: path
3196     integer(IK),intent(out)             :: value
3197     logical(LK),intent(out),optional    :: found
3198 
3199     type(json_value),pointer :: p
3200 
3201     if (.not. exception_thrown) then
3202 
3203         nullify(p)
3204         if (present(path)) then
3205             call json_get_by_path(this=this, path=path, p=p)
3206         else
3207             p => this
3208         end if
3209 
3210         if (.not. associated(p)) then
3211 
3212             call throw_exception('Error in json_get_integer:'//&
3213                                  ' Unable to resolve path: '// trim(path))
3214 
3215         else
3216 
3217             select case(p%var_type)
3218             case (json_integer)
3219                 value = p%int_value
3220             case (json_double)
3221                 value = int(p%dbl_value)
3222             case (json_logical)
3223                 if (p%log_value) then
3224                     value = 1
3225                 else
3226                     value = 0
3227                 end if
3228             case default
3229                 call throw_exception('Error in get_integer:'//&
3230                                      ' Unable to resolve value to integer: '//&
3231                                      trim(path))
3232             end select
3233 
3234             nullify(p)
3235 
3236         end if
3237 
3238         if (exception_thrown) then
3239             if (present(found)) then
3240                 found = .false.
3241                 call json_clear_exceptions()
3242             end if
3243         else
3244             if (present(found)) found = .true.
3245         end if
3246 
3247     else
3248 
3249         value = 0
3250         if (present(found)) found = .false.
3251 
3252     end if
3253 
3254     end subroutine json_get_integer

json_module/json_get_integer_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_integer_vec

DESCRIPTION

    Get an integer vector from a JSON value.

AUTHOR

    Jacob Williams : 5/14/2014

SOURCE

3271     subroutine json_get_integer_vec(me, path, vec, found)
3272 
3273     implicit none
3274 
3275     type(json_value),pointer                         :: me
3276     character(kind=CK,len=*),intent(in)              :: path
3277     integer(IK),dimension(:),allocatable,intent(out) :: vec
3278     logical(LK),intent(out),optional                 :: found
3279 
3280     logical(LK) :: initialized
3281 
3282     initialized = .false.
3283 
3284     if (allocated(vec)) deallocate(vec)
3285 
3286     !the callback function is called for each element of the array:
3287     call json_get(me, path=path, array_callback=get_int_from_array, found=found)
3288 
3289     contains
3290 
3291         ! callback function for integer
3292         subroutine get_int_from_array(element, i, count)
3293         implicit none
3294 
3295         type(json_value),pointer,intent(in) :: element
3296         integer(IK),intent(in)              :: i        !index
3297         integer(IK),intent(in)              :: count    !size of array
3298 
3299         !size the output array:
3300         if (.not. initialized) then
3301             allocate(vec(count))
3302             initialized = .true.
3303         end if
3304 
3305         !populate the elements:
3306         call json_get(element, value=vec(i))
3307 
3308         end subroutine get_int_from_array
3309 
3310     end subroutine json_get_integer_vec

json_module/json_get_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_logical

DESCRIPTION

    Get a logical value from a json_value.

SOURCE

3459     subroutine json_get_logical(this, path, value, found)
3460 
3461     implicit none
3462 
3463     type(json_value),pointer,intent(in) :: this
3464     character(kind=CK,len=*),optional   :: path
3465     logical(LK)                         :: value
3466     logical(LK),intent(out),optional    :: found
3467 
3468     type(json_value),pointer :: p
3469 
3470     if (.not. exception_thrown) then
3471 
3472         nullify(p)
3473 
3474         if (present(path)) then
3475             call json_get_by_path(this=this, path=path, p=p)
3476         else
3477             p => this
3478         end if
3479 
3480         if (.not. associated(p)) then
3481 
3482             call throw_exception('Error in json_get_logical:'//&
3483                                  ' Unable to resolve path: '//trim(path))
3484 
3485         else
3486 
3487             select case (p%var_type)
3488             case (json_integer)
3489                 value = (p%int_value > 0)
3490             case (json_logical)
3491                 value = p % log_value
3492             case default
3493                 call throw_exception('Error in json_get_logical:'//&
3494                                      ' Unable to resolve value to logical: '//&
3495                                      trim(path))
3496             end select
3497 
3498             nullify(p)
3499 
3500         end if
3501 
3502         if (exception_thrown) then
3503             if (present(found)) then
3504                 found = .false.
3505                 call json_clear_exceptions()
3506             end if
3507         else
3508             if (present(found)) found = .true.
3509         end if
3510 
3511     else
3512 
3513         value = .false.
3514         if (present(found)) found = .false.
3515 
3516     end if
3517 
3518     end subroutine json_get_logical

json_module/json_get_logical_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_logical_vec

DESCRIPTION

    Get a logical vector from a JSON value.

AUTHOR

    Jacob Williams : 5/14/2014

SOURCE

3535     subroutine json_get_logical_vec(me, path, vec, found)
3536 
3537     implicit none
3538 
3539     type(json_value),pointer,intent(in)              :: me
3540     character(kind=CK,len=*),intent(in)              :: path
3541     logical(LK),dimension(:),allocatable,intent(out) :: vec
3542     logical(LK),intent(out),optional                 :: found
3543 
3544     logical(LK) :: initialized
3545 
3546     initialized = .false.
3547 
3548     if (allocated(vec)) deallocate(vec)
3549 
3550     !the callback function is called for each element of the array:
3551     call json_get(me, path=path, array_callback=get_logical_from_array, found=found)
3552 
3553     contains
3554 
3555         ! callback function for logical
3556         subroutine get_logical_from_array(element, i, count)
3557         implicit none
3558 
3559         type(json_value),pointer,intent(in) :: element
3560         integer(IK),intent(in)              :: i        !index
3561         integer(IK),intent(in)              :: count    !size of array
3562 
3563         !size the output array:
3564         if (.not. initialized) then
3565             allocate(vec(count))
3566             initialized = .true.
3567         end if
3568 
3569         !populate the elements:
3570         call json_get(element, value=vec(i))
3571 
3572         end subroutine get_logical_from_array
3573 
3574     end subroutine json_get_logical_vec

json_module/json_get_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_string

DESCRIPTION

    Get a character string from a json_value.

SOURCE

3588     subroutine json_get_string(this, path, value, found)
3589 
3590     implicit none
3591 
3592     type(json_value),pointer,intent(in)              :: this
3593     character(kind=CK,len=*),intent(in),optional     :: path
3594     character(kind=CK,len=:),allocatable,intent(out) :: value
3595     logical(LK),intent(out),optional                 :: found
3596 
3597     type(json_value),pointer :: p
3598     character(kind=CK,len=:),allocatable :: s,pre,post
3599     integer(IK) :: j,jprev,n
3600     character(kind=CK,len=1) :: c
3601 
3602     if (.not. exception_thrown) then
3603 
3604         nullify(p)
3605 
3606         if (present(path)) then
3607             call json_get_by_path(this=this, path=path, p=p)
3608         else
3609             p => this
3610         end if
3611 
3612         if (.not. associated(p)) then
3613 
3614             call throw_exception('Error in json_get_string:'//&
3615                                  ' Unable to resolve path: '//trim(path))
3616 
3617         else
3618 
3619             select case (p%var_type)
3620             
3621             case (json_string)
3622             
3623                 if (allocated(p%str_value)) then
3624 
3625                     !get the value as is:
3626                     s = p%str_value
3627 
3628                     ! Now, have to remove the escape characters:
3629                     !
3630                     ! '\"'        quotation mark
3631                     ! '\\'        reverse solidus
3632                     ! '\/'        solidus
3633                     ! '\b'        backspace
3634                     ! '\f'        formfeed
3635                     ! '\n'        newline (LF)
3636                     ! '\r'        carriage return (CR)
3637                     ! '\t'        horizontal tab
3638                     ! '\uXXXX'    4 hexadecimal digits
3639                     !
3640 
3641                     !initialize:
3642                     n = len(s)
3643                     j = 1
3644 
3645                     do
3646 
3647                         jprev = j                      !initialize
3648                         j = index(s(j:n),backslash)    !look for an escape character
3649 
3650                         if (j>0) then            !an escape character was found
3651                         
3652                             !index in full string of the escape character:
3653                             j = j + (jprev-1)   
3654 
3655                             if (j<n) then
3656 
3657                                 !save the bit before the escape character:
3658                                 if (j>1) then
3659                                     pre = s( 1 : j-1 )
3660                                 else
3661                                     pre = ''
3662                                 end if
3663 
3664                                 !character after the escape character:
3665                                 c = s( j+1 : j+1 )
3666 
3667                                 select case (c)
3668                                 case(quotation_mark,backslash,slash,&
3669                                      'b','f','n','r','t')
3670 
3671                                     !save the bit after the escape characters:
3672                                     if (j+2<n) then
3673                                         post = s(j+2:n)
3674                                     else
3675                                         post = ''
3676                                     end if
3677 
3678                                     select case(c)
3679                                     case(quotation_mark,backslash,slash)
3680                                         !use c as is
3681                                     case('b')
3682                                         c = bspace
3683                                     case('f')
3684                                         c = formfeed
3685                                     case('n')
3686                                         c = newline
3687                                     case('r')
3688                                         c = carriage_return
3689                                     case('t')
3690                                         c = horizontal_tab
3691                                     end select
3692 
3693                                     s = pre//c//post
3694 
3695                                     n = n-1    !backslash character has been 
3696                                                ! removed from the string
3697 
3698                                 case('u')    !expecting 4 hexadecimal digits after 
3699                                              ! the escape character    [\uXXXX]
3700 
3701                                     !for now, we are just printing them as is
3702                                     ![not checking to see if it is a valid hex value]
3703 
3704                                     if (j+5<=n) then
3705                                         j=j+4
3706                                     else
3707                                         call throw_exception(&
3708                                             'Error in json_get_string:'//&
3709                                             ' Invalid hexadecimal sequence'//&
3710                                             ' in string: '//trim(c))
3711                                         exit
3712                                     end if
3713 
3714                                 case default
3715                                     !unknown escape character
3716                                     call throw_exception('Error in json_get_string:'//&
3717                                             ' unknown escape sequence in string "'//&
3718                                             trim(s)//'" ['//backslash//c//']')
3719                                     exit
3720                                 end select
3721 
3722                                 j=j+1    !go to the next character
3723 
3724                                 if (j>=n) exit    !finished
3725 
3726                             else
3727                                 !an escape character is the last character in 
3728                                 ! the string [this may not be valid syntax, 
3729                                 ! but just keep it]
3730                                 exit
3731                             end if
3732 
3733                         else
3734                             exit    !no more escape characters in the string
3735                         end if
3736 
3737                     end do
3738 
3739                     if (exception_thrown) then
3740                         if (allocated(value)) deallocate(value)
3741                     else
3742                         value = s
3743                     end if
3744 
3745                 else
3746                     call throw_exception('Error in json_get_string:'//&
3747                                          ' p%value not allocated')
3748                 end if
3749 
3750             case default
3751 
3752                 call throw_exception('Error in json_get_string:'//&
3753                                      ' Unable to resolve value to characters: '//&
3754                                      trim(path))
3755 
3756                 ! Note: for the other cases, we could do val to string conversions.
3757 
3758             end select
3759 
3760         end if
3761 
3762         if (allocated(value) .and. .not. exception_thrown) then
3763             if (present(found)) found = .true.
3764         else
3765             if (present(found)) then
3766                 found = .false.
3767                 call json_clear_exceptions()
3768             end if
3769         end if
3770 
3771         !cleanup:
3772         if (associated(p)) nullify(p)
3773         if (allocated(s)) deallocate(s)
3774         if (allocated(pre)) deallocate(pre)
3775         if (allocated(post)) deallocate(post)
3776 
3777     else
3778 
3779         value = ''
3780         found = .false.
3781 
3782     end if
3783 
3784     end subroutine json_get_string

json_module/json_get_string_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_string_vec

DESCRIPTION

    Get a string vector from a JSON file.

AUTHOR

    Jacob Williams : 5/14/2014

SOURCE

3801     subroutine json_get_string_vec(me, path, vec, found)
3802 
3803     implicit none
3804 
3805     type(json_value),pointer,intent(in)                           :: me
3806     character(kind=CK,len=*),intent(in)                           :: path
3807     character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec
3808     logical(LK),intent(out),optional                              :: found
3809 
3810     logical(LK) :: initialized
3811 
3812     initialized = .false.
3813 
3814     if (allocated(vec)) deallocate(vec)
3815 
3816     !the callback function is called for each element of the array:
3817     call json_get(me, path=path, array_callback=get_chars_from_array, found=found)
3818 
3819     contains
3820 
3821         ! callback function for chars
3822         subroutine get_chars_from_array(element, i, count)
3823         
3824         implicit none
3825 
3826         type(json_value),pointer,intent(in) :: element
3827         integer(IK),intent(in)              :: i        !index
3828         integer(IK),intent(in)              :: count    !size of array
3829 
3830         character(kind=CK,len=:),allocatable :: cval
3831 
3832         !size the output array:
3833         if (.not. initialized) then
3834             allocate(vec(count))
3835             initialized = .true.
3836         end if
3837 
3838         !populate the elements:
3839         call json_get(element, value=cval)
3840         if (allocated(cval)) then
3841             vec(i) = cval
3842             deallocate(cval)
3843         else
3844             vec(i) = ''
3845         end if
3846 
3847         end subroutine get_chars_from_array
3848 
3849     end subroutine json_get_string_vec

json_module/json_info [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_info

USAGE

    call me%info(path,found,var_type,n_children)

DESCRIPTION

    Returns information about a json_value

AUTHOR

    Jacob Williams : 2/13/2014

SOURCE

925     subroutine json_info(p,var_type,n_children)
926 
927     implicit none
928 
929     type(json_value),pointer         :: p
930     integer(IK),intent(out),optional :: var_type
931     integer(IK),intent(out),optional :: n_children
932         
933     if (present(var_type))    var_type = p%var_type  !variable type
934     if (present(n_children))  n_children = json_count(p)  !number of children
935     
936     end subroutine json_info

json_module/json_initialize [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_initialize

DESCRIPTION

    Initialize the module.
    Should be called before the routines are used.
    It can also be called after using the module and encountering exceptions.

AUTHOR

    Jacob Williams : 12/4/2013

SOURCE

1235     subroutine json_initialize(verbose)
1236 
1237     implicit none
1238     
1239     logical(LK),intent(in),optional :: verbose  !mainly useful for debugging (default is false)
1240     
1241     !optional input (if not present, value remains unchanged):
1242     if (present(verbose)) is_verbose = verbose
1243 
1244     !clear any errors from previous runs:
1245     call json_clear_exceptions()
1246 
1247     !Just in case, clear these global variables also:
1248     pushed_index = 0
1249     pushed_char  = ''
1250     char_count   = 0
1251     line_count   = 1
1252 
1253     end subroutine json_initialize

json_module/json_parse [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_parse

DESCRIPTION

    Parse the JSON file and populate the json_value tree.

    Inputs can be:
      file and unit : the specified unit is used to read JSON from file
                      [note if unit is already open, then the filename is ignored]
      file          : JSON is read from file using internal unit number
      str           : JSON data is read from the string instead

EXAMPLE

    type(json_value),pointer :: p
    call json_parse(file='myfile.json', p=p)

NOTES

    When calling this routine, any exceptions thrown from previous
        calls will automatically be cleared.

HISTORY

    Jacob Williams : 1/13/2015 : added read from string option.

SOURCE

3961     subroutine json_parse(file, p, unit, str)
3962 
3963     implicit none
3964 
3965     character(kind=CK,len=*),intent(in),optional :: file  !JSON file name
3966     type(json_value),pointer                     :: p     !output structure
3967     integer(IK),intent(in),optional              :: unit  !file unit number (/= 0)
3968     character(kind=CK,len=*),intent(in),optional :: str   !string with JSON data
3969 
3970     integer(IK) :: iunit, istat, i, i_nl_prev, i_nl
3971     character(kind=CK,len=:),allocatable :: line, arrow_str
3972     character(kind=CK,len=10) :: line_str, char_str
3973     logical(LK) :: is_open
3974     character(len=:),allocatable :: buffer
3975 
3976     !clear any exceptions and initialize:
3977     call json_initialize()
3978 
3979     if (present(unit) .and. present(file) .and. .not. present(str)) then
3980         
3981         if (unit==0) then
3982             call throw_exception('Error in json_parse: unit number must not be 0.')
3983             return      
3984         end if
3985         
3986         iunit = unit 
3987         
3988         !check to see if the file is already open
3989         ! if it is, then use it, otherwise open the file with the name given.
3990         inquire(unit=iunit, opened=is_open, iostat=istat)
3991         if (istat==0 .and. .not. is_open) then
3992            ! open the file
3993             open (  unit        = iunit, &
3994                     file        = file, &
3995                     status      = 'OLD', &
3996                     action      = 'READ', &
3997                     form        = 'FORMATTED', &
3998                     position    = 'REWIND', &
3999                     iostat      = istat)
4000         end if
4001         
4002     elseif (.not. present(unit) .and. present(file) .and. .not. present(str)) then
4003     
4004         ! open the file with a new unit number:
4005         open (  newunit     = iunit, &
4006                 file        = file, &
4007                 status      = 'OLD', &
4008                 action      = 'READ', &
4009                 form        = 'FORMATTED', &
4010                 position    = 'REWIND', &
4011                 iostat      = istat)
4012     
4013     elseif (.not. present(unit) .and. .not. present(file) .and. present(str)) then
4014     
4015         buffer = str
4016         iunit = 0    !indicates that json data will be read from buffer
4017         istat = 0
4018         
4019     else
4020         call throw_exception('Error in json_parse: Invalid inputs')
4021         return
4022     end if
4023 
4024     if (istat==0) then
4025 
4026         ! create the value and associate the pointer
4027         call json_value_create(p)
4028         
4029         ! Note: the name of the root json_value doesn't really matter,
4030         !  but we'll allocate something here just in case.
4031         if (present(file)) then
4032             p%name = trim(file)  !use the file name
4033         else
4034             p%name = ''          !if reading it from the string
4035         end if
4036         
4037         ! parse as a value
4038         call parse_value(unit=iunit, str=buffer, value=p)
4039         
4040         ! cleanup:
4041         if (allocated(buffer)) deallocate(buffer)
4042 
4043         !
4044         !  If there was an error reading the file, then
4045         !   print the line where the error occurred:
4046         !
4047         if (exception_thrown) then
4048             
4049             !the counters for the current line and the last character read:
4050             call integer_to_string(line_count, line_str)
4051             call integer_to_string(char_count, char_str)
4052             
4053             !draw the arrow string that points to the current character:
4054             arrow_str = repeat('-',max( 0, char_count - 1) )//'^'
4055         
4056             if (iunit/=0) then
4057             
4058                 call get_current_line_from_file(iunit,line)
4059                 
4060             else
4061             
4062                 !get the current line from the string:
4063                 ! [this is done by counting the newline characters]
4064                 i_nl_prev = 0  !index of previous newline character
4065                 do i=1,line_count
4066                     i_nl = index(str(i_nl_prev+1:),newline)
4067                     if (i_nl==0) then   !last line - no newline character
4068                         i_nl = len(str)+1
4069                         exit
4070                     end if
4071                     i_nl = i_nl + i_nl_prev   !index of current newline character
4072                     i_nl_prev = i_nl          !update for next iteration
4073                 end do
4074                 line = str(i_nl_prev+1 : i_nl-1)  !extract current line
4075                 
4076             end if
4077             
4078             !create the error message:
4079             err_message = err_message//newline//&
4080                            'line: '//trim(adjustl(line_str))//', '//&
4081                            'character: '//trim(adjustl(char_str))//newline//&
4082                            trim(line)//newline//arrow_str
4083                                         
4084             if (allocated(line)) deallocate(line)
4085                         
4086         end if
4087 
4088         ! close the file if necessary
4089         if (iunit/=0) close(unit=iunit, iostat=istat)
4090 
4091     else
4092 
4093         call throw_exception('Error in json_parse: Error opening file: '//trim(file))
4094         nullify(p)
4095 
4096     end if
4097 
4098     end subroutine json_parse

json_module/json_print [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_print

DESCRIPTION

    Print the json_value to a file.

EXAMPLE

    type(json_value) :: p
    ...
    call json_print(p,'test.json')  !this is json_print_2

SOURCE

448     interface json_print
449         module procedure :: json_print_1    !input is unit number
450         module procedure :: json_print_2    !input is file name
451     end interface

json_module/json_print_1 [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_print_1

DESCRIPTION

    Print the JSON structure to a file
    Input is the nonzero file unit (the file must already have been opened).

AUTHOR

    Jacob Williams, 6/20/2014

SOURCE

2603     subroutine json_print_1(me,iunit)
2604     
2605     implicit none
2606     
2607     type(json_value),pointer,intent(in) :: me
2608     integer(IK),intent(in)              :: iunit    !must be non-zero
2609     
2610     character(kind=CK,len=:),allocatable :: dummy
2611     
2612     if (iunit/=0) then
2613         call json_value_print(me,iunit,str=dummy, indent=1, colon=.true.)
2614     else
2615         call throw_exception('Error in json_print: iunit must be nonzero.')
2616     end if
2617     
2618     end subroutine json_print_1

json_module/json_print_2 [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_print_2

DESCRIPTION

    Print the JSON structure to a file.
    Input is the filename.

AUTHOR

    Jacob Williams, 12/23/2014

SOURCE

2635     subroutine json_print_2(me,filename)
2636     
2637     implicit none
2638     
2639     type(json_value),pointer,intent(in) :: me
2640     character(kind=CK,len=*),intent(in) :: filename
2641     
2642     integer(IK) :: iunit,istat
2643     
2644     open(newunit=iunit,file=filename,status='REPLACE',iostat=istat)
2645     if (istat==0) then
2646         call json_print(me,iunit)
2647         close(iunit,iostat=istat)
2648     else
2649         call throw_exception('Error in json_print: could not open file: '//&
2650                               trim(filename))
2651     end if
2652     
2653     end subroutine json_print_2

json_module/json_print_to_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_print_to_string

DESCRIPTION

    Print the json_value structure to an allocatable string.

SOURCE

428     interface json_print_to_string
429         module procedure :: json_value_to_string
430     end interface

json_module/json_remove [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_remove

DESCRIPTION

    Remove a json_value from a linked-list structure.

SOURCE

486     interface json_remove
487         module procedure :: json_value_remove
488     end interface

json_module/json_remove_if_present [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_remove_if_present

DESCRIPTION

    If the child variable is present, then remove it.

SOURCE

501     interface json_remove_if_present
502         module procedure :: json_value_remove_if_present
503     end interface

json_module/json_update [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_update

DESCRIPTION

    These are like json_add, except if a child with the same name is
     already present, then its value is simply updated.
    Note that currently, these only work for scalar variables.
    These routines can also change the variable's type (but an error will be 
     thrown if the existing variable is not a scalar).

NOTES

    It should not be used to change the type of a variable in an array,
     or it will produce an invalid JSON file.

SOURCE

390     interface json_update
391         module procedure :: json_update_logical,&
392                             json_update_double,&
393                             json_update_integer,&
394                             json_update_string
395     end interface json_update

json_module/json_update_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_update_double

DESCRIPTION

    Given the path string, if the variable is present, 
    and is a scalar, then update its value.
    If it is not present, then create it and set its value.

AUTHOR

    Jacob Williams : 12/6/2014

SOURCE

1821     subroutine json_update_double(p,name,val,found)
1822      
1823     implicit none
1824     
1825     type(json_value),pointer            :: p
1826     character(kind=CK,len=*),intent(in) :: name
1827     real(RK),intent(in)                 :: val
1828     logical(LK),intent(out)             :: found
1829     
1830     type(json_value),pointer :: p_var
1831     integer(IK) :: var_type
1832  
1833     call json_get(p,name,p_var,found)
1834     if (found) then
1835 
1836         call json_info(p_var,var_type)
1837         select case (var_type)
1838         case (json_null,json_logical,json_integer,json_double,json_string)
1839             call to_double(p_var,val)    !update the value
1840         case default
1841             found = .false.
1842             call throw_exception('Error in json_update_double: '//&
1843                                  'the variable is not a scalar value')
1844         end select          
1845 
1846     else
1847         call json_add(p,name,val)   !add the new element
1848     end if
1849                      
1850     end subroutine json_update_double

json_module/json_update_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_update_integer

DESCRIPTION

    Given the path string, if the variable is present, 
    and is a scalar, then update its value.
    If it is not present, then create it and set its value.

AUTHOR

    Jacob Williams : 12/6/2014

SOURCE

1869     subroutine json_update_integer(p,name,val,found)
1870 
1871     implicit none
1872     
1873     type(json_value),pointer            :: p
1874     character(kind=CK,len=*),intent(in) :: name
1875     integer(IK),intent(in)              :: val
1876     logical(LK),intent(out)             :: found
1877     
1878     type(json_value),pointer :: p_var
1879     integer(IK) :: var_type
1880  
1881     call json_get(p,name,p_var,found)
1882     if (found) then
1883 
1884         call json_info(p_var,var_type)
1885         select case (var_type)
1886         case (json_null,json_logical,json_integer,json_double,json_string)
1887             call to_integer(p_var,val)    !update the value
1888         case default
1889             found = .false.
1890             call throw_exception('Error in json_update_integer: '//&
1891                                  'the variable is not a scalar value')
1892         end select          
1893 
1894     else
1895         call json_add(p,name,val)   !add the new element
1896     end if
1897                      
1898     end subroutine json_update_integer

json_module/json_update_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_update_logical

DESCRIPTION

    Given the path string, if the variable is present, 
    and is a scalar, then update its value.
    If it is not present, then create it and set its value.

AUTHOR

    Jacob Williams : 12/6/2014

SOURCE

1773     subroutine json_update_logical(p,name,val,found)
1774      
1775     implicit none
1776     
1777     type(json_value),pointer            :: p
1778     character(kind=CK,len=*),intent(in) :: name
1779     logical(LK),intent(in)              :: val
1780     logical(LK),intent(out)             :: found
1781     
1782     type(json_value),pointer :: p_var
1783     integer(IK) :: var_type
1784  
1785     call json_get(p,name,p_var,found)
1786     if (found) then
1787 
1788         call json_info(p_var,var_type)
1789         select case (var_type)
1790         case (json_null,json_logical,json_integer,json_double,json_string)
1791             call to_logical(p_var,val)    !update the value
1792         case default
1793             found = .false.
1794             call throw_exception('Error in json_update_logical: '//&
1795                                  'the variable is not a scalar value')
1796         end select          
1797 
1798     else
1799         call json_add(p,name,val)   !add the new element
1800     end if
1801                      
1802     end subroutine json_update_logical

json_module/json_update_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_update_string

DESCRIPTION

    Given the path string, if the variable is present, 
    and is a scalar, then update its value.
    If it is not present, then create it and set its value.

AUTHOR

    Jacob Williams : 12/6/2014

SOURCE

1917     subroutine json_update_string(p,name,val,found)
1918      
1919     implicit none
1920     
1921     type(json_value),pointer            :: p
1922     character(kind=CK,len=*),intent(in) :: name
1923     character(kind=CK,len=*),intent(in) :: val
1924     logical(LK),intent(out)             :: found
1925     
1926     type(json_value),pointer :: p_var
1927     integer(IK) :: var_type
1928  
1929     call json_get(p,name,p_var,found)
1930     if (found) then
1931 
1932         call json_info(p_var,var_type)
1933         select case (var_type)
1934         case (json_null,json_logical,json_integer,json_double,json_string)
1935             call to_string(p_var,val)    !update the value
1936         case default
1937             found = .false.
1938             call throw_exception('Error in json_update_string: '//&
1939                                  'the variable is not a scalar value')
1940         end select          
1941 
1942     else
1943         call json_add(p,name,val)   !add the new element
1944     end if
1945                      
1946     end subroutine json_update_string

json_module/json_value [ Classes ]

[ Top ] [ json_module ] [ Classes ]

NAME

    json_value

DESCRIPTION

    Type used to construct the linked-list json structure

EXAMPLE

    type(json_value),pointer :: p
    call json_create_object(p) 
    call json_add(p,'year',1805)
    call json_add(p,'value',1.0d0)
    call json_print(p,'test.json')
    call json_destroy(p)

SOURCE

198         type,public :: json_value
199             
200         !force the constituents to be stored contiguously
201         ![note: on Intel, the order of the variables below
202         ! is significant to avoid the misaligned field warnings]
203         sequence  
204 
205         !for the linked list:
206         type(json_value),pointer :: previous => null()
207         type(json_value),pointer :: next     => null()
208         type(json_value),pointer :: parent   => null()
209         type(json_value),pointer :: children => null()
210         type(json_value),pointer :: tail     => null()
211 
212         !variable name:
213         character(kind=CK,len=:),allocatable :: name
214 
215         !the data for this variable:
216         real(RK),allocatable                 :: dbl_value
217         logical(LK),allocatable              :: log_value
218         character(kind=CK,len=:),allocatable :: str_value
219         integer(IK),allocatable              :: int_value
220         
221         integer(IK) :: var_type = json_unknown  !variable type
222     
223         integer(IK),private :: n_children = 0   !number of children
224 
225         end type json_value

json_module/json_value_add_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_add_double

DESCRIPTION

    Add a real value to the structure.

    These routines are part of the public API that can be
        used to build a json structure using data.

AUTHOR

    Jacob Williams : 1/19/2014

SOURCE

2010     subroutine json_value_add_double(me, name, val)
2011 
2012     implicit none
2013 
2014     type(json_value),pointer            :: me
2015     character(kind=CK,len=*),intent(in) :: name
2016     real(RK),intent(in)                 :: val
2017 
2018     type(json_value),pointer :: var
2019 
2020     !create the variable:
2021     call json_value_create(var)
2022     call to_double(var,val,name)
2023 
2024     !add it:
2025     call json_add(me, var)
2026 
2027     !cleanup:
2028     nullify(var)
2029 
2030     end subroutine json_value_add_double

json_module/json_value_add_double_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_add_double_vec

DESCRIPTION

    Add a real vector to the structure.

    These routines are part of the public API that can be
        used to build a json structure using data.

AUTHOR

    Jacob Williams : 1/20/2014

SOURCE

2050     subroutine json_value_add_double_vec(me, name, val)
2051 
2052     implicit none
2053 
2054     type(json_value),pointer            :: me
2055     character(kind=CK,len=*),intent(in) :: name
2056     real(RK),dimension(:),intent(in)    :: val
2057 
2058     type(json_value),pointer :: var
2059     integer(IK) :: i
2060 
2061     !create the variable as an array:
2062     call json_value_create(var)
2063     call to_array(var,name)
2064 
2065     !populate the array:
2066     do i=1,size(val)
2067         call json_add(var, '', val(i))
2068     end do
2069 
2070     !add it:
2071     call json_add(me, var)
2072 
2073     !cleanup:
2074     nullify(var)
2075 
2076     end subroutine json_value_add_double_vec

json_module/json_value_add_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_add_integer

DESCRIPTION

    Add an integer value to the structure.

    These routines are part of the public API that can be
        used to build a json structure using data.

AUTHOR

    Jacob Williams : 1/20/2014

SOURCE

2096     subroutine json_value_add_integer(me, name, val)
2097 
2098     implicit none
2099 
2100     type(json_value),pointer            :: me
2101     character(kind=CK,len=*),intent(in) :: name
2102     integer(IK),intent(in)              :: val
2103 
2104     type(json_value),pointer :: var
2105 
2106     !create the variable:
2107     call json_value_create(var)
2108     call to_integer(var,val,name)
2109 
2110     !add it:
2111     call json_add(me, var)
2112 
2113     !cleanup:
2114     nullify(var)
2115 
2116     end subroutine json_value_add_integer

json_module/json_value_add_integer_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_add_integer_vec

DESCRIPTION

    Add an integer vector to the structure.

    These routines are part of the public API that can be
        used to build a json structure using data.

AUTHOR

    Jacob Williams : 1/20/2014

SOURCE

2136     subroutine json_value_add_integer_vec(me, name, val)
2137 
2138     implicit none
2139 
2140     type(json_value),pointer            :: me
2141     character(kind=CK,len=*),intent(in) :: name
2142     integer(IK),dimension(:),intent(in) :: val
2143 
2144     type(json_value),pointer :: var
2145     integer(IK) :: i    !counter
2146 
2147     !create the variable as an array:
2148     call json_value_create(var)
2149     call to_array(var,name)
2150 
2151     !populate the array:
2152     do i=1,size(val)
2153         call json_add(var, '', val(i))
2154     end do
2155 
2156     !add it:
2157     call json_add(me, var)
2158 
2159     !cleanup:
2160     nullify(var)
2161 
2162     end subroutine json_value_add_integer_vec

json_module/json_value_add_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_add_logical

DESCRIPTION

    Add a logical value to the structure.

    These routines are part of the public API that can be
        used to build a json structure using data.

AUTHOR

    Jacob Williams : 1/20/2014

SOURCE

2182     subroutine json_value_add_logical(me, name, val)
2183 
2184     implicit none
2185 
2186     type(json_value),pointer            :: me
2187     character(kind=CK,len=*),intent(in) :: name
2188     logical(LK),intent(in)              :: val
2189 
2190     type(json_value),pointer :: var
2191 
2192     !create the variable:
2193     call json_value_create(var)
2194     call to_logical(var,val,name)
2195 
2196     !add it:
2197     call json_add(me, var)
2198 
2199     !cleanup:
2200     nullify(var)
2201 
2202     end subroutine json_value_add_logical

json_module/json_value_add_logical_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_add_logical_vec

DESCRIPTION

    Add a logical vector to the structure.

    These routines are part of the public API that can be
        used to build a json structure using data.

AUTHOR

    Jacob Williams : 1/20/2014

SOURCE

2222     subroutine json_value_add_logical_vec(me, name, val)
2223 
2224     implicit none
2225 
2226     type(json_value),pointer            :: me
2227     character(kind=CK,len=*),intent(in) :: name
2228     logical(LK),dimension(:),intent(in) :: val
2229 
2230     type(json_value),pointer :: var
2231     integer(IK) :: i    !counter
2232 
2233     !create the variable as an array:
2234     call json_value_create(var)
2235     call to_array(var,name)
2236 
2237     !populate the array:
2238     do i=1,size(val)
2239         call json_add(var, '', val(i))
2240     end do
2241 
2242     !add it:
2243     call json_add(me, var)
2244 
2245     !cleanup:
2246     nullify(var)
2247 
2248     end subroutine json_value_add_logical_vec

json_module/json_value_add_member [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_add_member

DESCRIPTION

    Adds the member as a child of this.

SOURCE

1960     subroutine json_value_add_member(this, member)
1961 
1962     implicit none
1963 
1964     type(json_value),pointer :: this, member
1965 
1966     if (.not. exception_thrown) then
1967 
1968         ! associate the parent
1969         member%parent => this
1970 
1971         ! add to linked list
1972         if (associated(this%children)) then
1973 
1974             this%tail%next => member
1975             member%previous => this%tail
1976 
1977         else
1978 
1979             this%children => member
1980             member%previous => null()  !first in the list
1981 
1982         end if
1983         
1984         ! new member is now the last one in the list
1985         this%tail => member
1986         this%n_children = this%n_children + 1
1987 
1988     end if
1989 
1990     end subroutine json_value_add_member

json_module/json_value_add_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_add_string

DESCRIPTION

    Add a character string the structure.

    These routines are part of the public API that can be
        used to build a json structure using data.

AUTHOR

    Jacob Williams : 1/19/2014

SOURCE

2268     subroutine json_value_add_string(me, name, val)
2269 
2270     implicit none
2271 
2272     type(json_value),pointer            :: me
2273     character(kind=CK,len=*),intent(in) :: name
2274     character(kind=CK,len=*),intent(in) :: val
2275 
2276     type(json_value),pointer :: var
2277     character(kind=CK,len=:),allocatable :: str
2278 
2279     !add escape characters if necessary:
2280     call escape_string(val, str)
2281 
2282     !create the variable:
2283     call json_value_create(var)
2284     call to_string(var,str,name)
2285 
2286     !add it:
2287     call json_add(me, var)
2288 
2289     !cleanup:
2290     nullify(var)
2291 
2292     end subroutine json_value_add_string

json_module/json_value_add_string_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_add_string_vec

DESCRIPTION

    Add an array of character strings to the structure.

    These routines are part of the public API that can be
        used to build a json structure using data.

AUTHOR

    Jacob Williams : 1/19/2014

SOURCE

2365     subroutine json_value_add_string_vec(me, name, val, trim_str, adjustl_str)
2366 
2367     implicit none
2368 
2369     type(json_value),pointer                         :: me
2370     character(kind=CK,len=*),intent(in)              :: name
2371     character(kind=CK,len=*),dimension(:),intent(in) :: val
2372     logical(LK),intent(in),optional                  :: trim_str
2373     logical(LK),intent(in),optional                  :: adjustl_str
2374 
2375     type(json_value),pointer :: var
2376     integer(IK) :: i
2377     logical(LK) :: trim_string, adjustl_string
2378     character(kind=CK,len=:),allocatable :: str
2379 
2380     !if the string is to be trimmed or not:
2381     if (present(trim_str)) then
2382         trim_string = trim_str
2383     else
2384         trim_string = .false.
2385     end if
2386     if (present(adjustl_str)) then
2387         adjustl_string = adjustl_str
2388     else
2389         adjustl_string = .false.
2390     end if
2391 
2392     !create the variable as an array:
2393     call json_value_create(var)
2394     call to_array(var,name)
2395 
2396     !populate the array:
2397     do i=1,size(val)
2398 
2399         !the string to write:
2400         str = val(i)
2401         if (adjustl_string) str = adjustl(str)
2402         if (trim_string)    str = trim(str)
2403 
2404         !write it:
2405         call json_add(var, '', str)
2406 
2407         !cleanup
2408         deallocate(str)
2409 
2410     end do
2411 
2412     !add it:
2413     call json_add(me, var)
2414 
2415     !cleanup:
2416     nullify(var)
2417 
2418     end subroutine json_value_add_string_vec

json_module/json_value_destroy [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_destroy

DESCRIPTION

    Destroy a json_value linked-list structure.

NOTES

    This routine does not check for exceptions.
    I believe the original version of this routine was not
        properly freeing the memory.  It has been rewritten.

AUTHOR

    Jacob Williams : 1/22/2014

SOURCE

1463     recursive subroutine json_value_destroy(this)
1464 
1465     implicit none
1466 
1467     type(json_value),pointer :: this
1468 
1469     if (associated(this)) then
1470 
1471         if (allocated(this%name)) deallocate(this%name)
1472 
1473         call destroy_json_data(this)
1474 
1475         if (associated(this%children)) call json_value_destroy(this%children)
1476         this%n_children = 0
1477         
1478         if (associated(this%next)) call json_value_destroy(this%next)
1479 
1480         if (associated(this%previous)) nullify(this%previous)
1481         if (associated(this%parent))   nullify(this%parent)
1482         if (associated(this%tail))     nullify(this%tail)
1483         
1484         deallocate(this)
1485 
1486         nullify(this)
1487 
1488     end if
1489 
1490     end subroutine json_value_destroy

json_module/json_value_get_by_index [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_get_by_index

DESCRIPTION

    Returns a child in the object or array given the index.

SOURCE

2460     subroutine json_value_get_by_index(this, idx, p)
2461 
2462     implicit none
2463 
2464     type(json_value),pointer,intent(in) :: this
2465     integer(IK),intent(in)              :: idx
2466     type(json_value),pointer            :: p
2467 
2468     integer(IK) :: i
2469     
2470     nullify(p)
2471 
2472     if (.not. exception_thrown) then
2473 
2474         if (associated(this%children)) then
2475 
2476             p => this%children
2477 
2478             do i = 1, idx - 1
2479 
2480                 if (associated(p%next)) then
2481                     p => p%next
2482                 else
2483                     call throw_exception('Error in json_value_get_by_index:'//&
2484                                          ' p%next is not associated.')
2485                     nullify(p)
2486                     return
2487                 end if
2488 
2489             end do
2490 
2491         else
2492 
2493             call throw_exception('Error in json_value_get_by_index:'//&
2494                                  ' this%children is not associated.')
2495 
2496         end if
2497 
2498     end if
2499 
2500     end subroutine json_value_get_by_index

json_module/json_value_get_by_name_chars [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_get_by_name_chars

DESCRIPTION

    Returns a child in the object or array given the name string.

NOTES

    It is a case-sensitive search, and the name string is not trimmed,
        So, for example, 'a ' /= 'A ' /= 'a  '
    Note that the name is not parsed like it is in json_get_by_path.

SOURCE

2519     subroutine json_value_get_by_name_chars(this, name, p)
2520 
2521     implicit none
2522 
2523     type(json_value),pointer,intent(in) :: this
2524     character(kind=CK,len=*),intent(in) :: name
2525     type(json_value),pointer            :: p
2526 
2527     integer(IK) :: i,n_children
2528     
2529     nullify(p)
2530 
2531     if (.not. exception_thrown) then
2532 
2533         if (associated(this)) then
2534 
2535             if (this%var_type==json_object) then
2536                 n_children = json_count(this)
2537                 p => this%children    !start with first one
2538                 do i=1, n_children
2539                     if (allocated(p%name)) then
2540                         if (p%name == name) return
2541                     end if
2542                     p => p%next
2543                 end do
2544             end if
2545 
2546             !did not find anything:
2547             call throw_exception('Error in json_value_get_by_name_chars: '//&
2548                                  'child variable '//trim(name)//' was not found.')
2549             nullify(p)
2550 
2551         else
2552             call throw_exception('Error in json_value_get_by_name_chars: '//&
2553                                  'pointer is not associated.')
2554         end if
2555 
2556     end if
2557 
2558     end subroutine json_value_get_by_name_chars

json_module/json_value_remove [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_remove

DESCRIPTION

    Remove a json_value (and all its children) 
      from a linked-list structure, preserving the rest of the structure.
    If destroy is not present, it is also destroyed.
    If destroy is present and true, it is destroyed.
    If destroy is present and false, it is not destroyed.

EXAMPLE

    !to extract an object from one json structure, and add it to another:
    type(json_value),pointer :: json1,json2,p
    logical :: found
    [create and populate json1 and json2]
    call json_get(json1,'name',p,found)  ! get pointer to name element of json1
    call json_remove(p,destroy=.false.)  ! remove it from json1 (don't destroy)
    call json_add(json2,p)         ! add it to json2

    !to remove an object from a json structure (and destroy it)
    type(json_value),pointer :: json1,p
    logical :: found
    [create and populate json1]
    call json_get(json1,'name',p,found)  ! get pointer to name element of json1
    call json_remove(p)                  ! remove and destroy it

AUTHOR

    Jacob Williams : 9/9/2014 

HISTORY

    JW : 12/28/2014 : added destroy optional argument.

SOURCE

1531     subroutine json_value_remove(me,destroy)
1532 
1533     implicit none
1534 
1535     type(json_value),pointer        :: me
1536     logical(LK),intent(in),optional :: destroy
1537     
1538     type(json_value),pointer :: parent,previous,next
1539     logical(LK) :: destroy_it
1540         
1541     if (associated(me)) then
1542         
1543         !optional input argument:
1544         if (present(destroy)) then
1545             destroy_it = destroy
1546         else
1547             destroy_it = .true.
1548         end if
1549     
1550         if (associated(me%parent)) then
1551         
1552             parent => me%parent                    
1553            
1554             if (associated(me%next)) then
1555         
1556                 !there are later items in the list:
1557             
1558                 next => me%next
1559                 nullify(me%next)
1560             
1561                 if (associated(me%previous)) then   
1562                     !there are earlier items in the list
1563                     previous => me%previous                
1564                     previous%next => next
1565                     next%previous => previous
1566                 else
1567                     !this is the first item in the list
1568                     parent%children => next
1569                     nullify(next%previous)
1570                 end if
1571                 
1572             else
1573             
1574                 if (associated(me%previous)) then
1575                     !there are earlier items in the list:
1576                     previous => me%previous 
1577                     nullify(previous%next)  
1578                     parent%tail => previous
1579                 else
1580                     !this is the only item in the list:
1581                     nullify(parent%children)
1582                     nullify(parent%tail)            
1583                 end if
1584                 
1585             end if
1586             
1587             parent%n_children = parent%n_children - 1
1588                    
1589         end if
1590             
1591         if (destroy_it) call json_value_destroy(me)
1592 
1593     end if
1594 
1595     end subroutine json_value_remove

json_module/json_value_remove_if_present [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_remove_if_present

DESCRIPTION

    Given the path string, remove the variable from 
    the json_value structure, if it exists.

AUTHOR

    Jacob Williams : 12/6/2014

SOURCE

1613     subroutine json_value_remove_if_present(p,name)
1614      
1615     implicit none
1616     
1617     type(json_value),pointer            :: p
1618     character(kind=CK,len=*),intent(in) :: name
1619     
1620     type(json_value),pointer :: p_var
1621     logical(LK) :: found
1622     
1623     call json_get(p,name,p_var,found)
1624     if (found) call json_remove(p_var)
1625      
1626     end subroutine json_value_remove_if_present

json_module/json_value_to_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_to_string

DESCRIPTION

    Print the JSON structure to an allocatable string.

AUTHOR

    Jacob Williams : 2/12/2014

SOURCE

2575     subroutine json_value_to_string(me,str)
2576 
2577     implicit none
2578     
2579     type(json_value),pointer,intent(in)              :: me
2580     character(kind=CK,len=:),intent(out),allocatable :: str
2581     
2582     str = ''
2583     call json_value_print(me, iunit=0, str=str, indent=1, colon=.true.)
2584         
2585     end subroutine json_value_to_string

json_module/kinds [ Definitions ]

[ Top ] [ json_module ] [ Definitions ]

NAME

    kinds

DESCRIPTION

   Kind definitions for real, integer, character, and logical variables.
   The sizes given here are valid for the Intel and Gfortran compilers 
    (and perhaps others)

SOURCE

113     !default real kind [8 bytes]
114     integer,parameter :: RK = real64     
115             
116     !default integer kind [4 bytes]
117     integer,parameter :: IK = int32  
118                 
119     !default character kind [1 byte]    
120     integer,parameter :: CK = character_kinds(1) 
121     
122     !default logical kind [4 bytes]  
123     !The statement here is to ensure a valid kind
124     ! if the compiler doesn't have a logical_kinds(3)
125     integer,parameter :: LK = logical_kinds(min(3,size(logical_kinds)))

json_module/var_type [ Definitions ]

[ Top ] [ json_module ] [ Definitions ]

NAME

    var_type

DESCRIPTION

   The types of JSON data.  
   These are the values returned by the var_type arguments
    of the routines json_file_variable_info and json_info.

SOURCE

169     integer(IK),parameter,public :: json_unknown   = 0
170     integer(IK),parameter,public :: json_null      = 1
171     integer(IK),parameter,public :: json_object    = 2
172     integer(IK),parameter,public :: json_array     = 3
173     integer(IK),parameter,public :: json_logical   = 4
174     integer(IK),parameter,public :: json_integer   = 5
175     integer(IK),parameter,public :: json_double    = 6
176     integer(IK),parameter,public :: json_string    = 7