JSON/json_module [ Modules ]
NAME
json_module
DESCRIPTION
JSON-FORTRAN: A Fortran 2008 JSON (JavaScript Object Notation) API.
NOTES
-Based on fson by Joseph A. Levin (see License below) -The original F95 code was split into four files: fson_path_m.f95, fson_string_m.f95, fson_value_m.f95, fson.f95 -The code has been extensively modified and combined into this one module (json_module.f90). -Some Fortran 2003/2008 features are now used (e.g., allocatable strings, newunit, generic, class, abstract interface) -The headers in this file follow the ROBODoc conventions. Compile with: robodoc --src ./ --doc ./doc --multidoc --html --tabsize 4 --ignore_case_when_linking --syntaxcolors --source_line_numbers --index
HISTORY
Joseph A. Levin : March 2012 Jacob Williams : 2/8/2013 : Extensive modifications to the original code.
SEE ALSO
[1] http://github.com/jacobwilliams/json-fortran [json-fortran development site] [2] http://jacobwilliams.github.io/json-fortran [json-fortran online documentation] [3] http://www.json.org/ [JSON website] [4] http://jsonlint.com/ [JSON validator]
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 [FSON code retrieved on 12/2/2013] Copyright (c) 2012 Joseph A. Levin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
json_module/kinds [ Parameters ]
[ Top ] [ json_module ] [ Parameters ]
NAME
kinds
DESCRIPTION
Kind definitions for real, integer, character, and logical variables. The sizes given here are valid for the Intel and Gfortran compilers (and perhaps others)
SOURCE
113 !default real kind [8 bytes] 114 integer,parameter :: RK = real64 115 116 !default integer kind [4 bytes] 117 integer,parameter :: IK = int32 118 119 !default character kind [1 byte] 120 integer,parameter :: CK = character_kinds(1) 121 122 !default logical kind [4 bytes] 123 !The statement here is to ensure a valid kind 124 ! if the compiler doesn't have a logical_kinds(3) 125 integer,parameter :: LK = logical_kinds(min(3,size(logical_kinds)))
json_module/var_type [ 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
179 integer(IK),parameter,public :: json_unknown = 0 180 integer(IK),parameter,public :: json_null = 1 181 integer(IK),parameter,public :: json_object = 2 182 integer(IK),parameter,public :: json_array = 3 183 integer(IK),parameter,public :: json_logical = 4 184 integer(IK),parameter,public :: json_integer = 5 185 integer(IK),parameter,public :: json_double = 6 186 integer(IK),parameter,public :: json_string = 7
json_module/json_file [ Classes ]
[ Top ] [ json_module ] [ Classes ]
NAME
json_file
DESCRIPTION
The json_file is the main public class that is used to open a file and get data from it.
EXAMPLE
type(json_file) :: json integer :: ival real(real64) :: rval character(len=:),allocatable :: cval logical :: found call json%load_file(filename='myfile.json') call json%print_file() !print to the console call json%get('var.i',ival,found) call json%get('var.r(3)',rval,found) call json%get('var.c',cval,found) call json%destroy()
AUTHOR
Jacob Williams : 12/9/2013
SOURCE
268 private 269 270 !the JSON structure read from the file: 271 type(json_value),pointer :: p => null() 272 273 contains 274 275 procedure,public :: load_file => json_file_load 276 procedure,public :: load_from_string => json_file_load_from_string 277 278 procedure,public :: destroy => json_file_destroy 279 procedure,public :: move => json_file_move_pointer 280 procedure,public :: info => json_file_variable_info 281 282 procedure,public :: print_to_string => json_file_print_to_string 283 284 generic,public :: print_file => json_file_print_to_console, & 285 json_file_print_1, & 286 json_file_print_2 287 288 generic,public :: get => json_file_get_object, & 289 json_file_get_integer, & 290 json_file_get_double, & 291 json_file_get_logical, & 292 json_file_get_string, & 293 json_file_get_integer_vec, & 294 json_file_get_double_vec, & 295 json_file_get_logical_vec, & 296 json_file_get_string_vec 297 298 generic,public :: update => json_file_update_integer, & 299 json_file_update_logical, & 300 json_file_update_real, & 301 json_file_update_string 302 303 !get: 304 procedure :: json_file_get_object 305 procedure :: json_file_get_integer 306 procedure :: json_file_get_double 307 procedure :: json_file_get_logical 308 procedure :: json_file_get_string 309 procedure :: json_file_get_integer_vec 310 procedure :: json_file_get_double_vec 311 procedure :: json_file_get_logical_vec 312 procedure :: json_file_get_string_vec 313 314 !update: 315 procedure :: json_file_update_integer 316 procedure :: json_file_update_logical 317 procedure :: json_file_update_real 318 procedure :: json_file_update_string 319 320 !print_file: 321 procedure :: json_file_print_to_console 322 procedure :: json_file_print_1 323 procedure :: json_file_print_2
json_module/json_value [ Classes ]
[ Top ] [ json_module ] [ Classes ]
NAME
json_value
DESCRIPTION
Type used to construct the linked-list json structure
EXAMPLE
type(json_value),pointer :: p call json_create_object(p) call json_add(p,'year',1805) call json_add(p,'value',1.0d0) call json_print(p,'test.json') call json_destroy(p)
SOURCE
208 type,public :: json_value 209 210 !force the constituents to be stored contiguously 211 ![note: on Intel, the order of the variables below 212 ! is significant to avoid the misaligned field warnings] 213 sequence 214 215 !for the linked list: 216 type(json_value),pointer :: previous => null() 217 type(json_value),pointer :: next => null() 218 type(json_value),pointer :: parent => null() 219 type(json_value),pointer :: children => null() 220 type(json_value),pointer :: tail => null() 221 222 !variable name: 223 character(kind=CK,len=:),allocatable :: name 224 225 !the data for this variable: 226 real(RK),allocatable :: dbl_value 227 logical(LK),allocatable :: log_value 228 character(kind=CK,len=:),allocatable :: str_value 229 integer(IK),allocatable :: int_value 230 231 integer(IK) :: var_type = json_unknown !variable type 232 233 integer(IK),private :: n_children = 0 !number of children 234 235 end type json_value
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
373 interface json_add 374 module procedure :: json_value_add_member 375 module procedure :: json_value_add_integer, json_value_add_integer_vec 376 module procedure :: json_value_add_double, json_value_add_double_vec 377 module procedure :: json_value_add_logical, json_value_add_logical_vec 378 module procedure :: json_value_add_string, json_value_add_string_vec 379 end interface json_add
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.
USAGE
type(json_value) :: p ... call json_destroy(p)
SOURCE
481 interface json_destroy 482 module procedure :: json_value_destroy 483 end interface
json_module/json_get [ Interfaces ]
[ Top ] [ json_module ] [ Interfaces ]
NAME
json_get
DESCRIPTION
Get data from a json_value linked list.
SOURCE
418 interface json_get 419 module procedure :: json_get_by_path 420 module procedure :: json_get_integer, json_get_integer_vec 421 module procedure :: json_get_double, json_get_double_vec 422 module procedure :: json_get_logical, json_get_logical_vec 423 module procedure :: json_get_string, json_get_string_vec 424 module procedure :: json_get_array 425 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
354 interface json_get_child 355 module procedure json_value_get_by_index 356 module procedure json_value_get_by_name_chars 357 end interface json_get_child
json_module/json_print [ Interfaces ]
[ Top ] [ json_module ] [ Interfaces ]
NAME
json_print
DESCRIPTION
Print the json_value to a file.
EXAMPLE
type(json_value) :: p ... call json_print(p,'test.json') !this is json_print_2
SOURCE
458 interface json_print 459 module procedure :: json_print_1 !input is unit number 460 module procedure :: json_print_2 !input is file name 461 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
438 interface json_print_to_string 439 module procedure :: json_value_to_string 440 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
496 interface json_remove 497 module procedure :: json_value_remove 498 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
511 interface json_remove_if_present 512 module procedure :: json_value_remove_if_present 513 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 will produce an invalid JSON file.
SOURCE
400 interface json_update 401 module procedure :: json_update_logical,& 402 json_update_double,& 403 json_update_integer,& 404 json_update_string 405 end interface json_update
json_module/json_check_for_errors [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_check_for_errors
DESCRIPTION
Retrieve error code from the module. This should be called after json_parse to check for errors. If an error is thrown, before using the module again, json_initialize should be called to clean up before it is used again.
EXAMPLE
type(json_file) :: json logical :: status_ok character(len=:),allocatable :: error_msg call json%load_file(filename='myfile.json') call json_check_for_errors(status_ok, error_msg) if (.not. status_ok) then write(*,*) 'Error: '//error_msg call json_clear_exceptions() call json%destroy() end if
SEE ALSO
json_failed
AUTHOR
Jacob Williams : 12/4/2013
SOURCE
1390 subroutine json_check_for_errors(status_ok, error_msg) 1391 1392 implicit none 1393 1394 logical(LK),intent(out) :: status_ok 1395 character(kind=CK,len=:),allocatable,intent(out) :: error_msg 1396 1397 status_ok = .not. exception_thrown 1398 1399 if (.not. status_ok) then 1400 if (allocated(err_message)) then 1401 error_msg = err_message 1402 else 1403 error_msg = 'Unknown Error' 1404 end if 1405 else 1406 error_msg = '' 1407 end if 1408 1409 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
1311 subroutine json_clear_exceptions() 1312 1313 implicit none 1314 1315 !clear the flag and message: 1316 exception_thrown = .false. 1317 err_message = '' 1318 1319 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
2478 function json_count(me) result(count) 2479 2480 implicit none 2481 2482 integer(IK) :: count 2483 type(json_value),pointer,intent(in) :: me 2484 2485 count = me%n_children 2486 2487 end function json_count
json_module/json_create_array [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_create_array
DESCRIPTION
Allocate a json_value pointer and make it an array variable. The pointer should not already be allocated.
EXAMPLE
type(json_value),pointer :: p call json_create(p,'arrayname')
AUTHOR
Jacob Williams
SOURCE
4515 subroutine json_create_array(me,name) 4516 4517 implicit none 4518 4519 type(json_value),pointer :: me 4520 character(kind=CK,len=*),intent(in) :: name 4521 4522 call json_value_create(me) 4523 call to_array(me,name) 4524 4525 end subroutine json_create_array
json_module/json_create_double [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_create_double
DESCRIPTION
Allocate a json_value pointer and make it a double variable. The pointer should not already be allocated.
EXAMPLE
type(json_value),pointer :: p call json_create(p,'value',1.0d0)
AUTHOR
Jacob Williams
SOURCE
4385 subroutine json_create_double(me,val,name) 4386 4387 implicit none 4388 4389 type(json_value),pointer :: me 4390 character(kind=CK,len=*),intent(in) :: name 4391 real(RK),intent(in) :: val 4392 4393 call json_value_create(me) 4394 call to_double(me,val,name) 4395 4396 end subroutine json_create_double
json_module/json_create_integer [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_create_integer
DESCRIPTION
Allocate a json_value pointer and make it an integer variable. The pointer should not already be allocated.
EXAMPLE
type(json_value),pointer :: p call json_create(p,'value',1)
AUTHOR
Jacob Williams
SOURCE
4352 subroutine json_create_integer(me,val,name) 4353 4354 implicit none 4355 4356 type(json_value),pointer :: me 4357 character(kind=CK,len=*),intent(in) :: name 4358 integer(IK),intent(in) :: val 4359 4360 call json_value_create(me) 4361 call to_integer(me,val,name) 4362 4363 end subroutine json_create_integer
json_module/json_create_logical [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_create_logical
DESCRIPTION
Allocate a json_value pointer and make it a logical variable. The pointer should not already be allocated.
EXAMPLE
type(json_value),pointer :: p call json_create(p,'value',.true.)
AUTHOR
Jacob Williams
SOURCE
4319 subroutine json_create_logical(me,val,name) 4320 4321 implicit none 4322 4323 type(json_value),pointer :: me 4324 character(kind=CK,len=*),intent(in) :: name 4325 logical(LK),intent(in) :: val 4326 4327 call json_value_create(me) 4328 call to_logical(me,val,name) 4329 4330 end subroutine json_create_logical
json_module/json_create_null [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_create_null
DESCRIPTION
Allocate a json_value pointer and make it a null variable. The pointer should not already be allocated.
EXAMPLE
type(json_value),pointer :: p call json_create(p,'value')
AUTHOR
Jacob Williams
SOURCE
4451 subroutine json_create_null(me,name) 4452 4453 implicit none 4454 4455 type(json_value),pointer :: me 4456 character(kind=CK,len=*),intent(in) :: name 4457 4458 call json_value_create(me) 4459 call to_null(me,name) 4460 4461 end subroutine json_create_null
json_module/json_create_object [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_create_object
DESCRIPTION
Allocate a json_value pointer and make it an object variable. The pointer should not already be allocated.
EXAMPLE
type(json_value),pointer :: p call json_create(p,'objectname')
AUTHOR
Jacob Williams
SOURCE
4483 subroutine json_create_object(me,name) 4484 4485 implicit none 4486 4487 type(json_value),pointer :: me 4488 character(kind=CK,len=*),intent(in) :: name 4489 4490 call json_value_create(me) 4491 call to_object(me,name) 4492 4493 end subroutine json_create_object
json_module/json_create_string [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_create_string
DESCRIPTION
Allocate a json_value pointer and make it a string variable. The pointer should not already be allocated.
EXAMPLE
type(json_value),pointer :: p call json_create(p,'value','hello')
AUTHOR
Jacob Williams
SOURCE
4418 subroutine json_create_string(me,val,name) 4419 4420 implicit none 4421 4422 type(json_value),pointer :: me 4423 character(kind=CK,len=*),intent(in) :: name 4424 character(kind=CK,len=*),intent(in) :: val 4425 4426 call json_value_create(me) 4427 call to_string(me,val,name) 4428 4429 end subroutine json_create_string
json_module/json_failed [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_failed
DESCRIPTION
Logical function to indicate if an exception has been thrown.
EXAMPLE
type(json_file) :: json logical :: status_ok character(len=:),allocatable :: error_msg call json%load_file(filename='myfile.json') if (json_failed()) then call json_check_for_errors(status_ok, error_msg) write(*,*) 'Error: '//error_msg call json_clear_exceptions() call json%destroy() end if
SEE ALSO
json_check_for_errors
AUTHOR
Jacob Williams : 12/5/2013
SOURCE
1441 function json_failed() result(failed) 1442 1443 implicit none 1444 1445 logical(LK) :: failed 1446 1447 failed = exception_thrown 1448 1449 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
615 subroutine json_file_destroy(me) 616 617 implicit none 618 619 class(json_file),intent(inout) :: me 620 621 if (associated(me%p)) call json_value_destroy(me%p) 622 623 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
1069 subroutine json_file_get_double (me, path, val, found) 1070 1071 implicit none 1072 1073 class(json_file),intent(inout) :: me 1074 character(kind=CK,len=*),intent(in) :: path 1075 real(RK),intent(out) :: val 1076 logical(LK),intent(out),optional :: found 1077 1078 call json_get(me%p, path=path, value=val, found=found) 1079 1080 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
1100 subroutine json_file_get_double_vec(me, path, vec, found) 1101 1102 implicit none 1103 1104 class(json_file),intent(inout) :: me 1105 character(kind=CK,len=*),intent(in) :: path 1106 real(RK),dimension(:),allocatable,intent(out) :: vec 1107 logical(LK),intent(out),optional :: found 1108 1109 call json_get(me%p, path, vec, found) 1110 1111 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
1007 subroutine json_file_get_integer(me, path, val, found) 1008 1009 implicit none 1010 1011 class(json_file),intent(inout) :: me 1012 character(kind=CK,len=*),intent(in) :: path 1013 integer(IK),intent(out) :: val 1014 logical(LK),intent(out),optional :: found 1015 1016 call json_get(me%p, path=path, value=val, found=found) 1017 1018 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
1038 subroutine json_file_get_integer_vec(me, path, vec, found) 1039 1040 implicit none 1041 1042 class(json_file),intent(inout) :: me 1043 character(kind=CK,len=*),intent(in) :: path 1044 integer(IK),dimension(:),allocatable,intent(out) :: vec 1045 logical(LK),intent(out),optional :: found 1046 1047 call json_get(me%p, path, vec, found) 1048 1049 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
1131 subroutine json_file_get_logical(me,path,val,found) 1132 1133 implicit none 1134 1135 class(json_file),intent(inout) :: me 1136 character(kind=CK,len=*),intent(in) :: path 1137 logical(LK),intent(out) :: val 1138 logical(LK),intent(out),optional :: found 1139 1140 call json_get(me%p, path=path, value=val, found=found) 1141 1142 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
1162 subroutine json_file_get_logical_vec(me, path, vec, found) 1163 1164 implicit none 1165 1166 class(json_file),intent(inout) :: me 1167 character(kind=CK,len=*),intent(in) :: path 1168 logical(LK),dimension(:),allocatable,intent(out) :: vec 1169 logical(LK),intent(out),optional :: found 1170 1171 call json_get(me%p, path, vec, found) 1172 1173 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
976 subroutine json_file_get_object(me, path, p, found) 977 978 implicit none 979 980 class(json_file),intent(inout) :: me 981 character(kind=CK,len=*),intent(in) :: path 982 type(json_value),pointer,intent(out) :: p 983 logical(LK),intent(out),optional :: found 984 985 call json_get_by_path(me%p, path=path, p=p, found=found) 986 987 end subroutine json_file_get_object
json_module/json_file_get_string [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_get_string
USAGE
call me%get(path,val)
DESCRIPTION
Get a character string from a json file. Note: val is an allocatable character string.
AUTHOR
Jacob Williams : 12/9/2013
SOURCE
1194 subroutine json_file_get_string(me, path, val, found) 1195 1196 implicit none 1197 1198 class(json_file),intent(inout) :: me 1199 character(kind=CK,len=*),intent(in) :: path 1200 character(kind=CK,len=:),allocatable,intent(out) :: val 1201 logical(LK),intent(out),optional :: found 1202 1203 call json_get(me%p, path=path, value=val, found=found) 1204 1205 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
1225 subroutine json_file_get_string_vec(me, path, vec, found) 1226 1227 implicit none 1228 1229 class(json_file),intent(inout) :: me 1230 character(kind=CK,len=*),intent(in) :: path 1231 character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec 1232 logical(LK),intent(out),optional :: found 1233 1234 call json_get(me%p, path, vec, found) 1235 1236 end subroutine json_file_get_string_vec
json_module/json_file_load [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_load
DESCRIPTION
Load a JSON file.
EXAMPLE
type(json_file) :: f call f%load_file('my_file.json')
AUTHOR
Jacob Williams : 12/9/2013
SOURCE
680 subroutine json_file_load(me, filename, unit) 681 682 implicit none 683 684 class(json_file),intent(inout) :: me 685 character(kind=CK,len=*),intent(in) :: filename 686 integer(IK),intent(in),optional :: unit 687 688 call json_parse(file=filename, p=me%p, unit=unit) 689 690 end subroutine json_file_load
json_module/json_file_load_from_string [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_load_from_string
DESCRIPTION
Load the JSON data from a string.
EXAMPLE
type(json_file) :: f call f%load_from_string('{ "name": "Leonidas" }')
AUTHOR
Jacob Williams : 1/13/2015
SOURCE
711 subroutine json_file_load_from_string(me, str) 712 713 implicit none 714 715 class(json_file),intent(inout) :: me 716 character(kind=CK,len=*),intent(in) :: str 717 718 call json_parse(str=str, p=me%p) 719 720 end subroutine json_file_load_from_string
json_module/json_file_move_pointer [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_move_pointer
USAGE
call to%move(from)
DESCRIPTION
Move the json_value pointer from one json_file to another. "from" is then nullified, but not destroyed. Note: if "from%p" is not associated, then an error is thrown.
AUTHOR
Jacob Williams : 12/5/2014
SOURCE
645 subroutine json_file_move_pointer(to,from) 646 647 implicit none 648 649 class(json_file),intent(inout) :: to 650 class(json_file),intent(inout) :: from 651 652 if (associated(from%p)) then 653 to%p => from%p 654 nullify(from%p) 655 else 656 call throw_exception('Error in json_file_move_pointer: '//& 657 'pointer is not associated.') 658 end if 659 660 end subroutine json_file_move_pointer
json_module/json_file_print_1 [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_print_1
USAGE
call me%print_file(iunit)
DESCRIPTION
Print the JSON file. Prints to the specified file unit (if not present, then prints to the console)
AUTHOR
Jacob Williams : 12/9/2013
SOURCE
771 subroutine json_file_print_1(me, iunit) 772 773 implicit none 774 775 class(json_file),intent(inout) :: me 776 integer(IK),intent(in) :: iunit !must be non-zero 777 778 integer(IK) :: i 779 character(kind=CK,len=:),allocatable :: dummy 780 781 if (iunit/=0) then 782 i = iunit 783 else 784 call throw_exception('Error in json_file_print_1: iunit must be nonzero.') 785 return 786 end if 787 788 call json_value_print(me%p,iunit=i,str=dummy,indent=1,colon=.true.) 789 790 end subroutine json_file_print_1
json_module/json_file_print_2 [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_print_2
USAGE
call me%print_file(filename)
DESCRIPTION
Print the JSON file. Input is the filename. The file is opened, printed, and then closed.
EXAMPLE
type(json_file) :: f logical :: found call f%load_file('my_file.json') !open the original file call f%update('version',4,found) !change the value of a variable call f%print_file('my_file_2.json') !save file as new name
AUTHOR
Jacob Williams : 1/11/2015
SOURCE
818 subroutine json_file_print_2(me,filename) 819 820 implicit none 821 822 class(json_file),intent(inout) :: me 823 character(kind=CK,len=*),intent(in) :: filename 824 825 integer(IK) :: iunit,istat 826 827 open(newunit=iunit,file=filename,status='REPLACE',iostat=istat) 828 if (istat==0) then 829 call me%print_file(iunit) !call the other routine 830 close(iunit,iostat=istat) 831 else 832 call throw_exception('Error in json_file_print_2: could not open file: '//& 833 trim(filename)) 834 end if 835 836 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
740 subroutine json_file_print_to_console(me) 741 742 implicit none 743 744 class(json_file),intent(inout) :: me 745 746 character(kind=CK,len=:),allocatable :: dummy 747 748 call json_value_print(me%p,iunit=output_unit,str=dummy,indent=1,colon=.true.) 749 750 end subroutine json_file_print_to_console
json_module/json_file_print_to_string [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_print_to_string
USAGE
call me%print_to_string(str)
DESCRIPTION
Print the JSON file to a string.
EXAMPLE
type(json_file) :: f character(kind=CK,len=:),allocatable :: str call f%load_file('my_file.json') call f%print_file(str)
AUTHOR
Jacob Williams : 1/11/2015
SOURCE
862 subroutine json_file_print_to_string(me,str) 863 864 implicit none 865 866 class(json_file),intent(inout) :: me 867 character(kind=CK,len=:),allocatable,intent(out) :: str 868 869 call json_value_to_string(me%p,str) 870 871 end subroutine json_file_print_to_string
json_module/json_file_update_integer [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_update_integer
DESCRIPTION
Given the path string, if the variable is present in the file, and is a scalar, then update its value. If it is not present, then create it and set its value.
SEE ALSO
json_update_integer
AUTHOR
Jacob Williams : 1/10/2015
SOURCE
1689 subroutine json_file_update_integer(me,name,val,found) 1690 implicit none 1691 1692 class(json_file),intent(inout) :: me 1693 character(kind=CK,len=*),intent(in) :: name 1694 integer(IK),intent(in) :: val 1695 logical(LK),intent(out) :: found 1696 1697 if (.not. exception_thrown) call json_update(me%p,name,val,found) 1698 1699 end subroutine json_file_update_integer
json_module/json_file_update_logical [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_update_logical
DESCRIPTION
Given the path string, if the variable is present in the file, and is a scalar, then update its value. If it is not present, then create it and set its value.
SEE ALSO
json_update_logical
AUTHOR
Jacob Williams : 1/10/2015
SOURCE
1721 subroutine json_file_update_logical(me,name,val,found) 1722 implicit none 1723 1724 class(json_file),intent(inout) :: me 1725 character(kind=CK,len=*),intent(in) :: name 1726 logical(LK),intent(in) :: val 1727 logical(LK),intent(out) :: found 1728 1729 if (.not. exception_thrown) call json_update(me%p,name,val,found) 1730 1731 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
1753 subroutine json_file_update_real(me,name,val,found) 1754 implicit none 1755 1756 class(json_file),intent(inout) :: me 1757 character(kind=CK,len=*),intent(in) :: name 1758 real(RK),intent(in) :: val 1759 logical(LK),intent(out) :: found 1760 1761 if (.not. exception_thrown) call json_update(me%p,name,val,found) 1762 1763 end subroutine json_file_update_real
json_module/json_file_update_string [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_update_string
DESCRIPTION
Given the path string, if the variable is present in the file, and is a scalar, then update its value. If it is not present, then create it and set its value.
SEE ALSO
json_update_string
AUTHOR
Jacob Williams : 1/10/2015
SOURCE
1785 subroutine json_file_update_string(me,name,val,found) 1786 implicit none 1787 1788 class(json_file),intent(inout) :: me 1789 character(kind=CK,len=*),intent(in) :: name 1790 character(kind=CK,len=*),intent(in) :: val 1791 logical(LK),intent(out) :: found 1792 1793 if (.not. exception_thrown) call json_update(me%p,name,val,found) 1794 1795 end subroutine json_file_update_string
json_module/json_file_variable_info [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_file_variable_info
USAGE
call me%info(path,found,var_type,n_children)
DESCRIPTION
Returns information about a variable in a file.
AUTHOR
Jacob Williams : 2/3/2014
SOURCE
891 subroutine json_file_variable_info(me,path,found,var_type,n_children) 892 893 implicit none 894 895 class(json_file),intent(inout) :: me 896 character(kind=CK,len=*),intent(in) :: path 897 logical(LK),intent(out) :: found 898 integer(IK),intent(out) :: var_type 899 integer(IK),intent(out) :: n_children 900 901 type(json_value),pointer :: p 902 903 !initialize: 904 nullify(p) 905 906 !get a pointer to the variable (if it is there): 907 call me%get(path,p,found) 908 909 if (found) then 910 911 !get info: 912 call json_info(p,var_type,n_children) 913 914 else 915 916 !set to dummy values: 917 var_type = json_unknown 918 n_children = 0 919 920 end if 921 922 !cleanup: 923 nullify(p) 924 925 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. Note: for integer, double, logical, and character arrays, a higher-level routine is provided (see json_get), so this routine does not have to be used for those cases.
SOURCE
3908 subroutine json_get_array(this, path, array_callback, found) 3909 3910 implicit none 3911 3912 type(json_value),pointer,intent(in) :: this 3913 character(kind=CK,len=*),intent(in),optional :: path 3914 procedure(array_callback_func) :: array_callback 3915 logical(LK),intent(out),optional :: found 3916 3917 type(json_value),pointer :: element,p 3918 integer(IK) :: i, count 3919 3920 if (.not. exception_thrown) then 3921 3922 nullify(p) 3923 3924 ! resolve the path to the value 3925 if (present(path)) then 3926 call json_get_by_path(this=this, path=path, p=p) 3927 else 3928 p => this 3929 end if 3930 3931 if (.not. associated(p)) then 3932 3933 call throw_exception('Error in json_get_array:'//& 3934 ' Unable to resolve path: '//trim(path)) 3935 3936 else 3937 3938 select case (p%var_type) 3939 case (json_array) 3940 count = json_count(p) 3941 element => p%children 3942 do i = 1, count ! callback for each child 3943 call array_callback(element, i, count) 3944 element => element%next 3945 end do 3946 case default 3947 call throw_exception('Error in json_get_array:'//& 3948 ' Resolved value is not an array. '//& 3949 trim(path)) 3950 end select 3951 3952 !cleanup: 3953 if (associated(p)) nullify(p) 3954 if (associated(element)) nullify(element) 3955 3956 end if 3957 3958 if (exception_thrown) then 3959 if (present(found)) then 3960 found = .false. 3961 call json_clear_exceptions() 3962 end if 3963 else 3964 if (present(found)) found = .true. 3965 end if 3966 3967 else 3968 if (present(found)) found = .false. 3969 end if 3970 3971 end subroutine json_get_array
json_module/json_get_by_path [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_get_by_path
USAGE
call json_get(me,path,p,found)
DESCRIPTION
Returns the json_value pointer given the path string.
EXAMPLE
type(json_value),pointer :: dat,p logical :: found ... call json_get(dat,'data(2).version',p,found)
NOTES
The following special characters are used to denote paths: $ - root @ - this . - child object member [] or () - child array element Thus, if any of these characters are present in the name key, this routine cannot be used to get the value. In that case, the json_get_child routines would need to be used.
SOURCE
2990 subroutine json_get_by_path(this, path, p, found) 2991 2992 implicit none 2993 2994 type(json_value),pointer,intent(in) :: this 2995 character(kind=CK,len=*),intent(in) :: path 2996 type(json_value),pointer,intent(out) :: p 2997 logical(LK),intent(out),optional :: found 2998 2999 character(kind=CK,len=1),parameter :: start_array_alt = '(' 3000 character(kind=CK,len=1),parameter :: end_array_alt = ')' 3001 3002 integer(IK) :: i, length, child_i 3003 character(kind=CK,len=1) :: c 3004 logical(LK) :: array 3005 type(json_value),pointer :: tmp 3006 3007 if (.not. exception_thrown) then 3008 3009 nullify(p) 3010 3011 ! default to assuming relative to this 3012 p => this 3013 3014 child_i = 1 3015 3016 array = .false. 3017 3018 length = len_trim(path) 3019 3020 do i=1, length 3021 3022 c = path(i:i) 3023 3024 select case (c) 3025 case ('$') 3026 3027 ! root 3028 do while (associated (p%parent)) 3029 p => p%parent 3030 end do 3031 child_i = i + 1 3032 3033 case ('@') 3034 3035 ! this 3036 p => this 3037 child_i = i + 1 3038 3039 case ('.') 3040 3041 ! get child member from p 3042 if (child_i < i) then 3043 nullify(tmp) 3044 call json_get_child(p, path(child_i:i-1), tmp) 3045 p => tmp 3046 nullify(tmp) 3047 else 3048 child_i = i + 1 3049 cycle 3050 end if 3051 3052 if (.not. associated(p)) then 3053 call throw_exception('Error in json_get_by_path:'//& 3054 ' Error getting child member.') 3055 exit 3056 end if 3057 3058 child_i = i+1 3059 3060 case (start_array,start_array_alt) 3061 3062 !....Modified to allow for 'var[3]' style syntax 3063 !Note: jmozmoz/fson has a slightly different version of this... 3064 3065 ! start looking for the array element index 3066 array = .true. 3067 3068 ! get child member from p 3069 if (child_i < i) then 3070 nullify(tmp) 3071 call json_get_child(p, path(child_i:i-1), tmp) 3072 p => tmp 3073 nullify(tmp) 3074 else 3075 child_i = i + 1 3076 cycle 3077 end if 3078 if (.not. associated(p)) then 3079 call throw_exception('Error in json_get_by_path:'//& 3080 ' Error getting array element') 3081 exit 3082 end if 3083 child_i = i + 1 3084 3085 case (end_array,end_array_alt) 3086 3087 if (.not.array) then 3088 call throw_exception('Error in json_get_by_path: Unexpected ]') 3089 exit 3090 end if 3091 array = .false. 3092 child_i = string_to_integer(path(child_i:i-1)) 3093 3094 nullify(tmp) 3095 call json_get_child(p, child_i, tmp) 3096 p => tmp 3097 nullify(tmp) 3098 3099 child_i= i + 1 3100 3101 end select 3102 3103 end do 3104 3105 if (exception_thrown) then 3106 3107 if (present(found)) then 3108 found = .false. 3109 call json_clear_exceptions() 3110 end if 3111 3112 else 3113 3114 ! grab the last child if present in the path 3115 if (child_i <= length) then 3116 nullify(tmp) 3117 call json_get_child(p, path(child_i:i-1), tmp) 3118 p => tmp 3119 nullify(tmp) 3120 end if 3121 if (associated(p)) then 3122 if (present(found)) found = .true. !everything seems to be ok 3123 else 3124 call throw_exception('Error in json_get_by_path:'//& 3125 ' variable not found: '//trim(path)) 3126 if (present(found)) then 3127 found = .false. 3128 call json_clear_exceptions() 3129 end if 3130 end if 3131 3132 end if 3133 3134 else 3135 if (present(found)) found = .false. 3136 end if 3137 3138 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
3365 subroutine json_get_double(this, path, value, found) 3366 3367 implicit none 3368 3369 type(json_value),pointer :: this 3370 character(kind=CK,len=*), optional :: path 3371 real(RK),intent(out) :: value 3372 logical(LK),intent(out),optional :: found 3373 3374 type(json_value),pointer :: p 3375 3376 if (.not. exception_thrown) then 3377 3378 nullify(p) 3379 3380 if (present(path)) then 3381 call json_get_by_path(this=this, path=path, p=p) 3382 else 3383 p => this 3384 end if 3385 3386 if (.not. associated(p)) then 3387 3388 call throw_exception('Error in json_get_double:'//& 3389 ' Unable to resolve path: '//trim(path)) 3390 3391 else 3392 3393 select case (p%var_type) 3394 case (json_integer) 3395 value = p%int_value 3396 case (json_double) 3397 value = p%dbl_value 3398 case (json_logical) 3399 if (p%log_value) then 3400 value = 1.0_RK 3401 else 3402 value = 0.0_RK 3403 end if 3404 case default 3405 call throw_exception('Error in json_get_double:'//& 3406 ' Unable to resolve value to double: '//& 3407 trim(path)) 3408 end select 3409 3410 nullify(p) 3411 3412 end if 3413 3414 if (exception_thrown) then 3415 if (present(found)) then 3416 found = .false. 3417 call json_clear_exceptions() 3418 end if 3419 else 3420 if (present(found)) found = .true. 3421 end if 3422 3423 else 3424 3425 value = 0.0_RK 3426 if (present(found)) found = .false. 3427 3428 end if 3429 3430 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
3447 subroutine json_get_double_vec(me, path, vec, found) 3448 3449 implicit none 3450 3451 type(json_value),pointer :: me 3452 character(kind=CK,len=*),intent(in) :: path 3453 real(RK),dimension(:),allocatable,intent(out) :: vec 3454 logical(LK),intent(out),optional :: found 3455 3456 logical(LK) :: initialized 3457 3458 initialized = .false. 3459 3460 if (allocated(vec)) deallocate(vec) 3461 3462 !the callback function is called for each element of the array: 3463 call json_get(me, path=path, array_callback=get_double_from_array, found=found) 3464 3465 contains 3466 3467 ! callback function for double 3468 subroutine get_double_from_array(element, i, count) 3469 implicit none 3470 3471 type(json_value),pointer,intent(in) :: element 3472 integer(IK),intent(in) :: i !index 3473 integer(IK),intent(in) :: count !size of array 3474 3475 !size the output array: 3476 if (.not. initialized) then 3477 allocate(vec(count)) 3478 initialized = .true. 3479 end if 3480 3481 !populate the elements: 3482 call json_get(element, value=vec(i)) 3483 3484 end subroutine get_double_from_array 3485 3486 end subroutine json_get_double_vec
json_module/json_get_integer [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_get_integer
DESCRIPTION
Get an integer value from a json_value.
SOURCE
3231 subroutine json_get_integer(this, path, value, found) 3232 3233 implicit none 3234 3235 type(json_value),pointer,intent(in) :: this 3236 character(kind=CK,len=*),optional :: path 3237 integer(IK),intent(out) :: value 3238 logical(LK),intent(out),optional :: found 3239 3240 type(json_value),pointer :: p 3241 3242 if (.not. exception_thrown) then 3243 3244 nullify(p) 3245 if (present(path)) then 3246 call json_get_by_path(this=this, path=path, p=p) 3247 else 3248 p => this 3249 end if 3250 3251 if (.not. associated(p)) then 3252 3253 call throw_exception('Error in json_get_integer:'//& 3254 ' Unable to resolve path: '// trim(path)) 3255 3256 else 3257 3258 select case(p%var_type) 3259 case (json_integer) 3260 value = p%int_value 3261 case (json_double) 3262 value = int(p%dbl_value) 3263 case (json_logical) 3264 if (p%log_value) then 3265 value = 1 3266 else 3267 value = 0 3268 end if 3269 case default 3270 call throw_exception('Error in get_integer:'//& 3271 ' Unable to resolve value to integer: '//& 3272 trim(path)) 3273 end select 3274 3275 nullify(p) 3276 3277 end if 3278 3279 if (exception_thrown) then 3280 if (present(found)) then 3281 found = .false. 3282 call json_clear_exceptions() 3283 end if 3284 else 3285 if (present(found)) found = .true. 3286 end if 3287 3288 else 3289 3290 value = 0 3291 if (present(found)) found = .false. 3292 3293 end if 3294 3295 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
3312 subroutine json_get_integer_vec(me, path, vec, found) 3313 3314 implicit none 3315 3316 type(json_value),pointer :: me 3317 character(kind=CK,len=*),intent(in) :: path 3318 integer(IK),dimension(:),allocatable,intent(out) :: vec 3319 logical(LK),intent(out),optional :: found 3320 3321 logical(LK) :: initialized 3322 3323 initialized = .false. 3324 3325 if (allocated(vec)) deallocate(vec) 3326 3327 !the callback function is called for each element of the array: 3328 call json_get(me, path=path, array_callback=get_int_from_array, found=found) 3329 3330 contains 3331 3332 ! callback function for integer 3333 subroutine get_int_from_array(element, i, count) 3334 implicit none 3335 3336 type(json_value),pointer,intent(in) :: element 3337 integer(IK),intent(in) :: i !index 3338 integer(IK),intent(in) :: count !size of array 3339 3340 !size the output array: 3341 if (.not. initialized) then 3342 allocate(vec(count)) 3343 initialized = .true. 3344 end if 3345 3346 !populate the elements: 3347 call json_get(element, value=vec(i)) 3348 3349 end subroutine get_int_from_array 3350 3351 end subroutine json_get_integer_vec
json_module/json_get_logical [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_get_logical
DESCRIPTION
Get a logical value from a json_value.
SOURCE
3500 subroutine json_get_logical(this, path, value, found) 3501 3502 implicit none 3503 3504 type(json_value),pointer,intent(in) :: this 3505 character(kind=CK,len=*),optional :: path 3506 logical(LK) :: value 3507 logical(LK),intent(out),optional :: found 3508 3509 type(json_value),pointer :: p 3510 3511 if (.not. exception_thrown) then 3512 3513 nullify(p) 3514 3515 if (present(path)) then 3516 call json_get_by_path(this=this, path=path, p=p) 3517 else 3518 p => this 3519 end if 3520 3521 if (.not. associated(p)) then 3522 3523 call throw_exception('Error in json_get_logical:'//& 3524 ' Unable to resolve path: '//trim(path)) 3525 3526 else 3527 3528 select case (p%var_type) 3529 case (json_integer) 3530 value = (p%int_value > 0) 3531 case (json_logical) 3532 value = p % log_value 3533 case default 3534 call throw_exception('Error in json_get_logical:'//& 3535 ' Unable to resolve value to logical: '//& 3536 trim(path)) 3537 end select 3538 3539 nullify(p) 3540 3541 end if 3542 3543 if (exception_thrown) then 3544 if (present(found)) then 3545 found = .false. 3546 call json_clear_exceptions() 3547 end if 3548 else 3549 if (present(found)) found = .true. 3550 end if 3551 3552 else 3553 3554 value = .false. 3555 if (present(found)) found = .false. 3556 3557 end if 3558 3559 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
3576 subroutine json_get_logical_vec(me, path, vec, found) 3577 3578 implicit none 3579 3580 type(json_value),pointer,intent(in) :: me 3581 character(kind=CK,len=*),intent(in) :: path 3582 logical(LK),dimension(:),allocatable,intent(out) :: vec 3583 logical(LK),intent(out),optional :: found 3584 3585 logical(LK) :: initialized 3586 3587 initialized = .false. 3588 3589 if (allocated(vec)) deallocate(vec) 3590 3591 !the callback function is called for each element of the array: 3592 call json_get(me, path=path, array_callback=get_logical_from_array, found=found) 3593 3594 contains 3595 3596 ! callback function for logical 3597 subroutine get_logical_from_array(element, i, count) 3598 implicit none 3599 3600 type(json_value),pointer,intent(in) :: element 3601 integer(IK),intent(in) :: i !index 3602 integer(IK),intent(in) :: count !size of array 3603 3604 !size the output array: 3605 if (.not. initialized) then 3606 allocate(vec(count)) 3607 initialized = .true. 3608 end if 3609 3610 !populate the elements: 3611 call json_get(element, value=vec(i)) 3612 3613 end subroutine get_logical_from_array 3614 3615 end subroutine json_get_logical_vec
json_module/json_get_string [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_get_string
DESCRIPTION
Get a character string from a json_value.
SOURCE
3629 subroutine json_get_string(this, path, value, found) 3630 3631 implicit none 3632 3633 type(json_value),pointer,intent(in) :: this 3634 character(kind=CK,len=*),intent(in),optional :: path 3635 character(kind=CK,len=:),allocatable,intent(out) :: value 3636 logical(LK),intent(out),optional :: found 3637 3638 type(json_value),pointer :: p 3639 character(kind=CK,len=:),allocatable :: s,pre,post 3640 integer(IK) :: j,jprev,n 3641 character(kind=CK,len=1) :: c 3642 3643 if (.not. exception_thrown) then 3644 3645 nullify(p) 3646 3647 if (present(path)) then 3648 call json_get_by_path(this=this, path=path, p=p) 3649 else 3650 p => this 3651 end if 3652 3653 if (.not. associated(p)) then 3654 3655 call throw_exception('Error in json_get_string:'//& 3656 ' Unable to resolve path: '//trim(path)) 3657 3658 else 3659 3660 select case (p%var_type) 3661 3662 case (json_string) 3663 3664 if (allocated(p%str_value)) then 3665 3666 !get the value as is: 3667 s = p%str_value 3668 3669 ! Now, have to remove the escape characters: 3670 ! 3671 ! '\"' quotation mark 3672 ! '\\' reverse solidus 3673 ! '\/' solidus 3674 ! '\b' backspace 3675 ! '\f' formfeed 3676 ! '\n' newline (LF) 3677 ! '\r' carriage return (CR) 3678 ! '\t' horizontal tab 3679 ! '\uXXXX' 4 hexadecimal digits 3680 ! 3681 3682 !initialize: 3683 n = len(s) 3684 j = 1 3685 3686 do 3687 3688 jprev = j !initialize 3689 j = index(s(j:n),backslash) !look for an escape character 3690 3691 if (j>0) then !an escape character was found 3692 3693 !index in full string of the escape character: 3694 j = j + (jprev-1) 3695 3696 if (j<n) then 3697 3698 !save the bit before the escape character: 3699 if (j>1) then 3700 pre = s( 1 : j-1 ) 3701 else 3702 pre = '' 3703 end if 3704 3705 !character after the escape character: 3706 c = s( j+1 : j+1 ) 3707 3708 select case (c) 3709 case(quotation_mark,backslash,slash,& 3710 'b','f','n','r','t') 3711 3712 !save the bit after the escape characters: 3713 if (j+2<n) then 3714 post = s(j+2:n) 3715 else 3716 post = '' 3717 end if 3718 3719 select case(c) 3720 case(quotation_mark,backslash,slash) 3721 !use c as is 3722 case('b') 3723 c = bspace 3724 case('f') 3725 c = formfeed 3726 case('n') 3727 c = newline 3728 case('r') 3729 c = carriage_return 3730 case('t') 3731 c = horizontal_tab 3732 end select 3733 3734 s = pre//c//post 3735 3736 n = n-1 !backslash character has been 3737 ! removed from the string 3738 3739 case('u') !expecting 4 hexadecimal digits after 3740 ! the escape character [\uXXXX] 3741 3742 !for now, we are just printing them as is 3743 ![not checking to see if it is a valid hex value] 3744 3745 if (j+5<=n) then 3746 j=j+4 3747 else 3748 call throw_exception(& 3749 'Error in json_get_string:'//& 3750 ' Invalid hexadecimal sequence'//& 3751 ' in string: '//trim(c)) 3752 exit 3753 end if 3754 3755 case default 3756 !unknown escape character 3757 call throw_exception('Error in json_get_string:'//& 3758 ' unknown escape sequence in string "'//& 3759 trim(s)//'" ['//backslash//c//']') 3760 exit 3761 end select 3762 3763 j=j+1 !go to the next character 3764 3765 if (j>=n) exit !finished 3766 3767 else 3768 !an escape character is the last character in 3769 ! the string [this may not be valid syntax, 3770 ! but just keep it] 3771 exit 3772 end if 3773 3774 else 3775 exit !no more escape characters in the string 3776 end if 3777 3778 end do 3779 3780 if (exception_thrown) then 3781 if (allocated(value)) deallocate(value) 3782 else 3783 value = s 3784 end if 3785 3786 else 3787 call throw_exception('Error in json_get_string:'//& 3788 ' p%value not allocated') 3789 end if 3790 3791 case default 3792 3793 call throw_exception('Error in json_get_string:'//& 3794 ' Unable to resolve value to characters: '//& 3795 trim(path)) 3796 3797 ! Note: for the other cases, we could do val to string conversions. 3798 3799 end select 3800 3801 end if 3802 3803 if (allocated(value) .and. .not. exception_thrown) then 3804 if (present(found)) found = .true. 3805 else 3806 if (present(found)) then 3807 found = .false. 3808 call json_clear_exceptions() 3809 end if 3810 end if 3811 3812 !cleanup: 3813 if (associated(p)) nullify(p) 3814 if (allocated(s)) deallocate(s) 3815 if (allocated(pre)) deallocate(pre) 3816 if (allocated(post)) deallocate(post) 3817 3818 else 3819 3820 value = '' 3821 found = .false. 3822 3823 end if 3824 3825 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
3842 subroutine json_get_string_vec(me, path, vec, found) 3843 3844 implicit none 3845 3846 type(json_value),pointer,intent(in) :: me 3847 character(kind=CK,len=*),intent(in) :: path 3848 character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec 3849 logical(LK),intent(out),optional :: found 3850 3851 logical(LK) :: initialized 3852 3853 initialized = .false. 3854 3855 if (allocated(vec)) deallocate(vec) 3856 3857 !the callback function is called for each element of the array: 3858 call json_get(me, path=path, array_callback=get_chars_from_array, found=found) 3859 3860 contains 3861 3862 ! callback function for chars 3863 subroutine get_chars_from_array(element, i, count) 3864 3865 implicit none 3866 3867 type(json_value),pointer,intent(in) :: element 3868 integer(IK),intent(in) :: i !index 3869 integer(IK),intent(in) :: count !size of array 3870 3871 character(kind=CK,len=:),allocatable :: cval 3872 3873 !size the output array: 3874 if (.not. initialized) then 3875 allocate(vec(count)) 3876 initialized = .true. 3877 end if 3878 3879 !populate the elements: 3880 call json_get(element, value=cval) 3881 if (allocated(cval)) then 3882 vec(i) = cval 3883 deallocate(cval) 3884 else 3885 vec(i) = '' 3886 end if 3887 3888 end subroutine get_chars_from_array 3889 3890 end subroutine json_get_string_vec
json_module/json_info [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_info
USAGE
call me%info(path,found,var_type,n_children)
DESCRIPTION
Returns information about a json_value
AUTHOR
Jacob Williams : 2/13/2014
SOURCE
945 subroutine json_info(p,var_type,n_children) 946 947 implicit none 948 949 type(json_value),pointer :: p 950 integer(IK),intent(out),optional :: var_type 951 integer(IK),intent(out),optional :: n_children 952 953 if (present(var_type)) var_type = p%var_type !variable type 954 if (present(n_children)) n_children = json_count(p) !number of children 955 956 end subroutine json_info
json_module/json_initialize [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_initialize
DESCRIPTION
Initialize the module. Should be called before the routines are used. It can also be called after using the module and encountering exceptions.
AUTHOR
Jacob Williams : 12/4/2013
MODIFIED
Izaak Beekman : 02/24/2015
SOURCE
1258 subroutine json_initialize(verbose,compact_reals) 1259 1260 implicit none 1261 1262 logical(LK),intent(in),optional :: verbose !mainly useful for debugging (default is false) 1263 logical(LK),intent(in),optional :: compact_reals !to compact the real number strings for output 1264 1265 character(kind=CK,len=10) :: w,do,e 1266 integer(IK) :: istat 1267 1268 !clear any errors from previous runs: 1269 call json_clear_exceptions() 1270 1271 !optional inputs (if not present, values remains unchanged): 1272 if (present(verbose)) is_verbose = verbose 1273 if (present(compact_reals)) compact_real = compact_reals !may be a bug here in Gfortran 5.0.0... check this... 1274 1275 ! set the default output/input format for reals: 1276 ! [this only needs to be done once, since it can't change] 1277 if (.not. allocated(real_fmt)) then 1278 write(w,'(I0)',iostat=istat) max_numeric_str_len 1279 if (istat==0) write(do,'(I0)',iostat=istat) real_precision 1280 if (istat==0) write(e,'(I0)',iostat=istat) real_exponent_digits 1281 if (istat==0) then 1282 real_fmt = '(E' // trim(w) // '.' // trim(do) // 'E' // trim(e) // ')' 1283 else 1284 real_fmt = '(E30.16E3)' !just use this one (should never happen) 1285 end if 1286 end if 1287 1288 !Just in case, clear these global variables also: 1289 pushed_index = 0 1290 pushed_char = '' 1291 char_count = 0 1292 line_count = 1 1293 1294 end subroutine json_initialize
json_module/json_parse [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_parse
DESCRIPTION
Parse the JSON file and populate the json_value tree. Inputs can be: file and unit : the specified unit is used to read JSON from file [note if unit is already open, then the filename is ignored] file : JSON is read from file using internal unit number str : JSON data is read from the string instead
EXAMPLE
type(json_value),pointer :: p call json_parse(file='myfile.json', p=p)
NOTES
When calling this routine, any exceptions thrown from previous calls will automatically be cleared.
HISTORY
Jacob Williams : 1/13/2015 : added read from string option.
SOURCE
4002 subroutine json_parse(file, p, unit, str) 4003 4004 implicit none 4005 4006 character(kind=CK,len=*),intent(in),optional :: file !JSON file name 4007 type(json_value),pointer :: p !output structure 4008 integer(IK),intent(in),optional :: unit !file unit number (/= 0) 4009 character(kind=CK,len=*),intent(in),optional :: str !string with JSON data 4010 4011 integer(IK) :: iunit, istat, i, i_nl_prev, i_nl 4012 character(kind=CK,len=:),allocatable :: line, arrow_str 4013 character(kind=CK,len=10) :: line_str, char_str 4014 logical(LK) :: is_open 4015 character(len=:),allocatable :: buffer 4016 4017 !clear any exceptions and initialize: 4018 call json_initialize() 4019 4020 if (present(unit) .and. present(file) .and. .not. present(str)) then 4021 4022 if (unit==0) then 4023 call throw_exception('Error in json_parse: unit number must not be 0.') 4024 return 4025 end if 4026 4027 iunit = unit 4028 4029 !check to see if the file is already open 4030 ! if it is, then use it, otherwise open the file with the name given. 4031 inquire(unit=iunit, opened=is_open, iostat=istat) 4032 if (istat==0 .and. .not. is_open) then 4033 ! open the file 4034 open ( unit = iunit, & 4035 file = file, & 4036 status = 'OLD', & 4037 action = 'READ', & 4038 form = 'FORMATTED', & 4039 position = 'REWIND', & 4040 iostat = istat) 4041 end if 4042 4043 elseif (.not. present(unit) .and. present(file) .and. .not. present(str)) then 4044 4045 ! open the file with a new unit number: 4046 open ( newunit = iunit, & 4047 file = file, & 4048 status = 'OLD', & 4049 action = 'READ', & 4050 form = 'FORMATTED', & 4051 position = 'REWIND', & 4052 iostat = istat) 4053 4054 elseif (.not. present(unit) .and. .not. present(file) .and. present(str)) then 4055 4056 buffer = str 4057 iunit = 0 !indicates that json data will be read from buffer 4058 istat = 0 4059 4060 else 4061 call throw_exception('Error in json_parse: Invalid inputs') 4062 return 4063 end if 4064 4065 if (istat==0) then 4066 4067 ! create the value and associate the pointer 4068 call json_value_create(p) 4069 4070 ! Note: the name of the root json_value doesn't really matter, 4071 ! but we'll allocate something here just in case. 4072 if (present(file)) then 4073 p%name = trim(file) !use the file name 4074 else 4075 p%name = '' !if reading it from the string 4076 end if 4077 4078 ! parse as a value 4079 call parse_value(unit=iunit, str=buffer, value=p) 4080 4081 ! cleanup: 4082 if (allocated(buffer)) deallocate(buffer) 4083 4084 ! 4085 ! If there was an error reading the file, then 4086 ! print the line where the error occurred: 4087 ! 4088 if (exception_thrown) then 4089 4090 !the counters for the current line and the last character read: 4091 call integer_to_string(line_count, line_str) 4092 call integer_to_string(char_count, char_str) 4093 4094 !draw the arrow string that points to the current character: 4095 arrow_str = repeat('-',max( 0, char_count - 1) )//'^' 4096 4097 if (iunit/=0) then 4098 4099 call get_current_line_from_file(iunit,line) 4100 4101 else 4102 4103 !get the current line from the string: 4104 ! [this is done by counting the newline characters] 4105 i_nl_prev = 0 !index of previous newline character 4106 do i=1,line_count 4107 i_nl = index(str(i_nl_prev+1:),newline) 4108 if (i_nl==0) then !last line - no newline character 4109 i_nl = len(str)+1 4110 exit 4111 end if 4112 i_nl = i_nl + i_nl_prev !index of current newline character 4113 i_nl_prev = i_nl !update for next iteration 4114 end do 4115 line = str(i_nl_prev+1 : i_nl-1) !extract current line 4116 4117 end if 4118 4119 !create the error message: 4120 err_message = err_message//newline//& 4121 'line: '//trim(adjustl(line_str))//', '//& 4122 'character: '//trim(adjustl(char_str))//newline//& 4123 trim(line)//newline//arrow_str 4124 4125 if (allocated(line)) deallocate(line) 4126 4127 end if 4128 4129 ! close the file if necessary 4130 if (iunit/=0) close(unit=iunit, iostat=istat) 4131 4132 else 4133 4134 call throw_exception('Error in json_parse: Error opening file: '//trim(file)) 4135 nullify(p) 4136 4137 end if 4138 4139 end subroutine json_parse
json_module/json_print_1 [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_print_1
DESCRIPTION
Print the JSON structure to a file Input is the nonzero file unit (the file must already have been opened).
AUTHOR
Jacob Williams, 6/20/2014
SOURCE
2644 subroutine json_print_1(me,iunit) 2645 2646 implicit none 2647 2648 type(json_value),pointer,intent(in) :: me 2649 integer(IK),intent(in) :: iunit !must be non-zero 2650 2651 character(kind=CK,len=:),allocatable :: dummy 2652 2653 if (iunit/=0) then 2654 call json_value_print(me,iunit,str=dummy, indent=1, colon=.true.) 2655 else 2656 call throw_exception('Error in json_print: iunit must be nonzero.') 2657 end if 2658 2659 end subroutine json_print_1
json_module/json_print_2 [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_print_2
DESCRIPTION
Print the JSON structure to a file. Input is the filename.
AUTHOR
Jacob Williams, 12/23/2014
SOURCE
2676 subroutine json_print_2(me,filename) 2677 2678 implicit none 2679 2680 type(json_value),pointer,intent(in) :: me 2681 character(kind=CK,len=*),intent(in) :: filename 2682 2683 integer(IK) :: iunit,istat 2684 2685 open(newunit=iunit,file=filename,status='REPLACE',iostat=istat) 2686 if (istat==0) then 2687 call json_print(me,iunit) 2688 close(iunit,iostat=istat) 2689 else 2690 call throw_exception('Error in json_print: could not open file: '//& 2691 trim(filename)) 2692 end if 2693 2694 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
1862 subroutine json_update_double(p,name,val,found) 1863 1864 implicit none 1865 1866 type(json_value),pointer :: p 1867 character(kind=CK,len=*),intent(in) :: name 1868 real(RK),intent(in) :: val 1869 logical(LK),intent(out) :: found 1870 1871 type(json_value),pointer :: p_var 1872 integer(IK) :: var_type 1873 1874 call json_get(p,name,p_var,found) 1875 if (found) then 1876 1877 call json_info(p_var,var_type) 1878 select case (var_type) 1879 case (json_null,json_logical,json_integer,json_double,json_string) 1880 call to_double(p_var,val) !update the value 1881 case default 1882 found = .false. 1883 call throw_exception('Error in json_update_double: '//& 1884 'the variable is not a scalar value') 1885 end select 1886 1887 else 1888 call json_add(p,name,val) !add the new element 1889 end if 1890 1891 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
1910 subroutine json_update_integer(p,name,val,found) 1911 1912 implicit none 1913 1914 type(json_value),pointer :: p 1915 character(kind=CK,len=*),intent(in) :: name 1916 integer(IK),intent(in) :: val 1917 logical(LK),intent(out) :: found 1918 1919 type(json_value),pointer :: p_var 1920 integer(IK) :: var_type 1921 1922 call json_get(p,name,p_var,found) 1923 if (found) then 1924 1925 call json_info(p_var,var_type) 1926 select case (var_type) 1927 case (json_null,json_logical,json_integer,json_double,json_string) 1928 call to_integer(p_var,val) !update the value 1929 case default 1930 found = .false. 1931 call throw_exception('Error in json_update_integer: '//& 1932 'the variable is not a scalar value') 1933 end select 1934 1935 else 1936 call json_add(p,name,val) !add the new element 1937 end if 1938 1939 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
1814 subroutine json_update_logical(p,name,val,found) 1815 1816 implicit none 1817 1818 type(json_value),pointer :: p 1819 character(kind=CK,len=*),intent(in) :: name 1820 logical(LK),intent(in) :: val 1821 logical(LK),intent(out) :: found 1822 1823 type(json_value),pointer :: p_var 1824 integer(IK) :: var_type 1825 1826 call json_get(p,name,p_var,found) 1827 if (found) then 1828 1829 call json_info(p_var,var_type) 1830 select case (var_type) 1831 case (json_null,json_logical,json_integer,json_double,json_string) 1832 call to_logical(p_var,val) !update the value 1833 case default 1834 found = .false. 1835 call throw_exception('Error in json_update_logical: '//& 1836 'the variable is not a scalar value') 1837 end select 1838 1839 else 1840 call json_add(p,name,val) !add the new element 1841 end if 1842 1843 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
1958 subroutine json_update_string(p,name,val,found) 1959 1960 implicit none 1961 1962 type(json_value),pointer :: p 1963 character(kind=CK,len=*),intent(in) :: name 1964 character(kind=CK,len=*),intent(in) :: val 1965 logical(LK),intent(out) :: found 1966 1967 type(json_value),pointer :: p_var 1968 integer(IK) :: var_type 1969 1970 call json_get(p,name,p_var,found) 1971 if (found) then 1972 1973 call json_info(p_var,var_type) 1974 select case (var_type) 1975 case (json_null,json_logical,json_integer,json_double,json_string) 1976 call to_string(p_var,val) !update the value 1977 case default 1978 found = .false. 1979 call throw_exception('Error in json_update_string: '//& 1980 'the variable is not a scalar value') 1981 end select 1982 1983 else 1984 call json_add(p,name,val) !add the new element 1985 end if 1986 1987 end subroutine json_update_string
json_module/json_value_add_double [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_add_double
DESCRIPTION
Add a real value to the structure. These routines are part of the public API that can be used to build a json structure using data.
AUTHOR
Jacob Williams : 1/19/2014
SOURCE
2051 subroutine json_value_add_double(me, name, val) 2052 2053 implicit none 2054 2055 type(json_value),pointer :: me 2056 character(kind=CK,len=*),intent(in) :: name 2057 real(RK),intent(in) :: val 2058 2059 type(json_value),pointer :: var 2060 2061 !create the variable: 2062 call json_value_create(var) 2063 call to_double(var,val,name) 2064 2065 !add it: 2066 call json_add(me, var) 2067 2068 !cleanup: 2069 nullify(var) 2070 2071 end subroutine json_value_add_double
json_module/json_value_add_double_vec [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_add_double_vec
DESCRIPTION
Add a real vector to the structure. These routines are part of the public API that can be used to build a json structure using data.
AUTHOR
Jacob Williams : 1/20/2014
SOURCE
2091 subroutine json_value_add_double_vec(me, name, val) 2092 2093 implicit none 2094 2095 type(json_value),pointer :: me 2096 character(kind=CK,len=*),intent(in) :: name 2097 real(RK),dimension(:),intent(in) :: val 2098 2099 type(json_value),pointer :: var 2100 integer(IK) :: i 2101 2102 !create the variable as an array: 2103 call json_value_create(var) 2104 call to_array(var,name) 2105 2106 !populate the array: 2107 do i=1,size(val) 2108 call json_add(var, '', val(i)) 2109 end do 2110 2111 !add it: 2112 call json_add(me, var) 2113 2114 !cleanup: 2115 nullify(var) 2116 2117 end subroutine json_value_add_double_vec
json_module/json_value_add_integer [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_add_integer
DESCRIPTION
Add an integer value to the structure. These routines are part of the public API that can be used to build a json structure using data.
AUTHOR
Jacob Williams : 1/20/2014
SOURCE
2137 subroutine json_value_add_integer(me, name, val) 2138 2139 implicit none 2140 2141 type(json_value),pointer :: me 2142 character(kind=CK,len=*),intent(in) :: name 2143 integer(IK),intent(in) :: val 2144 2145 type(json_value),pointer :: var 2146 2147 !create the variable: 2148 call json_value_create(var) 2149 call to_integer(var,val,name) 2150 2151 !add it: 2152 call json_add(me, var) 2153 2154 !cleanup: 2155 nullify(var) 2156 2157 end subroutine json_value_add_integer
json_module/json_value_add_integer_vec [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_add_integer_vec
DESCRIPTION
Add an integer vector to the structure. These routines are part of the public API that can be used to build a json structure using data.
AUTHOR
Jacob Williams : 1/20/2014
SOURCE
2177 subroutine json_value_add_integer_vec(me, name, val) 2178 2179 implicit none 2180 2181 type(json_value),pointer :: me 2182 character(kind=CK,len=*),intent(in) :: name 2183 integer(IK),dimension(:),intent(in) :: val 2184 2185 type(json_value),pointer :: var 2186 integer(IK) :: i !counter 2187 2188 !create the variable as an array: 2189 call json_value_create(var) 2190 call to_array(var,name) 2191 2192 !populate the array: 2193 do i=1,size(val) 2194 call json_add(var, '', val(i)) 2195 end do 2196 2197 !add it: 2198 call json_add(me, var) 2199 2200 !cleanup: 2201 nullify(var) 2202 2203 end subroutine json_value_add_integer_vec
json_module/json_value_add_logical [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_add_logical
DESCRIPTION
Add a logical value to the structure. These routines are part of the public API that can be used to build a json structure using data.
AUTHOR
Jacob Williams : 1/20/2014
SOURCE
2223 subroutine json_value_add_logical(me, name, val) 2224 2225 implicit none 2226 2227 type(json_value),pointer :: me 2228 character(kind=CK,len=*),intent(in) :: name 2229 logical(LK),intent(in) :: val 2230 2231 type(json_value),pointer :: var 2232 2233 !create the variable: 2234 call json_value_create(var) 2235 call to_logical(var,val,name) 2236 2237 !add it: 2238 call json_add(me, var) 2239 2240 !cleanup: 2241 nullify(var) 2242 2243 end subroutine json_value_add_logical
json_module/json_value_add_logical_vec [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_add_logical_vec
DESCRIPTION
Add a logical vector to the structure. These routines are part of the public API that can be used to build a json structure using data.
AUTHOR
Jacob Williams : 1/20/2014
SOURCE
2263 subroutine json_value_add_logical_vec(me, name, val) 2264 2265 implicit none 2266 2267 type(json_value),pointer :: me 2268 character(kind=CK,len=*),intent(in) :: name 2269 logical(LK),dimension(:),intent(in) :: val 2270 2271 type(json_value),pointer :: var 2272 integer(IK) :: i !counter 2273 2274 !create the variable as an array: 2275 call json_value_create(var) 2276 call to_array(var,name) 2277 2278 !populate the array: 2279 do i=1,size(val) 2280 call json_add(var, '', val(i)) 2281 end do 2282 2283 !add it: 2284 call json_add(me, var) 2285 2286 !cleanup: 2287 nullify(var) 2288 2289 end subroutine json_value_add_logical_vec
json_module/json_value_add_member [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_add_member
DESCRIPTION
Adds the member as a child of this.
SOURCE
2001 subroutine json_value_add_member(this, member) 2002 2003 implicit none 2004 2005 type(json_value),pointer :: this, member 2006 2007 if (.not. exception_thrown) then 2008 2009 ! associate the parent 2010 member%parent => this 2011 2012 ! add to linked list 2013 if (associated(this%children)) then 2014 2015 this%tail%next => member 2016 member%previous => this%tail 2017 2018 else 2019 2020 this%children => member 2021 member%previous => null() !first in the list 2022 2023 end if 2024 2025 ! new member is now the last one in the list 2026 this%tail => member 2027 this%n_children = this%n_children + 1 2028 2029 end if 2030 2031 end subroutine json_value_add_member
json_module/json_value_add_string [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_add_string
DESCRIPTION
Add a character string the structure. These routines are part of the public API that can be used to build a json structure using data.
AUTHOR
Jacob Williams : 1/19/2014
SOURCE
2309 subroutine json_value_add_string(me, name, val) 2310 2311 implicit none 2312 2313 type(json_value),pointer :: me 2314 character(kind=CK,len=*),intent(in) :: name 2315 character(kind=CK,len=*),intent(in) :: val 2316 2317 type(json_value),pointer :: var 2318 character(kind=CK,len=:),allocatable :: str 2319 2320 !add escape characters if necessary: 2321 call escape_string(val, str) 2322 2323 !create the variable: 2324 call json_value_create(var) 2325 call to_string(var,str,name) 2326 2327 !add it: 2328 call json_add(me, var) 2329 2330 !cleanup: 2331 nullify(var) 2332 2333 end subroutine json_value_add_string
json_module/json_value_add_string_vec [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_add_string_vec
DESCRIPTION
Add an array of character strings to the structure. These routines are part of the public API that can be used to build a json structure using data.
AUTHOR
Jacob Williams : 1/19/2014
SOURCE
2406 subroutine json_value_add_string_vec(me, name, val, trim_str, adjustl_str) 2407 2408 implicit none 2409 2410 type(json_value),pointer :: me 2411 character(kind=CK,len=*),intent(in) :: name 2412 character(kind=CK,len=*),dimension(:),intent(in) :: val 2413 logical(LK),intent(in),optional :: trim_str 2414 logical(LK),intent(in),optional :: adjustl_str 2415 2416 type(json_value),pointer :: var 2417 integer(IK) :: i 2418 logical(LK) :: trim_string, adjustl_string 2419 character(kind=CK,len=:),allocatable :: str 2420 2421 !if the string is to be trimmed or not: 2422 if (present(trim_str)) then 2423 trim_string = trim_str 2424 else 2425 trim_string = .false. 2426 end if 2427 if (present(adjustl_str)) then 2428 adjustl_string = adjustl_str 2429 else 2430 adjustl_string = .false. 2431 end if 2432 2433 !create the variable as an array: 2434 call json_value_create(var) 2435 call to_array(var,name) 2436 2437 !populate the array: 2438 do i=1,size(val) 2439 2440 !the string to write: 2441 str = val(i) 2442 if (adjustl_string) str = adjustl(str) 2443 if (trim_string) str = trim(str) 2444 2445 !write it: 2446 call json_add(var, '', str) 2447 2448 !cleanup 2449 deallocate(str) 2450 2451 end do 2452 2453 !add it: 2454 call json_add(me, var) 2455 2456 !cleanup: 2457 nullify(var) 2458 2459 end subroutine json_value_add_string_vec
json_module/json_value_destroy [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_destroy
DESCRIPTION
Destroy a json_value linked-list structure.
NOTES
This routine does not check for exceptions. I believe the original version of this routine was not properly freeing the memory. It has been rewritten.
AUTHOR
Jacob Williams : 1/22/2014
SOURCE
1504 recursive subroutine json_value_destroy(this) 1505 1506 implicit none 1507 1508 type(json_value),pointer :: this 1509 1510 if (associated(this)) then 1511 1512 if (allocated(this%name)) deallocate(this%name) 1513 1514 call destroy_json_data(this) 1515 1516 if (associated(this%children)) call json_value_destroy(this%children) 1517 this%n_children = 0 1518 1519 if (associated(this%next)) call json_value_destroy(this%next) 1520 1521 if (associated(this%previous)) nullify(this%previous) 1522 if (associated(this%parent)) nullify(this%parent) 1523 if (associated(this%tail)) nullify(this%tail) 1524 1525 deallocate(this) 1526 1527 nullify(this) 1528 1529 end if 1530 1531 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
2501 subroutine json_value_get_by_index(this, idx, p) 2502 2503 implicit none 2504 2505 type(json_value),pointer,intent(in) :: this 2506 integer(IK),intent(in) :: idx 2507 type(json_value),pointer :: p 2508 2509 integer(IK) :: i 2510 2511 nullify(p) 2512 2513 if (.not. exception_thrown) then 2514 2515 if (associated(this%children)) then 2516 2517 p => this%children 2518 2519 do i = 1, idx - 1 2520 2521 if (associated(p%next)) then 2522 p => p%next 2523 else 2524 call throw_exception('Error in json_value_get_by_index:'//& 2525 ' p%next is not associated.') 2526 nullify(p) 2527 return 2528 end if 2529 2530 end do 2531 2532 else 2533 2534 call throw_exception('Error in json_value_get_by_index:'//& 2535 ' this%children is not associated.') 2536 2537 end if 2538 2539 end if 2540 2541 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
2560 subroutine json_value_get_by_name_chars(this, name, p) 2561 2562 implicit none 2563 2564 type(json_value),pointer,intent(in) :: this 2565 character(kind=CK,len=*),intent(in) :: name 2566 type(json_value),pointer :: p 2567 2568 integer(IK) :: i,n_children 2569 2570 nullify(p) 2571 2572 if (.not. exception_thrown) then 2573 2574 if (associated(this)) then 2575 2576 if (this%var_type==json_object) then 2577 n_children = json_count(this) 2578 p => this%children !start with first one 2579 do i=1, n_children 2580 if (allocated(p%name)) then 2581 if (p%name == name) return 2582 end if 2583 p => p%next 2584 end do 2585 end if 2586 2587 !did not find anything: 2588 call throw_exception('Error in json_value_get_by_name_chars: '//& 2589 'child variable '//trim(name)//' was not found.') 2590 nullify(p) 2591 2592 else 2593 call throw_exception('Error in json_value_get_by_name_chars: '//& 2594 'pointer is not associated.') 2595 end if 2596 2597 end if 2598 2599 end subroutine json_value_get_by_name_chars
json_module/json_value_remove [ Functions ]
[ Top ] [ json_module ] [ Functions ]
NAME
json_value_remove
DESCRIPTION
Remove a json_value (and all its children) from a linked-list structure, preserving the rest of the structure. If destroy is not present, it is also destroyed. If destroy is present and true, it is destroyed. If destroy is present and false, it is not destroyed.
EXAMPLE
!to extract an object from one json structure, and add it to another: type(json_value),pointer :: json1,json2,p logical :: found [create and populate json1 and json2] call json_get(json1,'name',p,found) ! get pointer to name element of json1 call json_remove(p,destroy=.false.) ! remove it from json1 (don't destroy) call json_add(json2,p) ! add it to json2 !to remove an object from a json structure (and destroy it) type(json_value),pointer :: json1,p logical :: found [create and populate json1] call json_get(json1,'name',p,found) ! get pointer to name element of json1 call json_remove(p) ! remove and destroy it
AUTHOR
Jacob Williams : 9/9/2014
HISTORY
JW : 12/28/2014 : added destroy optional argument.
SOURCE
1572 subroutine json_value_remove(me,destroy) 1573 1574 implicit none 1575 1576 type(json_value),pointer :: me 1577 logical(LK),intent(in),optional :: destroy 1578 1579 type(json_value),pointer :: parent,previous,next 1580 logical(LK) :: destroy_it 1581 1582 if (associated(me)) then 1583 1584 !optional input argument: 1585 if (present(destroy)) then 1586 destroy_it = destroy 1587 else 1588 destroy_it = .true. 1589 end if 1590 1591 if (associated(me%parent)) then 1592 1593 parent => me%parent 1594 1595 if (associated(me%next)) then 1596 1597 !there are later items in the list: 1598 1599 next => me%next 1600 nullify(me%next) 1601 1602 if (associated(me%previous)) then 1603 !there are earlier items in the list 1604 previous => me%previous 1605 previous%next => next 1606 next%previous => previous 1607 else 1608 !this is the first item in the list 1609 parent%children => next 1610 nullify(next%previous) 1611 end if 1612 1613 else 1614 1615 if (associated(me%previous)) then 1616 !there are earlier items in the list: 1617 previous => me%previous 1618 nullify(previous%next) 1619 parent%tail => previous 1620 else 1621 !this is the only item in the list: 1622 nullify(parent%children) 1623 nullify(parent%tail) 1624 end if 1625 1626 end if 1627 1628 parent%n_children = parent%n_children - 1 1629 1630 end if 1631 1632 if (destroy_it) call json_value_destroy(me) 1633 1634 end if 1635 1636 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
1654 subroutine json_value_remove_if_present(p,name) 1655 1656 implicit none 1657 1658 type(json_value),pointer :: p 1659 character(kind=CK,len=*),intent(in) :: name 1660 1661 type(json_value),pointer :: p_var 1662 logical(LK) :: found 1663 1664 call json_get(p,name,p_var,found) 1665 if (found) call json_remove(p_var) 1666 1667 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
2616 subroutine json_value_to_string(me,str) 2617 2618 implicit none 2619 2620 type(json_value),pointer,intent(in) :: me 2621 character(kind=CK,len=:),intent(out),allocatable :: str 2622 2623 str = '' 2624 call json_value_print(me, iunit=0, str=str, indent=1, colon=.true.) 2625 2626 end subroutine json_value_to_string