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, associate, newunit, generic, class, 
            and 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/destroy_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    destroy_json_file

USAGE

    call me%destroy()

DESCRIPTION

    Destroy the JSON file.

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

530     subroutine destroy_json_file(me)
531     
532     implicit none
533 
534     class(json_file),intent(inout) :: me
535 
536     if (associated(me%p)) call json_value_destroy(me%p)
537 
538     end subroutine destroy_json_file

json_module/get_by_index [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_by_index

DESCRIPTION

    Returns a child in the object given the index.

SOURCE

2096     subroutine get_by_index(this, idx, p)
2097 
2098     implicit none
2099 
2100     type(json_value),pointer,intent(in) :: this
2101     integer,intent(in)                  :: idx
2102     type(json_value), pointer           :: p
2103 
2104     integer :: i
2105 
2106     if (.not. exception_thrown) then
2107 
2108         nullify(p)
2109 
2110         if (associated(this%children)) then
2111 
2112             p => this%children
2113 
2114             do i = 1, idx - 1
2115 
2116                 if (associated(p%next)) then
2117                     p => p%next
2118                 else
2119                     call throw_exception('Error in get_by_index:'//&
2120                                          ' p%next is not associated.')
2121                     return
2122                 end if
2123 
2124             end do
2125 
2126         else
2127 
2128             call throw_exception('Error in get_by_index:'//&
2129                                  ' this%children is not associated.')
2130 
2131         end if
2132 
2133     end if
2134 
2135     end subroutine get_by_index

json_module/get_by_name_chars [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_by_name_chars

DESCRIPTION

    Returns a child in the object given the name string.

SOURCE

2149     subroutine get_by_name_chars(this, name, p)
2150 
2151     implicit none
2152 
2153     type(json_value),pointer,intent(in) :: this
2154     character(len=*),intent(in)         :: name
2155     type(json_value),pointer            :: p
2156 
2157     integer :: i
2158 
2159     if (.not. exception_thrown) then
2160 
2161         if (associated(this)) then
2162 
2163             nullify(p)
2164 
2165             if (this%data%var_type==json_object) then
2166                 do i=1, json_value_count(this)
2167                     call json_value_get(this, i, p)
2168                     if (allocated(p%name)) then
2169                         if (p%name == name) return
2170                     end if
2171                 end do
2172             end if
2173 
2174             !did not find anything:
2175             nullify(p)
2176 
2177         else
2178             call throw_exception('Error in get_by_name_chars: pointer is not associated.')
2179         end if
2180 
2181     end if
2182 
2183     end subroutine get_by_name_chars

json_module/get_char_vec_from_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_char_vec_from_json_file

USAGE

    call me%get(path,vec)

DESCRIPTION

    Get a char vector from a JSON file.

AUTHOR

    Jacob Williams : 1/19/2014

SOURCE

1003     subroutine get_char_vec_from_json_file(me, path, vec, found)
1004 
1005     implicit none
1006 
1007     class(json_file),intent(inout)                         :: me
1008     character(len=*),intent(in)                            :: path
1009     character(len=*),dimension(:),allocatable,intent(out)  :: vec
1010     logical,intent(out),optional                           :: found
1011 
1012     call json_get(me%p, path, vec, found)
1013 
1014     end subroutine get_char_vec_from_json_file

json_module/get_chars_from_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_chars_from_json_file

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

972     subroutine get_chars_from_json_file(me, path, val, found)
973 
974     implicit none
975 
976     class(json_file),intent(inout)              :: me
977     character(len=*),intent(in)                 :: path
978     character(len=:),allocatable,intent(out)    :: val
979     logical,intent(out),optional                :: found
980 
981     call json_get(me%p, path=path, value=val, found=found)
982 
983     end subroutine get_chars_from_json_file

json_module/get_double_from_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_double_from_json_file

USAGE

    call me%get(path,val,found)

DESCRIPTION

    Get a double from a JSON file.

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

847     subroutine get_double_from_json_file (me, path, val, found)
848 
849     implicit none
850 
851     class(json_file),intent(inout)  :: me
852     character(len=*),intent(in)     :: path
853     real(wp),intent(out)            :: val
854     logical,intent(out),optional    :: found
855 
856     call json_get(me%p, path=path, value=val, found=found)
857 
858     end subroutine get_double_from_json_file

json_module/get_double_vec_from_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_double_vec_from_json_file

USAGE

    call me%get(path,vec,found)

DESCRIPTION

    Get a double vector from a JSON file.

AUTHOR

    Jacob Williams : 1/19/2014

SOURCE

878     subroutine get_double_vec_from_json_file(me, path, vec, found)
879     
880     implicit none
881 
882     class(json_file),intent(inout)                  :: me
883     character(len=*),intent(in)                     :: path
884     real(wp),dimension(:),allocatable,intent(out)   :: vec
885     logical,intent(out),optional                    :: found
886 
887     call json_get(me%p, path, vec, found)
888 
889     end subroutine get_double_vec_from_json_file

json_module/get_integer_from_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_integer_from_json_file

USAGE

    call me%get(path,val)

DESCRIPTION

    Get an integer from a JSON file.

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

785     subroutine get_integer_from_json_file(me, path, val, found)
786 
787     implicit none
788 
789     class(json_file),intent(inout)    :: me
790     character(len=*),intent(in)       :: path
791     integer,intent(out)               :: val
792     logical,intent(out),optional      :: found
793 
794     call json_get(me%p, path=path, value=val, found=found)
795 
796     end subroutine get_integer_from_json_file

json_module/get_integer_vec_from_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_integer_vec_from_json_file

USAGE

    call me%get(path,vec)

DESCRIPTION

    Get an integer vector from a JSON file.

AUTHOR

    Jacob Williams : 1/20/2014

SOURCE

816     subroutine get_integer_vec_from_json_file(me, path, vec, found)
817 
818     implicit none
819 
820     class(json_file),intent(inout)                  :: me
821     character(len=*),intent(in)                     :: path
822     integer,dimension(:),allocatable,intent(out)    :: vec
823     logical,intent(out),optional                    :: found
824 
825     call json_get(me%p, path, vec, found)
826 
827     end subroutine get_integer_vec_from_json_file

json_module/get_logical_from_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_logical_from_json_file

USAGE

    call me%get(path,val,found)

DESCRIPTION

    Get a logical from a JSON file.

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

909     subroutine get_logical_from_json_file(me,path,val,found)
910 
911     implicit none
912 
913     class(json_file),intent(inout)  :: me
914     character(len=*),intent(in)     :: path
915     logical,intent(out)             :: val
916     logical,intent(out),optional    :: found
917 
918     call json_get(me%p, path=path, value=val, found=found)
919 
920     end subroutine get_logical_from_json_file

json_module/get_logical_vec_from_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_logical_vec_from_json_file

USAGE

    call me%get(path,vec)

DESCRIPTION

    Get a logical vector from a JSON file.

AUTHOR

    Jacob Williams : 1/20/2014

SOURCE

940     subroutine get_logical_vec_from_json_file(me, path, vec, found)
941 
942     implicit none
943 
944     class(json_file),intent(inout)                 :: me
945     character(len=*),intent(in)                    :: path
946     logical,dimension(:),allocatable,intent(out)   :: vec
947     logical,intent(out),optional                   :: found
948     
949     call json_get(me%p, path, vec, found)
950 
951     end subroutine get_logical_vec_from_json_file

json_module/get_object_from_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    get_object_from_json_file

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

754     subroutine get_object_from_json_file(me, path, p, found)
755     
756     implicit none
757 
758     class(json_file),intent(inout)          :: me
759     character(len=*),intent(in)             :: path
760     type(json_value),pointer,intent(out)    :: p
761     logical,intent(out),optional            :: found
762 
763     call json_get_by_path(me%p, path=path, p=p, found=found)
764 
765     end subroutine get_object_from_json_file

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

1134     subroutine json_check_for_errors(status_ok, error_msg)
1135 
1136     implicit none
1137 
1138     logical,intent(out) :: status_ok
1139     character(len=:),allocatable,intent(out) :: error_msg
1140 
1141     status_ok = .not. exception_thrown
1142 
1143     if (.not. status_ok) then
1144         if (allocated(err_message)) then
1145             error_msg = err_message
1146         else
1147             error_msg = 'Unknown Error'
1148         end if
1149     else
1150         error_msg = ''
1151     end if
1152 
1153     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

1063     subroutine json_clear_exceptions()
1064 
1065     implicit none
1066 
1067     !clear the flag and message:
1068     exception_thrown = .false.
1069     err_message = ''
1070 
1071     end subroutine json_clear_exceptions

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

404     interface json_destroy
405         module procedure :: json_value_destroy
406     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

1185     function json_failed() result(failed)
1186 
1187     implicit none
1188 
1189     logical :: failed
1190 
1191     failed = exception_thrown
1192 
1193     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(wp) :: rval
    character(len=:),allocatable :: cval
    logical :: found
    call json%load_file(filename='myfile.json')
    call json%print_file()
    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

224         private
225 
226         type(json_value), pointer :: p => null()    !the JSON structure read from the file
227 
228         contains
229 
230         procedure,public :: load_file   => load_json_file
231         procedure,public :: print_file  => print_json_file
232         procedure,public :: destroy     => destroy_json_file
233         procedure,public :: move        => move_pointer_in_json_file
234         procedure,public :: info        => variable_info_in_file
235 
236         generic,public :: get => get_pointer,&
237                                  get_integer,&
238                                  get_double,&
239                                  get_logical,&
240                                  get_chars,&
241                                  get_double_vec,&
242                                  get_char_vec,&
243                                  get_integer_vec,&
244                                  get_logical_vec
245 
246         !scalars:
247         procedure :: get_pointer        => get_object_from_json_file
248         procedure :: get_integer        => get_integer_from_json_file
249         procedure :: get_double         => get_double_from_json_file
250         procedure :: get_logical        => get_logical_from_json_file
251         procedure :: get_chars          => get_chars_from_json_file
252 
253         !vectors:
254         procedure :: get_integer_vec    => get_integer_vec_from_json_file
255         procedure :: get_double_vec     => get_double_vec_from_json_file
256         procedure :: get_logical_vec    => get_logical_vec_from_json_file
257         procedure :: get_char_vec       => get_char_vec_from_json_file

json_module/json_get [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get

DESCRIPTION

    Get data from a json_value linked list.

SOURCE

341     interface json_get
342         module procedure :: json_get_by_path
343         module procedure :: json_get_integer, json_get_integer_vec
344         module procedure :: json_get_double,  json_get_double_vec
345         module procedure :: json_get_logical, json_get_logical_vec
346         module procedure :: json_get_chars,   json_get_char_vec
347         module procedure :: json_get_array
348     end interface json_get

json_module/json_get_array [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_array

DESCRIPTION

    Get an array from a json_value.
    This routine calls the user-supplied array_callback subroutine
        for each element in the array.

SOURCE

3455     subroutine json_get_array(this, path, array_callback, found)
3456 
3457     implicit none
3458 
3459     type(json_value),pointer,intent(in)  :: this
3460     character(len=*),intent(in),optional :: path
3461     procedure(array_callback_func)       :: array_callback
3462     logical,intent(out),optional         :: found
3463 
3464     type(json_value), pointer :: element,p
3465     integer :: i, count
3466 
3467     if (.not. exception_thrown) then
3468 
3469         nullify(p)
3470 
3471         ! resolve the path to the value
3472         if (present(path)) then
3473             call json_get_by_path(this=this, path=path, p=p)
3474         else
3475             p => this
3476         end if
3477 
3478         if (.not.associated(p)) then
3479 
3480             call throw_exception('Error in json_get_array:'//&
3481                                  ' Unable to resolve path: '//trim(path))
3482 
3483         else
3484 
3485             !associate (d => p%data)
3486                 select case (p%data%var_type)
3487                 case (json_array)
3488                     count = json_value_count(p)
3489                     do i = 1, count
3490                         call json_value_get(p, i, element)
3491                         call array_callback(element, i, count)
3492                     end do
3493                 case default
3494                     call throw_exception('Error in json_get_array:'//&
3495                                          ' Resolved value is not an array. '//trim(path))
3496                 end select
3497             !end associate
3498 
3499             !cleanup:
3500             if (associated(p))       nullify(p)
3501             if (associated(element)) nullify(element)
3502 
3503         end if
3504 
3505         if (exception_thrown) then
3506             if (present(found)) then
3507                 found = .false.
3508                 call json_clear_exceptions()
3509             end if
3510         else
3511             if (present(found)) found = .true.
3512         end if
3513 
3514     else
3515         if (present(found)) found = .false.
3516     end if
3517 
3518     end subroutine json_get_array

json_module/json_get_by_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_by_path

DESCRIPTION

    Returns the json_value pointer given the path string.

NOTES

     $         root
     @         this
     .         child object member
     [] or ()  child array element

SOURCE

2535     subroutine json_get_by_path(this, path, p, found)
2536 
2537     implicit none
2538 
2539     type(json_value),pointer,intent(in)     :: this
2540     character(len=*),intent(in)             :: path
2541     type(json_value),pointer,intent(out)    :: p
2542     logical,intent(out),optional            :: found
2543 
2544     integer :: i, length, child_i
2545     character(len=1) :: c
2546     logical :: array
2547     type(json_value),pointer :: tmp
2548 
2549     if (.not. exception_thrown) then
2550 
2551         nullify(p)
2552 
2553         ! default to assuming relative to this
2554         p => this
2555 
2556         child_i = 1
2557 
2558         array = .false.
2559 
2560         length = len_trim(path)
2561 
2562         do i=1, length
2563 
2564             c = path(i:i)
2565 
2566             select case (c)
2567             case ('$')
2568 
2569                 ! root
2570                 do while (associated (p % parent))
2571                     p => p % parent
2572                 end do
2573                 child_i = i + 1
2574 
2575             case ('@')
2576 
2577                 ! this
2578                 p => this
2579                 child_i = i + 1
2580 
2581             case ('.')
2582 
2583                 ! get child member from p
2584                 if (child_i < i) then
2585                     nullify(tmp)
2586                     call json_value_get(p, path(child_i:i-1), tmp)
2587                     p => tmp
2588                     nullify(tmp)
2589                 else
2590                     child_i = i + 1
2591                     cycle
2592                 end if
2593 
2594                 if (.not.associated(p)) then
2595                     call throw_exception('Error in json_get_by_path:'//&
2596                                          ' Error getting child member.')
2597                     exit
2598                 end if
2599 
2600                 child_i = i+1
2601 
2602             case ('[','(')
2603 
2604                 !....Modified to allow for 'var[3]' style syntax
2605                 !Note: jmozmoz/fson has a slightly different version of this...
2606 
2607                 ! start looking for the array element index
2608                 array = .true.
2609 
2610                 ! get child member from p
2611                 if (child_i < i) then
2612                     nullify(tmp)
2613                     call json_value_get(p, path(child_i:i-1), tmp)
2614                     p => tmp
2615                     nullify(tmp)
2616                 else
2617                     child_i = i + 1
2618                     cycle
2619                 end if
2620                 if (.not.associated(p)) then
2621                     call throw_exception('Error in json_get_by_path:'//&
2622                                          ' Error getting array element')
2623                     exit
2624                 end if
2625                 child_i = i + 1
2626 
2627             case (']',')')
2628 
2629                 if (.not.array) then
2630                     call throw_exception('Error in json_get_by_path: Unexpected ]')
2631                     exit
2632                 end if
2633                 array = .false.
2634                 child_i = string_to_integer(path(child_i:i-1))
2635 
2636                 nullify(tmp)
2637                 call json_value_get(p, child_i, tmp)
2638                 p => tmp
2639                 nullify(tmp)
2640 
2641                 child_i= i + 1
2642 
2643             end select
2644 
2645         end do
2646 
2647         if (exception_thrown) then
2648 
2649             if (present(found)) then
2650                 found = .false.
2651                 call json_clear_exceptions()
2652             end if
2653 
2654         else
2655 
2656             ! grab the last child if present in the path
2657             if (child_i <= length) then
2658                 nullify(tmp)
2659                 call json_value_get(p, path(child_i:i-1), tmp)
2660                 p => tmp
2661                 nullify(tmp)
2662             end if
2663             if (associated(p)) then
2664                 if (present(found)) found = .true.    !everything seems to be ok
2665             else
2666                 call throw_exception('Error in json_get_by_path:'//&
2667                                      ' variable not found: '//trim(path))
2668                 if (present(found)) then
2669                     found = .false.
2670                     call json_clear_exceptions()
2671                 end if
2672             end if
2673             
2674         end if
2675 
2676     else
2677         if (present(found)) found = .false.
2678     end if
2679 
2680     end subroutine json_get_by_path

json_module/json_get_char_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_char_vec

DESCRIPTION

    Get a char vector from a JSON file.

AUTHOR

    Jacob Williams : 5/14/2014

SOURCE

3391     subroutine json_get_char_vec(me, path, vec, found)
3392 
3393     implicit none
3394 
3395     type(json_value),pointer,intent(in)                    :: me
3396     character(len=*),intent(in)                            :: path
3397     character(len=*),dimension(:),allocatable,intent(out)  :: vec
3398     logical,intent(out),optional                           :: found
3399 
3400     logical :: initialized
3401 
3402     initialized = .false.
3403 
3404     if (allocated(vec)) deallocate(vec)
3405 
3406     !the callback function is called for each element of the array:
3407     call json_get(me, path=path, array_callback=get_chars_from_array, found=found)
3408 
3409     contains
3410 
3411         ! callback function for chars
3412         subroutine get_chars_from_array(element, i, count)
3413         
3414         implicit none
3415 
3416         type(json_value),pointer,intent(in)  :: element
3417         integer,intent(in)                   :: i        !index
3418         integer,intent(in)                   :: count    !size of array
3419 
3420         character(len=:),allocatable :: cval
3421 
3422         !size the output array:
3423         if (.not. initialized) then
3424             allocate(vec(count))
3425             initialized = .true.
3426         end if
3427 
3428         !populate the elements:
3429         call json_get(element, value=cval)
3430         if (allocated(cval)) then
3431             vec(i) = cval
3432             deallocate(cval)
3433         else
3434             vec(i) = ''
3435         end if
3436 
3437         end subroutine get_chars_from_array
3438 
3439     end subroutine json_get_char_vec

json_module/json_get_chars [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_chars

DESCRIPTION

    Get a character string from a json_value.

SOURCE

3177     subroutine json_get_chars(this, path, value, found)
3178 
3179     implicit none
3180 
3181     type(json_value),pointer,intent(in)         :: this
3182     character(len=*),intent(in),optional        :: path
3183     character(len=:),allocatable,intent(out)    :: value
3184     logical,intent(out),optional                :: found
3185 
3186     type(json_value), pointer :: p
3187     character(len=:),allocatable :: s,pre,post
3188     integer :: j,jprev,n
3189     character(len=1) :: c
3190 
3191     if (.not. exception_thrown) then
3192 
3193         nullify(p)
3194 
3195         if (present(path)) then
3196             call json_get_by_path(this=this, path=path, p=p)
3197         else
3198             p => this
3199         end if
3200 
3201         if (.not.associated(p)) then
3202 
3203             call throw_exception('Error in json_get_chars:'//&
3204                                  ' Unable to resolve path: '//trim(path))
3205 
3206         else
3207 
3208             !associate (d => p%data)
3209                 select case (p%data%var_type)
3210                 case (json_string)
3211                     if (allocated(p%data%str_value)) then
3212 
3213                         !get the value as is:
3214                         s = p%data%str_value
3215 
3216                         ! Now, have to remove the escape characters:
3217                         !
3218                         ! '\"'        quotation mark
3219                         ! '\\'        reverse solidus
3220                         ! '\/'        solidus
3221                         ! '\b'        backspace
3222                         ! '\f'        formfeed
3223                         ! '\n'        newline (LF)
3224                         ! '\r'        carriage return (CR)
3225                         ! '\t'        horizontal tab
3226                         ! '\uXXXX'    4 hexadecimal digits
3227                         !
3228 
3229                         !initialize:
3230                         n = len(s)
3231                         j = 1
3232 
3233                         do
3234 
3235                             jprev = j                      !initialize
3236                             j = index(s(j:n),backslash)    !look for an escape character
3237 
3238                             if (j>0) then            !an escape character was found
3239                             
3240                                 !index in full string of the escape character:
3241                                 j = j + (jprev-1)   
3242 
3243                                 if (j<n) then
3244 
3245                                     !save the bit before the escape character:
3246                                     if (j>1) then
3247                                         pre = s( 1 : j-1 )
3248                                     else
3249                                         pre = ''
3250                                     end if
3251 
3252                                     !character after the escape character:
3253                                     c = s( j+1 : j+1 )
3254 
3255                                     select case (c)
3256                                     case(quotation_mark,backslash,slash,&
3257                                          'b','f','n','r','t')
3258 
3259                                         !save the bit after the escape characters:
3260                                         if (j+2<n) then
3261                                             post = s(j+2:n)
3262                                         else
3263                                             post = ''
3264                                         end if
3265 
3266                                         select case(c)
3267                                         case(quotation_mark,backslash,slash)
3268                                             !use c as is
3269                                         case('b')
3270                                             c = bspace
3271                                         case('f')
3272                                             c = formfeed
3273                                         case('n')
3274                                             c = newline
3275                                         case('r')
3276                                             c = carriage_return
3277                                         case('t')
3278                                             c = horizontal_tab
3279                                         end select
3280 
3281                                         s = pre//c//post
3282 
3283                                         n = n-1    !backslash character has been 
3284                                                    ! removed from the string
3285 
3286                                     case('u')    !expecting 4 hexadecimal digits after 
3287                                                  ! the escape character    [\uXXXX]
3288 
3289                                         !for now, we are just printing them as is
3290                                         ![not checking to see if it is a valid hex value]
3291 
3292                                         if (j+5<=n) then
3293                                             j=j+4
3294                                         else
3295                                             call throw_exception(&
3296                                                 'Error in json_get_chars:'//&
3297                                                 ' Invalid hexadecimal sequence'//&
3298                                                 ' in string: '//trim(c))
3299                                             exit
3300                                         end if
3301 
3302                                     case default
3303                                         !unknown escape character
3304                                         call throw_exception('Error in json_get_chars:'//&
3305                                                 ' unknown escape sequence in string "'//&
3306                                                 trim(s)//'" ['//backslash//c//']')
3307                                         exit
3308                                     end select
3309 
3310                                     j=j+1    !go to the next character
3311 
3312                                     if (j>=n) exit    !finished
3313 
3314                                 else
3315                                     !an escape character is the last character in 
3316                                     ! the string [this may not be valid syntax, 
3317                                     ! but just keep it]
3318                                     exit
3319                                 end if
3320 
3321                             else
3322                                 exit    !no more escape characters in the string
3323                             end if
3324 
3325                         end do
3326 
3327                         if (exception_thrown) then
3328                             if (allocated(value)) deallocate(value)
3329                         else
3330                             value = s
3331                         end if
3332 
3333                     else
3334                         call throw_exception('Error in json_get_chars:'//&
3335                                              ' p%data%value not allocated')
3336                     end if
3337 
3338                 !class default
3339                 case default
3340 
3341                     call throw_exception('Error in json_get_chars:'//&
3342                                          ' Unable to resolve value to characters: '//&
3343                                          trim(path))
3344 
3345                     ! Note: for the other cases, we could do val to string conversions.
3346 
3347                 end select
3348             !end associate
3349 
3350         end if
3351 
3352         if (allocated(value) .and. .not. exception_thrown) then
3353             if (present(found)) found = .true.
3354         else
3355             if (present(found)) then
3356                 found = .false.
3357                 call json_clear_exceptions()
3358             end if
3359         end if
3360 
3361         !cleanup:
3362         if (associated(p)) nullify(p)
3363         if (allocated(s)) deallocate(s)
3364         if (allocated(pre)) deallocate(pre)
3365         if (allocated(post)) deallocate(post)
3366 
3367     else
3368 
3369         value = ''
3370         found = .false.
3371 
3372     end if
3373 
3374     end subroutine json_get_chars

json_module/json_get_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_get_double

DESCRIPTION

    Get a double value from a json_value.

SOURCE

2909     subroutine json_get_double(this, path, value, found)
2910 
2911     implicit none
2912 
2913     type(json_value), pointer       :: this
2914     character(len=*), optional      :: path
2915     real(wp),intent(out)            :: value
2916     logical,intent(out),optional    :: found
2917 
2918     type(json_value), pointer :: p
2919 
2920     if (.not. exception_thrown) then
2921 
2922         nullify(p)
2923 
2924         if (present(path)) then
2925             call json_get_by_path(this=this, path=path, p=p)
2926         else
2927             p => this
2928         end if
2929 
2930         if (.not.associated(p)) then
2931 
2932             call throw_exception('Error in json_get_double:'//&
2933                                  ' Unable to resolve path: '//trim(path))
2934 
2935         else
2936 
2937             !associate (d => p%data)
2938                 select case (p%data%var_type)
2939                 case (json_integer)
2940                     value = p%data%int_value
2941                 case (json_double)
2942                     value = p%data%dbl_value
2943                 case (json_logical)
2944                     if (p%data%log_value) then
2945                         value = 1.0_wp
2946                     else
2947                         value = 0.0_wp
2948                     end if
2949                 case default
2950                     call throw_exception('Error in json_get_double:'//&
2951                                          ' Unable to resolve value to double: '//&
2952                                          trim(path))
2953                 end select
2954             !end associate
2955 
2956             nullify(p)
2957 
2958         end if
2959 
2960         if (exception_thrown) then
2961             if (present(found)) then
2962                 found = .false.
2963                 call json_clear_exceptions()
2964             end if
2965         else
2966             if (present(found)) found = .true.
2967         end if
2968 
2969     else
2970 
2971         value = 0.0_wp
2972         if (present(found)) found = .false.
2973 
2974     end if
2975 
2976     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

2993     subroutine json_get_double_vec(me, path, vec, found)
2994 
2995     implicit none
2996 
2997     type(json_value), pointer                       :: me
2998     character(len=*),intent(in)                     :: path
2999     real(wp),dimension(:),allocatable,intent(out)    :: vec
3000     logical,intent(out),optional                    :: found
3001 
3002     logical :: initialized
3003 
3004     initialized = .false.
3005 
3006     if (allocated(vec)) deallocate(vec)
3007 
3008     !the callback function is called for each element of the array:
3009     call json_get(me, path=path, array_callback=get_double_from_array, found=found)
3010 
3011     contains
3012 
3013         ! callback function for double
3014         subroutine get_double_from_array(element, i, count)
3015         implicit none
3016 
3017         type(json_value),pointer,intent(in)     :: element
3018         integer,intent(in)                      :: i        !index
3019         integer,intent(in)                      :: count    !size of array
3020 
3021         !size the output array:
3022         if (.not. initialized) then
3023             allocate(vec(count))
3024             initialized = .true.
3025         end if
3026 
3027         !populate the elements:
3028         call json_get(element, value=vec(i))
3029 
3030         end subroutine get_double_from_array
3031 
3032     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

2773     subroutine json_get_integer(this, path, value, found)
2774 
2775     implicit none
2776 
2777     type(json_value),pointer,intent(in)  :: this
2778     character(len=*),optional            :: path
2779     integer,intent(out)                  :: value
2780     logical,intent(out),optional         :: found
2781 
2782     type(json_value), pointer :: p
2783 
2784     if (.not. exception_thrown) then
2785 
2786         nullify(p)
2787         if (present(path)) then
2788             call json_get_by_path(this=this, path=path, p=p)
2789         else
2790             p => this
2791         end if
2792 
2793         if (.not.associated(p)) then
2794 
2795             call throw_exception('Error in json_get_integer:'//&
2796                                  ' Unable to resolve path: '// trim(path))
2797 
2798         else
2799 
2800             !associate (d => p%data)
2801                 select case(p%data%var_type)
2802                 case (json_integer)
2803                     value = p%data%int_value
2804                 case (json_double)
2805                     value = int(p%data%dbl_value)
2806                 case (json_logical)
2807                     if (p%data%log_value) then
2808                         value = 1
2809                     else
2810                         value = 0
2811                     end if
2812                 case default
2813                     call throw_exception('Error in get_integer:'//&
2814                                          ' Unable to resolve value to integer: '//&
2815                                          trim(path))
2816                 end select
2817             !end associate
2818 
2819             nullify(p)
2820 
2821         end if
2822 
2823         if (exception_thrown) then
2824             if (present(found)) then
2825                 found = .false.
2826                 call json_clear_exceptions()
2827             end if
2828         else
2829             if (present(found)) found = .true.
2830         end if
2831 
2832     else
2833 
2834         value = 0
2835         if (present(found)) found = .false.
2836 
2837     end if
2838 
2839     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

2856     subroutine json_get_integer_vec(me, path, vec, found)
2857 
2858     implicit none
2859 
2860     type(json_value), pointer                       :: me
2861     character(len=*),intent(in)                     :: path
2862     integer,dimension(:),allocatable,intent(out)    :: vec
2863     logical,intent(out),optional                    :: found
2864 
2865     logical :: initialized
2866 
2867     initialized = .false.
2868 
2869     if (allocated(vec)) deallocate(vec)
2870 
2871     !the callback function is called for each element of the array:
2872     call json_get(me, path=path, array_callback=get_int_from_array, found=found)
2873 
2874     contains
2875 
2876         ! callback function for integer
2877         subroutine get_int_from_array(element, i, count)
2878         implicit none
2879 
2880         type(json_value),pointer,intent(in)     :: element
2881         integer,intent(in)                      :: i        !index
2882         integer,intent(in)                      :: count    !size of array
2883 
2884         !size the output array:
2885         if (.not. initialized) then
2886             allocate(vec(count))
2887             initialized = .true.
2888         end if
2889 
2890         !populate the elements:
2891         call json_get(element, value=vec(i))
2892 
2893         end subroutine get_int_from_array
2894 
2895     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

3046     subroutine json_get_logical(this, path, value, found)
3047 
3048     implicit none
3049 
3050     type(json_value),pointer,intent(in) :: this
3051     character(len=*),optional           :: path
3052     logical                             :: value
3053     logical,intent(out),optional        :: found
3054 
3055     type(json_value), pointer :: p
3056 
3057     if (.not. exception_thrown) then
3058 
3059         nullify(p)
3060 
3061         if (present(path)) then
3062             call json_get_by_path(this=this, path=path, p=p)
3063         else
3064             p => this
3065         end if
3066 
3067         if (.not.associated(p)) then
3068 
3069             call throw_exception('Error in json_get_logical:'//&
3070                                  ' Unable to resolve path: '//trim(path))
3071 
3072         else
3073 
3074             !associate (d => p%data)
3075                 select case (p%data%var_type)
3076                 case (json_integer)
3077                     value = (p%data%int_value > 0)
3078                 case (json_logical)
3079                     value = p%data % log_value
3080                 case default
3081                     call throw_exception('Error in json_get_logical:'//&
3082                                          ' Unable to resolve value to logical: '//&
3083                                          trim(path))
3084                 end select
3085             !end associate
3086 
3087             nullify(p)
3088 
3089         end if
3090 
3091         if (exception_thrown) then
3092             if (present(found)) then
3093                 found = .false.
3094                 call json_clear_exceptions()
3095             end if
3096         else
3097             if (present(found)) found = .true.
3098         end if
3099 
3100     else
3101 
3102         value = .false.
3103         if (present(found)) found = .false.
3104 
3105     end if
3106 
3107     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

3124     subroutine json_get_logical_vec(me, path, vec, found)
3125 
3126     implicit none
3127 
3128     type(json_value),pointer,intent(in)            :: me
3129     character(len=*),intent(in)                    :: path
3130     logical,dimension(:),allocatable,intent(out)   :: vec
3131     logical,intent(out),optional                   :: found
3132 
3133     logical :: initialized
3134 
3135     initialized = .false.
3136 
3137     if (allocated(vec)) deallocate(vec)
3138 
3139     !the callback function is called for each element of the array:
3140     call json_get(me, path=path, array_callback=get_logical_from_array, found=found)
3141 
3142     contains
3143 
3144         ! callback function for logical
3145         subroutine get_logical_from_array(element, i, count)
3146         implicit none
3147 
3148         type(json_value),pointer,intent(in)  :: element
3149         integer,intent(in)                   :: i        !index
3150         integer,intent(in)                   :: count    !size of array
3151 
3152         !size the output array:
3153         if (.not. initialized) then
3154             allocate(vec(count))
3155             initialized = .true.
3156         end if
3157 
3158         !populate the elements:
3159         call json_get(element, value=vec(i))
3160 
3161         end subroutine get_logical_from_array
3162 
3163     end subroutine json_get_logical_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

723     subroutine json_info(p,var_type,n_children)
724 
725     implicit none
726 
727     type(json_value),pointer        :: p
728     integer,intent(out),optional    :: var_type
729     integer,intent(out),optional    :: n_children
730         
731     if (present(var_type))    var_type = p%data%var_type        !variable type
732     if (present(n_children))  n_children = json_value_count(p)  !number of children
733     
734     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

1033     subroutine json_initialize()
1034 
1035     implicit none
1036 
1037     !clear any errors from previous runs:
1038     call json_clear_exceptions()
1039 
1040     !Just in case, clear these global variables also:
1041     pushed_index = 0
1042     pushed_char = ''
1043     char_count = 0
1044     line_count = 1
1045 
1046     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.

NOTES

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

SOURCE

3536     subroutine json_parse(file, p, unit)
3537 
3538     implicit none
3539 
3540     character(len=*),intent(in) :: file
3541     type(json_value),pointer    :: p
3542     integer,intent(in),optional :: unit
3543 
3544     integer :: iunit, istat
3545     character(len=:),allocatable :: line, arrow_str
3546     character(len=10) :: line_str, char_str
3547     logical :: is_open
3548 
3549     !clean any exceptions and initialize:
3550     call json_initialize()
3551 
3552     if (present(unit)) then
3553         
3554         iunit = unit   
3555         
3556         !check to see if the file is already open
3557         ! if it is, then use it, otherwise open the file.
3558         inquire(unit=iunit, opened=is_open, iostat=istat)
3559         if (istat==0 .and. .not. is_open) then
3560            ! open the file
3561             open (  unit        = iunit, &
3562                     file        = file, &
3563                     status      = 'OLD', &
3564                     action      = 'READ', &
3565                     form        = 'FORMATTED', &
3566                     position    = 'REWIND', &
3567                     iostat      = istat)
3568         end if
3569         
3570     else
3571     
3572         ! open the file with a new unit number:
3573         open (  newunit     = iunit, &
3574                 file        = file, &
3575                 status      = 'OLD', &
3576                 action      = 'READ', &
3577                 form        = 'FORMATTED', &
3578                 position    = 'REWIND', &
3579                 iostat      = istat)
3580     
3581     end if
3582 
3583     if (istat==0) then
3584 
3585         ! create the value and associate the pointer
3586         call json_value_create(p)
3587 
3588         !add the file name as the name of the overall structure:
3589         p%name = trim(file)
3590 
3591         ! parse as a value
3592         call parse_value(unit = iunit, value = p)
3593 
3594         !
3595         !  If there was an error reading the file, then
3596         !   can print the line where the error occurred:
3597         !
3598         if (exception_thrown) then
3599         
3600             call get_current_line_from_file(iunit,line)
3601             
3602             !the counters for the current line and the last character read:
3603             call integer_to_string(line_count, line_str)
3604             call integer_to_string(char_count, char_str)
3605             
3606             !draw the arrow string that points to the current character:
3607             arrow_str = repeat('-',max( 0, char_count - 1) )//'^'
3608 
3609             !create the error message:
3610             err_message = err_message//newline//&
3611                            'line: '//trim(adjustl(line_str))//', '//&
3612                            'character: '//trim(adjustl(char_str))//newline//&
3613                            trim(line)//newline//arrow_str
3614                                         
3615             if (allocated(line)) deallocate(line)
3616                         
3617         end if
3618 
3619         ! close the file
3620         close (iunit, iostat=istat)
3621 
3622     else
3623 
3624         call throw_exception('Error in json_parse: Error opening file: '//trim(file))
3625 
3626     end if
3627 
3628     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

381     interface json_print
382         module procedure :: json_print_1    !input is unit number
383         module procedure :: json_print_2    !input is file name
384     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

2228     subroutine json_print_1(me,iunit)
2229     
2230     implicit none
2231     
2232     type(json_value),pointer,intent(in)  :: me
2233     integer,intent(in) :: iunit                   !must be non-zero
2234     
2235     character(len=:),allocatable :: dummy
2236     
2237     if (iunit/=0) then
2238         call json_value_print(me,iunit,str=dummy)
2239     else
2240         call throw_exception('Error in json_print: iunit must be nonzero.')
2241     end if
2242     
2243     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

2260     subroutine json_print_2(me,filename)
2261     
2262     implicit none
2263     
2264     type(json_value),pointer,intent(in)  :: me
2265     character(len=*),intent(in) :: filename
2266     
2267     integer :: iunit,istat
2268     
2269     open(newunit=iunit,file=filename,status='REPLACE',iostat=istat)
2270     if (istat==0) then
2271         call json_print(me,iunit)
2272         close(iunit,iostat=istat)
2273     else
2274         call throw_exception('Error in json_print: could not open file: '//trim(filename))
2275     end if
2276     
2277     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

361     interface json_print_to_string
362         module procedure :: json_value_to_string
363     end interface

json_module/json_remove [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_remove

DESCRIPTION

    Remove and destroy a json_value (and all its children) 
        from a linked-list structure.
    The rest of the structure is preserved.

SOURCE

421     interface json_remove
422         module procedure :: json_value_remove
423     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

436     interface json_remove_if_present
437         module procedure :: json_value_remove_if_present
438     end interface

json_module/json_update [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_update

DESCRIPTION

    These are like json_value_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).

SOURCE

323     interface json_update
324         module procedure :: json_update_logical,&
325                             json_update_double,&
326                             json_update_integer,&
327                             json_update_chars
328     end interface json_update

json_module/json_update_chars [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_update_chars

DESCRIPTION

    If the child 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

1527     subroutine json_update_chars(p,name,val,found)
1528      
1529     implicit none
1530     
1531     type(json_value), pointer   :: p
1532     character(len=*),intent(in) :: name
1533     character(len=*),intent(in) :: val
1534     logical,intent(out)         :: found
1535     
1536     type(json_value),pointer :: p_var
1537     integer :: var_type
1538  
1539     call json_get(p,name,p_var,found)
1540     if (found) then
1541 
1542         call json_info(p_var,var_type)
1543         select case (var_type)
1544         case (json_null,json_logical,json_integer,json_double,json_string)
1545             call to_string(p_var,val)    !update the value
1546         case default
1547             found = .false.
1548             call throw_exception('Error in json_update_chars: '//&
1549                                  'the variable is not a scalar value')
1550         end select          
1551 
1552     else
1553         call json_value_add(p,name,val)   !add the new element
1554     end if
1555                      
1556     end subroutine json_update_chars

json_module/json_update_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_update_double

DESCRIPTION

    If the child 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

1433     subroutine json_update_double(p,name,val,found)
1434      
1435     implicit none
1436     
1437     type(json_value), pointer   :: p
1438     character(len=*),intent(in) :: name
1439     real(wp),intent(in)         :: val
1440     logical,intent(out)         :: found
1441     
1442     type(json_value),pointer :: p_var
1443     integer :: var_type
1444  
1445     call json_get(p,name,p_var,found)
1446     if (found) then
1447 
1448         call json_info(p_var,var_type)
1449         select case (var_type)
1450         case (json_null,json_logical,json_integer,json_double,json_string)
1451             call to_double(p_var,val)    !update the value
1452         case default
1453             found = .false.
1454             call throw_exception('Error in json_update_double: '//&
1455                                  'the variable is not a scalar value')
1456         end select          
1457 
1458     else
1459         call json_value_add(p,name,val)   !add the new element
1460     end if
1461                      
1462     end subroutine json_update_double

json_module/json_update_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_update_integer

DESCRIPTION

    If the child 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

1480     subroutine json_update_integer(p,name,val,found)
1481 
1482     implicit none
1483     
1484     type(json_value), pointer   :: p
1485     character(len=*),intent(in) :: name
1486     integer,intent(in)          :: val
1487     logical,intent(out)         :: found
1488     
1489     type(json_value),pointer :: p_var
1490     integer :: var_type
1491  
1492     call json_get(p,name,p_var,found)
1493     if (found) then
1494 
1495         call json_info(p_var,var_type)
1496         select case (var_type)
1497         case (json_null,json_logical,json_integer,json_double,json_string)
1498             call to_integer(p_var,val)    !update the value
1499         case default
1500             found = .false.
1501             call throw_exception('Error in json_update_integer: '//&
1502                                  'the variable is not a scalar value')
1503         end select          
1504 
1505     else
1506         call json_value_add(p,name,val)   !add the new element
1507     end if
1508                      
1509     end subroutine json_update_integer

json_module/json_update_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_update_logical

DESCRIPTION

    If the child 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

1386     subroutine json_update_logical(p,name,val,found)
1387      
1388     implicit none
1389     
1390     type(json_value), pointer   :: p
1391     character(len=*),intent(in) :: name
1392     logical,intent(in)          :: val
1393     logical,intent(out)         :: found
1394     
1395     type(json_value),pointer :: p_var
1396     integer :: var_type
1397  
1398     call json_get(p,name,p_var,found)
1399     if (found) then
1400 
1401         call json_info(p_var,var_type)
1402         select case (var_type)
1403         case (json_null,json_logical,json_integer,json_double,json_string)
1404             call to_logical(p_var,val)    !update the value
1405         case default
1406             found = .false.
1407             call throw_exception('Error in json_update_logical: '//&
1408                                  'the variable is not a scalar value')
1409         end select          
1410 
1411     else
1412         call json_value_add(p,name,val)   !add the new element
1413     end if
1414                      
1415     end subroutine json_update_logical

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_value_create(p) 
    call to_object(p) 
    call json_value_add(p,'year',1805)
    call json_value_add(p,'value',1.0_wp)
    call json_print(p,'test.json')
    call json_destroy(p)

SOURCE

177         type,public :: json_value
178 
179         !variable name:
180         character(len=:),allocatable :: name
181 
182         !the data for this variable:
183         type(json_data_non_polymorphic) :: data
184 
185         !for the linked list:
186         type(json_value), pointer :: previous => null()
187         type(json_value), pointer :: next => null()
188         type(json_value), pointer :: parent => null()
189         type(json_value), pointer :: children => null()
190 
191         end type json_value

json_module/json_value_add [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_add

DESCRIPTION

    Add objects to a linked list of json_values.

SOURCE

300     interface json_value_add
301         module procedure :: json_value_add_member
302         module procedure :: json_value_add_integer, json_value_add_integer_vec
303         module procedure :: json_value_add_double,  json_value_add_double_vec
304         module procedure :: json_value_add_logical, json_value_add_logical_vec
305         module procedure :: json_value_add_string,  json_value_add_string_vec
306     end interface json_value_add

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

1628     subroutine json_value_add_double(me, name, val)
1629 
1630     implicit none
1631 
1632     type(json_value), pointer   :: me
1633     character(len=*),intent(in) :: name
1634     real(wp),intent(in)         :: val
1635 
1636     type(json_value),pointer :: var
1637 
1638     !create the variable:
1639     call json_value_create(var)
1640     call to_double(var,val,name)
1641 
1642     !add it:
1643     call json_value_add(me, var)
1644 
1645     !cleanup:
1646     nullify(var)
1647 
1648     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

1668     subroutine json_value_add_double_vec(me, name, val)
1669 
1670     implicit none
1671 
1672     type(json_value), pointer         :: me
1673     character(len=*),intent(in)       :: name
1674     real(wp),dimension(:),intent(in)  :: val
1675 
1676     type(json_value),pointer :: var
1677     integer :: i
1678 
1679     !create the variable as an array:
1680     call json_value_create(var)
1681     call to_array(var,name)
1682 
1683     !populate the array:
1684     do i=1,size(val)
1685         call json_value_add(var, '', val(i))
1686     end do
1687 
1688     !add it:
1689     call json_value_add(me, var)
1690 
1691     !cleanup:
1692     nullify(var)
1693 
1694     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

1714     subroutine json_value_add_integer(me, name, val)
1715 
1716     implicit none
1717 
1718     type(json_value), pointer     :: me
1719     character(len=*),intent(in)   :: name
1720     integer,intent(in)            :: val
1721 
1722     type(json_value),pointer :: var
1723 
1724     !create the variable:
1725     call json_value_create(var)
1726     call to_integer(var,val,name)
1727 
1728     !add it:
1729     call json_value_add(me, var)
1730 
1731     !cleanup:
1732     nullify(var)
1733 
1734     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

1754     subroutine json_value_add_integer_vec(me, name, val)
1755 
1756     implicit none
1757 
1758     type(json_value), pointer       :: me
1759     character(len=*),intent(in)     :: name
1760     integer,dimension(:),intent(in) :: val
1761 
1762     type(json_value),pointer :: var
1763     integer :: i    !counter
1764 
1765     !create the variable as an array:
1766     call json_value_create(var)
1767     call to_array(var,name)
1768 
1769     !populate the array:
1770     do i=1,size(val)
1771         call json_value_add(var, '', val(i))
1772     end do
1773 
1774     !add it:
1775     call json_value_add(me, var)
1776 
1777     !cleanup:
1778     nullify(var)
1779 
1780     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

1800     subroutine json_value_add_logical(me, name, val)
1801 
1802     implicit none
1803 
1804     type(json_value), pointer   :: me
1805     character(len=*),intent(in) :: name
1806     logical,intent(in)          :: val
1807 
1808     type(json_value),pointer :: var
1809 
1810     !create the variable:
1811     call json_value_create(var)
1812     call to_logical(var,val,name)
1813 
1814     !add it:
1815     call json_value_add(me, var)
1816 
1817     !cleanup:
1818     nullify(var)
1819 
1820     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

1840     subroutine json_value_add_logical_vec(me, name, val)
1841 
1842     implicit none
1843 
1844     type(json_value), pointer       :: me
1845     character(len=*),intent(in)     :: name
1846     logical,dimension(:),intent(in) :: val
1847 
1848     type(json_value),pointer :: var
1849     integer :: i    !counter
1850 
1851     !create the variable as an array:
1852     call json_value_create(var)
1853     call to_array(var,name)
1854 
1855     !populate the array:
1856     do i=1,size(val)
1857         call json_value_add(var, '', val(i))
1858     end do
1859 
1860     !add it:
1861     call json_value_add(me, var)
1862 
1863     !cleanup:
1864     nullify(var)
1865 
1866     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

1570     subroutine json_value_add_member(this, member)
1571 
1572     implicit none
1573 
1574     type(json_value), pointer :: this, member
1575 
1576     type(json_value), pointer :: p
1577 
1578     if (.not. exception_thrown) then
1579 
1580         nullify(p)
1581 
1582         ! associate the parent
1583         member % parent => this
1584 
1585         ! add to linked list
1586         if (associated(this % children)) then
1587 
1588             ! get to the tail of the linked list
1589             p => this % children
1590             do while (associated(p % next))
1591                 p => p % next
1592             end do
1593 
1594             p%next => member
1595             member%previous => p
1596 
1597             nullify(p)    !cleanup
1598 
1599         else
1600 
1601             this%children => member
1602             member%previous => null()
1603 
1604         end if
1605 
1606     end if
1607 
1608     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

1886     subroutine json_value_add_string(me, name, val)
1887 
1888     implicit none
1889 
1890     type(json_value), pointer   :: me
1891     character(len=*),intent(in) :: name
1892     character(len=*),intent(in) :: val
1893 
1894     type(json_value),pointer        :: var
1895     character(len=:),allocatable    :: str
1896 
1897     !add escape characters if necessary:
1898     call escape_string(val, str)
1899 
1900     !create the variable:
1901     call json_value_create(var)
1902     call to_string(var,str,name)
1903 
1904     !add it:
1905     call json_value_add(me, var)
1906 
1907     !cleanup:
1908     nullify(var)
1909 
1910     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 string 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

1983     subroutine json_value_add_string_vec(me, name, val, trim_str, adjustl_str)
1984 
1985     implicit none
1986 
1987     type(json_value), pointer                :: me
1988     character(len=*),intent(in)              :: name
1989     character(len=*),dimension(:),intent(in) :: val
1990     logical,intent(in),optional              :: trim_str
1991     logical,intent(in),optional              :: adjustl_str
1992 
1993     type(json_value),pointer :: var
1994     integer :: i
1995     logical :: trim_string, adjustl_string
1996     character(len=:),allocatable :: str
1997 
1998     !if the string is to be trimmed or not:
1999     if (present(trim_str)) then
2000         trim_string = trim_str
2001     else
2002         trim_string = .false.
2003     end if
2004     if (present(adjustl_str)) then
2005         adjustl_string = adjustl_str
2006     else
2007         adjustl_string = .false.
2008     end if
2009 
2010     !create the variable as an array:
2011     call json_value_create(var)
2012     call to_array(var,name)
2013 
2014     !populate the array:
2015     do i=1,size(val)
2016 
2017         !the string to write:
2018         str = val(i)
2019         if (adjustl_string) str = adjustl(str)
2020         if (trim_string)    str = trim(str)
2021 
2022         !write it:
2023         call json_value_add(var, '', str)
2024 
2025         !cleanup
2026         deallocate(str)
2027 
2028     end do
2029 
2030     !add it:
2031     call json_value_add(me, var)
2032 
2033     !cleanup:
2034     nullify(var)
2035 
2036     end subroutine json_value_add_string_vec

json_module/json_value_count [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_count

DESCRIPTION

    Count the number of children.

SOURCE

2050     function json_value_count(this) result(count)
2051 
2052     implicit none
2053 
2054     integer :: count
2055     type(json_value),pointer,intent(in) :: this
2056 
2057     type(json_value), pointer :: p
2058 
2059     if (.not. exception_thrown) then
2060 
2061         count = 0
2062         
2063         if (associated(this)) then
2064 
2065             if (associated(this%children)) then
2066 
2067                 p => this%children
2068 
2069                 do while (associated(p))
2070                     count = count + 1
2071                     p => p%next
2072                 end do
2073 
2074                 nullify(p)
2075 
2076             end if
2077             
2078         end if
2079         
2080     end if
2081 
2082     end function json_value_count

json_module/json_value_create [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_create

DESCRIPTION

    Allocate a json_value pointer variable.
    This should be called before adding data to it.

EXAMPLE

    type(json_value),pointer :: var
    call json_value_create(var)
    call to_double(var,1.0d0)

NOTES

    This routine does not check for exceptions.
    The pointer should not already be allocated.

SOURCE

1217     subroutine json_value_create(p)
1218 
1219     implicit none
1220 
1221     type(json_value), pointer :: p
1222 
1223     nullify(p)
1224     allocate(p)
1225 
1226     end subroutine json_value_create

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

1248     recursive subroutine json_value_destroy(this)
1249 
1250     implicit none
1251 
1252     type(json_value),pointer :: this
1253 
1254     if (associated(this)) then
1255 
1256         if (allocated(this%name)) deallocate(this%name)
1257 
1258         call this%data%destroy()
1259 
1260         if (associated(this%children))    call json_value_destroy(this%children)
1261 
1262         if (associated(this%next))        call json_value_destroy(this%next)
1263 
1264         deallocate(this)
1265 
1266         nullify(this)
1267 
1268     end if
1269 
1270     end subroutine json_value_destroy

json_module/json_value_get [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_get

DESCRIPTION

    Get a child, either by index or name string.

SOURCE

284     interface json_value_get           !consider renaming this json_value_get_child
285         module procedure get_by_index
286         module procedure get_by_name_chars
287     end interface json_value_get

json_module/json_value_remove [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_destroy

DESCRIPTION

    Remove and destroy a json_value (and all its children) 
        from a linked-list structure.
    The rest of the structure is preserved.

AUTHOR

    Jacob Williams : 9/9/2014

SOURCE

1289     subroutine json_value_remove(me)
1290 
1291     implicit none
1292 
1293     type(json_value),pointer :: me
1294     
1295     type(json_value),pointer :: parent,previous,next
1296     
1297     if (associated(me)) then
1298         if (associated(me%parent)) then
1299             if (associated(me%next)) then
1300         
1301                 !there are later items in the list:
1302             
1303                 next => me%next
1304                 nullify(me%next)
1305             
1306                 if (associated(me%previous)) then           
1307                     !there are earlier items in the list
1308                     previous => me%previous                
1309                     previous%next => next
1310                     next%previous => previous
1311                 else
1312                     !this is the first item in the list
1313                     parent => me%parent                    
1314                     parent%children => next
1315                     next%previous => null()
1316                 end if
1317                 
1318             else
1319             
1320                 if (associated(me%previous)) then
1321                     !there are earlier items in the list:
1322                     previous => me%previous 
1323                     previous%next => null()  
1324                 else
1325                     !this is the only item in the list:
1326                     parent => me%parent 
1327                     parent%children => null()                   
1328                 end if
1329                 
1330             end if     
1331                    
1332         end if
1333             
1334         call json_value_destroy(me)
1335 
1336     end if
1337 
1338     end subroutine json_value_remove

json_module/json_value_remove_if_present [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    json_value_remove_if_present

DESCRIPTION

    If the child variable is present, then remove it.

AUTHOR

    Jacob Williams : 12/6/2014

SOURCE

1355     subroutine json_value_remove_if_present(p,name)
1356      
1357     implicit none
1358     
1359     type(json_value),pointer    :: p
1360     character(len=*),intent(in) :: name
1361     
1362     type(json_value),pointer :: p_var
1363     logical :: found
1364     
1365     call json_get(p,name,p_var,found)
1366     if (found) call json_remove(p_var)
1367      
1368     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

2200     subroutine json_value_to_string(me,str)
2201 
2202     implicit none
2203     
2204     type(json_value),pointer,intent(in)        :: me
2205     character(len=:),intent(out),allocatable   :: str
2206     
2207     str = ''
2208     call json_value_print(me, iunit=0, str=str)
2209         
2210     end subroutine json_value_to_string

json_module/load_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    load_json_file

USAGE

    call me%load_file(filename)

DESCRIPTION

    Load the JSON file.

AUTHOR

    Jacob Williams : 12/9/2013

SOURCE

594     subroutine load_json_file(me, filename)
595 
596     implicit none
597 
598     class(json_file),intent(inout) :: me
599     character(len=*),intent(in) :: filename
600 
601     call json_parse(filename, me%p)
602 
603     end subroutine load_json_file

json_module/move_pointer_in_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    move_pointer_in_json_file

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

560     subroutine move_pointer_in_json_file(to,from)
561     
562     implicit none
563 
564     class(json_file),intent(inout) :: to
565     class(json_file),intent(inout) :: from
566 
567     if (associated(from%p)) then
568         to%p => from%p
569         nullify(from%p)
570     else
571         call throw_exception('Error in move_pointer_in_json_file: '//&
572                              'pointer is not associated.')
573     end if
574 
575     end subroutine move_pointer_in_json_file

json_module/print_json_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    print_json_file

USAGE

    call me%print_file()

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

624     subroutine print_json_file(me, iunit)
625 
626     use, intrinsic :: iso_fortran_env,    only: output_unit
627 
628     implicit none
629 
630     class(json_file),intent(inout) :: me
631     integer,intent(in),optional :: iunit  !must be non-zero
632 
633     integer :: i
634     character(len=:),allocatable :: dummy
635 
636     if (present(iunit)) then
637         if (iunit/=0) then
638             i = iunit
639         else
640             call throw_exception('Error in print_json_file: iunit must be nonzero.')
641             return
642         end if
643     else
644         i = output_unit
645     end if
646 
647     call json_value_print(me%p,iunit=i,str=dummy)
648 
649     end subroutine print_json_file

json_module/throw_exception [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    throw_exception

DESCRIPTION

    Throw an exception in the JSON module.
    This routine sets the error flag, and prevents any subsequent routine
    from doing anything, until json_clear_exceptions is called.

AUTHOR

    Jacob Williams : 12/4/2013

SOURCE

1090     subroutine throw_exception(msg)
1091 
1092     implicit none
1093 
1094     character(len=*),intent(in) :: msg    !the error message
1095 
1096     exception_thrown = .true.
1097     err_message = trim(msg)
1098 
1099     end subroutine throw_exception

json_module/to_array [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    to_array

DESCRIPTION

    Change the variable to an array.

AUTHOR

    Jacob Williams

SOURCE

4032     subroutine to_array(me,name)
4033 
4034     implicit none
4035 
4036     type(json_value),intent(inout)         :: me
4037     character(len=*),intent(in),optional   :: name
4038 
4039     !set type and value:
4040     !associate (d => me%data)
4041         call me%data%destroy()
4042         me%data%var_type = json_array
4043     !end associate
4044 
4045     !name:
4046     if (present(name)) me%name = trim(name)
4047 
4048     end subroutine to_array

json_module/to_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    to_double

DESCRIPTION

    Change the variable to a double.

AUTHOR

    Jacob Williams

SOURCE

3886     subroutine to_double(me,val,name)
3887 
3888     implicit none
3889 
3890     type(json_value),intent(inout)         :: me
3891     character(len=*),intent(in),optional   :: name
3892     real(wp),intent(in),optional           :: val
3893 
3894     !set type and value:
3895     !associate (d => me%data)
3896         call me%data%destroy()
3897         me%data%var_type = json_double
3898         allocate(me%data%dbl_value)
3899         if (present(val)) then
3900             me%data%dbl_value = val
3901         else
3902             me%data%dbl_value = 0.0_wp    !default value
3903         end if
3904     !end associate
3905 
3906     !name:
3907     if (present(name)) me%name = trim(name)
3908 
3909     end subroutine to_double

json_module/to_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    to_integer

DESCRIPTION

    Change the variable to an integer.

AUTHOR

    Jacob Williams

SOURCE

3846     subroutine to_integer(me,val,name)
3847 
3848     implicit none
3849 
3850     type(json_value),intent(inout)         :: me
3851     character(len=*),intent(in),optional   :: name
3852     integer,intent(in),optional            :: val
3853 
3854     !set type and value:
3855     !associate (d => me%data)
3856         call me%data%destroy()
3857         me%data%var_type = json_integer
3858         allocate(me%data%int_value)
3859         if (present(val)) then
3860             me%data%int_value = val
3861         else
3862             me%data%int_value = 0    !default value
3863         end if
3864     !end associate
3865 
3866     !name:
3867     if (present(name)) me%name = trim(name)
3868 
3869     end subroutine to_integer

json_module/to_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    to_logical

DESCRIPTION

    Change the variable to a logical.

AUTHOR

    Jacob Williams

SOURCE

3806     subroutine to_logical(me,val,name)
3807 
3808     implicit none
3809 
3810     type(json_value),intent(inout)         :: me
3811     character(len=*),intent(in),optional   :: name
3812     logical,intent(in),optional            :: val
3813 
3814     !set type and value:
3815     !associate (d => me%data)
3816         call me%data%destroy()
3817         me%data%var_type = json_logical
3818         allocate(me%data%log_value)
3819         if (present(val)) then
3820             me%data%log_value = val
3821         else
3822             me%data%log_value = .false.    !default value
3823         end if
3824     !end associate
3825 
3826     !name:
3827     if (present(name)) me%name = trim(name)
3828 
3829     end subroutine to_logical

json_module/to_null [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    to_null

DESCRIPTION

    Change the variable to a null.

AUTHOR

    Jacob Williams

SOURCE

3965     subroutine to_null(me,name)
3966 
3967     implicit none
3968 
3969     type(json_value),intent(inout)         :: me
3970     character(len=*),intent(in),optional   :: name
3971 
3972     !set type and value:
3973     !associate (d => me%data)
3974         call me%data%destroy()
3975         me%data%var_type = json_null
3976     !end associate
3977 
3978     !name:
3979     if (present(name)) me%name = trim(name)
3980 
3981     end subroutine to_null

json_module/to_object [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    to_object

DESCRIPTION

    Change the variable to an object.

AUTHOR

    Jacob Williams

SOURCE

3998     subroutine to_object(me,name)
3999 
4000     implicit none
4001 
4002     type(json_value),intent(inout)         :: me
4003     !type(json_value),pointer,intent(inout) :: me  !this causes crash in gfortran (compiler bug?)
4004     character(len=*),intent(in),optional   :: name
4005     
4006     !set type and value:
4007     !associate (d => me%data)
4008         call me%data%destroy()
4009         me%data%var_type = json_object
4010     !end associate
4011 
4012     !name:
4013     if (present(name)) me%name = trim(name)
4014 
4015     end subroutine to_object

json_module/to_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    to_string

DESCRIPTION

    Change the variable to a string.

AUTHOR

    Jacob Williams

SOURCE

3926     subroutine to_string(me,val,name)
3927 
3928     implicit none
3929 
3930     type(json_value),intent(inout)         :: me
3931     character(len=*),intent(in),optional   :: name
3932     character(len=*),intent(in),optional   :: val
3933 
3934     !set type and value:
3935     !associate (d => me%data)
3936         call me%data%destroy()
3937         me%data%var_type = json_string
3938         if (present(val)) then
3939             me%data%str_value = val
3940         else
3941             me%data%str_value = ''    !default value
3942         end if
3943     !end associate
3944 
3945     !name:
3946     if (present(name)) me%name = trim(name)
3947 
3948     end subroutine to_string

json_module/variable_info_in_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

    variable_info_in_file

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

669     subroutine variable_info_in_file(me,path,found,var_type,n_children)
670 
671     implicit none
672 
673     class(json_file),intent(inout) :: me
674     character(len=*),intent(in)    :: path
675     logical,intent(out)            :: found
676     integer,intent(out)            :: var_type
677     integer,intent(out)            :: n_children
678 
679     type(json_value),pointer :: p
680 
681     !initialize:
682     nullify(p)
683 
684     !get a pointer to the variable (if it is there):
685     call me%get(path,p,found)
686 
687     if (found) then
688 
689         !get info:
690         call json_info(p,var_type,n_children)
691 
692     else
693 
694         !set to dummy values:
695         var_type = json_unknown
696         n_children = 0
697 
698     end if
699 
700     !cleanup:
701     nullify(p)
702 
703     end subroutine variable_info_in_file