JSON/json_module [ Modules ]

[ Top ] [ Modules ]

NAME

json_module

DESCRIPTION

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

SEE ALSO

LICENSE

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:

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

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.

HISTORY


json_module/CDK [ Parameters ]

[ Top ] [ json_module ] [ Parameters ]

NAME

CDK

DESCRIPTION

Processor dependendant 'DEFAULT' character kind. This is 1 byte for the Intel and Gfortran compilers.

NOTES

CK and CDK are the json-fortran character kind and json-fortran default character kind respectively. Client code must ensure characters of kind=CK are used for all character variables and strings passed to the json-fortran library *EXCEPT* for file names which must be of 'DEFAULT' character kind, provided here as CDK. In particular, any:

passed to the json-fortran library *MUST* be of type CDK. This will be the case for all string literals nor prepended with CK_ and only if ISO 10646 is supported and enabled, will strings of kind CK be different than CDK

SOURCE

237     integer,parameter,public :: CDK = selected_char_kind('DEFAULT')

json_module/CK [ Parameters ]

[ Top ] [ json_module ] [ Parameters ]

NAME

CK

DESCRIPTION

Default character kind used by json-fortran. If ISO 10646 (UCS4) support is available, use that, otherwise, gracefully fall back on 'DEFAULT' characters. Currently only gfortran >= 4.9.2 will correctly support UCS4 which is stored in 4 bytes. (and perhaps others).

NOTES

CK and CDK are the json-fortran character kind and json-fortran default character kind respectively. Client code must ensure characters of kind=CK are used for all character variables and strings passed to the json-fortran library *EXCEPT* for file names which must be of 'DEFAULT' character kind, provided here as JDCK. In particular, any:

passed to the json-fortran library *MUST* be of type CK.

SEE ALSO

STRING_KIND

SOURCE

209     integer,parameter,public :: CK = selected_char_kind( STRING_KIND )

json_module/IK [ Parameters ]

[ Top ] [ json_module ] [ Parameters ]

NAME

IK

DESCRIPTION

Default integer kind [4 bytes].

SOURCE

129     integer,parameter :: IK = int32

json_module/LK [ Parameters ]

[ Top ] [ json_module ] [ Parameters ]

NAME

LK

DESCRIPTION

Default logical kind. This is 4 bytes for the Intel and Gfortran compilers (and perhaps others). The declaration ensures a valid kind if the compiler doesn't have a logical_kinds(3).

SOURCE

254     integer,parameter :: LK = logical_kinds(min(3,size(logical_kinds)))

json_module/RK [ Parameters ]

[ Top ] [ json_module ] [ Parameters ]

NAME

RK

DESCRIPTION

Default real kind [8 bytes].

SOURCE

116     integer,parameter :: RK = real64

json_module/var_type [ Parameters ]

[ Top ] [ json_module ] [ Parameters ]

NAME

var_type

DESCRIPTION

The types of JSON data.

These are the values returned by the var_type arguments of the routines json_file_variable_info and json_info.

SOURCE

358     integer(IK),parameter,public :: json_unknown   = 0
359     integer(IK),parameter,public :: json_null      = 1
360     integer(IK),parameter,public :: json_object    = 2
361     integer(IK),parameter,public :: json_array     = 3
362     integer(IK),parameter,public :: json_logical   = 4
363     integer(IK),parameter,public :: json_integer   = 5
364     integer(IK),parameter,public :: json_double    = 6
365     integer(IK),parameter,public :: json_string    = 7

json_module/FILE_ENCODING [ Macros ]

[ Top ] [ json_module ] [ Macros ]

NAME

FILE_ENCODING

DESCRIPTION

File encoding preprocessor macro.

SOURCE

164 # define FILE_ENCODING
165     ! don't ask for utf-8 file encoding unless using UCS4
166     ! this may let us use unformatted stream io to read in files more quickly
167     ! even with unicode support turned on `inquire( ... encoding=FL_ENCODING)`
168     ! may be able to detect json files in which each character is exactly one
169     ! byte
170 # ifdef __GFORTRAN__
171 #   ifdef USE_UCS4
172     ! gfortran compiler AND UCS4 support requested, & silence redefine warning:
173     ! Make sure we output files with utf-8 encoding too
174 #     undef FILE_ENCODING
175 #     define FILE_ENCODING ,encoding='utf-8'
176 #   endif
177 # endif

json_module/MAYBEWRAP [ Macros ]

[ Top ] [ json_module ] [ Macros ]

NAME

MAYBEWRAP

DESCRIPTION

This C preprocessor macro will take a procedure name as an input, and output either that same procedure name if the code is compiled without USE_UCS4 being defined or it will expand the procedure name to the original procedure name, followed by a comma and then the original procedure name with 'wrap_' prepended to it. This is suitable for creating overloaded interfaces that will accept UCS4 character actual arguments as well as DEFAULT/ASCII character arguments, based on whether or not ISO 10646 is supported and requested.

SOURCE

275 # ifdef USE_UCS4
276 #   ifdef __GFORTRAN__
277     ! gfortran uses cpp in old-school compatibility mode so
278     ! the # stringify and ## concatenate operators don't work
279     ! but we can use C/C++ style comment to ensure PROCEDURE is
280     ! correctly tokenized and prepended with 'wrap_' when the
281     ! macro is expanded
282 #     define MAYBEWRAP(PROCEDURE) PROCEDURE , wrap_/**/PROCEDURE
283 #   endif
284 !   ifdef __INTEL_COMPILER
285     ! Intel's fpp does support the more contemporary ## concatenation
286     ! operator, but doesn't treat the C/C++ comments the same way.
287     ! If you use the gfortran approach and pass the -noB switch to
288     ! fpp, the macro will expand, but with a space between wrap_ and
289     ! whatever PROCEDURE expands to
290     ! Intel doesn't support ISO 10646 yet, but this is here to
291     ! ease the transition once they do.
292 !     define MAYBEWRAP(PROCEDURE) PROCEDURE , wrap_##PROCEDURE
293 !   endif
294 # else
295 #   define MAYBEWRAP(PROCEDURE) PROCEDURE
296 # endif

json_module/STRING_KIND [ Macros ]

[ Top ] [ json_module ] [ Macros ]

NAME

STRING_KIND

DESCRIPTION

String kind preprocessor macro.

SOURCE

142 # define STRING_KIND 'DEFAULT'
143     ! this is the string kind to use unless compiling with GFortran AND
144     ! UCS4/ISO 10646 support is requested
145 # ifdef __GFORTRAN__
146 #   ifdef USE_UCS4
147     ! gfortran compiler AND UCS4 support requested, & silence redefine warning:
148 #     undef  STRING_KIND
149 #     define STRING_KIND 'ISO_10646'
150 #   endif
151 # endif

json_module/USE_UCS4 [ Macros ]

[ Top ] [ json_module ] [ Macros ]

NAME

USE_UCS4

DESCRIPTION

USE_UCS4 is an optional preprocessor flag. When present, Unicode support is enabled.

USAGE

When compiling:


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

Consider the following example:

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

AUTHOR

Jacob Williams : 12/9/2013

SOURCE

447     type,public :: json_file
448 
449         private
450 
451         !the JSON structure read from the file:
452         type(json_value),pointer :: p => null()
453 
454     contains
455 
456         procedure,public :: load_file        => json_file_load
457 
458         generic,  public :: load_from_string => MAYBEWRAP(json_file_load_from_string)
459 
460         procedure,public :: destroy     => json_file_destroy
461         procedure,public :: move        => json_file_move_pointer
462         generic  ,public :: info        => MAYBEWRAP(json_file_variable_info)
463 
464         procedure,public :: print_to_string => json_file_print_to_string
465 
466         generic,public :: print_file => json_file_print_to_console, &
467                                         json_file_print_1, &
468                                         json_file_print_2
469 
470         generic,public :: get => MAYBEWRAP(json_file_get_object),      &
471                                  MAYBEWRAP(json_file_get_integer),     &
472                                  MAYBEWRAP(json_file_get_double),      &
473                                  MAYBEWRAP(json_file_get_logical),     &
474                                  MAYBEWRAP(json_file_get_string),      &
475                                  MAYBEWRAP(json_file_get_integer_vec), &
476                                  MAYBEWRAP(json_file_get_double_vec),  &
477                                  MAYBEWRAP(json_file_get_logical_vec), &
478                                  MAYBEWRAP(json_file_get_string_vec)
479 
480 
481 
482 
483         generic,public :: update =>  MAYBEWRAP(json_file_update_integer),  &
484                                      MAYBEWRAP(json_file_update_logical),  &
485                                      MAYBEWRAP(json_file_update_real),     &
486                                      MAYBEWRAP(json_file_update_string)
487 #     ifdef USE_UCS4
488         generic,public :: update => json_file_update_string_name_ascii, &
489                                     json_file_update_string_val_ascii
490 #     endif
491 
492         !load from string:
493         procedure :: MAYBEWRAP(json_file_load_from_string)
494 
495         !git info:
496         procedure :: MAYBEWRAP(json_file_variable_info)
497 
498         !get:
499         procedure :: MAYBEWRAP(json_file_get_object)
500         procedure :: MAYBEWRAP(json_file_get_integer)
501         procedure :: MAYBEWRAP(json_file_get_double)
502         procedure :: MAYBEWRAP(json_file_get_logical)
503         procedure :: MAYBEWRAP(json_file_get_string)
504         procedure :: MAYBEWRAP(json_file_get_integer_vec)
505         procedure :: MAYBEWRAP(json_file_get_double_vec)
506         procedure :: MAYBEWRAP(json_file_get_logical_vec)
507         procedure :: MAYBEWRAP(json_file_get_string_vec)
508 
509         !update:
510         procedure :: MAYBEWRAP(json_file_update_integer)
511         procedure :: MAYBEWRAP(json_file_update_logical)
512         procedure :: MAYBEWRAP(json_file_update_real)
513         procedure :: MAYBEWRAP(json_file_update_string)
514 #     ifdef USE_UCS4
515         procedure :: json_file_update_string_name_ascii
516         procedure :: json_file_update_string_val_ascii
517 #     endif
518 
519         !print_file:
520         procedure :: json_file_print_to_console
521         procedure :: json_file_print_1
522         procedure :: json_file_print_2
523 
524     end type json_file

json_module/json_value [ Classes ]

[ Top ] [ json_module ] [ Classes ]

NAME

json_value

DESCRIPTION

Type used to construct the linked-list json structure

EXAMPLE

Consider the following example:

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

SOURCE

388     type,public :: json_value
389 
390         !force the constituents to be stored contiguously
391         ![note: on Intel, the order of the variables below
392         ! is significant to avoid the misaligned field warnings]
393         sequence
394 
395         !for the linked list:
396         type(json_value),pointer :: previous => null()
397         type(json_value),pointer :: next     => null()
398         type(json_value),pointer :: parent   => null()
399         type(json_value),pointer :: children => null()
400         type(json_value),pointer :: tail     => null()
401 
402         !variable name:
403         character(kind=CK,len=:),allocatable :: name
404 
405         !the data for this variable:
406         real(RK),allocatable                 :: dbl_value
407         logical(LK),allocatable              :: log_value
408         character(kind=CK,len=:),allocatable :: str_value
409         integer(IK),allocatable              :: int_value
410 
411         integer(IK) :: var_type = json_unknown  !variable type
412 
413         integer(IK),private :: n_children = 0   !number of children
414 
415     end type json_value

