JSON/json_module [ Modules ]
NAME
json_module
DESCRIPTION
JSON-FORTRAN: A Fortran 2008 JSON (JavaScript Object Notation) API.
SEE ALSO
- http://github.com/jacobwilliams/json-fortran [json-fortran development site]
- http://jacobwilliams.github.io/json-fortran [json-fortran online documentation]
- http://www.json.org/ [JSON website]
- http://jsonlint.com/ [JSON validator]
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:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original FSON License:
http://github.com/josephalevin/fson
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
- Joseph A. Levin : March 2012 : Original FSON code [retrieved on 12/2/2013].
- Jacob Williams : 2/8/2014 : Extensive modifications to the original FSON code. The original F95 code was split into four files: fson_path_m.f95, fson_string_m.f95, fson_value_m.f95, and fson.f95. The new code has been extensively updated, refactored and combined into this one module (json_module.F90). Various Fortran 2003/2008 features are now used (e.g., allocatable strings, newunit, generic, class, and abstract interface).
- Development continues at: http://github.com/jacobwilliams/json-fortran
json_module/access_spec [ Parameters ]
[ Top ] [ json_module ] [ Parameters ]
NAME
access_spec
DESCRIPTION
If Unicode is not enabled, then JSON files are opened using access='STREAM' and form='UNFORMATTED'. This allows the file to be read faster.
SEE ALSO
SOURCE
336 #ifdef USE_UCS4 337 character(kind=CDK,len=*),parameter :: access_spec = 'SEQUENTIAL' 338 #else 339 character(kind=CDK,len=*),parameter :: access_spec = 'STREAM' 340 #endif
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:
- file name
- format statement
- file path
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
157 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:
- json path
- character or string
- object name
passed to the json-fortran library *MUST* be of type CK.
SEE ALSO
STRING_KIND
SOURCE
226 integer,parameter,public :: CK = selected_char_kind(json_fortran_string_kind)
json_module/form_spec [ Parameters ]
[ Top ] [ json_module ] [ Parameters ]
NAME
form_spec
DESCRIPTION
If Unicode is not enabled, then JSON files are opened using access='STREAM' and form='UNFORMATTED'. This allows the file to be read faster.
SEE ALSO
SOURCE
360 #ifdef USE_UCS4 361 character(kind=CDK,len=*),parameter :: form_spec = 'FORMATTED' 362 #else 363 character(kind=CDK,len=*),parameter :: form_spec = 'UNFORMATTED' 364 #endif
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
174 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/use_unformatted_stream [ Parameters ]
[ Top ] [ json_module ] [ Parameters ]
NAME
use_unformatted_stream
DESCRIPTION
If Unicode is not enabled, then JSON files are opened using access='STREAM' and form='UNFORMATTED'. This allows the file to be read faster.
SEE ALSO
SOURCE
312 #ifdef USE_UCS4 313 logical,parameter :: use_unformatted_stream = .false. 314 #else 315 logical,parameter :: use_unformatted_stream = .true. 316 #endif
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
381 integer(IK),parameter,public :: json_unknown = 0 382 integer(IK),parameter,public :: json_null = 1 383 integer(IK),parameter,public :: json_object = 2 384 integer(IK),parameter,public :: json_array = 3 385 integer(IK),parameter,public :: json_logical = 4 386 integer(IK),parameter,public :: json_integer = 5 387 integer(IK),parameter,public :: json_double = 6 388 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
239 #if defined __GFORTRAN__ && defined USE_UCS4 240 ! gfortran compiler AND UCS4 support requested, & silence redefine warning: 241 ! Make sure we output files with utf-8 encoding too 242 #define FILE_ENCODING ,encoding='UTF-8' 243 #else 244 ! don't ask for utf-8 file encoding unless using UCS4 245 ! this may let us use unformatted stream io to read in files more quickly 246 ! even with unicode support turned on `inquire( ... encoding=FL_ENCODING)` 247 ! may be able to detect json files in which each character is exactly one 248 ! byte 249 #define FILE_ENCODING 250 #endif
json_module/json_fortran_string_kind [ Macros ]
[ Top ] [ json_module ] [ Macros ]
NAME
json_fortran_string_kind
DESCRIPTION
String kind preprocessor macro.
SOURCE
187 #if defined __GFORTRAN__ && defined USE_UCS4 188 ! gfortran compiler AND UCS4 support requested: 189 character(kind=CDK,len=*),parameter :: json_fortran_string_kind = 'ISO_10646' 190 #else 191 ! this is the string kind to use unless compiling with GFortran AND 192 ! UCS4/ISO 10646 support is requested 193 character(kind=CDK,len=*),parameter :: json_fortran_string_kind = 'DEFAULT' 194 #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
271 # ifdef USE_UCS4 272 # ifdef __GFORTRAN__ 273 ! gfortran uses cpp in old-school compatibility mode so 274 ! the # stringify and ## concatenate operators don't work 275 ! but we can use C/C++ style comment to ensure PROCEDURE is 276 ! correctly tokenized and prepended with 'wrap_' when the 277 ! macro is expanded 278 # define MAYBEWRAP(PROCEDURE) PROCEDURE , wrap_/**/PROCEDURE 279 # endif 280 ! ifdef __INTEL_COMPILER 281 ! Intel's fpp does support the more contemporary ## concatenation 282 ! operator, but doesn't treat the C/C++ comments the same way. 283 ! If you use the gfortran approach and pass the -noB switch to 284 ! fpp, the macro will expand, but with a space between wrap_ and 285 ! whatever PROCEDURE expands to 286 ! Intel doesn't support ISO 10646 yet, but this is here to 287 ! ease the transition once they do. 288 ! define MAYBEWRAP(PROCEDURE) PROCEDURE , wrap_##PROCEDURE 289 ! endif 290 # else 291 # define MAYBEWRAP(PROCEDURE) PROCEDURE 292 # 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:
- gfortran -DUSE_UCS4 ...
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
470 type,public :: json_file 471 472 private 473 474 !the JSON structure read from the file: 475 type(json_value),pointer :: p => null() 476 477 contains 478 479 procedure,public :: load_file => json_file_load 480 481 generic, public :: load_from_string => MAYBEWRAP(json_file_load_from_string) 482 483 procedure,public :: destroy => json_file_destroy 484 procedure,public :: move => json_file_move_pointer 485 generic ,public :: info => MAYBEWRAP(json_file_variable_info) 486 487 procedure,public :: print_to_string => json_file_print_to_string 488 489 generic,public :: print_file => json_file_print_to_console, & 490 json_file_print_1, & 491 json_file_print_2 492 493 generic,public :: get => MAYBEWRAP(json_file_get_object), & 494 MAYBEWRAP(json_file_get_integer), & 495 MAYBEWRAP(json_file_get_double), & 496 MAYBEWRAP(json_file_get_logical), & 497 MAYBEWRAP(json_file_get_string), & 498 MAYBEWRAP(json_file_get_integer_vec), & 499 MAYBEWRAP(json_file_get_double_vec), & 500 MAYBEWRAP(json_file_get_logical_vec), & 501 MAYBEWRAP(json_file_get_string_vec) 502 503 504 505 506 generic,public :: update => MAYBEWRAP(json_file_update_integer), & 507 MAYBEWRAP(json_file_update_logical), & 508 MAYBEWRAP(json_file_update_real), & 509 MAYBEWRAP(json_file_update_string) 510 # ifdef USE_UCS4 511 generic,public :: update => json_file_update_string_name_ascii, & 512 json_file_update_string_val_ascii 513 # endif 514 515 !load from string: 516 procedure :: MAYBEWRAP(json_file_load_from_string) 517 518 !git info: 519 procedure :: MAYBEWRAP(json_file_variable_info) 520 521 !get: 522 procedure :: MAYBEWRAP(json_file_get_object) 523 procedure :: MAYBEWRAP(json_file_get_integer) 524 procedure :: MAYBEWRAP(json_file_get_double) 525 procedure :: MAYBEWRAP(json_file_get_logical) 526 procedure :: MAYBEWRAP(json_file_get_string) 527 procedure :: MAYBEWRAP(json_file_get_integer_vec) 528 procedure :: MAYBEWRAP(json_file_get_double_vec) 529 procedure :: MAYBEWRAP(json_file_get_logical_vec) 530 procedure :: MAYBEWRAP(json_file_get_string_vec) 531 532 !update: 533 procedure :: MAYBEWRAP(json_file_update_integer) 534 procedure :: MAYBEWRAP(json_file_update_logical) 535 procedure :: MAYBEWRAP(json_file_update_real) 536 procedure :: MAYBEWRAP(json_file_update_string) 537 # ifdef USE_UCS4 538 procedure :: json_file_update_string_name_ascii 539 procedure :: json_file_update_string_val_ascii 540 # endif 541 542 !print_file: 543 procedure :: json_file_print_to_console 544 procedure :: json_file_print_1 545 procedure :: json_file_print_2 546 547 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,'') !root call json_add(p,'year',1805) call json_add(p,'value',1.0d0) call json_print(p,'test.json') call json_destroy(p)
SOURCE
411 type,public :: json_value 412 413 !force the constituents to be stored contiguously 414 ![note: on Intel, the order of the variables below 415 ! is significant to avoid the misaligned field warnings] 416 sequence 417 418 !for the linked list: 419 type(json_value),pointer :: previous => null() 420 type(json_value),pointer :: next => null() 421 type(json_value),pointer :: parent => null() 422 type(json_value),pointer :: children => null() 423 type(json_value),pointer :: tail => null() 424 425 !variable name: 426 character(kind=CK,len=:),allocatable :: name 427 428 !the data for this variable: 429 real(RK),allocatable :: dbl_value 430 logical(LK),allocatable :: log_value 431 character(kind=CK,len=:),allocatable :: str_value 432 integer(IK),allocatable :: int_value 433 434 integer(IK) :: var_type = json_unknown !variable type 435 436 integer(IK),private :: n_children = 0 !number of children 437 438 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
SOURCE
563 abstract interface 564 subroutine array_callback_func(element, i, count) 565 import :: json_value,IK 566 implicit none 567 type(json_value), pointer,intent(in) :: element 568 integer(IK),intent(in) :: i !index 569 integer(IK),intent(in) :: count !size of array 570 end subroutine array_callback_func 571 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
619 interface json_add 620 module procedure json_value_add_member 621 module procedure MAYBEWRAP(json_value_add_integer) 622 module procedure MAYBEWRAP(json_value_add_integer_vec) 623 module procedure MAYBEWRAP(json_value_add_double) 624 module procedure MAYBEWRAP(json_value_add_double_vec) 625 module procedure MAYBEWRAP(json_value_add_logical) 626 module procedure MAYBEWRAP(json_value_add_logical_vec) 627 module procedure MAYBEWRAP(json_value_add_string) 628 module procedure MAYBEWRAP(json_value_add_string_vec) 629 # ifdef USE_UCS4 630 module procedure json_value_add_string_name_ascii 631 module procedure json_value_add_string_val_ascii 632 module procedure json_value_add_string_vec_name_ascii 633 module procedure json_value_add_string_vec_val_ascii 634 # endif 635 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
852 interface json_create_array 853 module procedure MAYBEWRAP(json_value_create_array) 854 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
828 interface json_create_double 829 module procedure MAYBEWRAP(json_value_create_double) 830 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
952 interface json_create_integer 953 module procedure MAYBEWRAP(json_value_create_integer) 954 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
976 interface json_create_logical 977 module procedure MAYBEWRAP(json_value_create_logical) 978 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
904 interface json_create_null 905 module procedure MAYBEWRAP(json_value_create_null) 906 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
880 interface json_create_object 881 module procedure MAYBEWRAP(json_value_create_object) 882 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
928 interface json_create_string 929 module procedure MAYBEWRAP(json_value_create_string) 930 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
774 interface json_destroy 775 module procedure json_value_destroy 776 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
684 interface json_get 685 module procedure MAYBEWRAP(json_get_by_path) 686 module procedure json_get_integer, MAYBEWRAP(json_get_integer_with_path) 687 module procedure json_get_integer_vec, MAYBEWRAP(json_get_integer_vec_with_path) 688 module procedure json_get_double, MAYBEWRAP(json_get_double_with_path) 689 module procedure json_get_double_vec, MAYBEWRAP(json_get_double_vec_with_path) 690 module procedure json_get_logical, MAYBEWRAP(json_get_logical_with_path) 691 module procedure json_get_logical_vec, MAYBEWRAP(json_get_logical_vec_with_path) 692 module procedure json_get_string, MAYBEWRAP(json_get_string_with_path) 693 module procedure json_get_string_vec, MAYBEWRAP(json_get_string_vec_with_path) 694 module procedure json_get_array, MAYBEWRAP(json_get_array_with_path) 695 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
600 interface json_get_child 601 module procedure json_value_get_by_index 602 module procedure MAYBEWRAP(json_value_get_by_name_chars) 603 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:
- file and unit : the specified unit is used to read JSON from file. [note if unit is already open, then the filename is ignored]
- file : JSON is read from file using internal unit number
- str : JSON data is read from the string instead
EXAMPLE
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
1010 interface json_parse 1011 module procedure json_parse_file, MAYBEWRAP(json_parse_string) 1012 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
729 interface json_print 730 module procedure json_print_1 !input is unit number 731 module procedure json_print_2 !input is file name 732 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
708 interface json_print_to_string 709 module procedure json_value_to_string 710 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
789 interface json_remove 790 module procedure json_value_remove 791 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
804 interface json_remove_if_present 805 module procedure MAYBEWRAP(json_value_remove_if_present) 806 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
656 interface json_update 657 module procedure MAYBEWRAP(json_update_logical),& 658 MAYBEWRAP(json_update_double),& 659 MAYBEWRAP(json_update_integer),& 660 MAYBEWRAP(json_update_string) 661 # ifdef USE_UCS4 662 module procedure json_update_string_name_ascii 663 module procedure json_update_string_val_ascii 664 # endif 665 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
1025 interface to_unicode 1026 module procedure to_uni, to_uni_vec 1027 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
AUTHOR
Jacob Williams : 12/4/2013
SOURCE
2292 subroutine json_check_for_errors(status_ok, error_msg) 2293 2294 implicit none 2295 2296 logical(LK),intent(out) :: status_ok 2297 character(kind=CK,len=:),allocatable,intent(out) :: error_msg 2298 2299 status_ok = .not. exception_thrown 2300 2301 if (.not. status_ok) then 2302 if (allocated(err_message)) then 2303 error_msg = err_message 2304 else 2305 error_msg = 'Unknown Error' 2306 end if 2307 else 2308 error_msg = '' 2309 end if 2310 2311 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
2190 subroutine json_clear_exceptions() 2191 2192 implicit none 2193 2194 !clear the flag and message: 2195 exception_thrown = .false. 2196 err_message = '' 2197 2198 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
4052 pure function json_count(me) result(count) 4053 4054 implicit none 4055 4056 integer(IK) :: count 4057 type(json_value),pointer,intent(in) :: me 4058 4059 count = me%n_children 4060 4061 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
AUTHOR
Jacob Williams : 12/5/2013
SOURCE
2344 function json_failed() result(failed) 2345 2346 implicit none 2347 2348 logical(LK) :: failed 2349 2350 failed = exception_thrown 2351 2352 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
1205 subroutine json_file_destroy(me) 1206 1207 implicit none 1208 1209 class(json_file),intent(inout) :: me 1210 1211 if (associated(me%p)) call json_value_destroy(me%p) 1212 1213 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
1786 subroutine json_file_get_double (me, path, val, found) 1787 1788 implicit none 1789 1790 class(json_file),intent(inout) :: me 1791 character(kind=CK,len=*),intent(in) :: path 1792 real(RK),intent(out) :: val 1793 logical(LK),intent(out),optional :: found 1794 1795 call json_get(me%p, path=path, value=val, found=found) 1796 1797 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
1842 subroutine json_file_get_double_vec(me, path, vec, found) 1843 1844 implicit none 1845 1846 class(json_file),intent(inout) :: me 1847 character(kind=CK,len=*),intent(in) :: path 1848 real(RK),dimension(:),allocatable,intent(out) :: vec 1849 logical(LK),intent(out),optional :: found 1850 1851 call json_get(me%p, path, vec, found) 1852 1853 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
1674 subroutine json_file_get_integer(me, path, val, found) 1675 1676 implicit none 1677 1678 class(json_file),intent(inout) :: me 1679 character(kind=CK,len=*),intent(in) :: path 1680 integer(IK),intent(out) :: val 1681 logical(LK),intent(out),optional :: found 1682 1683 call json_get(me%p, path=path, value=val, found=found) 1684 1685 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
1730 subroutine json_file_get_integer_vec(me, path, vec, found) 1731 1732 implicit none 1733 1734 class(json_file),intent(inout) :: me 1735 character(kind=CK,len=*),intent(in) :: path 1736 integer(IK),dimension(:),allocatable,intent(out) :: vec 1737 logical(LK),intent(out),optional :: found 1738 1739 call json_get(me%p, path, vec, found) 1740 1741 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
1898 subroutine json_file_get_logical(me,path,val,found) 1899 1900 implicit none 1901 1902 class(json_file),intent(inout) :: me 1903 character(kind=CK,len=*),intent(in) :: path 1904 logical(LK),intent(out) :: val 1905 logical(LK),intent(out),optional :: found 1906 1907 call json_get(me%p, path=path, value=val, found=found) 1908 1909 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
1954 subroutine json_file_get_logical_vec(me, path, vec, found) 1955 1956 implicit none 1957 1958 class(json_file),intent(inout) :: me 1959 character(kind=CK,len=*),intent(in) :: path 1960 logical(LK),dimension(:),allocatable,intent(out) :: vec 1961 logical(LK),intent(out),optional :: found 1962 1963 call json_get(me%p, path, vec, found) 1964 1965 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
1618 subroutine json_file_get_object(me, path, p, found) 1619 1620 implicit none 1621 1622 class(json_file),intent(inout) :: me 1623 character(kind=CK,len=*),intent(in) :: path 1624 type(json_value),pointer,intent(out) :: p 1625 logical(LK),intent(out),optional :: found 1626 1627 call json_get_by_path(me%p, path=path, p=p, found=found) 1628 1629 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
2011 subroutine json_file_get_string(me, path, val, found) 2012 2013 implicit none 2014 2015 class(json_file),intent(inout) :: me 2016 character(kind=CK,len=*),intent(in) :: path 2017 character(kind=CK,len=:),allocatable,intent(out) :: val 2018 logical(LK),intent(out),optional :: found 2019 2020 call json_get(me%p, path=path, value=val, found=found) 2021 2022 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
2067 subroutine json_file_get_string_vec(me, path, vec, found) 2068 2069 implicit none 2070 2071 class(json_file),intent(inout) :: me 2072 character(kind=CK,len=*),intent(in) :: path 2073 character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec 2074 logical(LK),intent(out),optional :: found 2075 2076 call json_get(me%p, path, vec, found) 2077 2078 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
1273 subroutine json_file_load(me, filename, unit) 1274 1275 implicit none 1276 1277 class(json_file),intent(inout) :: me 1278 character(kind=CDK,len=*),intent(in) :: filename 1279 integer(IK),intent(in),optional :: unit 1280 1281 call json_parse(file=filename, p=me%p, unit=unit) 1282 1283 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
1305 subroutine json_file_load_from_string(me, str) 1306 1307 implicit none 1308 1309 class(json_file),intent(inout) :: me 1310 character(kind=CK,len=*),intent(in) :: str 1311 1312 call json_parse(str=str, p=me%p) 1313 1314 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
1237 subroutine json_file_move_pointer(to,from) 1238 1239 implicit none 1240 1241 class(json_file),intent(inout) :: to 1242 class(json_file),intent(inout) :: from 1243 1244 if (associated(from%p)) then 1245 to%p => from%p 1246 nullify(from%p) 1247 else 1248 call throw_exception('Error in json_file_move_pointer: '//& 1249 'pointer is not associated.') 1250 end if 1251 1252 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
1387 subroutine json_file_print_1(me, iunit) 1388 1389 implicit none 1390 1391 class(json_file),intent(inout) :: me 1392 integer(IK),intent(in) :: iunit !must be non-zero 1393 1394 integer(IK) :: i 1395 character(kind=CK,len=:),allocatable :: dummy 1396 1397 if (iunit/=0) then 1398 i = iunit 1399 else 1400 call throw_exception('Error in json_file_print_1: iunit must be nonzero.') 1401 return 1402 end if 1403 1404 call json_value_print(me%p,iunit=i,str=dummy,indent=1,colon=.true.) 1405 1406 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
1436 subroutine json_file_print_2(me,filename) 1437 1438 implicit none 1439 1440 class(json_file),intent(inout) :: me 1441 character(kind=CDK,len=*),intent(in) :: filename 1442 1443 integer(IK) :: iunit,istat 1444 1445 open(newunit=iunit,file=filename,status='REPLACE',iostat=istat FILE_ENCODING ) 1446 if (istat==0) then 1447 call me%print_file(iunit) !call the other routine 1448 close(iunit,iostat=istat) 1449 else 1450 call throw_exception('Error in json_file_print_2: could not open file: '//& 1451 trim(filename)) 1452 end if 1453 1454 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
1357 subroutine json_file_print_to_console(me) 1358 1359 implicit none 1360 1361 class(json_file),intent(inout) :: me 1362 1363 character(kind=CK,len=:),allocatable :: dummy 1364 1365 call json_value_print(me%p,iunit=output_unit,str=dummy,indent=1,colon=.true.) 1366 1367 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
1481 subroutine json_file_print_to_string(me,str) 1482 1483 implicit none 1484 1485 class(json_file),intent(inout) :: me 1486 character(kind=CK,len=:),allocatable,intent(out) :: str 1487 1488 call json_value_to_string(me%p,str) 1489 1490 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
AUTHOR
Jacob Williams : 1/10/2015
SOURCE
2633 subroutine json_file_update_integer(me,name,val,found) 2634 implicit none 2635 2636 class(json_file),intent(inout) :: me 2637 character(kind=CK,len=*),intent(in) :: name 2638 integer(IK),intent(in) :: val 2639 logical(LK),intent(out) :: found 2640 2641 if (.not. exception_thrown) call json_update(me%p,name,val,found) 2642 2643 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
AUTHOR
Jacob Williams : 1/10/2015
SOURCE
2689 subroutine json_file_update_logical(me,name,val,found) 2690 implicit none 2691 2692 class(json_file),intent(inout) :: me 2693 character(kind=CK,len=*),intent(in) :: name 2694 logical(LK),intent(in) :: val 2695 logical(LK),intent(out) :: found 2696 2697 if (.not. exception_thrown) call json_update(me%p,name,val,found) 2698 2699 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
2745 subroutine json_file_update_real(me,name,val,found) 2746 implicit none 2747 2748 class(json_file),intent(inout) :: me 2749 character(kind=CK,len=*),intent(in) :: name 2750 real(RK),intent(in) :: val 2751 logical(LK),intent(out) :: found 2752 2753 if (.not. exception_thrown) call json_update(me%p,name,val,found) 2754 2755 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
AUTHOR
Jacob Williams : 1/10/2015
SOURCE
2801 subroutine json_file_update_string(me,name,val,found) 2802 implicit none 2803 2804 class(json_file),intent(inout) :: me 2805 character(kind=CK,len=*),intent(in) :: name 2806 character(kind=CK,len=*),intent(in) :: val 2807 logical(LK),intent(out) :: found 2808 2809 if (.not. exception_thrown) call json_update(me%p,name,val,found) 2810 2811 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
2846 subroutine json_file_update_string_name_ascii(me,name,val,found) 2847 implicit none 2848 2849 class(json_file),intent(inout) :: me 2850 character(kind=CDK,len=*),intent(in) :: name 2851 character(kind=CK, len=*),intent(in) :: val 2852 logical(LK),intent(out) :: found 2853 2854 call json_file_update_string(me,to_unicode(name),val,found) 2855 2856 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
2867 subroutine json_file_update_string_val_ascii(me,name,val,found) 2868 implicit none 2869 2870 class(json_file),intent(inout) :: me 2871 character(kind=CK, len=*),intent(in) :: name 2872 character(kind=CDK,len=*),intent(in) :: val 2873 logical(LK),intent(out) :: found 2874 2875 call json_file_update_string(me,name,to_unicode(val),found) 2876 2877 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
1510 subroutine json_file_variable_info(me,path,found,var_type,n_children) 1511 1512 implicit none 1513 1514 class(json_file),intent(inout) :: me 1515 character(kind=CK,len=*),intent(in) :: path 1516 logical(LK),intent(out) :: found 1517 integer(IK),intent(out) :: var_type 1518 integer(IK),intent(out) :: n_children 1519 1520 type(json_value),pointer :: p 1521 1522 !initialize: 1523 nullify(p) 1524 1525 !get a pointer to the variable (if it is there): 1526 call me%get(path,p,found) 1527 1528 if (found) then 1529 1530 !get info: 1531 call json_info(p,var_type,n_children) 1532 1533 else 1534 1535 !set to dummy values: 1536 var_type = json_unknown 1537 n_children = 0 1538 1539 end if 1540 1541 !cleanup: 1542 nullify(p) 1543 1544 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
6024 subroutine json_get_array(me, array_callback) 6025 6026 implicit none 6027 6028 type(json_value),pointer,intent(in) :: me 6029 procedure(array_callback_func) :: array_callback 6030 6031 type(json_value),pointer :: element 6032 integer(IK) :: i, count 6033 6034 if ( exception_thrown ) return 6035 6036 nullify(element) 6037 6038 select case (me%var_type) 6039 case (json_array) 6040 count = json_count(me) 6041 element => me%children 6042 do i = 1, count ! callback for each child 6043 call array_callback(element, i, count) 6044 element => element%next 6045 end do 6046 case default 6047 6048 call throw_exception('Error in json_get_array:'//& 6049 ' Resolved value is not an array ') 6050 6051 end select 6052 6053 !cleanup: 6054 if (associated(element)) nullify(element) 6055 6056 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
6071 subroutine json_get_array_with_path(me, path, array_callback, found) 6072 6073 implicit none 6074 6075 type(json_value),pointer,intent(in) :: me 6076 character(kind=CK,len=*),intent(in) :: path 6077 procedure(array_callback_func) :: array_callback 6078 logical(LK),intent(out),optional :: found 6079 6080 type(json_value),pointer :: p 6081 6082 if ( exception_thrown ) then 6083 if ( present(found) ) found = .false. 6084 return 6085 end if 6086 6087 nullify(p) 6088 6089 ! resolve the path to the value 6090 call json_get_by_path(me=me, path=path, p=p) 6091 6092 if (.not. associated(p)) then 6093 call throw_exception('Error in json_get_array:'//& 6094 ' Unable to resolve path: '//trim(path)) 6095 else 6096 call json_get_array(me=p,array_callback=array_callback) 6097 nullify(p) 6098 end if 6099 if ( exception_thrown ) then 6100 if ( present(found) ) then 6101 found = .false. 6102 call json_clear_exceptions() 6103 end if 6104 else 6105 if ( present(found) ) found = .true. 6106 end if 6107 6108 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
4594 subroutine json_get_by_path(me, path, p, found) 4595 4596 implicit none 4597 4598 type(json_value),pointer,intent(in) :: me 4599 character(kind=CK,len=*),intent(in) :: path 4600 type(json_value),pointer,intent(out) :: p 4601 logical(LK),intent(out),optional :: found 4602 4603 character(kind=CK,len=1),parameter :: start_array_alt = '(' 4604 character(kind=CK,len=1),parameter :: end_array_alt = ')' 4605 4606 integer(IK) :: i,length,child_i 4607 character(kind=CK,len=1) :: c 4608 logical(LK) :: array 4609 type(json_value),pointer :: tmp 4610 4611 if (.not. exception_thrown) then 4612 4613 nullify(p) 4614 4615 ! default to assuming relative to this 4616 p => me 4617 4618 child_i = 1 4619 4620 array = .false. 4621 4622 length = len_trim(path) 4623 4624 do i=1, length 4625 4626 c = path(i:i) 4627 4628 select case (c) 4629 case (CK_'$') 4630 4631 ! root 4632 do while (associated (p%parent)) 4633 p => p%parent 4634 end do 4635 child_i = i + 1 4636 4637 case (CK_'@') 4638 4639 ! this 4640 p => me 4641 child_i = i + 1 4642 4643 case (CK_'.') 4644 4645 ! get child member from p 4646 if (child_i < i) then 4647 nullify(tmp) 4648 call json_get_child(p, path(child_i:i-1), tmp) 4649 p => tmp 4650 nullify(tmp) 4651 else 4652 child_i = i + 1 4653 cycle 4654 end if 4655 4656 if (.not. associated(p)) then 4657 call throw_exception('Error in json_get_by_path:'//& 4658 ' Error getting child member.') 4659 exit 4660 end if 4661 4662 child_i = i+1 4663 4664 case (start_array,start_array_alt) 4665 4666 !....Modified to allow for 'var[3]' style syntax 4667 !Note: jmozmoz/fson has a slightly different version of this... 4668 4669 ! start looking for the array element index 4670 array = .true. 4671 4672 ! get child member from p 4673 if (child_i < i) then 4674 nullify(tmp) 4675 call json_get_child(p, path(child_i:i-1), tmp) 4676 p => tmp 4677 nullify(tmp) 4678 else 4679 child_i = i + 1 4680 cycle 4681 end if 4682 if (.not. associated(p)) then 4683 call throw_exception('Error in json_get_by_path:'//& 4684 ' Error getting array element') 4685 exit 4686 end if 4687 child_i = i + 1 4688 4689 case (end_array,end_array_alt) 4690 4691 if (.not.array) then 4692 call throw_exception('Error in json_get_by_path: Unexpected ]') 4693 exit 4694 end if 4695 array = .false. 4696 child_i = string_to_integer(path(child_i:i-1)) 4697 4698 nullify(tmp) 4699 call json_get_child(p, child_i, tmp) 4700 p => tmp 4701 nullify(tmp) 4702 4703 child_i= i + 1 4704 4705 end select 4706 4707 end do 4708 4709 if (exception_thrown) then 4710 4711 if (present(found)) then 4712 found = .false. 4713 call json_clear_exceptions() 4714 end if 4715 4716 else 4717 4718 ! grab the last child if present in the path 4719 if (child_i <= length) then 4720 nullify(tmp) 4721 call json_get_child(p, path(child_i:i-1), tmp) 4722 p => tmp 4723 nullify(tmp) 4724 end if 4725 if (associated(p)) then 4726 if (present(found)) found = .true. !everything seems to be ok 4727 else 4728 call throw_exception('Error in json_get_by_path:'//& 4729 ' variable not found: '//trim(path)) 4730 if (present(found)) then 4731 found = .false. 4732 call json_clear_exceptions() 4733 end if 4734 end if 4735 4736 end if 4737 4738 else 4739 if (present(found)) found = .false. 4740 end if 4741 4742 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
5113 subroutine json_get_double(me, value) 5114 5115 implicit none 5116 5117 type(json_value),pointer :: me 5118 real(RK),intent(out) :: value 5119 5120 value = 0.0_RK 5121 if ( exception_thrown ) return 5122 5123 select case (me%var_type) 5124 case (json_integer) 5125 value = me%int_value 5126 case (json_double) 5127 value = me%dbl_value 5128 case (json_logical) 5129 if (me%log_value) then 5130 value = 1.0_RK 5131 else 5132 value = 0.0_RK 5133 end if 5134 case default 5135 5136 call throw_exception('Error in json_get_double:'//& 5137 ' Unable to resolve value to double: '//me%name) 5138 5139 end select 5140 5141 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
5239 subroutine json_get_double_vec(me, vec) 5240 5241 implicit none 5242 5243 type(json_value),pointer :: me 5244 real(RK),dimension(:),allocatable,intent(out) :: vec 5245 5246 logical(LK) :: initialized 5247 5248 initialized = .false. 5249 5250 if (allocated(vec)) deallocate(vec) 5251 5252 !the callback function is called for each element of the array: 5253 call json_get(me, array_callback=get_double_from_array) 5254 5255 contains 5256 5257 ! callback function for double 5258 subroutine get_double_from_array(element, i, count) 5259 implicit none 5260 5261 type(json_value),pointer,intent(in) :: element 5262 integer(IK),intent(in) :: i !index 5263 integer(IK),intent(in) :: count !size of array 5264 5265 !size the output array: 5266 if (.not. initialized) then 5267 allocate(vec(count)) 5268 initialized = .true. 5269 end if 5270 5271 !populate the elements: 5272 call json_get(element, value=vec(i)) 5273 5274 end subroutine get_double_from_array 5275 5276 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
5290 subroutine json_get_double_vec_with_path(me, path, vec, found) 5291 5292 implicit none 5293 5294 type(json_value),pointer :: me 5295 character(kind=CK,len=*),intent(in) :: path 5296 real(RK),dimension(:),allocatable,intent(out) :: vec 5297 logical(LK),intent(out),optional :: found 5298 5299 logical(LK) :: initialized 5300 5301 initialized = .false. 5302 5303 if (allocated(vec)) deallocate(vec) 5304 5305 !the callback function is called for each element of the array: 5306 call json_get(me, path=path, array_callback=get_double_from_array, found=found) 5307 5308 contains 5309 5310 ! callback function for double 5311 subroutine get_double_from_array(element, i, count) 5312 implicit none 5313 5314 type(json_value),pointer,intent(in) :: element 5315 integer(IK),intent(in) :: i !index 5316 integer(IK),intent(in) :: count !size of array 5317 5318 !size the output array: 5319 if (.not. initialized) then 5320 allocate(vec(count)) 5321 initialized = .true. 5322 end if 5323 5324 !populate the elements: 5325 call json_get(element, value=vec(i)) 5326 5327 end subroutine get_double_from_array 5328 5329 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
5155 subroutine json_get_double_with_path(me, path, value, found) 5156 5157 implicit none 5158 5159 type(json_value),pointer :: me 5160 character(kind=CK,len=*),intent(in) :: path 5161 real(RK),intent(out) :: value 5162 logical(LK),intent(out),optional :: found 5163 5164 type(json_value),pointer :: p 5165 5166 value = 0.0_RK 5167 if ( exception_thrown ) then 5168 if ( present(found) ) found = .false. 5169 return 5170 end if 5171 5172 nullify(p) 5173 5174 call json_get_by_path(me=me, path=path, p=p) 5175 5176 if (.not. associated(p)) then 5177 5178 call throw_exception('Error in json_get_double:'//& 5179 ' Unable to resolve path: '//trim(path)) 5180 5181 else 5182 5183 call json_get_double(p,value) 5184 nullify(p) 5185 5186 end if 5187 5188 if (exception_thrown) then 5189 if (present(found)) then 5190 found = .false. 5191 call json_clear_exceptions() 5192 end if 5193 else 5194 if (present(found)) found = .true. 5195 end if 5196 5197 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
4867 subroutine json_get_integer(me, value) 4868 4869 implicit none 4870 4871 type(json_value),pointer,intent(in) :: me 4872 integer(IK),intent(out) :: value 4873 4874 value = 0 4875 if ( exception_thrown ) return 4876 4877 select case(me%var_type) 4878 case (json_integer) 4879 value = me%int_value 4880 case (json_double) 4881 value = int(me%dbl_value) 4882 case (json_logical) 4883 if (me%log_value) then 4884 value = 1 4885 else 4886 value = 0 4887 end if 4888 case default 4889 call throw_exception('Error in get_integer:'//& 4890 ' Unable to resolve value to integer: '//me%name) 4891 end select 4892 4893 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
4986 subroutine json_get_integer_vec(me, vec) 4987 4988 implicit none 4989 4990 type(json_value),pointer :: me 4991 integer(IK),dimension(:),allocatable,intent(out) :: vec 4992 4993 logical(LK) :: initialized 4994 4995 initialized = .false. 4996 4997 if (allocated(vec)) deallocate(vec) 4998 4999 !the callback function is called for each element of the array: 5000 call json_get(me, array_callback=get_int_from_array) 5001 5002 contains 5003 5004 ! callback function for integer 5005 subroutine get_int_from_array(element, i, count) 5006 implicit none 5007 5008 type(json_value),pointer,intent(in) :: element 5009 integer(IK),intent(in) :: i !index 5010 integer(IK),intent(in) :: count !size of array 5011 5012 !size the output array: 5013 if (.not. initialized) then 5014 allocate(vec(count)) 5015 initialized = .true. 5016 end if 5017 5018 !populate the elements: 5019 call json_get(element, value=vec(i)) 5020 5021 end subroutine get_int_from_array 5022 5023 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
5037 subroutine json_get_integer_vec_with_path(me, path, vec, found) 5038 5039 implicit none 5040 5041 type(json_value),pointer :: me 5042 character(kind=CK,len=*),intent(in) :: path 5043 integer(IK),dimension(:),allocatable,intent(out) :: vec 5044 logical(LK),intent(out),optional :: found 5045 5046 logical(LK) :: initialized 5047 5048 initialized = .false. 5049 5050 call json_get(me, path=path, array_callback=get_int_from_array, found=found) 5051 5052 ! need to duplicate callback function, no other way 5053 contains 5054 5055 ! callback function for integer 5056 subroutine get_int_from_array(element, i, count) 5057 implicit none 5058 5059 type(json_value),pointer,intent(in) :: element 5060 integer(IK),intent(in) :: i !index 5061 integer(IK),intent(in) :: count !size of array 5062 5063 !size the output array: 5064 if (.not. initialized) then 5065 allocate(vec(count)) 5066 initialized = .true. 5067 end if 5068 5069 !populate the elements: 5070 call json_get(element, value=vec(i)) 5071 5072 end subroutine get_int_from_array 5073 5074 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
4907 subroutine json_get_integer_with_path(me, path, value, found) 4908 4909 implicit none 4910 4911 type(json_value),pointer,intent(in) :: me 4912 character(kind=CK,len=*),intent(in) :: path 4913 integer(IK),intent(out) :: value 4914 logical(LK),intent(out),optional :: found 4915 4916 type(json_value),pointer :: p 4917 4918 value = 0 4919 if ( exception_thrown ) then 4920 if ( present(found) ) found = .false. 4921 return 4922 end if 4923 4924 nullify(p) 4925 4926 call json_get_by_path(me=me, path=path, p=p) 4927 4928 if (.not. associated(p)) then 4929 call throw_exception('Error in json_get_integer:'//& 4930 ' Unable to resolve path: '// trim(path)) 4931 else 4932 call json_get_integer(p,value) 4933 nullify(p) 4934 end if 4935 if ( exception_thrown ) then 4936 if ( present(found) ) then 4937 found = .false. 4938 call json_clear_exceptions() 4939 end if 4940 else 4941 if ( present(found) ) found = .true. 4942 end if 4943 4944 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
5368 subroutine json_get_logical(me, value) 5369 5370 implicit none 5371 5372 type(json_value),pointer,intent(in) :: me 5373 logical(LK) :: value 5374 5375 value = .false. 5376 if ( exception_thrown ) return 5377 5378 select case (me%var_type) 5379 case (json_integer) 5380 value = (me%int_value > 0) 5381 case (json_logical) 5382 value = me % log_value 5383 case default 5384 call throw_exception('Error in json_get_logical:'//& 5385 ' Unable to resolve value to logical: '//me%name) 5386 end select 5387 5388 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
5486 subroutine json_get_logical_vec(me, vec) 5487 5488 implicit none 5489 5490 type(json_value),pointer,intent(in) :: me 5491 logical(LK),dimension(:),allocatable,intent(out) :: vec 5492 5493 logical(LK) :: initialized 5494 5495 initialized = .false. 5496 5497 if (allocated(vec)) deallocate(vec) 5498 5499 !the callback function is called for each element of the array: 5500 call json_get(me, array_callback=get_logical_from_array) 5501 5502 contains 5503 5504 ! callback function for logical 5505 subroutine get_logical_from_array(element, i, count) 5506 implicit none 5507 5508 type(json_value),pointer,intent(in) :: element 5509 integer(IK),intent(in) :: i !index 5510 integer(IK),intent(in) :: count !size of array 5511 5512 !size the output array: 5513 if (.not. initialized) then 5514 allocate(vec(count)) 5515 initialized = .true. 5516 end if 5517 5518 !populate the elements: 5519 call json_get(element, value=vec(i)) 5520 5521 end subroutine get_logical_from_array 5522 5523 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
5537 subroutine json_get_logical_vec_with_path(me, path, vec, found) 5538 5539 implicit none 5540 5541 type(json_value),pointer,intent(in) :: me 5542 character(kind=CK,len=*),intent(in) :: path 5543 logical(LK),dimension(:),allocatable,intent(out) :: vec 5544 logical(LK),intent(out),optional :: found 5545 5546 logical(LK) :: initialized 5547 5548 initialized = .false. 5549 5550 if (allocated(vec)) deallocate(vec) 5551 5552 !the callback function is called for each element of the array: 5553 call json_get(me, path=path, array_callback=get_logical_from_array, found=found) 5554 5555 contains 5556 5557 ! callback function for logical 5558 subroutine get_logical_from_array(element, i, count) 5559 implicit none 5560 5561 type(json_value),pointer,intent(in) :: element 5562 integer(IK),intent(in) :: i !index 5563 integer(IK),intent(in) :: count !size of array 5564 5565 !size the output array: 5566 if (.not. initialized) then 5567 allocate(vec(count)) 5568 initialized = .true. 5569 end if 5570 5571 !populate the elements: 5572 call json_get(element, value=vec(i)) 5573 5574 end subroutine get_logical_from_array 5575 5576 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
5402 subroutine json_get_logical_with_path(me, path, value, found) 5403 5404 implicit none 5405 5406 type(json_value),pointer,intent(in) :: me 5407 character(kind=CK,len=*),intent(in) :: path 5408 logical(LK) :: value 5409 logical(LK),intent(out),optional :: found 5410 5411 type(json_value),pointer :: p 5412 5413 value = .false. 5414 if ( exception_thrown) then 5415 if ( present(found) ) found = .false. 5416 return 5417 end if 5418 5419 nullify(p) 5420 5421 call json_get_by_path(me=me, path=path, p=p) 5422 5423 if (.not. associated(p)) then 5424 5425 call throw_exception('Error in json_get_logical:'//& 5426 ' Unable to resolve path: '//trim(path)) 5427 5428 else 5429 5430 call json_get_logical(p,value) 5431 nullify(p) 5432 5433 end if 5434 5435 if (exception_thrown) then 5436 if (present(found)) then 5437 found = .false. 5438 call json_clear_exceptions() 5439 end if 5440 else 5441 if (present(found)) found = .true. 5442 end if 5443 5444 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
5615 subroutine json_get_string(me, value) 5616 5617 implicit none 5618 5619 type(json_value),pointer,intent(in) :: me 5620 character(kind=CK,len=:),allocatable,intent(out) :: value 5621 5622 character(kind=CK ,len=:),allocatable :: s,pre,post 5623 integer(IK) :: j,jprev,n 5624 character(kind=CK,len=1) :: c 5625 5626 value = '' 5627 if ( exception_thrown) return 5628 5629 select case (me%var_type) 5630 5631 case (json_string) 5632 5633 if (allocated(me%str_value)) then 5634 5635 !get the value as is: 5636 s = me%str_value 5637 5638 ! Now, have to remove the escape characters: 5639 ! 5640 ! '\"' quotation mark 5641 ! '\\' reverse solidus 5642 ! '\/' solidus 5643 ! '\b' backspace 5644 ! '\f' formfeed 5645 ! '\n' newline (LF) 5646 ! '\r' carriage return (CR) 5647 ! '\t' horizontal tab 5648 ! '\uXXXX' 4 hexadecimal digits 5649 ! 5650 5651 !initialize: 5652 n = len(s) 5653 j = 1 5654 5655 do 5656 5657 jprev = j !initialize 5658 j = index(s(j:n),backslash) !look for an escape character 5659 5660 if (j>0) then !an escape character was found 5661 5662 !index in full string of the escape character: 5663 j = j + (jprev-1) 5664 5665 if (j<n) then 5666 5667 !save the bit before the escape character: 5668 if (j>1) then 5669 pre = s( 1 : j-1 ) 5670 else 5671 pre = '' 5672 end if 5673 5674 !character after the escape character: 5675 c = s( j+1 : j+1 ) 5676 5677 if (any(c == [quotation_mark,backslash,slash, & 5678 to_unicode(['b','f','n','r','t'])])) then 5679 5680 !save the bit after the escape characters: 5681 if (j+2<n) then 5682 post = s(j+2:n) 5683 else 5684 post = '' 5685 end if 5686 5687 select case(c) 5688 case (quotation_mark,backslash,slash) 5689 !use c as is 5690 case (CK_'b') 5691 c = bspace 5692 case (CK_'f') 5693 c = formfeed 5694 case (CK_'n') 5695 c = newline 5696 case (CK_'r') 5697 c = carriage_return 5698 case (CK_'t') 5699 c = horizontal_tab 5700 end select 5701 5702 s = pre//c//post 5703 5704 n = n-1 !backslash character has been 5705 ! removed from the string 5706 5707 else if (c == 'u') then !expecting 4 hexadecimal digits after 5708 !the escape character [\uXXXX] 5709 5710 !for now, we are just printing them as is 5711 ![not checking to see if it is a valid hex value] 5712 5713 if (j+5<=n) then 5714 j=j+4 5715 else 5716 call throw_exception('Error in json_get_string:'//& 5717 ' Invalid hexadecimal sequence'//& 5718 ' in string: '//trim(c)) 5719 exit 5720 end if 5721 5722 else 5723 !unknown escape character 5724 call throw_exception('Error in json_get_string:'//& 5725 ' unknown escape sequence in string "'//& 5726 trim(s)//'" ['//backslash//c//']') 5727 exit 5728 end if 5729 5730 j=j+1 !go to the next character 5731 5732 if (j>=n) exit !finished 5733 5734 else 5735 !an escape character is the last character in 5736 ! the string [this may not be valid syntax, 5737 ! but just keep it] 5738 exit 5739 end if 5740 5741 else 5742 exit !no more escape characters in the string 5743 end if 5744 5745 end do 5746 5747 if (exception_thrown) then 5748 if (allocated(value)) deallocate(value) 5749 else 5750 value = s 5751 end if 5752 5753 else 5754 call throw_exception('Error in json_get_string:'//& 5755 ' me%value not allocated') 5756 end if 5757 5758 case default 5759 call throw_exception('Error in json_get_string:'//& 5760 ' Unable to resolve value to characters: '//me%name) 5761 5762 ! Note: for the other cases, we could do val to string conversions. 5763 5764 end select 5765 5766 !cleanup: 5767 if (allocated(s)) deallocate(s) 5768 if (allocated(pre)) deallocate(pre) 5769 if (allocated(post)) deallocate(post) 5770 5771 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
5871 subroutine json_get_string_vec(me, vec) 5872 5873 implicit none 5874 5875 type(json_value),pointer,intent(in) :: me 5876 character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec 5877 5878 logical(LK) :: initialized 5879 5880 initialized = .false. 5881 5882 if (allocated(vec)) deallocate(vec) 5883 5884 !the callback function is called for each element of the array: 5885 call json_get(me, array_callback=get_chars_from_array) 5886 5887 contains 5888 5889 ! callback function for chars 5890 subroutine get_chars_from_array(element, i, count) 5891 5892 implicit none 5893 5894 type(json_value),pointer,intent(in) :: element 5895 integer(IK),intent(in) :: i !index 5896 integer(IK),intent(in) :: count !size of array 5897 5898 character(kind=CK,len=:),allocatable :: cval 5899 5900 !size the output array: 5901 if (.not. initialized) then 5902 allocate(vec(count)) 5903 initialized = .true. 5904 end if 5905 5906 !populate the elements: 5907 call json_get(element, value=cval) 5908 if (allocated(cval)) then 5909 vec(i) = cval 5910 deallocate(cval) 5911 else 5912 vec(i) = '' 5913 end if 5914 5915 end subroutine get_chars_from_array 5916 5917 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
5931 subroutine json_get_string_vec_with_path(me, path, vec, found) 5932 5933 implicit none 5934 5935 type(json_value),pointer,intent(in) :: me 5936 character(kind=CK,len=*),intent(in) :: path 5937 character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec 5938 logical(LK),intent(out),optional :: found 5939 5940 logical(LK) :: initialized 5941 5942 initialized = .false. 5943 5944 if (allocated(vec)) deallocate(vec) 5945 5946 !the callback function is called for each element of the array: 5947 call json_get(me, path=path, array_callback=get_chars_from_array, found=found) 5948 5949 contains 5950 5951 ! callback function for chars 5952 subroutine get_chars_from_array(element, i, count) 5953 5954 implicit none 5955 5956 type(json_value),pointer,intent(in) :: element 5957 integer(IK),intent(in) :: i !index 5958 integer(IK),intent(in) :: count !size of array 5959 5960 character(kind=CK,len=:),allocatable :: cval 5961 5962 !size the output array: 5963 if (.not. initialized) then 5964 allocate(vec(count)) 5965 initialized = .true. 5966 end if 5967 5968 !populate the elements: 5969 call json_get(element, value=cval) 5970 if (allocated(cval)) then 5971 vec(i) = cval 5972 deallocate(cval) 5973 else 5974 vec(i) = '' 5975 end if 5976 5977 end subroutine get_chars_from_array 5978 5979 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
5785 subroutine json_get_string_with_path(me, path, value, found) 5786 5787 implicit none 5788 5789 type(json_value),pointer,intent(in) :: me 5790 character(kind=CK,len=*),intent(in) :: path 5791 character(kind=CK,len=:),allocatable,intent(out) :: value 5792 logical(LK),intent(out),optional :: found 5793 5794 type(json_value),pointer :: p 5795 5796 value = '' 5797 if ( exception_thrown ) then 5798 if ( present(found) ) found = .false. 5799 return 5800 end if 5801 5802 nullify(p) 5803 5804 call json_get_by_path(me=me, path=path, p=p) 5805 5806 if (.not. associated(p)) then 5807 call throw_exception('Error in json_get_string:'//& 5808 ' Unable to resolve path: '//trim(path)) 5809 5810 else 5811 5812 call json_get_string(p,value) 5813 nullify(p) 5814 5815 end if 5816 5817 if (allocated(value) .and. .not. exception_thrown) then 5818 if (present(found)) found = .true. 5819 else 5820 if (present(found)) then 5821 found = .false. 5822 call json_clear_exceptions() 5823 end if 5824 end if 5825 5826 !cleanup: 5827 if (associated(p)) nullify(p) 5828 5829 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
1587 subroutine json_info(p,var_type,n_children) 1588 1589 implicit none 1590 1591 type(json_value),pointer :: p 1592 integer(IK),intent(out),optional :: var_type 1593 integer(IK),intent(out),optional :: n_children 1594 1595 if (present(var_type)) var_type = p%var_type !variable type 1596 if (present(n_children)) n_children = json_count(p) !number of children 1597 1598 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
2125 subroutine json_initialize(verbose,compact_reals) 2126 2127 implicit none 2128 2129 logical(LK),intent(in),optional :: verbose !mainly useful for debugging (default is false) 2130 logical(LK),intent(in),optional :: compact_reals !to compact the real number strings for output 2131 2132 character(kind=CDK,len=10) :: w,do,e 2133 integer(IK) :: istat 2134 2135 !clear any errors from previous runs: 2136 call json_clear_exceptions() 2137 2138 # ifdef USE_UCS4 2139 ! reopen stdout and stderr with utf-8 encoding 2140 open(output_unit,encoding='utf-8') 2141 open(error_unit, encoding='utf-8') 2142 # endif 2143 2144 !Ensure gfortran bug work around "parameters" are set properly 2145 null_str = 'null' 2146 true_str = 'true' 2147 false_str = 'false' 2148 2149 !optional inputs (if not present, values remains unchanged): 2150 if (present(verbose)) is_verbose = verbose 2151 if (present(compact_reals)) compact_real = compact_reals !may be a bug here in Gfortran 5.0.0... check this... 2152 2153 ! set the default output/input format for reals: 2154 ! [this only needs to be done once, since it can't change] 2155 if (.not. allocated(real_fmt)) then 2156 write(w,'(I0)',iostat=istat) max_numeric_str_len 2157 if (istat==0) write(do,'(I0)',iostat=istat) real_precision 2158 if (istat==0) write(e,'(I0)',iostat=istat) real_exponent_digits 2159 if (istat==0) then 2160 real_fmt = '(E' // trim(w) // '.' // trim(do) // 'E' // trim(e) // ')' 2161 else 2162 real_fmt = '(E30.16E3)' !just use this one (should never happen) 2163 end if 2164 end if 2165 2166 !Just in case, clear these global variables also: 2167 pushed_index = 0 2168 pushed_char = '' 2169 char_count = 0 2170 line_count = 1 2171 ipos = 1 2172 2173 end subroutine json_initialize
json_module/json_parse_file [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
DESCRIPTION
Parse the JSON file and populate the json_value tree.
INPUTS
The inputs can be:
- file and unit : the specified unit is used to read JSON from file. [note if unit is already open, then the filename is ignored]
- file : JSON is read from file using internal unit number
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
6168 subroutine json_parse_file(file, p, unit) 6169 6170 implicit none 6171 6172 character(kind=CDK,len=*),intent(in) :: file !JSON file name 6173 type(json_value),pointer :: p !output structure 6174 integer(IK),intent(in),optional :: unit !file unit number (/= 0) 6175 6176 integer(IK) :: iunit, istat 6177 logical(LK) :: is_open 6178 6179 !clear any exceptions and initialize: 6180 call json_initialize() 6181 6182 if ( present(unit) ) then 6183 6184 if (unit==0) then 6185 call throw_exception('Error in json_parse: unit number must not be 0.') 6186 return 6187 end if 6188 6189 iunit = unit 6190 6191 !check to see if the file is already open 6192 ! if it is, then use it, otherwise open the file with the name given. 6193 inquire(unit=iunit, opened=is_open, iostat=istat) 6194 if (istat==0 .and. .not. is_open) then 6195 ! open the file 6196 open ( unit = iunit, & 6197 file = file, & 6198 status = 'OLD', & 6199 action = 'READ', & 6200 form = form_spec, & 6201 access = access_spec, & 6202 iostat = istat & 6203 FILE_ENCODING ) 6204 else 6205 !if the file is already open, then we need to make sure 6206 ! that it is open with the correct form/access/etc... 6207 end if 6208 6209 else 6210 6211 ! open the file with a new unit number: 6212 open ( newunit = iunit, & 6213 file = file, & 6214 status = 'OLD', & 6215 action = 'READ', & 6216 form = form_spec, & 6217 access = access_spec, & 6218 iostat = istat & 6219 FILE_ENCODING ) 6220 6221 end if 6222 6223 if (istat==0) then 6224 6225 ! create the value and associate the pointer 6226 call json_value_create(p) 6227 6228 ! Note: the name of the root json_value doesn't really matter, 6229 ! but we'll allocate something here just in case. 6230 p%name = trim(file) !use the file name 6231 6232 ! parse as a value 6233 call parse_value(unit=iunit, str=CK_'', value=p) 6234 if (exception_thrown) call annotate_invalid_json(iunit,CK_'') 6235 6236 ! close the file if necessary 6237 close(unit=iunit, iostat=istat) 6238 6239 else 6240 6241 call throw_exception('Error in json_parse: Error opening file: '//trim(file)) 6242 nullify(p) 6243 6244 end if 6245 6246 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
SOURCE
6263 subroutine json_parse_string(p, str) 6264 6265 implicit none 6266 6267 type(json_value),pointer :: p !output structure 6268 character(kind=CK,len=*),intent(in) :: str !string with JSON data 6269 6270 integer(IK),parameter :: iunit = 0 !indicates that json data will be read from buffer 6271 6272 if ( .not. exception_thrown ) then 6273 6274 !clear any exceptions and initialize: 6275 call json_initialize() 6276 6277 ! create the value and associate the pointer 6278 call json_value_create(p) 6279 6280 ! Note: the name of the root json_value doesn't really matter, 6281 ! but we'll allocate something here just in case. 6282 p%name = '' 6283 6284 ! parse as a value 6285 call parse_value(unit=iunit, str=str, value=p) 6286 6287 if (exception_thrown) call annotate_invalid_json(iunit,str) 6288 6289 end if 6290 6291 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
- iunit is the nonzero file unit (the file must already have been opened).
AUTHOR
Jacob Williams, 6/20/2014
SOURCE
4245 subroutine json_print_1(me,iunit) 4246 4247 implicit none 4248 4249 type(json_value),pointer,intent(in) :: me 4250 integer(IK),intent(in) :: iunit !must be non-zero 4251 character(kind=CK,len=:),allocatable :: dummy 4252 4253 if (iunit/=0) then 4254 call json_value_print(me,iunit,str=dummy, indent=1, colon=.true.) 4255 else 4256 call throw_exception('Error in json_print: iunit must be nonzero.') 4257 end if 4258 4259 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
4279 subroutine json_print_2(me,filename) 4280 4281 implicit none 4282 4283 type(json_value),pointer,intent(in) :: me 4284 character(kind=CDK,len=*),intent(in) :: filename 4285 integer(IK) :: iunit,istat 4286 4287 open(newunit=iunit,file=filename,status='REPLACE',iostat=istat FILE_ENCODING ) 4288 if (istat==0) then 4289 call json_print(me,iunit) 4290 close(iunit,iostat=istat) 4291 else 4292 call throw_exception('Error in json_print: could not open file: '//& 4293 trim(filename)) 4294 end if 4295 4296 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
2969 subroutine json_update_double(p,name,val,found) 2970 2971 implicit none 2972 2973 type(json_value),pointer :: p 2974 character(kind=CK,len=*),intent(in) :: name 2975 real(RK),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_double(p_var,val) !update the value 2988 case default 2989 found = .false. 2990 call throw_exception('Error in json_update_double: '//& 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_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
3042 subroutine json_update_integer(p,name,val,found) 3043 3044 implicit none 3045 3046 type(json_value),pointer :: p 3047 character(kind=CK,len=*),intent(in) :: name 3048 integer(IK),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_integer(p_var,val) !update the value 3061 case default 3062 found = .false. 3063 call throw_exception('Error in json_update_integer: '//& 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_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
2896 subroutine json_update_logical(p,name,val,found) 2897 2898 implicit none 2899 2900 type(json_value),pointer :: p 2901 character(kind=CK,len=*),intent(in) :: name 2902 logical(LK),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_logical(p_var,val) !update the value 2915 case default 2916 found = .false. 2917 call throw_exception('Error in json_update_logical: '//& 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_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
3115 subroutine json_update_string(p,name,val,found) 3116 3117 implicit none 3118 3119 type(json_value),pointer :: p 3120 character(kind=CK,len=*),intent(in) :: name 3121 character(kind=CK,len=*),intent(in) :: val 3122 logical(LK),intent(out) :: found 3123 3124 type(json_value),pointer :: p_var 3125 integer(IK) :: var_type 3126 3127 call json_get(p,name,p_var,found) 3128 if (found) then 3129 3130 call json_info(p_var,var_type) 3131 select case (var_type) 3132 case (json_null,json_logical,json_integer,json_double,json_string) 3133 call to_string(p_var,val) !update the value 3134 case default 3135 found = .false. 3136 call throw_exception('Error in json_update_string: '//& 3137 'the variable is not a scalar value') 3138 end select 3139 3140 else 3141 call json_add(p,name,val) !add the new element 3142 end if 3143 3144 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
SOURCE
3183 subroutine json_update_string_name_ascii(p,name,val,found) 3184 3185 implicit none 3186 3187 type(json_value),pointer :: p 3188 character(kind=CDK,len=*),intent(in) :: name 3189 character(kind=CK, len=*),intent(in) :: val 3190 logical(LK),intent(out) :: found 3191 3192 call json_update_string(p,to_unicode(name),val,found) 3193 3194 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
SOURCE
3208 subroutine json_update_string_val_ascii(p,name,val,found) 3209 3210 implicit none 3211 3212 type(json_value),pointer :: p 3213 character(kind=CK, len=*),intent(in) :: name 3214 character(kind=CDK,len=*),intent(in) :: val 3215 logical(LK),intent(out) :: found 3216 3217 call json_update_string(p,name,to_unicode(val),found) 3218 3219 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
3284 subroutine json_value_add_double(me, name, val) 3285 3286 implicit none 3287 3288 type(json_value),pointer :: me 3289 character(kind=CK,len=*),intent(in) :: name 3290 real(RK),intent(in) :: val 3291 3292 type(json_value),pointer :: var 3293 3294 !create the variable: 3295 call json_value_create(var) 3296 call to_double(var,val,name) 3297 3298 !add it: 3299 call json_add(me, var) 3300 3301 !cleanup: 3302 nullify(var) 3303 3304 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
3349 subroutine json_value_add_double_vec(me, name, val) 3350 3351 implicit none 3352 3353 type(json_value),pointer :: me 3354 character(kind=CK,len=*),intent(in) :: name 3355 real(RK),dimension(:),intent(in) :: val 3356 3357 type(json_value),pointer :: var 3358 integer(IK) :: i 3359 3360 !create the variable as an array: 3361 call json_value_create(var) 3362 call to_array(var,name) 3363 3364 !populate the array: 3365 do i=1,size(val) 3366 call json_add(var, '', val(i)) 3367 end do 3368 3369 !add it: 3370 call json_add(me, var) 3371 3372 !cleanup: 3373 nullify(var) 3374 3375 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
3420 subroutine json_value_add_integer(me, name, val) 3421 3422 implicit none 3423 3424 type(json_value),pointer :: me 3425 character(kind=CK,len=*),intent(in) :: name 3426 integer(IK),intent(in) :: val 3427 3428 type(json_value),pointer :: var 3429 3430 !create the variable: 3431 call json_value_create(var) 3432 call to_integer(var,val,name) 3433 3434 !add it: 3435 call json_add(me, var) 3436 3437 !cleanup: 3438 nullify(var) 3439 3440 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
3485 subroutine json_value_add_integer_vec(me, name, val) 3486 3487 implicit none 3488 3489 type(json_value),pointer :: me 3490 character(kind=CK,len=*),intent(in) :: name 3491 integer(IK),dimension(:),intent(in) :: val 3492 3493 type(json_value),pointer :: var 3494 integer(IK) :: i !counter 3495 3496 !create the variable as an array: 3497 call json_value_create(var) 3498 call to_array(var,name) 3499 3500 !populate the array: 3501 do i=1,size(val) 3502 call json_add(var, '', val(i)) 3503 end do 3504 3505 !add it: 3506 call json_add(me, var) 3507 3508 !cleanup: 3509 nullify(var) 3510 3511 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
3556 subroutine json_value_add_logical(me, name, val) 3557 3558 implicit none 3559 3560 type(json_value),pointer :: me 3561 character(kind=CK,len=*),intent(in) :: name 3562 logical(LK),intent(in) :: val 3563 3564 type(json_value),pointer :: var 3565 3566 !create the variable: 3567 call json_value_create(var) 3568 call to_logical(var,val,name) 3569 3570 !add it: 3571 call json_add(me, var) 3572 3573 !cleanup: 3574 nullify(var) 3575 3576 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
3621 subroutine json_value_add_logical_vec(me, name, val) 3622 3623 implicit none 3624 3625 type(json_value),pointer :: me 3626 character(kind=CK,len=*),intent(in) :: name 3627 logical(LK),dimension(:),intent(in) :: val 3628 3629 type(json_value),pointer :: var 3630 integer(IK) :: i !counter 3631 3632 !create the variable as an array: 3633 call json_value_create(var) 3634 call to_array(var,name) 3635 3636 !populate the array: 3637 do i=1,size(val) 3638 call json_add(var, '', val(i)) 3639 end do 3640 3641 !add it: 3642 call json_add(me, var) 3643 3644 !cleanup: 3645 nullify(var) 3646 3647 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
3233 subroutine json_value_add_member(me, member) 3234 3235 implicit none 3236 3237 type(json_value),pointer :: me, member 3238 3239 if (.not. exception_thrown) then 3240 3241 ! associate the parent 3242 member%parent => me 3243 3244 ! add to linked list 3245 if (associated(me%children)) then 3246 3247 me%tail%next => member 3248 member%previous => me%tail 3249 3250 else 3251 3252 me%children => member 3253 member%previous => null() !first in the list 3254 3255 end if 3256 3257 ! new member is now the last one in the list 3258 me%tail => member 3259 me%n_children = me%n_children + 1 3260 3261 end if 3262 3263 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
3692 subroutine json_value_add_string(me, name, val) 3693 3694 implicit none 3695 3696 type(json_value),pointer :: me 3697 character(kind=CK,len=*),intent(in) :: name 3698 character(kind=CK,len=*),intent(in) :: val 3699 3700 type(json_value),pointer :: var 3701 character(kind=CK,len=:),allocatable :: str 3702 3703 !add escape characters if necessary: 3704 call escape_string(val, str) 3705 3706 !create the variable: 3707 call json_value_create(var) 3708 call to_string(var,str,name) 3709 3710 !add it: 3711 call json_add(me, var) 3712 3713 !cleanup: 3714 nullify(var) 3715 3716 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
SOURCE
3754 subroutine json_value_add_string_name_ascii(me, name, val) 3755 3756 implicit none 3757 3758 type(json_value),pointer :: me 3759 character(kind=CDK,len=*),intent(in) :: name 3760 character(kind=CK, len=*),intent(in) :: val 3761 3762 call json_value_add_string(me, to_unicode(name), val) 3763 3764 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
SOURCE
3778 subroutine json_value_add_string_val_ascii(me, name, val) 3779 3780 implicit none 3781 3782 type(json_value),pointer :: me 3783 character(kind=CK, len=*),intent(in) :: name 3784 character(kind=CDK,len=*),intent(in) :: val 3785 3786 call json_value_add_string(me, name, to_unicode(val)) 3787 3788 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
3902 subroutine json_value_add_string_vec(me, name, val, trim_str, adjustl_str) 3903 3904 implicit none 3905 3906 type(json_value),pointer :: me 3907 character(kind=CK,len=*),intent(in) :: name 3908 character(kind=CK,len=*),dimension(:),intent(in) :: val 3909 logical(LK),intent(in),optional :: trim_str 3910 logical(LK),intent(in),optional :: adjustl_str 3911 3912 type(json_value),pointer :: var 3913 integer(IK) :: i 3914 logical(LK) :: trim_string, adjustl_string 3915 character(kind=CK,len=:),allocatable :: str 3916 3917 !if the string is to be trimmed or not: 3918 if (present(trim_str)) then 3919 trim_string = trim_str 3920 else 3921 trim_string = .false. 3922 end if 3923 if (present(adjustl_str)) then 3924 adjustl_string = adjustl_str 3925 else 3926 adjustl_string = .false. 3927 end if 3928 3929 !create the variable as an array: 3930 call json_value_create(var) 3931 call to_array(var,name) 3932 3933 !populate the array: 3934 do i=1,size(val) 3935 3936 !the string to write: 3937 str = val(i) 3938 if (adjustl_string) str = adjustl(str) 3939 if (trim_string) str = trim(str) 3940 3941 !write it: 3942 call json_add(var, '', str) 3943 3944 !cleanup 3945 deallocate(str) 3946 3947 end do 3948 3949 !add it: 3950 call json_add(me, var) 3951 3952 !cleanup: 3953 nullify(var) 3954 3955 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
SOURCE
3995 subroutine json_value_add_string_vec_name_ascii(me, name, val, trim_str, adjustl_str) 3996 3997 implicit none 3998 3999 type(json_value),pointer :: me 4000 character(kind=CDK,len=*),intent(in) :: name 4001 character(kind=CK, len=*),dimension(:),intent(in) :: val 4002 logical(LK),intent(in),optional :: trim_str 4003 logical(LK),intent(in),optional :: adjustl_str 4004 4005 call json_value_add_string_vec(me, to_unicode(name), val, trim_str, adjustl_str) 4006 4007 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
SOURCE
4021 subroutine json_value_add_string_vec_val_ascii(me, name, val, trim_str, adjustl_str) 4022 4023 implicit none 4024 4025 type(json_value),pointer :: me 4026 character(kind=CK, len=*),intent(in) :: name 4027 character(kind=CDK,len=*),dimension(:),intent(in) :: val 4028 logical(LK),intent(in),optional :: trim_str 4029 logical(LK),intent(in),optional :: adjustl_str 4030 4031 call json_value_add_string_vec(me, name, to_unicode(val), trim_str, adjustl_str) 4032 4033 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
2405 recursive subroutine json_value_destroy(me,destroy_next) 2406 2407 implicit none 2408 2409 type(json_value),pointer :: me 2410 logical(LK),intent(in),optional :: destroy_next !if true, then me%next is also destroyed (default is true) 2411 2412 logical(LK) :: des_next 2413 type(json_value), pointer :: p 2414 2415 if (associated(me)) then 2416 2417 if (present(destroy_next)) then 2418 des_next = destroy_next 2419 else 2420 des_next = .true. 2421 end if 2422 2423 if (allocated(me%name)) deallocate(me%name) 2424 2425 call destroy_json_data(me) 2426 2427 if (associated(me%children)) then 2428 do while (me%n_children > 0) 2429 p => me%children 2430 me%children => me%children%next 2431 me%n_children = me%n_children - 1 2432 call json_value_destroy(p,.false.) 2433 end do 2434 nullify(me%children) 2435 nullify(p) 2436 end if 2437 2438 if (associated(me%next) .and. des_next) call json_value_destroy(me%next) 2439 2440 if (associated(me%previous)) nullify(me%previous) 2441 if (associated(me%parent)) nullify(me%parent) 2442 if (associated(me%tail)) nullify(me%tail) 2443 2444 deallocate(me) 2445 2446 nullify(me) 2447 2448 end if 2449 2450 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
4075 subroutine json_value_get_by_index(me, idx, p) 4076 4077 implicit none 4078 4079 type(json_value),pointer,intent(in) :: me 4080 integer(IK),intent(in) :: idx 4081 type(json_value),pointer :: p 4082 4083 integer(IK) :: i 4084 4085 nullify(p) 4086 4087 if (.not. exception_thrown) then 4088 4089 if (associated(me%children)) then 4090 4091 p => me%children 4092 4093 do i = 1, idx - 1 4094 4095 if (associated(p%next)) then 4096 p => p%next 4097 else 4098 call throw_exception('Error in json_value_get_by_index:'//& 4099 ' p%next is not associated.') 4100 nullify(p) 4101 return 4102 end if 4103 4104 end do 4105 4106 else 4107 4108 call throw_exception('Error in json_value_get_by_index:'//& 4109 ' me%children is not associated.') 4110 4111 end if 4112 4113 end if 4114 4115 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
4135 subroutine json_value_get_by_name_chars(me, name, p) 4136 4137 implicit none 4138 4139 type(json_value),pointer,intent(in) :: me 4140 character(kind=CK,len=*),intent(in) :: name 4141 type(json_value),pointer :: p 4142 4143 integer(IK) :: i,n_children 4144 4145 nullify(p) 4146 4147 if (.not. exception_thrown) then 4148 4149 if (associated(me)) then 4150 4151 if (me%var_type==json_object) then 4152 n_children = json_count(me) 4153 p => me%children !start with first one 4154 do i=1, n_children 4155 if (allocated(p%name)) then 4156 if (p%name == name) return 4157 end if 4158 p => p%next 4159 end do 4160 end if 4161 4162 !did not find anything: 4163 call throw_exception('Error in json_value_get_by_name_chars: '//& 4164 'child variable '//trim(name)//' was not found.') 4165 nullify(p) 4166 4167 else 4168 call throw_exception('Error in json_value_get_by_name_chars: '//& 4169 'pointer is not associated.') 4170 end if 4171 4172 end if 4173 4174 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
- If destroy is not present, it is also destroyed.
- If destroy is present and true, it is destroyed.
- If destroy is present and false, it is not destroyed.
EXAMPLE
To extract an object from one json structure, and add it to another:
type(json_value),pointer :: json1,json2,p logical :: found !create and populate json1 and json2 call json_get(json1,'name',p,found) ! get pointer to name element of json1 call json_remove(p,destroy=.false.) ! remove it from json1 (don't destroy) call json_add(json2,p) ! add it to json2
To remove an object from a json structure (and destroy it):
type(json_value),pointer :: json1,p logical :: found !create and populate json1 call json_get(json1,'name',p,found) ! get pointer to name element of json1 call json_remove(p) ! remove and destroy it
AUTHOR
Jacob Williams : 9/9/2014
HISTORY
JW : 12/28/2014 : added destroy optional argument.
SOURCE
2493 subroutine json_value_remove(me,destroy) 2494 2495 implicit none 2496 2497 type(json_value),pointer :: me 2498 logical(LK),intent(in),optional :: destroy 2499 2500 type(json_value),pointer :: parent,previous,next 2501 logical(LK) :: destroy_it 2502 2503 if (associated(me)) then 2504 2505 !optional input argument: 2506 if (present(destroy)) then 2507 destroy_it = destroy 2508 else 2509 destroy_it = .true. 2510 end if 2511 2512 if (associated(me%parent)) then 2513 2514 parent => me%parent 2515 2516 if (associated(me%next)) then 2517 2518 !there are later items in the list: 2519 2520 next => me%next 2521 nullify(me%next) 2522 2523 if (associated(me%previous)) then 2524 !there are earlier items in the list 2525 previous => me%previous 2526 previous%next => next 2527 next%previous => previous 2528 else 2529 !this is the first item in the list 2530 parent%children => next 2531 nullify(next%previous) 2532 end if 2533 2534 else 2535 2536 if (associated(me%previous)) then 2537 !there are earlier items in the list: 2538 previous => me%previous 2539 nullify(previous%next) 2540 parent%tail => previous 2541 else 2542 !this is the only item in the list: 2543 nullify(parent%children) 2544 nullify(parent%tail) 2545 end if 2546 2547 end if 2548 2549 parent%n_children = parent%n_children - 1 2550 2551 end if 2552 2553 if (destroy_it) call json_value_destroy(me) 2554 2555 end if 2556 2557 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
2575 subroutine json_value_remove_if_present(p,name) 2576 2577 implicit none 2578 2579 type(json_value),pointer :: p 2580 character(kind=CK,len=*),intent(in) :: name 2581 2582 type(json_value),pointer :: p_var 2583 logical(LK) :: found 2584 2585 call json_get(p,name,p_var,found) 2586 if (found) call json_remove(p_var) 2587 2588 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
4215 subroutine json_value_to_string(me,str) 4216 4217 implicit none 4218 4219 type(json_value),pointer,intent(in) :: me 4220 character(kind=CK,len=:),intent(out),allocatable :: str 4221 4222 str = '' 4223 call json_value_print(me, iunit=0, str=str, indent=1, colon=.true.) 4224 4225 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
8177 pure function to_uni(str) 8178 8179 implicit none 8180 8181 character(kind=CDK,len=*), intent(in) :: str 8182 character(kind=CK,len=len(str)) :: to_uni 8183 8184 to_uni = str 8185 8186 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
8206 pure function to_uni_vec(str) 8207 8208 implicit none 8209 8210 character(kind=CDK,len=*), dimension(:), intent(in) :: str 8211 character(kind=CK,len=len(str)), dimension(size(str)) :: to_uni_vec 8212 8213 to_uni_vec = str 8214 8215 end function to_uni_vec