json_module/array_callback_func [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

array_callback_func

DESCRIPTION

Array element callback function. Used by json_get_array.

SEE ALSO

json_get_array

SOURCE

540     abstract interface
541         subroutine array_callback_func(element, i, count)
542             import :: json_value,IK
543             implicit none
544             type(json_value), pointer,intent(in) :: element
545             integer(IK),intent(in) :: i        !index
546             integer(IK),intent(in) :: count    !size of array
547         end subroutine array_callback_func
548     end interface

json_module/json_add [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_add

DESCRIPTION

Add objects to a linked list of json_values.

NOTES

Formerly, this was called json_value_add

SOURCE

596     interface json_add
597         module procedure json_value_add_member
598         module procedure MAYBEWRAP(json_value_add_integer)
599         module procedure MAYBEWRAP(json_value_add_integer_vec)
600         module procedure MAYBEWRAP(json_value_add_double)
601         module procedure MAYBEWRAP(json_value_add_double_vec)
602         module procedure MAYBEWRAP(json_value_add_logical)
603         module procedure MAYBEWRAP(json_value_add_logical_vec)
604         module procedure MAYBEWRAP(json_value_add_string)
605         module procedure MAYBEWRAP(json_value_add_string_vec)
606 #     ifdef USE_UCS4
607         module procedure json_value_add_string_name_ascii
608         module procedure json_value_add_string_val_ascii
609         module procedure json_value_add_string_vec_name_ascii
610         module procedure json_value_add_string_vec_val_ascii
611 #     endif
612     end interface json_add

json_module/json_create_array [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_create_array

DESCRIPTION

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

EXAMPLE

Example usage:

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

AUTHOR

Jacob Williams

SOURCE

829     interface json_create_array
830         module procedure  MAYBEWRAP(json_value_create_array)
831     end interface

json_module/json_create_double [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_create_double

DESCRIPTION

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

EXAMPLE

Example usage:

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

AUTHOR

Jacob Williams

SOURCE

805     interface json_create_double
806         module procedure  MAYBEWRAP(json_value_create_double)
807     end interface

json_module/json_create_integer [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_create_integer

DESCRIPTION

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

EXAMPLE

Example usage:

     type(json_value),pointer :: p
     call json_create_integer(p,'value',42)

AUTHOR

Jacob Williams

SOURCE

929     interface json_create_integer
930         module procedure  MAYBEWRAP(json_value_create_integer)
931     end interface

json_module/json_create_logical [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_create_logical

DESCRIPTION

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

EXAMPLE

Example usage:

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

AUTHOR

Jacob Williams

SOURCE

953     interface json_create_logical
954         module procedure  MAYBEWRAP(json_value_create_logical)
955     end interface

json_module/json_create_null [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_create_null

DESCRIPTION

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

EXAMPLE

Example usage:

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

AUTHOR

Jacob Williams

SOURCE

881     interface json_create_null
882         module procedure  MAYBEWRAP(json_value_create_null)
883     end interface

json_module/json_create_object [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_create_object

DESCRIPTION

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

EXAMPLE

Example usage:

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

NOTES

The name is not significant for the root structure or an array element. In those cases, an empty string can be used.

AUTHOR

Jacob Williams

SOURCE

857     interface json_create_object
858         module procedure  MAYBEWRAP(json_value_create_object)
859     end interface

json_module/json_create_string [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_create_string

DESCRIPTION

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

EXAMPLE

Example usage:

     type(json_value),pointer :: p
     call json_create_string(p,'value','foobar')

AUTHOR

Jacob Williams

SOURCE

905     interface json_create_string
906         module procedure  MAYBEWRAP(json_value_create_string)
907     end interface

json_module/json_destroy [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

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.

EXAMPLE

Destroy the json_value pointer before the variable goes out of scope:

     subroutine example1()
     type(json_value),pointer :: p
     call json_create_object(p,'')
     call json_add(p,'year',2015)
     call json_print(p)
     call json_destroy(p)
     end subroutine example1

Note: it should NOT be called for a json_value pointer than has already been added to another json_value structure, since doing so may render the other structure invalid. Consider the following example:

     subroutine example2(p)
     type(json_value),pointer,intent(out) :: p
     type(json_value),pointer :: q
     call json_create_object(p,'')
     call json_add(p,'year',2015)
     call json_create_object(q,'q')
     call json_add(q,'val',1)
     call json_add(p, q)  !add q to p structure
     ! do NOT call json_destroy(q) here, because q is
     ! now part of the output structure p.  p should be destroyed
     ! somewhere upstream by the caller of this routine.
     nullify(q) !OK, but not strictly necessary
     end subroutine example2

SOURCE

751     interface json_destroy
752         module procedure json_value_destroy
753     end interface

json_module/json_get [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_get

DESCRIPTION

Get data from a json_value linked list.

NOTES

There are two versions (e.g. json_get_integer and json_get_integer_with_path). The first one gets the value from the json_value passed into the routine, while the second one gets the value from the json_value found by parsing the path. The path version is split up into unicode and non-unicode versions.

SOURCE

661     interface json_get
662         module procedure                       MAYBEWRAP(json_get_by_path)
663         module procedure json_get_integer,     MAYBEWRAP(json_get_integer_with_path)
664         module procedure json_get_integer_vec, MAYBEWRAP(json_get_integer_vec_with_path)
665         module procedure json_get_double,      MAYBEWRAP(json_get_double_with_path)
666         module procedure json_get_double_vec,  MAYBEWRAP(json_get_double_vec_with_path)
667         module procedure json_get_logical,     MAYBEWRAP(json_get_logical_with_path)
668         module procedure json_get_logical_vec, MAYBEWRAP(json_get_logical_vec_with_path)
669         module procedure json_get_string,      MAYBEWRAP(json_get_string_with_path)
670         module procedure json_get_string_vec,  MAYBEWRAP(json_get_string_vec_with_path)
671         module procedure json_get_array,       MAYBEWRAP(json_get_array_with_path)
672     end interface json_get

json_module/json_get_child [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_get_child

DESCRIPTION

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

NOTES

Formerly, this was called json_value_get_child

SOURCE

577     interface json_get_child
578         module procedure json_value_get_by_index
579         module procedure MAYBEWRAP(json_value_get_by_name_chars)
580     end interface json_get_child

json_module/json_parse [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_parse

DESCRIPTION

Parse the JSON file and populate the json_value tree.

INPUTS

The inputs can be:

EXAMPLE

Consider the following example:

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

NOTES

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

HISTORY

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

SOURCE

987     interface json_parse
988        module procedure  json_parse_file, MAYBEWRAP(json_parse_string)
989     end interface

json_module/json_print [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_print

DESCRIPTION

Print the json_value to a file.

EXAMPLE

Consider the following example:

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

SOURCE

706     interface json_print
707         module procedure json_print_1    !input is unit number
708         module procedure json_print_2    !input is file name
709     end interface

json_module/json_print_to_string [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_print_to_string

DESCRIPTION

Print the json_value structure to an allocatable string.

SOURCE

685     interface json_print_to_string
686         module procedure json_value_to_string
687     end interface

json_module/json_remove [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_remove

DESCRIPTION

Remove a json_value from a linked-list structure.

SOURCE

766     interface json_remove
767         module procedure json_value_remove
768     end interface

json_module/json_remove_if_present [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_remove_if_present

DESCRIPTION

If the child variable is present, then remove it.

SOURCE

781     interface json_remove_if_present
782         module procedure MAYBEWRAP(json_value_remove_if_present)
783     end interface

json_module/json_update [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

json_update

DESCRIPTION

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

NOTES

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

SOURCE

633     interface json_update
634         module procedure MAYBEWRAP(json_update_logical),&
635                          MAYBEWRAP(json_update_double),&
636                          MAYBEWRAP(json_update_integer),&
637                          MAYBEWRAP(json_update_string)
638 #     ifdef USE_UCS4
639         module procedure json_update_string_name_ascii
640         module procedure json_update_string_val_ascii
641 #     endif
642     end interface json_update

json_module/to_unicode [ Interfaces ]

[ Top ] [ json_module ] [ Interfaces ]

NAME

to_unicode

DESCRIPTION

Convert a 'DEFAULT' kind character input to 'ISO_10646' kind and return it

SOURCE

1002     interface to_unicode
1003         module procedure to_uni, to_uni_vec
1004     end interface

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

Consider the following example:

     type(json_file) :: json
     logical :: status_ok
     character(kind=CK,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

2219     subroutine json_check_for_errors(status_ok, error_msg)
2220 
2221     implicit none
2222 
2223     logical(LK),intent(out) :: status_ok
2224     character(kind=CK,len=:),allocatable,intent(out) :: error_msg
2225 
2226     status_ok = .not. exception_thrown
2227 
2228     if (.not. status_ok) then
2229         if (allocated(err_message)) then
2230             error_msg = err_message
2231         else
2232             error_msg = 'Unknown Error'
2233         end if
2234     else
2235         error_msg = ''
2236     end if
2237 
2238     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

2117     subroutine json_clear_exceptions()
2118 
2119     implicit none
2120 
2121     !clear the flag and message:
2122     exception_thrown = .false.
2123     err_message = ''
2124 
2125     end subroutine json_clear_exceptions

json_module/json_count [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_count

DESCRIPTION

Count the number of children.

HISTORY

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

SOURCE

3939     pure function json_count(me) result(count)
3940 
3941     implicit none
3942 
3943     integer(IK)                         :: count
3944     type(json_value),pointer,intent(in) :: me
3945 
3946     count = me%n_children
3947 
3948     end function json_count

json_module/json_failed [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_failed

DESCRIPTION

Logical function to indicate if an exception has been thrown.

EXAMPLE

Consider the following 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

2271     function json_failed() result(failed)
2272 
2273     implicit none
2274 
2275     logical(LK) :: failed
2276 
2277     failed = exception_thrown
2278 
2279     end function json_failed

json_module/json_file_destroy [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_destroy

USAGE

call me%destroy()

DESCRIPTION

Destroy the JSON file.

AUTHOR

Jacob Williams : 12/9/2013

SOURCE

1133     subroutine json_file_destroy(me)
1134 
1135     implicit none
1136 
1137     class(json_file),intent(inout) :: me
1138 
1139     if (associated(me%p)) call json_value_destroy(me%p)
1140 
1141     end subroutine json_file_destroy

json_module/json_file_get_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_get_double

USAGE

call me%get(path,val,found)

DESCRIPTION

Get a double from a JSON file.

AUTHOR

Jacob Williams : 12/9/2013

SOURCE

1714     subroutine json_file_get_double (me, path, val, found)
1715 
1716     implicit none
1717 
1718     class(json_file),intent(inout)      :: me
1719     character(kind=CK,len=*),intent(in) :: path
1720     real(RK),intent(out)                :: val
1721     logical(LK),intent(out),optional    :: found
1722 
1723     call json_get(me%p, path=path, value=val, found=found)
1724 
1725     end subroutine json_file_get_double

json_module/json_file_get_double_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_get_double_vec

USAGE

call me%get(path,vec,found)

DESCRIPTION

Get a double vector from a JSON file.

AUTHOR

Jacob Williams : 1/19/2014

SOURCE

1770     subroutine json_file_get_double_vec(me, path, vec, found)
1771 
1772     implicit none
1773 
1774     class(json_file),intent(inout)                :: me
1775     character(kind=CK,len=*),intent(in)           :: path
1776     real(RK),dimension(:),allocatable,intent(out) :: vec
1777     logical(LK),intent(out),optional              :: found
1778 
1779     call json_get(me%p, path, vec, found)
1780 
1781     end subroutine json_file_get_double_vec

json_module/json_file_get_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_get_integer

USAGE

call me%get(path,val)

DESCRIPTION

Get an integer from a JSON file.

AUTHOR

Jacob Williams : 12/9/2013

SOURCE

1602     subroutine json_file_get_integer(me, path, val, found)
1603 
1604     implicit none
1605 
1606     class(json_file),intent(inout)      :: me
1607     character(kind=CK,len=*),intent(in) :: path
1608     integer(IK),intent(out)             :: val
1609     logical(LK),intent(out),optional    :: found
1610 
1611     call json_get(me%p, path=path, value=val, found=found)
1612 
1613     end subroutine json_file_get_integer

json_module/json_file_get_integer_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_get_integer_vec

USAGE

call me%get(path,vec)

DESCRIPTION

Get an integer vector from a JSON file.

AUTHOR

Jacob Williams : 1/20/2014

SOURCE

1658     subroutine json_file_get_integer_vec(me, path, vec, found)
1659 
1660     implicit none
1661 
1662     class(json_file),intent(inout)                   :: me
1663     character(kind=CK,len=*),intent(in)              :: path
1664     integer(IK),dimension(:),allocatable,intent(out) :: vec
1665     logical(LK),intent(out),optional                 :: found
1666 
1667     call json_get(me%p, path, vec, found)
1668 
1669     end subroutine json_file_get_integer_vec

json_module/json_file_get_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_get_logical

USAGE

call me%get(path,val,found)

DESCRIPTION

Get a logical from a JSON file.

AUTHOR

Jacob Williams : 12/9/2013

SOURCE

1826     subroutine json_file_get_logical(me,path,val,found)
1827 
1828     implicit none
1829 
1830     class(json_file),intent(inout)       :: me
1831     character(kind=CK,len=*),intent(in)  :: path
1832     logical(LK),intent(out)              :: val
1833     logical(LK),intent(out),optional     :: found
1834 
1835     call json_get(me%p, path=path, value=val, found=found)
1836 
1837     end subroutine json_file_get_logical

json_module/json_file_get_logical_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_get_logical_vec

USAGE

call me%get(path,vec)

DESCRIPTION

Get a logical vector from a JSON file.

AUTHOR

Jacob Williams : 1/20/2014

SOURCE

1882     subroutine json_file_get_logical_vec(me, path, vec, found)
1883 
1884     implicit none
1885 
1886     class(json_file),intent(inout)                   :: me
1887     character(kind=CK,len=*),intent(in)              :: path
1888     logical(LK),dimension(:),allocatable,intent(out) :: vec
1889     logical(LK),intent(out),optional                 :: found
1890 
1891     call json_get(me%p, path, vec, found)
1892 
1893     end subroutine json_file_get_logical_vec

json_module/json_file_get_object [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_get_object

USAGE

call me%get(path,p,found)

DESCRIPTION

Get a pointer to an object from a JSON file.

AUTHOR

Jacob Williams : 2/3/2014

SOURCE

1546     subroutine json_file_get_object(me, path, p, found)
1547 
1548     implicit none
1549 
1550     class(json_file),intent(inout)       :: me
1551     character(kind=CK,len=*),intent(in)  :: path
1552     type(json_value),pointer,intent(out) :: p
1553     logical(LK),intent(out),optional     :: found
1554 
1555     call json_get_by_path(me%p, path=path, p=p, found=found)
1556 
1557     end subroutine json_file_get_object

json_module/json_file_get_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_get_string

USAGE

call me%get(path,val)

DESCRIPTION

Get a character string from a json file. The output val is an allocatable character string.

AUTHOR

Jacob Williams : 12/9/2013

SOURCE

1939     subroutine json_file_get_string(me, path, val, found)
1940 
1941     implicit none
1942 
1943     class(json_file),intent(inout)                   :: me
1944     character(kind=CK,len=*),intent(in)              :: path
1945     character(kind=CK,len=:),allocatable,intent(out) :: val
1946     logical(LK),intent(out),optional                 :: found
1947 
1948     call json_get(me%p, path=path, value=val, found=found)
1949 
1950     end subroutine json_file_get_string

json_module/json_file_get_string_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_get_string_vec

USAGE

call me%get(path,vec)

DESCRIPTION

Get a string vector from a JSON file.

AUTHOR

Jacob Williams : 1/19/2014

SOURCE

1995     subroutine json_file_get_string_vec(me, path, vec, found)
1996 
1997     implicit none
1998 
1999     class(json_file),intent(inout)                                :: me
2000     character(kind=CK,len=*),intent(in)                           :: path
2001     character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec
2002     logical(LK),intent(out),optional                              :: found
2003 
2004     call json_get(me%p, path, vec, found)
2005 
2006     end subroutine json_file_get_string_vec

json_module/json_file_load [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_load

DESCRIPTION

Load a JSON file.

EXAMPLE

Load a file:

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

AUTHOR

Jacob Williams : 12/9/2013

SOURCE

1201     subroutine json_file_load(me, filename, unit)
1202 
1203     implicit none
1204 
1205     class(json_file),intent(inout)      :: me
1206     character(kind=CDK,len=*),intent(in) :: filename
1207     integer(IK),intent(in),optional     :: unit
1208 
1209     call json_parse(file=filename, p=me%p, unit=unit)
1210 
1211     end subroutine json_file_load

json_module/json_file_load_from_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_load_from_string

DESCRIPTION

Load the JSON data from a string.

EXAMPLE

Load JSON from a string:

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

AUTHOR

Jacob Williams : 1/13/2015

SOURCE

1233     subroutine json_file_load_from_string(me, str)
1234 
1235     implicit none
1236 
1237     class(json_file),intent(inout)      :: me
1238     character(kind=CK,len=*),intent(in) :: str
1239 
1240     call json_parse(str=str, p=me%p)
1241 
1242     end subroutine json_file_load_from_string

json_module/json_file_move_pointer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_move_pointer

USAGE

call to%move(from)

DESCRIPTION

Move the json_value pointer from one json_file to another. The "from" pointer is then nullified, but not destroyed.

NOTES

If "from%p" is not associated, then an error is thrown.

AUTHOR

Jacob Williams : 12/5/2014

SOURCE

1165     subroutine json_file_move_pointer(to,from)
1166 
1167     implicit none
1168 
1169     class(json_file),intent(inout) :: to
1170     class(json_file),intent(inout) :: from
1171 
1172     if (associated(from%p)) then
1173         to%p => from%p
1174         nullify(from%p)
1175     else
1176         call throw_exception('Error in json_file_move_pointer: '//&
1177                              'pointer is not associated.')
1178     end if
1179 
1180     end subroutine json_file_move_pointer

json_module/json_file_print_1 [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_print_1

USAGE

call me%print_file(iunit)

DESCRIPTION

Prints the JSON file the specified (non zero) file unit.

AUTHOR

Jacob Williams : 12/9/2013

SOURCE

1315     subroutine json_file_print_1(me, iunit)
1316 
1317     implicit none
1318 
1319     class(json_file),intent(inout)  :: me
1320     integer(IK),intent(in)          :: iunit  !must be non-zero
1321 
1322     integer(IK) :: i
1323     character(kind=CK,len=:),allocatable :: dummy
1324 
1325     if (iunit/=0) then
1326         i = iunit
1327     else
1328         call throw_exception('Error in json_file_print_1: iunit must be nonzero.')
1329         return
1330     end if
1331 
1332     call json_value_print(me%p,iunit=i,str=dummy,indent=1,colon=.true.)
1333 
1334     end subroutine json_file_print_1

json_module/json_file_print_2 [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_print_2

USAGE

call me%print_file(filename)

DESCRIPTION

Print the JSON structure to the specified filename. The file is opened, printed, and then closed.

EXAMPLE

Example loading a JSON file, changing a value, and then printing result to a new file:

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

AUTHOR

Jacob Williams : 1/11/2015

SOURCE

1364     subroutine json_file_print_2(me,filename)
1365 
1366     implicit none
1367 
1368     class(json_file),intent(inout)      :: me
1369     character(kind=CDK,len=*),intent(in) :: filename
1370 
1371     integer(IK) :: iunit,istat
1372 
1373     open(newunit=iunit,file=filename,status='REPLACE',iostat=istat FILE_ENCODING )
1374     if (istat==0) then
1375         call me%print_file(iunit)    !call the other routine
1376         close(iunit,iostat=istat)
1377     else
1378         call throw_exception('Error in json_file_print_2: could not open file: '//&
1379                               trim(filename))
1380     end if
1381 
1382     end subroutine json_file_print_2

json_module/json_file_print_to_console [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_print_to_console

USAGE

call me%print_file()

DESCRIPTION

Print the JSON file to the console.

AUTHOR

Jacob Williams : 1/11/2015

SOURCE

1285     subroutine json_file_print_to_console(me)
1286 
1287     implicit none
1288 
1289     class(json_file),intent(inout)  :: me
1290 
1291     character(kind=CK,len=:),allocatable :: dummy
1292 
1293     call json_value_print(me%p,iunit=output_unit,str=dummy,indent=1,colon=.true.)
1294 
1295     end subroutine json_file_print_to_console

json_module/json_file_print_to_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_print_to_string

USAGE

call me%print_to_string(str)

DESCRIPTION

Print the JSON file to a string.

EXAMPLE

Open a JSON file, and then print the contents to a string:

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

AUTHOR

Jacob Williams : 1/11/2015

SOURCE

1409     subroutine json_file_print_to_string(me,str)
1410 
1411     implicit none
1412 
1413     class(json_file),intent(inout)                   :: me
1414     character(kind=CK,len=:),allocatable,intent(out) :: str
1415 
1416     call json_value_to_string(me%p,str)
1417 
1418     end subroutine json_file_print_to_string

json_module/json_file_update_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_update_integer

DESCRIPTION

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

SEE ALSO

json_update_integer

AUTHOR

Jacob Williams : 1/10/2015

SOURCE

2560     subroutine json_file_update_integer(me,name,val,found)
2561     implicit none
2562 
2563     class(json_file),intent(inout)      :: me
2564     character(kind=CK,len=*),intent(in) :: name
2565     integer(IK),intent(in)              :: val
2566     logical(LK),intent(out)             :: found
2567 
2568     if (.not. exception_thrown) call json_update(me%p,name,val,found)
2569 
2570     end subroutine json_file_update_integer

json_module/json_file_update_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_update_logical

DESCRIPTION

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

SEE ALSO

json_update_logical

AUTHOR

Jacob Williams : 1/10/2015

SOURCE

2616     subroutine json_file_update_logical(me,name,val,found)
2617     implicit none
2618 
2619     class(json_file),intent(inout)      :: me
2620     character(kind=CK,len=*),intent(in) :: name
2621     logical(LK),intent(in)              :: val
2622     logical(LK),intent(out)             :: found
2623 
2624     if (.not. exception_thrown) call json_update(me%p,name,val,found)
2625 
2626     end subroutine json_file_update_logical

json_module/json_file_update_real [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_update_real

DESCRIPTION

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

SEE ALSO

json_update_real

AUTHOR

Jacob Williams : 1/10/2015

SOURCE

2672     subroutine json_file_update_real(me,name,val,found)
2673     implicit none
2674 
2675     class(json_file),intent(inout)      :: me
2676     character(kind=CK,len=*),intent(in) :: name
2677     real(RK),intent(in)                 :: val
2678     logical(LK),intent(out)             :: found
2679 
2680     if (.not. exception_thrown) call json_update(me%p,name,val,found)
2681 
2682     end subroutine json_file_update_real

json_module/json_file_update_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_update_string

DESCRIPTION

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

SEE ALSO

json_update_string

AUTHOR

Jacob Williams : 1/10/2015

SOURCE

2728     subroutine json_file_update_string(me,name,val,found)
2729     implicit none
2730 
2731     class(json_file),intent(inout)      :: me
2732     character(kind=CK,len=*),intent(in) :: name
2733     character(kind=CK,len=*),intent(in) :: val
2734     logical(LK),intent(out)             :: found
2735 
2736     if (.not. exception_thrown) call json_update(me%p,name,val,found)
2737 
2738     end subroutine json_file_update_string

json_module/json_file_update_string_name_ascii [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_update_string_name_ascii

SOURCE

2773     subroutine json_file_update_string_name_ascii(me,name,val,found)
2774     implicit none
2775 
2776     class(json_file),intent(inout)       :: me
2777     character(kind=CDK,len=*),intent(in) :: name
2778     character(kind=CK, len=*),intent(in) :: val
2779     logical(LK),intent(out)              :: found
2780 
2781     call json_file_update_string(me,to_unicode(name),val,found)
2782 
2783     end subroutine json_file_update_string_name_ascii

json_module/json_file_update_string_val_ascii [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_update_string_val_ascii

SOURCE

2794     subroutine json_file_update_string_val_ascii(me,name,val,found)
2795     implicit none
2796 
2797     class(json_file),intent(inout)       :: me
2798     character(kind=CK, len=*),intent(in) :: name
2799     character(kind=CDK,len=*),intent(in) :: val
2800     logical(LK),intent(out)              :: found
2801 
2802     call json_file_update_string(me,name,to_unicode(val),found)
2803 
2804     end subroutine json_file_update_string_val_ascii

json_module/json_file_variable_info [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_file_variable_info

USAGE

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

DESCRIPTION

Returns information about a variable in a file.

AUTHOR

Jacob Williams : 2/3/2014

SOURCE

1438     subroutine json_file_variable_info(me,path,found,var_type,n_children)
1439 
1440     implicit none
1441 
1442     class(json_file),intent(inout)      :: me
1443     character(kind=CK,len=*),intent(in) :: path
1444     logical(LK),intent(out)             :: found
1445     integer(IK),intent(out)             :: var_type
1446     integer(IK),intent(out)             :: n_children
1447 
1448     type(json_value),pointer :: p
1449 
1450     !initialize:
1451     nullify(p)
1452 
1453     !get a pointer to the variable (if it is there):
1454     call me%get(path,p,found)
1455 
1456     if (found) then
1457 
1458         !get info:
1459         call json_info(p,var_type,n_children)
1460 
1461     else
1462 
1463         !set to dummy values:
1464         var_type = json_unknown
1465         n_children = 0
1466 
1467     end if
1468 
1469     !cleanup:
1470     nullify(p)
1471 
1472     end subroutine json_file_variable_info

json_module/json_get_array [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_array

DESCRIPTION

This routine calls the user-supplied array_callback subroutine for each element in the array.

NOTES

For integer, double, logical, and character arrays, a higher-level routine is provided (see json_get), so this routine does not have to be used for those cases.

SOURCE

5910     subroutine json_get_array(me, array_callback)
5911 
5912     implicit none
5913 
5914     type(json_value),pointer,intent(in)          :: me
5915     procedure(array_callback_func)               :: array_callback
5916 
5917     type(json_value),pointer :: element
5918     integer(IK) :: i, count
5919 
5920     if ( exception_thrown ) return
5921 
5922     nullify(element)
5923 
5924     select case (me%var_type)
5925     case (json_array)
5926         count = json_count(me)
5927         element => me%children
5928         do i = 1, count ! callback for each child
5929             call array_callback(element, i, count)
5930             element => element%next
5931         end do
5932     case default
5933 
5934         call throw_exception('Error in json_get_array:'//&
5935              ' Resolved value is not an array ')
5936 
5937     end select
5938 
5939     !cleanup:
5940     if (associated(element)) nullify(element)
5941 
5942     end subroutine json_get_array

json_module/json_get_array_with_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_array_with_path

DESCRIPTION

This routine calls the user-supplied array_callback subroutine for each element in the array.

SOURCE

5957     subroutine json_get_array_with_path(me, path, array_callback, found)
5958 
5959     implicit none
5960 
5961     type(json_value),pointer,intent(in) :: me
5962     character(kind=CK,len=*),intent(in) :: path
5963     procedure(array_callback_func)      :: array_callback
5964     logical(LK),intent(out),optional    :: found
5965 
5966     type(json_value),pointer :: p
5967 
5968     if ( exception_thrown ) then
5969         if ( present(found) ) found = .false.
5970         return
5971     end if
5972 
5973     nullify(p)
5974 
5975     ! resolve the path to the value
5976     call json_get_by_path(me=me, path=path, p=p)
5977 
5978     if (.not. associated(p)) then
5979         call throw_exception('Error in json_get_array:'//&
5980              ' Unable to resolve path: '//trim(path))
5981     else
5982        call json_get_array(me=p,array_callback=array_callback)
5983        nullify(p)
5984     end if
5985     if ( exception_thrown ) then
5986         if ( present(found) ) then
5987             found = .false.
5988             call json_clear_exceptions()
5989         end if
5990     else
5991         if ( present(found) ) found = .true.
5992     end if
5993 
5994     end subroutine json_get_array_with_path

json_module/json_get_by_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_by_path

USAGE

call json_get(me,path,p,found)

DESCRIPTION

Returns the json_value pointer given the path string.

EXAMPLE

Consider the following example:

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

NOTES

The following special characters are used to denote paths:

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

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

SOURCE

4480     subroutine json_get_by_path(me, path, p, found)
4481 
4482     implicit none
4483 
4484     type(json_value),pointer,intent(in)  :: me
4485     character(kind=CK,len=*),intent(in)  :: path
4486     type(json_value),pointer,intent(out) :: p
4487     logical(LK),intent(out),optional     :: found
4488 
4489     character(kind=CK,len=1),parameter :: start_array_alt = '('
4490     character(kind=CK,len=1),parameter :: end_array_alt   = ')'
4491 
4492     integer(IK)              :: i,length,child_i
4493     character(kind=CK,len=1) :: c
4494     logical(LK)              :: array
4495     type(json_value),pointer :: tmp
4496 
4497     if (.not. exception_thrown) then
4498 
4499         nullify(p)
4500 
4501         ! default to assuming relative to this
4502         p => me
4503 
4504         child_i = 1
4505 
4506         array = .false.
4507 
4508         length = len_trim(path)
4509 
4510         do i=1, length
4511 
4512             c = path(i:i)
4513 
4514             select case (c)
4515             case (CK_'$')
4516 
4517                 ! root
4518                 do while (associated (p%parent))
4519                     p => p%parent
4520                 end do
4521                 child_i = i + 1
4522 
4523             case (CK_'@')
4524 
4525                 ! this
4526                 p => me
4527                 child_i = i + 1
4528 
4529             case (CK_'.')
4530 
4531                 ! get child member from p
4532                 if (child_i < i) then
4533                     nullify(tmp)
4534                     call json_get_child(p, path(child_i:i-1), tmp)
4535                     p => tmp
4536                     nullify(tmp)
4537                 else
4538                     child_i = i + 1
4539                     cycle
4540                 end if
4541 
4542                 if (.not. associated(p)) then
4543                     call throw_exception('Error in json_get_by_path:'//&
4544                                          ' Error getting child member.')
4545                     exit
4546                 end if
4547 
4548                 child_i = i+1
4549 
4550             case (start_array,start_array_alt)
4551 
4552                 !....Modified to allow for 'var[3]' style syntax
4553                 !Note: jmozmoz/fson has a slightly different version of this...
4554 
4555                 ! start looking for the array element index
4556                 array = .true.
4557 
4558                 ! get child member from p
4559                 if (child_i < i) then
4560                     nullify(tmp)
4561                     call json_get_child(p, path(child_i:i-1), tmp)
4562                     p => tmp
4563                     nullify(tmp)
4564                 else
4565                     child_i = i + 1
4566                     cycle
4567                 end if
4568                 if (.not. associated(p)) then
4569                     call throw_exception('Error in json_get_by_path:'//&
4570                                          ' Error getting array element')
4571                     exit
4572                 end if
4573                 child_i = i + 1
4574 
4575             case (end_array,end_array_alt)
4576 
4577                 if (.not.array) then
4578                     call throw_exception('Error in json_get_by_path: Unexpected ]')
4579                     exit
4580                 end if
4581                 array = .false.
4582                 child_i = string_to_integer(path(child_i:i-1))
4583 
4584                 nullify(tmp)
4585                 call json_get_child(p, child_i, tmp)
4586                 p => tmp
4587                 nullify(tmp)
4588 
4589                 child_i= i + 1
4590 
4591             end select
4592 
4593         end do
4594 
4595         if (exception_thrown) then
4596 
4597             if (present(found)) then
4598                 found = .false.
4599                 call json_clear_exceptions()
4600             end if
4601 
4602         else
4603 
4604             ! grab the last child if present in the path
4605             if (child_i <= length) then
4606                 nullify(tmp)
4607                 call json_get_child(p, path(child_i:i-1), tmp)
4608                 p => tmp
4609                 nullify(tmp)
4610             end if
4611             if (associated(p)) then
4612                 if (present(found)) found = .true.    !everything seems to be ok
4613             else
4614                 call throw_exception('Error in json_get_by_path:'//&
4615                                      ' variable not found: '//trim(path))
4616                 if (present(found)) then
4617                     found = .false.
4618                     call json_clear_exceptions()
4619                 end if
4620             end if
4621 
4622         end if
4623 
4624     else
4625         if (present(found)) found = .false.
4626     end if
4627 
4628     end subroutine json_get_by_path

json_module/json_get_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_double

DESCRIPTION

Get a double value from a json_value.

SOURCE

4999     subroutine json_get_double(me, value)
5000 
5001     implicit none
5002 
5003     type(json_value),pointer           :: me
5004     real(RK),intent(out)               :: value
5005 
5006     value = 0.0_RK
5007     if ( exception_thrown ) return
5008 
5009     select case (me%var_type)
5010     case (json_integer)
5011         value = me%int_value
5012     case (json_double)
5013         value = me%dbl_value
5014     case (json_logical)
5015         if (me%log_value) then
5016             value = 1.0_RK
5017         else
5018             value = 0.0_RK
5019         end if
5020     case default
5021 
5022         call throw_exception('Error in json_get_double:'//&
5023                              ' Unable to resolve value to double: '//me%name)
5024 
5025     end select
5026 
5027     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

5125     subroutine json_get_double_vec(me, vec)
5126 
5127     implicit none
5128 
5129     type(json_value),pointer                      :: me
5130     real(RK),dimension(:),allocatable,intent(out) :: vec
5131 
5132     logical(LK) :: initialized
5133 
5134     initialized = .false.
5135 
5136     if (allocated(vec)) deallocate(vec)
5137 
5138     !the callback function is called for each element of the array:
5139     call json_get(me, array_callback=get_double_from_array)
5140 
5141     contains
5142 
5143         ! callback function for double
5144         subroutine get_double_from_array(element, i, count)
5145         implicit none
5146 
5147         type(json_value),pointer,intent(in) :: element
5148         integer(IK),intent(in)              :: i        !index
5149         integer(IK),intent(in)              :: count    !size of array
5150 
5151         !size the output array:
5152         if (.not. initialized) then
5153             allocate(vec(count))
5154             initialized = .true.
5155         end if
5156 
5157         !populate the elements:
5158         call json_get(element, value=vec(i))
5159 
5160         end subroutine get_double_from_array
5161 
5162     end subroutine json_get_double_vec

json_module/json_get_double_vec_with_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_double_vec_with_path

DESCRIPTION

Get a double vector from a JSON value.

SOURCE

5176     subroutine json_get_double_vec_with_path(me, path, vec, found)
5177 
5178     implicit none
5179 
5180     type(json_value),pointer                      :: me
5181     character(kind=CK,len=*),intent(in)           :: path
5182     real(RK),dimension(:),allocatable,intent(out) :: vec
5183     logical(LK),intent(out),optional              :: found
5184 
5185     logical(LK) :: initialized
5186 
5187     initialized = .false.
5188 
5189     if (allocated(vec)) deallocate(vec)
5190 
5191     !the callback function is called for each element of the array:
5192     call json_get(me, path=path, array_callback=get_double_from_array, found=found)
5193 
5194     contains
5195 
5196         ! callback function for double
5197         subroutine get_double_from_array(element, i, count)
5198         implicit none
5199 
5200         type(json_value),pointer,intent(in) :: element
5201         integer(IK),intent(in)              :: i        !index
5202         integer(IK),intent(in)              :: count    !size of array
5203 
5204         !size the output array:
5205         if (.not. initialized) then
5206             allocate(vec(count))
5207             initialized = .true.
5208         end if
5209 
5210         !populate the elements:
5211         call json_get(element, value=vec(i))
5212 
5213         end subroutine get_double_from_array
5214 
5215     end subroutine json_get_double_vec_with_path

json_module/json_get_double_with_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_double_with_path

DESCRIPTION

Get a double value from a json_value.

SOURCE

5041     subroutine json_get_double_with_path(me, path, value, found)
5042 
5043     implicit none
5044 
5045     type(json_value),pointer            :: me
5046     character(kind=CK,len=*),intent(in) :: path
5047     real(RK),intent(out)                :: value
5048     logical(LK),intent(out),optional    :: found
5049 
5050     type(json_value),pointer :: p
5051 
5052     value = 0.0_RK
5053     if ( exception_thrown ) then
5054         if ( present(found) ) found = .false.
5055         return
5056     end if
5057 
5058     nullify(p)
5059 
5060     call json_get_by_path(me=me, path=path, p=p)
5061 
5062     if (.not. associated(p)) then
5063 
5064         call throw_exception('Error in json_get_double:'//&
5065                              ' Unable to resolve path: '//trim(path))
5066 
5067     else
5068 
5069         call json_get_double(p,value)
5070         nullify(p)
5071 
5072     end if
5073 
5074     if (exception_thrown) then
5075         if (present(found)) then
5076             found = .false.
5077             call json_clear_exceptions()
5078         end if
5079     else
5080         if (present(found)) found = .true.
5081     end if
5082 
5083     end subroutine json_get_double_with_path

json_module/json_get_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_integer

DESCRIPTION

Get an integer value from a json_value.

SOURCE

4753     subroutine json_get_integer(me, value)
4754 
4755     implicit none
4756 
4757     type(json_value),pointer,intent(in) :: me
4758     integer(IK),intent(out)             :: value
4759 
4760     value = 0
4761     if ( exception_thrown ) return
4762 
4763     select case(me%var_type)
4764     case (json_integer)
4765         value = me%int_value
4766     case (json_double)
4767         value = int(me%dbl_value)
4768     case (json_logical)
4769         if (me%log_value) then
4770             value = 1
4771         else
4772             value = 0
4773         end if
4774     case default
4775         call throw_exception('Error in get_integer:'//&
4776              ' Unable to resolve value to integer: '//me%name)
4777     end select
4778 
4779     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

4872     subroutine json_get_integer_vec(me, vec)
4873 
4874     implicit none
4875 
4876     type(json_value),pointer                         :: me
4877     integer(IK),dimension(:),allocatable,intent(out) :: vec
4878 
4879     logical(LK) :: initialized
4880 
4881     initialized = .false.
4882 
4883     if (allocated(vec)) deallocate(vec)
4884 
4885     !the callback function is called for each element of the array:
4886     call json_get(me, array_callback=get_int_from_array)
4887 
4888     contains
4889 
4890         ! callback function for integer
4891         subroutine get_int_from_array(element, i, count)
4892         implicit none
4893 
4894         type(json_value),pointer,intent(in) :: element
4895         integer(IK),intent(in)              :: i        !index
4896         integer(IK),intent(in)              :: count    !size of array
4897 
4898         !size the output array:
4899         if (.not. initialized) then
4900             allocate(vec(count))
4901             initialized = .true.
4902         end if
4903 
4904         !populate the elements:
4905         call json_get(element, value=vec(i))
4906 
4907         end subroutine get_int_from_array
4908 
4909     end subroutine json_get_integer_vec

json_module/json_get_integer_vec_with_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_integer_vec_with_path

DESCRIPTION

Get an integer vector from a JSON value.

SOURCE

4923     subroutine json_get_integer_vec_with_path(me, path, vec, found)
4924 
4925     implicit none
4926 
4927     type(json_value),pointer                         :: me
4928     character(kind=CK,len=*),intent(in)              :: path
4929     integer(IK),dimension(:),allocatable,intent(out) :: vec
4930     logical(LK),intent(out),optional                 :: found
4931 
4932     logical(LK) :: initialized
4933 
4934     initialized = .false.
4935 
4936     call json_get(me, path=path, array_callback=get_int_from_array, found=found)
4937 
4938     ! need to duplicate callback function, no other way
4939     contains
4940 
4941         ! callback function for integer
4942         subroutine get_int_from_array(element, i, count)
4943         implicit none
4944 
4945         type(json_value),pointer,intent(in) :: element
4946         integer(IK),intent(in)              :: i        !index
4947         integer(IK),intent(in)              :: count    !size of array
4948 
4949         !size the output array:
4950         if (.not. initialized) then
4951             allocate(vec(count))
4952             initialized = .true.
4953         end if
4954 
4955         !populate the elements:
4956         call json_get(element, value=vec(i))
4957 
4958         end subroutine get_int_from_array
4959 
4960     end subroutine json_get_integer_vec_with_path

json_module/json_get_integer_with_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_integer_with_path

DESCRIPTION

Get an integer value from a json_value.

SOURCE

4793     subroutine json_get_integer_with_path(me, path, value, found)
4794 
4795     implicit none
4796 
4797     type(json_value),pointer,intent(in) :: me
4798     character(kind=CK,len=*),intent(in) :: path
4799     integer(IK),intent(out)             :: value
4800     logical(LK),intent(out),optional    :: found
4801 
4802     type(json_value),pointer :: p
4803 
4804     value = 0
4805     if ( exception_thrown ) then
4806        if ( present(found) ) found = .false.
4807        return
4808     end if
4809 
4810     nullify(p)
4811 
4812     call json_get_by_path(me=me, path=path, p=p)
4813 
4814     if (.not. associated(p)) then
4815         call throw_exception('Error in json_get_integer:'//&
4816             ' Unable to resolve path: '// trim(path))
4817     else
4818         call json_get_integer(p,value)
4819         nullify(p)
4820     end if
4821     if ( exception_thrown ) then
4822         if ( present(found) ) then
4823             found = .false.
4824             call json_clear_exceptions()
4825         end if
4826     else
4827         if ( present(found) ) found = .true.
4828     end if
4829 
4830     end subroutine json_get_integer_with_path

json_module/json_get_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_logical

DESCRIPTION

Get a logical value from a json_value.

SOURCE

5254     subroutine json_get_logical(me, value)
5255 
5256     implicit none
5257 
5258     type(json_value),pointer,intent(in) :: me
5259     logical(LK)                         :: value
5260 
5261     value = .false.
5262     if ( exception_thrown ) return
5263 
5264     select case (me%var_type)
5265     case (json_integer)
5266         value = (me%int_value > 0)
5267     case (json_logical)
5268         value = me % log_value
5269     case default
5270         call throw_exception('Error in json_get_logical:'//&
5271                              ' Unable to resolve value to logical: '//me%name)
5272     end select
5273 
5274     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

5372     subroutine json_get_logical_vec(me, vec)
5373 
5374     implicit none
5375 
5376     type(json_value),pointer,intent(in)              :: me
5377     logical(LK),dimension(:),allocatable,intent(out) :: vec
5378 
5379     logical(LK) :: initialized
5380 
5381     initialized = .false.
5382 
5383     if (allocated(vec)) deallocate(vec)
5384 
5385     !the callback function is called for each element of the array:
5386     call json_get(me, array_callback=get_logical_from_array)
5387 
5388     contains
5389 
5390         ! callback function for logical
5391         subroutine get_logical_from_array(element, i, count)
5392         implicit none
5393 
5394         type(json_value),pointer,intent(in) :: element
5395         integer(IK),intent(in)              :: i        !index
5396         integer(IK),intent(in)              :: count    !size of array
5397 
5398         !size the output array:
5399         if (.not. initialized) then
5400             allocate(vec(count))
5401             initialized = .true.
5402         end if
5403 
5404         !populate the elements:
5405         call json_get(element, value=vec(i))
5406 
5407         end subroutine get_logical_from_array
5408 
5409     end subroutine json_get_logical_vec

json_module/json_get_logical_vec_with_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_logical_vec_with_path

DESCRIPTION

Get a logical vector from a JSON value.

SOURCE

5423     subroutine json_get_logical_vec_with_path(me, path, vec, found)
5424 
5425     implicit none
5426 
5427     type(json_value),pointer,intent(in)              :: me
5428     character(kind=CK,len=*),intent(in)              :: path
5429     logical(LK),dimension(:),allocatable,intent(out) :: vec
5430     logical(LK),intent(out),optional                 :: found
5431 
5432     logical(LK) :: initialized
5433 
5434     initialized = .false.
5435 
5436     if (allocated(vec)) deallocate(vec)
5437 
5438     !the callback function is called for each element of the array:
5439     call json_get(me, path=path, array_callback=get_logical_from_array, found=found)
5440 
5441     contains
5442 
5443         ! callback function for logical
5444         subroutine get_logical_from_array(element, i, count)
5445         implicit none
5446 
5447         type(json_value),pointer,intent(in) :: element
5448         integer(IK),intent(in)              :: i        !index
5449         integer(IK),intent(in)              :: count    !size of array
5450 
5451         !size the output array:
5452         if (.not. initialized) then
5453             allocate(vec(count))
5454             initialized = .true.
5455         end if
5456 
5457         !populate the elements:
5458         call json_get(element, value=vec(i))
5459 
5460         end subroutine get_logical_from_array
5461 
5462     end subroutine json_get_logical_vec_with_path

json_module/json_get_logical_with_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_logical_with_path

DESCRIPTION

Get a logical value from a json_value.

SOURCE

5288     subroutine json_get_logical_with_path(me, path, value, found)
5289 
5290     implicit none
5291 
5292     type(json_value),pointer,intent(in) :: me
5293     character(kind=CK,len=*),intent(in) :: path
5294     logical(LK)                         :: value
5295     logical(LK),intent(out),optional    :: found
5296 
5297     type(json_value),pointer :: p
5298 
5299     value = .false.
5300     if ( exception_thrown) then
5301         if ( present(found) ) found = .false.
5302         return
5303     end if
5304 
5305     nullify(p)
5306 
5307     call json_get_by_path(me=me, path=path, p=p)
5308 
5309     if (.not. associated(p)) then
5310 
5311         call throw_exception('Error in json_get_logical:'//&
5312                              ' Unable to resolve path: '//trim(path))
5313 
5314     else
5315 
5316         call json_get_logical(p,value)
5317         nullify(p)
5318 
5319     end if
5320 
5321     if (exception_thrown) then
5322         if (present(found)) then
5323             found = .false.
5324             call json_clear_exceptions()
5325         end if
5326     else
5327         if (present(found)) found = .true.
5328     end if
5329 
5330     end subroutine json_get_logical_with_path

json_module/json_get_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_string

DESCRIPTION

Get a character string from a json_value.

SOURCE

5501     subroutine json_get_string(me, value)
5502 
5503     implicit none
5504 
5505     type(json_value),pointer,intent(in)              :: me
5506     character(kind=CK,len=:),allocatable,intent(out) :: value
5507 
5508     character(kind=CK ,len=:),allocatable :: s,pre,post
5509     integer(IK) :: j,jprev,n
5510     character(kind=CK,len=1) :: c
5511 
5512     value = ''
5513     if ( exception_thrown) return
5514 
5515     select case (me%var_type)
5516 
5517     case (json_string)
5518 
5519         if (allocated(me%str_value)) then
5520 
5521             !get the value as is:
5522             s = me%str_value
5523 
5524             ! Now, have to remove the escape characters:
5525             !
5526             ! '\"'        quotation mark
5527             ! '\\'        reverse solidus
5528             ! '\/'        solidus
5529             ! '\b'        backspace
5530             ! '\f'        formfeed
5531             ! '\n'        newline (LF)
5532             ! '\r'        carriage return (CR)
5533             ! '\t'        horizontal tab
5534             ! '\uXXXX'    4 hexadecimal digits
5535             !
5536 
5537             !initialize:
5538             n = len(s)
5539             j = 1
5540 
5541             do
5542 
5543                 jprev = j                      !initialize
5544                 j = index(s(j:n),backslash)    !look for an escape character
5545 
5546                 if (j>0) then            !an escape character was found
5547 
5548                     !index in full string of the escape character:
5549                     j = j + (jprev-1)
5550 
5551                     if (j<n) then
5552 
5553                         !save the bit before the escape character:
5554                         if (j>1) then
5555                             pre = s( 1 : j-1 )
5556                         else
5557                             pre = ''
5558                         end if
5559 
5560                         !character after the escape character:
5561                         c = s( j+1 : j+1 )
5562 
5563                         if (any(c == [quotation_mark,backslash,slash, &
5564                              to_unicode(['b','f','n','r','t'])])) then
5565 
5566                             !save the bit after the escape characters:
5567                             if (j+2<n) then
5568                                  post = s(j+2:n)
5569                             else
5570                                  post = ''
5571                             end if
5572 
5573                             select case(c)
5574                             case (quotation_mark,backslash,slash)
5575                                 !use c as is
5576                             case (CK_'b')
5577                                  c = bspace
5578                             case (CK_'f')
5579                                  c = formfeed
5580                             case (CK_'n')
5581                                  c = newline
5582                             case (CK_'r')
5583                                  c = carriage_return
5584                             case (CK_'t')
5585                                  c = horizontal_tab
5586                             end select
5587 
5588                             s = pre//c//post
5589 
5590                             n = n-1    !backslash character has been
5591                                        ! removed from the string
5592 
5593                         else if (c == 'u') then !expecting 4 hexadecimal digits after
5594                                                 !the escape character    [\uXXXX]
5595 
5596                             !for now, we are just printing them as is
5597                             ![not checking to see if it is a valid hex value]
5598 
5599                             if (j+5<=n) then
5600                                 j=j+4
5601                             else
5602                                 call throw_exception('Error in json_get_string:'//&
5603                                                      ' Invalid hexadecimal sequence'//&
5604                                                      ' in string: '//trim(c))
5605                                 exit
5606                             end if
5607 
5608                         else
5609                             !unknown escape character
5610                             call throw_exception('Error in json_get_string:'//&
5611                                                  ' unknown escape sequence in string "'//&
5612                                                  trim(s)//'" ['//backslash//c//']')
5613                             exit
5614                         end if
5615 
5616                         j=j+1    !go to the next character
5617 
5618                         if (j>=n) exit    !finished
5619 
5620                     else
5621                         !an escape character is the last character in
5622                         ! the string [this may not be valid syntax,
5623                         ! but just keep it]
5624                         exit
5625                     end if
5626 
5627                 else
5628                     exit    !no more escape characters in the string
5629                 end if
5630 
5631             end do
5632 
5633             if (exception_thrown) then
5634                if (allocated(value)) deallocate(value)
5635             else
5636                value = s
5637             end if
5638 
5639         else
5640            call throw_exception('Error in json_get_string:'//&
5641                 ' me%value not allocated')
5642         end if
5643 
5644     case default
5645         call throw_exception('Error in json_get_string:'//&
5646              ' Unable to resolve value to characters: '//me%name)
5647 
5648         ! Note: for the other cases, we could do val to string conversions.
5649 
5650     end select
5651 
5652     !cleanup:
5653     if (allocated(s)) deallocate(s)
5654     if (allocated(pre)) deallocate(pre)
5655     if (allocated(post)) deallocate(post)
5656 
5657     end subroutine json_get_string

json_module/json_get_string_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_string_vec

DESCRIPTION

Get a string vector from a JSON file.

AUTHOR

Jacob Williams : 5/14/2014

SOURCE

5757     subroutine json_get_string_vec(me, vec)
5758 
5759     implicit none
5760 
5761     type(json_value),pointer,intent(in)                           :: me
5762     character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec
5763 
5764     logical(LK) :: initialized
5765 
5766     initialized = .false.
5767 
5768     if (allocated(vec)) deallocate(vec)
5769 
5770     !the callback function is called for each element of the array:
5771     call json_get(me, array_callback=get_chars_from_array)
5772 
5773     contains
5774 
5775         ! callback function for chars
5776         subroutine get_chars_from_array(element, i, count)
5777 
5778         implicit none
5779 
5780         type(json_value),pointer,intent(in) :: element
5781         integer(IK),intent(in)              :: i        !index
5782         integer(IK),intent(in)              :: count    !size of array
5783 
5784         character(kind=CK,len=:),allocatable :: cval
5785 
5786         !size the output array:
5787         if (.not. initialized) then
5788             allocate(vec(count))
5789             initialized = .true.
5790         end if
5791 
5792         !populate the elements:
5793         call json_get(element, value=cval)
5794         if (allocated(cval)) then
5795             vec(i) = cval
5796             deallocate(cval)
5797         else
5798             vec(i) = ''
5799         end if
5800 
5801         end subroutine get_chars_from_array
5802 
5803     end subroutine json_get_string_vec

json_module/json_get_string_vec_with_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_string_vec_with_path

DESCRIPTION

Get a string vector from a JSON file.

SOURCE

5817     subroutine json_get_string_vec_with_path(me, path, vec, found)
5818 
5819     implicit none
5820 
5821     type(json_value),pointer,intent(in)                           :: me
5822     character(kind=CK,len=*),intent(in)                           :: path
5823     character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec
5824     logical(LK),intent(out),optional                              :: found
5825 
5826     logical(LK) :: initialized
5827 
5828     initialized = .false.
5829 
5830     if (allocated(vec)) deallocate(vec)
5831 
5832     !the callback function is called for each element of the array:
5833     call json_get(me, path=path, array_callback=get_chars_from_array, found=found)
5834 
5835     contains
5836 
5837         ! callback function for chars
5838         subroutine get_chars_from_array(element, i, count)
5839 
5840         implicit none
5841 
5842         type(json_value),pointer,intent(in) :: element
5843         integer(IK),intent(in)              :: i        !index
5844         integer(IK),intent(in)              :: count    !size of array
5845 
5846         character(kind=CK,len=:),allocatable :: cval
5847 
5848         !size the output array:
5849         if (.not. initialized) then
5850             allocate(vec(count))
5851             initialized = .true.
5852         end if
5853 
5854         !populate the elements:
5855         call json_get(element, value=cval)
5856         if (allocated(cval)) then
5857             vec(i) = cval
5858             deallocate(cval)
5859         else
5860             vec(i) = ''
5861         end if
5862 
5863         end subroutine get_chars_from_array
5864 
5865     end subroutine json_get_string_vec_with_path

json_module/json_get_string_with_path [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_get_string_with_path

DESCRIPTION

Get a character string from a json_value.

SOURCE

5671     subroutine json_get_string_with_path(me, path, value, found)
5672 
5673     implicit none
5674 
5675     type(json_value),pointer,intent(in)              :: me
5676     character(kind=CK,len=*),intent(in)              :: path
5677     character(kind=CK,len=:),allocatable,intent(out) :: value
5678     logical(LK),intent(out),optional                 :: found
5679 
5680     type(json_value),pointer :: p
5681 
5682     value = ''
5683     if ( exception_thrown ) then
5684         if ( present(found) ) found = .false.
5685         return
5686     end if
5687 
5688     nullify(p)
5689 
5690     call json_get_by_path(me=me, path=path, p=p)
5691 
5692     if (.not. associated(p)) then
5693         call throw_exception('Error in json_get_string:'//&
5694                              ' Unable to resolve path: '//trim(path))
5695 
5696     else
5697 
5698         call json_get_string(p,value)
5699         nullify(p)
5700 
5701     end if
5702 
5703     if (allocated(value) .and. .not. exception_thrown) then
5704         if (present(found)) found = .true.
5705     else
5706         if (present(found)) then
5707             found = .false.
5708             call json_clear_exceptions()
5709         end if
5710     end if
5711 
5712     !cleanup:
5713     if (associated(p)) nullify(p)
5714 
5715     end subroutine json_get_string_with_path

json_module/json_info [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_info

DESCRIPTION

Returns information about a json_value

AUTHOR

Jacob Williams : 2/13/2014

SOURCE

1515     subroutine json_info(p,var_type,n_children)
1516 
1517     implicit none
1518 
1519     type(json_value),pointer         :: p
1520     integer(IK),intent(out),optional :: var_type
1521     integer(IK),intent(out),optional :: n_children
1522 
1523     if (present(var_type))    var_type = p%var_type  !variable type
1524     if (present(n_children))  n_children = json_count(p)  !number of children
1525 
1526     end subroutine json_info

json_module/json_initialize [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_initialize

DESCRIPTION

Initialize the json-fortran module. The routine must be called before any of the routines are used. It can also be called after using the module and encountering exceptions.

AUTHOR

Jacob Williams : 12/4/2013

MODIFIED

Izaak Beekman : 02/24/2015

SOURCE

2053     subroutine json_initialize(verbose,compact_reals)
2054 
2055     implicit none
2056 
2057     logical(LK),intent(in),optional :: verbose  !mainly useful for debugging (default is false)
2058     logical(LK),intent(in),optional :: compact_reals !to compact the real number strings for output
2059 
2060     character(kind=CDK,len=10) :: w,do,e
2061     integer(IK) :: istat
2062 
2063     !clear any errors from previous runs:
2064     call json_clear_exceptions()
2065 
2066 # ifdef USE_UCS4
2067     ! reopen stdout and stderr with utf-8 encoding
2068     open(output_unit,encoding='utf-8')
2069     open(error_unit, encoding='utf-8')
2070 # endif
2071 
2072     !Ensure gfortran bug work around "parameters" are set properly
2073     null_str  = 'null'
2074     true_str  = 'true'
2075     false_str = 'false'
2076 
2077     !optional inputs (if not present, values remains unchanged):
2078     if (present(verbose))       is_verbose   = verbose
2079     if (present(compact_reals)) compact_real = compact_reals  !may be a bug here in Gfortran 5.0.0... check this...
2080 
2081     ! set the default output/input format for reals:
2082     !  [this only needs to be done once, since it can't change]
2083     if (.not. allocated(real_fmt)) then
2084                       write(w,'(I0)',iostat=istat) max_numeric_str_len
2085         if (istat==0) write(do,'(I0)',iostat=istat) real_precision
2086         if (istat==0) write(e,'(I0)',iostat=istat) real_exponent_digits
2087         if (istat==0) then
2088             real_fmt = '(E' // trim(w) // '.' // trim(do) // 'E' // trim(e) // ')'
2089         else
2090             real_fmt = '(E30.16E3)'  !just use this one (should never happen)
2091         end if
2092     end if
2093 
2094     !Just in case, clear these global variables also:
2095     pushed_index = 0
2096     pushed_char  = ''
2097     char_count   = 0
2098     line_count   = 1
2099 
2100     end subroutine json_initialize

json_module/json_parse_file [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_parse

DESCRIPTION

Parse the JSON file and populate the json_value tree.

INPUTS

The inputs can be:

EXAMPLE

Consider the following example:

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

NOTES

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

HISTORY

Jacob Williams : 01/13/2015 : added read from string option. Izaak Beekman : 03/08/2015 : moved read from string to separate subroutine, and error annotation to separate subroutine

SOURCE

6054     subroutine json_parse_file(file, p, unit)
6055 
6056     implicit none
6057 
6058     character(kind=CDK,len=*),intent(in)         :: file  !JSON file name
6059     type(json_value),pointer                     :: p     !output structure
6060     integer(IK),intent(in),optional              :: unit  !file unit number (/= 0)
6061 
6062     integer(IK) :: iunit, istat
6063     logical(LK) :: is_open
6064     character(kind=CK,len=:),allocatable :: buffer
6065 
6066     !clear any exceptions and initialize:
6067     call json_initialize()
6068 
6069     if ( present(unit) ) then
6070 
6071         if (unit==0) then
6072             call throw_exception('Error in json_parse: unit number must not be 0.')
6073             return
6074         end if
6075 
6076         iunit = unit
6077 
6078         !check to see if the file is already open
6079         ! if it is, then use it, otherwise open the file with the name given.
6080         inquire(unit=iunit, opened=is_open, iostat=istat)
6081         if (istat==0 .and. .not. is_open) then
6082            ! open the file
6083             open (  unit        = iunit, &
6084                     file        = file, &
6085                     status      = 'OLD', &
6086                     action      = 'READ', &
6087                     form        = 'FORMATTED', &
6088                     position    = 'REWIND', &
6089                     iostat      = istat &
6090                     FILE_ENCODING )
6091         end if
6092 
6093     else
6094 
6095         ! open the file with a new unit number:
6096         open (  newunit     = iunit, &
6097                 file        = file, &
6098                 status      = 'OLD', &
6099                 action      = 'READ', &
6100                 form        = 'FORMATTED', &
6101                 position    = 'REWIND', &
6102                 iostat      = istat &
6103                 FILE_ENCODING )
6104 
6105     end if
6106 
6107     if (istat==0) then
6108 
6109         ! create the value and associate the pointer
6110         call json_value_create(p)
6111 
6112         ! Note: the name of the root json_value doesn't really matter,
6113         !  but we'll allocate something here just in case.
6114         p%name = trim(file)  !use the file name
6115 
6116         ! parse as a value
6117         buffer = ''
6118         call parse_value(unit=iunit, str=buffer, value=p)
6119 
6120         if ( exception_thrown ) call annotate_invalid_json(iunit,buffer)
6121 
6122         ! cleanup:
6123         if (allocated(buffer)) deallocate(buffer)
6124 
6125         ! close the file if necessary
6126         if (iunit/=0) close(unit=iunit, iostat=istat)
6127 
6128     else
6129 
6130         call throw_exception('Error in json_parse: Error opening file: '//trim(file))
6131         nullify(p)
6132 
6133     end if
6134 
6135     end subroutine json_parse_file

json_module/json_parse_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_parse_string

DESCRIPTION

Parse the JSON string and populate the json_value tree.

SEE ALSO

json_parse_file

SOURCE

6152     subroutine json_parse_string(p, str)
6153 
6154     implicit none
6155 
6156     type(json_value),pointer            :: p     !output structure
6157     character(kind=CK,len=*),intent(in) :: str   !string with JSON data
6158 
6159     integer(IK) :: iunit
6160     character(kind=CK,len=:),allocatable :: buffer
6161 
6162     if ( exception_thrown ) return ! to caller
6163 
6164     !clear any exceptions and initialize:
6165     call json_initialize()
6166 
6167     buffer = str
6168     iunit = 0    !indicates that json data will be read from buffer
6169 
6170     ! create the value and associate the pointer
6171     call json_value_create(p)
6172 
6173     ! Note: the name of the root json_value doesn't really matter,
6174     !  but we'll allocate something here just in case.
6175     p%name = ''          !if reading it from the string
6176 
6177     ! parse as a value
6178     call parse_value(unit=iunit, str=buffer, value=p)
6179 
6180     if ( exception_thrown ) call annotate_invalid_json(iunit,str) ! always 0
6181 
6182     ! cleanup:
6183     if (allocated(buffer)) deallocate(buffer)
6184 
6185     end subroutine json_parse_string

json_module/json_print_1 [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_print_1

DESCRIPTION

Print the JSON structure to a file.

INPUT

AUTHOR

Jacob Williams, 6/20/2014

SOURCE

4132     subroutine json_print_1(me,iunit)
4133 
4134     implicit none
4135 
4136     type(json_value),pointer,intent(in)  :: me
4137     integer(IK),intent(in)               :: iunit    !must be non-zero
4138     character(kind=CK,len=:),allocatable :: dummy
4139 
4140     if (iunit/=0) then
4141         call json_value_print(me,iunit,str=dummy, indent=1, colon=.true.)
4142     else
4143         call throw_exception('Error in json_print: iunit must be nonzero.')
4144     end if
4145 
4146     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

Input is the filename.

AUTHOR

Jacob Williams, 12/23/2014

SOURCE

4165     subroutine json_print_2(me,filename)
4166 
4167     implicit none
4168 
4169     type(json_value),pointer,intent(in) :: me
4170     character(kind=CDK,len=*),intent(in) :: filename
4171     integer(IK) :: iunit,istat
4172 
4173     open(newunit=iunit,file=filename,status='REPLACE',iostat=istat FILE_ENCODING )
4174     if (istat==0) then
4175         call json_print(me,iunit)
4176         close(iunit,iostat=istat)
4177     else
4178         call throw_exception('Error in json_print: could not open file: '//&
4179                               trim(filename))
4180     end if
4181 
4182     end subroutine json_print_2

json_module/json_update_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_update_double

DESCRIPTION

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

AUTHOR

Jacob Williams : 12/6/2014

SOURCE

2896     subroutine json_update_double(p,name,val,found)
2897 
2898     implicit none
2899 
2900     type(json_value),pointer            :: p
2901     character(kind=CK,len=*),intent(in) :: name
2902     real(RK),intent(in)                 :: val
2903     logical(LK),intent(out)             :: found
2904 
2905     type(json_value),pointer :: p_var
2906     integer(IK) :: var_type
2907 
2908     call json_get(p,name,p_var,found)
2909     if (found) then
2910 
2911         call json_info(p_var,var_type)
2912         select case (var_type)
2913         case (json_null,json_logical,json_integer,json_double,json_string)
2914             call to_double(p_var,val)    !update the value
2915         case default
2916             found = .false.
2917             call throw_exception('Error in json_update_double: '//&
2918                                  'the variable is not a scalar value')
2919         end select
2920 
2921     else
2922         call json_add(p,name,val)   !add the new element
2923     end if
2924 
2925     end subroutine json_update_double

json_module/json_update_integer [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_update_integer

DESCRIPTION

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

AUTHOR

Jacob Williams : 12/6/2014

SOURCE

2969     subroutine json_update_integer(p,name,val,found)
2970 
2971     implicit none
2972 
2973     type(json_value),pointer            :: p
2974     character(kind=CK,len=*),intent(in) :: name
2975     integer(IK),intent(in)              :: val
2976     logical(LK),intent(out)             :: found
2977 
2978     type(json_value),pointer :: p_var
2979     integer(IK) :: var_type
2980 
2981     call json_get(p,name,p_var,found)
2982     if (found) then
2983 
2984         call json_info(p_var,var_type)
2985         select case (var_type)
2986         case (json_null,json_logical,json_integer,json_double,json_string)
2987             call to_integer(p_var,val)    !update the value
2988         case default
2989             found = .false.
2990             call throw_exception('Error in json_update_integer: '//&
2991                                  'the variable is not a scalar value')
2992         end select
2993 
2994     else
2995         call json_add(p,name,val)   !add the new element
2996     end if
2997 
2998     end subroutine json_update_integer

json_module/json_update_logical [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_update_logical

DESCRIPTION

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

AUTHOR

Jacob Williams : 12/6/2014

SOURCE

2823     subroutine json_update_logical(p,name,val,found)
2824 
2825     implicit none
2826 
2827     type(json_value),pointer            :: p
2828     character(kind=CK,len=*),intent(in) :: name
2829     logical(LK),intent(in)              :: val
2830     logical(LK),intent(out)             :: found
2831 
2832     type(json_value),pointer :: p_var
2833     integer(IK) :: var_type
2834 
2835     call json_get(p,name,p_var,found)
2836     if (found) then
2837 
2838         call json_info(p_var,var_type)
2839         select case (var_type)
2840         case (json_null,json_logical,json_integer,json_double,json_string)
2841             call to_logical(p_var,val)    !update the value
2842         case default
2843             found = .false.
2844             call throw_exception('Error in json_update_logical: '//&
2845                                  'the variable is not a scalar value')
2846         end select
2847 
2848     else
2849         call json_add(p,name,val)   !add the new element
2850     end if
2851 
2852     end subroutine json_update_logical

json_module/json_update_string [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_update_string

DESCRIPTION

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

AUTHOR

Jacob Williams : 12/6/2014

SOURCE

3042     subroutine json_update_string(p,name,val,found)
3043 
3044     implicit none
3045 
3046     type(json_value),pointer            :: p
3047     character(kind=CK,len=*),intent(in) :: name
3048     character(kind=CK,len=*),intent(in) :: val
3049     logical(LK),intent(out)             :: found
3050 
3051     type(json_value),pointer :: p_var
3052     integer(IK) :: var_type
3053 
3054     call json_get(p,name,p_var,found)
3055     if (found) then
3056 
3057         call json_info(p_var,var_type)
3058         select case (var_type)
3059         case (json_null,json_logical,json_integer,json_double,json_string)
3060             call to_string(p_var,val)    !update the value
3061         case default
3062             found = .false.
3063             call throw_exception('Error in json_update_string: '//&
3064                                  'the variable is not a scalar value')
3065         end select
3066 
3067     else
3068         call json_add(p,name,val)   !add the new element
3069     end if
3070 
3071     end subroutine json_update_string

json_module/json_update_string_name_ascii [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_update_string_name_ascii

SEE ALSO

json_update_string

SOURCE

3110     subroutine json_update_string_name_ascii(p,name,val,found)
3111 
3112     implicit none
3113 
3114     type(json_value),pointer             :: p
3115     character(kind=CDK,len=*),intent(in) :: name
3116     character(kind=CK, len=*),intent(in) :: val
3117     logical(LK),intent(out)              :: found
3118 
3119     call json_update_string(p,to_unicode(name),val,found)
3120 
3121     end subroutine json_update_string_name_ascii

json_module/json_update_string_val_ascii [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_update_string_val_ascii

SEE ALSO

json_update_string

SOURCE

3135     subroutine json_update_string_val_ascii(p,name,val,found)
3136 
3137     implicit none
3138 
3139     type(json_value),pointer             :: p
3140     character(kind=CK, len=*),intent(in) :: name
3141     character(kind=CDK,len=*),intent(in) :: val
3142     logical(LK),intent(out)              :: found
3143 
3144     call json_update_string(p,name,to_unicode(val),found)
3145 
3146     end subroutine json_update_string_val_ascii

json_module/json_value_add_double [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_add_double

DESCRIPTION

Add a real value to the structure.

NOTES

This routine is part of the public API that can be used to build a JSON structure using data.

AUTHOR

Jacob Williams : 1/19/2014

SOURCE

3211     subroutine json_value_add_double(me, name, val)
3212 
3213     implicit none
3214 
3215     type(json_value),pointer            :: me
3216     character(kind=CK,len=*),intent(in) :: name
3217     real(RK),intent(in)                 :: val
3218 
3219     type(json_value),pointer :: var
3220 
3221     !create the variable:
3222     call json_value_create(var)
3223     call to_double(var,val,name)
3224 
3225     !add it:
3226     call json_add(me, var)
3227 
3228     !cleanup:
3229     nullify(var)
3230 
3231     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.

NOTES

This routine is part of the public API that can be used to build a JSON structure using data.

AUTHOR

Jacob Williams : 1/20/2014

SOURCE

3276     subroutine json_value_add_double_vec(me, name, val)
3277 
3278     implicit none
3279 
3280     type(json_value),pointer            :: me
3281     character(kind=CK,len=*),intent(in) :: name
3282     real(RK),dimension(:),intent(in)    :: val
3283 
3284     type(json_value),pointer :: var
3285     integer(IK) :: i
3286 
3287     !create the variable as an array:
3288     call json_value_create(var)
3289     call to_array(var,name)
3290 
3291     !populate the array:
3292     do i=1,size(val)
3293         call json_add(var, '', val(i))
3294     end do
3295 
3296     !add it:
3297     call json_add(me, var)
3298 
3299     !cleanup:
3300     nullify(var)
3301 
3302     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.

NOTES

This routine is part of the public API that can be used to build a JSON structure using data.

AUTHOR

Jacob Williams : 1/20/2014

SOURCE

3347     subroutine json_value_add_integer(me, name, val)
3348 
3349     implicit none
3350 
3351     type(json_value),pointer            :: me
3352     character(kind=CK,len=*),intent(in) :: name
3353     integer(IK),intent(in)              :: val
3354 
3355     type(json_value),pointer :: var
3356 
3357     !create the variable:
3358     call json_value_create(var)
3359     call to_integer(var,val,name)
3360 
3361     !add it:
3362     call json_add(me, var)
3363 
3364     !cleanup:
3365     nullify(var)
3366 
3367     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.

NOTES

This routine is part of the public API that can be used to build a JSON structure using data.

AUTHOR

Jacob Williams : 1/20/2014

SOURCE

3412     subroutine json_value_add_integer_vec(me, name, val)
3413 
3414     implicit none
3415 
3416     type(json_value),pointer            :: me
3417     character(kind=CK,len=*),intent(in) :: name
3418     integer(IK),dimension(:),intent(in) :: val
3419 
3420     type(json_value),pointer :: var
3421     integer(IK) :: i    !counter
3422 
3423     !create the variable as an array:
3424     call json_value_create(var)
3425     call to_array(var,name)
3426 
3427     !populate the array:
3428     do i=1,size(val)
3429         call json_add(var, '', val(i))
3430     end do
3431 
3432     !add it:
3433     call json_add(me, var)
3434 
3435     !cleanup:
3436     nullify(var)
3437 
3438     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.

NOTES

This routine is part of the public API that can be used to build a JSON structure using data.

AUTHOR

Jacob Williams : 1/20/2014

SOURCE

3483     subroutine json_value_add_logical(me, name, val)
3484 
3485     implicit none
3486 
3487     type(json_value),pointer            :: me
3488     character(kind=CK,len=*),intent(in) :: name
3489     logical(LK),intent(in)              :: val
3490 
3491     type(json_value),pointer :: var
3492 
3493     !create the variable:
3494     call json_value_create(var)
3495     call to_logical(var,val,name)
3496 
3497     !add it:
3498     call json_add(me, var)
3499 
3500     !cleanup:
3501     nullify(var)
3502 
3503     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.

NOTES

This routine is part of the public API that can be used to build a JSON structure using data.

AUTHOR

Jacob Williams : 1/20/2014

SOURCE

3548     subroutine json_value_add_logical_vec(me, name, val)
3549 
3550     implicit none
3551 
3552     type(json_value),pointer            :: me
3553     character(kind=CK,len=*),intent(in) :: name
3554     logical(LK),dimension(:),intent(in) :: val
3555 
3556     type(json_value),pointer :: var
3557     integer(IK) :: i    !counter
3558 
3559     !create the variable as an array:
3560     call json_value_create(var)
3561     call to_array(var,name)
3562 
3563     !populate the array:
3564     do i=1,size(val)
3565         call json_add(var, '', val(i))
3566     end do
3567 
3568     !add it:
3569     call json_add(me, var)
3570 
3571     !cleanup:
3572     nullify(var)
3573 
3574     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 me.

SOURCE

3160     subroutine json_value_add_member(me, member)
3161 
3162     implicit none
3163 
3164     type(json_value),pointer :: me, member
3165 
3166     if (.not. exception_thrown) then
3167 
3168         ! associate the parent
3169         member%parent => me
3170 
3171         ! add to linked list
3172         if (associated(me%children)) then
3173 
3174             me%tail%next => member
3175             member%previous => me%tail
3176 
3177         else
3178 
3179             me%children => member
3180             member%previous => null()  !first in the list
3181 
3182         end if
3183 
3184         ! new member is now the last one in the list
3185         me%tail => member
3186         me%n_children = me%n_children + 1
3187 
3188     end if
3189 
3190     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.

NOTES

This routine is part of the public API that can be used to build a JSON structure using data.

AUTHOR

Jacob Williams : 1/19/2014

SOURCE

3619     subroutine json_value_add_string(me, name, val)
3620 
3621     implicit none
3622 
3623     type(json_value),pointer            :: me
3624     character(kind=CK,len=*),intent(in) :: name
3625     character(kind=CK,len=*),intent(in) :: val
3626 
3627     type(json_value),pointer :: var
3628     character(kind=CK,len=:),allocatable :: str
3629 
3630     !add escape characters if necessary:
3631     call escape_string(val, str)
3632 
3633     !create the variable:
3634     call json_value_create(var)
3635     call to_string(var,str,name)
3636 
3637     !add it:
3638     call json_add(me, var)
3639 
3640     !cleanup:
3641     nullify(var)
3642 
3643     end subroutine json_value_add_string

json_module/json_value_add_string_name_ascii [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_add_string_name_ascii

SEE ALSO

json_value_add_string

SOURCE

3681     subroutine json_value_add_string_name_ascii(me, name, val)
3682 
3683     implicit none
3684 
3685     type(json_value),pointer             :: me
3686     character(kind=CDK,len=*),intent(in) :: name
3687     character(kind=CK, len=*),intent(in) :: val
3688 
3689     call json_value_add_string(me, to_unicode(name), val)
3690 
3691     end subroutine json_value_add_string_name_ascii

json_module/json_value_add_string_val_ascii [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_add_string_val_ascii

SEE ALSO

json_value_add_string

SOURCE

3705     subroutine json_value_add_string_val_ascii(me, name, val)
3706 
3707     implicit none
3708 
3709     type(json_value),pointer             :: me
3710     character(kind=CK, len=*),intent(in) :: name
3711     character(kind=CDK,len=*),intent(in) :: val
3712 
3713     call json_value_add_string(me, name, to_unicode(val))
3714 
3715     end subroutine json_value_add_string_val_ascii

json_module/json_value_add_string_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_add_string_vec

DESCRIPTION

Add an array of character strings to the structure.

NOTES

This routine is part of the public API that can be used to build a JSON structure using data.

AUTHOR

Jacob Williams : 1/19/2014

SOURCE

3789     subroutine json_value_add_string_vec(me, name, val, trim_str, adjustl_str)
3790 
3791     implicit none
3792 
3793     type(json_value),pointer                         :: me
3794     character(kind=CK,len=*),intent(in)              :: name
3795     character(kind=CK,len=*),dimension(:),intent(in) :: val
3796     logical(LK),intent(in),optional                  :: trim_str
3797     logical(LK),intent(in),optional                  :: adjustl_str
3798 
3799     type(json_value),pointer :: var
3800     integer(IK) :: i
3801     logical(LK) :: trim_string, adjustl_string
3802     character(kind=CK,len=:),allocatable :: str
3803 
3804     !if the string is to be trimmed or not:
3805     if (present(trim_str)) then
3806         trim_string = trim_str
3807     else
3808         trim_string = .false.
3809     end if
3810     if (present(adjustl_str)) then
3811         adjustl_string = adjustl_str
3812     else
3813         adjustl_string = .false.
3814     end if
3815 
3816     !create the variable as an array:
3817     call json_value_create(var)
3818     call to_array(var,name)
3819 
3820     !populate the array:
3821     do i=1,size(val)
3822 
3823         !the string to write:
3824         str = val(i)
3825         if (adjustl_string) str = adjustl(str)
3826         if (trim_string)    str = trim(str)
3827 
3828         !write it:
3829         call json_add(var, '', str)
3830 
3831         !cleanup
3832         deallocate(str)
3833 
3834     end do
3835 
3836     !add it:
3837     call json_add(me, var)
3838 
3839     !cleanup:
3840     nullify(var)
3841 
3842     end subroutine json_value_add_string_vec

json_module/json_value_add_string_vec_name_ascii [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_add_string_vec_name_ascii

SEE ALSO

json_value_add_string_vec

SOURCE

3882     subroutine json_value_add_string_vec_name_ascii(me, name, val, trim_str, adjustl_str)
3883 
3884     implicit none
3885 
3886     type(json_value),pointer                          :: me
3887     character(kind=CDK,len=*),intent(in)              :: name
3888     character(kind=CK, len=*),dimension(:),intent(in) :: val
3889     logical(LK),intent(in),optional                   :: trim_str
3890     logical(LK),intent(in),optional                   :: adjustl_str
3891 
3892     call json_value_add_string_vec(me, to_unicode(name), val, trim_str, adjustl_str)
3893 
3894     end subroutine json_value_add_string_vec_name_ascii

json_module/json_value_add_string_vec_val_ascii [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_add_string_vec_val_ascii

SEE ALSO

json_value_add_string_vec

SOURCE

3908     subroutine json_value_add_string_vec_val_ascii(me, name, val, trim_str, adjustl_str)
3909 
3910     implicit none
3911 
3912     type(json_value),pointer                          :: me
3913     character(kind=CK, len=*),intent(in)              :: name
3914     character(kind=CDK,len=*),dimension(:),intent(in) :: val
3915     logical(LK),intent(in),optional                   :: trim_str
3916     logical(LK),intent(in),optional                   :: adjustl_str
3917 
3918     call json_value_add_string_vec(me, name, to_unicode(val), trim_str, adjustl_str)
3919 
3920     end subroutine json_value_add_string_vec_val_ascii

json_module/json_value_destroy [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_destroy

DESCRIPTION

Destroy a json_value linked-list structure.

AUTHOR

Jacob Williams : 1/22/2014 : The original version of this routine was not properly freeing the memory. It has been rewritten.

SOURCE

2332     recursive subroutine json_value_destroy(me,destroy_next)
2333 
2334     implicit none
2335 
2336     type(json_value),pointer :: me
2337     logical(LK),intent(in),optional :: destroy_next  !if true, then me%next is also destroyed (default is true)
2338 
2339     logical(LK) :: des_next
2340     type(json_value), pointer :: p
2341 
2342     if (associated(me)) then
2343 
2344         if (present(destroy_next)) then
2345             des_next = destroy_next
2346         else
2347             des_next = .true.
2348         end if
2349 
2350         if (allocated(me%name)) deallocate(me%name)
2351 
2352         call destroy_json_data(me)
2353 
2354         if (associated(me%children)) then
2355             do while (me%n_children > 0)
2356                 p => me%children
2357                 me%children => me%children%next
2358                 me%n_children = me%n_children - 1
2359                 call json_value_destroy(p,.false.)
2360             end do
2361             nullify(me%children)
2362             nullify(p)
2363         end if
2364 
2365         if (associated(me%next) .and. des_next) call json_value_destroy(me%next)
2366 
2367         if (associated(me%previous)) nullify(me%previous)
2368         if (associated(me%parent))   nullify(me%parent)
2369         if (associated(me%tail))     nullify(me%tail)
2370 
2371         deallocate(me)
2372 
2373         nullify(me)
2374 
2375     end if
2376 
2377     end subroutine json_value_destroy

json_module/json_value_get_by_index [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_get_by_index

DESCRIPTION

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

SOURCE

3962     subroutine json_value_get_by_index(me, idx, p)
3963 
3964     implicit none
3965 
3966     type(json_value),pointer,intent(in) :: me
3967     integer(IK),intent(in)              :: idx
3968     type(json_value),pointer            :: p
3969 
3970     integer(IK) :: i
3971 
3972     nullify(p)
3973 
3974     if (.not. exception_thrown) then
3975 
3976         if (associated(me%children)) then
3977 
3978             p => me%children
3979 
3980             do i = 1, idx - 1
3981 
3982                 if (associated(p%next)) then
3983                     p => p%next
3984                 else
3985                     call throw_exception('Error in json_value_get_by_index:'//&
3986                                          ' p%next is not associated.')
3987                     nullify(p)
3988                     return
3989                 end if
3990 
3991             end do
3992 
3993         else
3994 
3995             call throw_exception('Error in json_value_get_by_index:'//&
3996                                  ' me%children is not associated.')
3997 
3998         end if
3999 
4000     end if
4001 
4002     end subroutine json_value_get_by_index

json_module/json_value_get_by_name_chars [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_get_by_name_chars

DESCRIPTION

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

NOTES

It is a case-sensitive search, and the name string is not trimmed, So, for example,

     'a ' /= 'A ' /= 'a  '

Note that the name is not parsed like it is in json_get_by_path.

SOURCE

4022     subroutine json_value_get_by_name_chars(me, name, p)
4023 
4024     implicit none
4025 
4026     type(json_value),pointer,intent(in) :: me
4027     character(kind=CK,len=*),intent(in) :: name
4028     type(json_value),pointer            :: p
4029 
4030     integer(IK) :: i,n_children
4031 
4032     nullify(p)
4033 
4034     if (.not. exception_thrown) then
4035 
4036         if (associated(me)) then
4037 
4038             if (me%var_type==json_object) then
4039                 n_children = json_count(me)
4040                 p => me%children    !start with first one
4041                 do i=1, n_children
4042                     if (allocated(p%name)) then
4043                         if (p%name == name) return
4044                     end if
4045                     p => p%next
4046                 end do
4047             end if
4048 
4049             !did not find anything:
4050             call throw_exception('Error in json_value_get_by_name_chars: '//&
4051                                  'child variable '//trim(name)//' was not found.')
4052             nullify(p)
4053 
4054         else
4055             call throw_exception('Error in json_value_get_by_name_chars: '//&
4056                                  'pointer is not associated.')
4057         end if
4058 
4059     end if
4060 
4061     end subroutine json_value_get_by_name_chars

json_module/json_value_remove [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_remove

DESCRIPTION

Remove a json_value (and all its children) from a linked-list structure, preserving the rest of the structure.

INPUT

EXAMPLE

To extract an object from one json structure, and add it to another:

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

To remove an object from a json structure (and destroy it):

     type(json_value),pointer :: json1,p
     logical :: found
     !create and populate json1
     call json_get(json1,'name',p,found)  ! get pointer to name element of json1
     call json_remove(p)                  ! remove and destroy it

AUTHOR

Jacob Williams : 9/9/2014

HISTORY

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

SOURCE

2420     subroutine json_value_remove(me,destroy)
2421 
2422     implicit none
2423 
2424     type(json_value),pointer        :: me
2425     logical(LK),intent(in),optional :: destroy
2426 
2427     type(json_value),pointer :: parent,previous,next
2428     logical(LK) :: destroy_it
2429 
2430     if (associated(me)) then
2431 
2432         !optional input argument:
2433         if (present(destroy)) then
2434             destroy_it = destroy
2435         else
2436             destroy_it = .true.
2437         end if
2438 
2439         if (associated(me%parent)) then
2440 
2441             parent => me%parent
2442 
2443             if (associated(me%next)) then
2444 
2445                 !there are later items in the list:
2446 
2447                 next => me%next
2448                 nullify(me%next)
2449 
2450                 if (associated(me%previous)) then
2451                     !there are earlier items in the list
2452                     previous => me%previous
2453                     previous%next => next
2454                     next%previous => previous
2455                 else
2456                     !this is the first item in the list
2457                     parent%children => next
2458                     nullify(next%previous)
2459                 end if
2460 
2461             else
2462 
2463                 if (associated(me%previous)) then
2464                     !there are earlier items in the list:
2465                     previous => me%previous
2466                     nullify(previous%next)
2467                     parent%tail => previous
2468                 else
2469                     !this is the only item in the list:
2470                     nullify(parent%children)
2471                     nullify(parent%tail)
2472                 end if
2473 
2474             end if
2475 
2476             parent%n_children = parent%n_children - 1
2477 
2478         end if
2479 
2480         if (destroy_it) call json_value_destroy(me)
2481 
2482     end if
2483 
2484     end subroutine json_value_remove

json_module/json_value_remove_if_present [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

json_value_remove_if_present

DESCRIPTION

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

AUTHOR

Jacob Williams : 12/6/2014

SOURCE

2502     subroutine json_value_remove_if_present(p,name)
2503 
2504     implicit none
2505 
2506     type(json_value),pointer            :: p
2507     character(kind=CK,len=*),intent(in) :: name
2508 
2509     type(json_value),pointer :: p_var
2510     logical(LK) :: found
2511 
2512     call json_get(p,name,p_var,found)
2513     if (found) call json_remove(p_var)
2514 
2515     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

4102     subroutine json_value_to_string(me,str)
4103 
4104     implicit none
4105 
4106     type(json_value),pointer,intent(in)              :: me
4107     character(kind=CK,len=:),intent(out),allocatable :: str
4108 
4109     str = ''
4110     call json_value_print(me, iunit=0, str=str, indent=1, colon=.true.)
4111 
4112     end subroutine json_value_to_string

json_module/to_uni [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

to_uni

DESCRIPTION

Convert string to unicode (CDK to CK).

AUTHOR

Izaak Beekman

SOURCE

7938     pure function to_uni(str)
7939 
7940     implicit none
7941 
7942     character(kind=CDK,len=*), intent(in) :: str
7943     character(kind=CK,len=len(str))       :: to_uni
7944 
7945     to_uni = str
7946 
7947     end function to_uni

json_module/to_uni_vec [ Functions ]

[ Top ] [ json_module ] [ Functions ]

NAME

to_uni_vec

DESCRIPTION

Convert array of strings to unicode (CDK to CK).

NOTES

JW: may be able to remove this by making to_uni PURE ELEMENTAL ?

AUTHOR

Izaak Beekman

SOURCE

7967     pure function to_uni_vec(str)
7968 
7969     implicit none
7970 
7971     character(kind=CDK,len=*), dimension(:), intent(in)   :: str
7972     character(kind=CK,len=len(str)), dimension(size(str)) :: to_uni_vec
7973 
7974     to_uni_vec = str
7975 
7976     end function to_uni_vec