| 1 |
!----------------------------------------------------------------- |
| 2 |
! |
| 3 |
! This file is (or was) part of SPLASH, a visualisation tool |
| 4 |
! for Smoothed Particle Hydrodynamics written by Daniel Price: |
| 5 |
! |
| 6 |
! http://users.monash.edu.au/~dprice/splash |
| 7 |
! |
| 8 |
! SPLASH comes with ABSOLUTELY NO WARRANTY. |
| 9 |
! This is free software; and you are welcome to redistribute |
| 10 |
! it under the terms of the GNU General Public License |
| 11 |
! (see LICENSE file for details) and the provision that |
| 12 |
! this notice remains intact. If you modify this file, please |
| 13 |
! note section 2a) of the GPLv2 states that: |
| 14 |
! |
| 15 |
! a) You must cause the modified files to carry prominent notices |
| 16 |
! stating that you changed the files and the date of any change. |
| 17 |
! |
| 18 |
! Copyright (C) 2005-2025 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
!----------------------------------------------------------------- |
| 24 |
! |
| 25 |
! Helper utilities for reading simple JSON files in SPLASH |
| 26 |
! |
| 27 |
!----------------------------------------------------------------- |
| 28 |
module json_utils |
| 29 |
use iso_fortran_env, only:int64 |
| 30 |
use params, only:doub_prec |
| 31 |
implicit none |
| 32 |
private |
| 33 |
|
| 34 |
integer, parameter, public :: json_kind_unknown = 0 |
| 35 |
integer, parameter, public :: json_kind_object = 1 |
| 36 |
integer, parameter, public :: json_kind_array = 2 |
| 37 |
integer, parameter, public :: json_kind_string = 3 |
| 38 |
integer, parameter, public :: json_kind_number = 4 |
| 39 |
integer, parameter, public :: json_kind_boolean = 5 |
| 40 |
integer, parameter, public :: json_kind_null = 6 |
| 41 |
|
| 42 |
integer, parameter, public :: json_success = 0 |
| 43 |
integer, parameter, public :: json_err_not_found = 1 |
| 44 |
integer, parameter, public :: json_err_parse = 2 |
| 45 |
integer, parameter, public :: json_err_type = 3 |
| 46 |
integer, parameter, public :: json_err_io = 4 |
| 47 |
|
| 48 |
type :: json_path_token |
| 49 |
character(:), allocatable :: key |
| 50 |
logical :: has_index = .false. |
| 51 |
integer :: index = -1 |
| 52 |
end type json_path_token |
| 53 |
|
| 54 |
public :: json_read_file |
| 55 |
public :: json_get_raw_value |
| 56 |
public :: json_get_string_or_array |
| 57 |
public :: json_get_real |
| 58 |
public :: json_get_integer |
| 59 |
public :: json_get_logical |
| 60 |
public :: json_get_value_by_path |
| 61 |
public :: json_array_get_element |
| 62 |
public :: json_array_length |
| 63 |
public :: json_read |
| 64 |
|
| 65 |
interface json_read |
| 66 |
module procedure json_get_string_or_array, json_get_real, json_get_integer, json_get_logical,& |
| 67 |
json_get_value_by_path, json_get_array_int64, json_get_array_int |
| 68 |
end interface json_read |
| 69 |
|
| 70 |
contains |
| 71 |
|
| 72 |
!----------------------------------------------------------------- |
| 73 |
! read a JSON file into a string |
| 74 |
!----------------------------------------------------------------- |
| 75 |
subroutine json_read_file(filename, content, ierr) |
| 76 |
character(len=*), intent(in) :: filename |
| 77 |
character(:), allocatable, intent(out) :: content |
| 78 |
integer, intent(out) :: ierr |
| 79 |
integer :: iu, ios |
| 80 |
logical :: exists |
| 81 |
integer(int64) :: file_size |
| 82 |
|
| 83 |
inquire(file=filename, exist=exists) |
| 84 |
if (.not. exists) then |
| 85 |
ierr = json_err_io |
| 86 |
allocate(character(len=0) :: content) |
| 87 |
return |
| 88 |
endif |
| 89 |
|
| 90 |
open(newunit=iu,file=trim(filename),status='old',action='read',access='stream', & |
| 91 |
form='unformatted',iostat=ios) |
| 92 |
if (ios /= 0) then |
| 93 |
ierr = json_err_io |
| 94 |
allocate(character(len=0) :: content) |
| 95 |
return |
| 96 |
endif |
| 97 |
|
| 98 |
inquire(unit=iu, size=file_size, iostat=ios) |
| 99 |
if (ios /= 0 .or. file_size < 0_int64) then |
| 100 |
close(iu) |
| 101 |
ierr = json_err_io |
| 102 |
allocate(character(len=0) :: content) |
| 103 |
return |
| 104 |
endif |
| 105 |
|
| 106 |
if (file_size == 0_int64) then |
| 107 |
allocate(character(len=0) :: content) |
| 108 |
else |
| 109 |
allocate(character(len=int(file_size)) :: content) |
| 110 |
read(iu,iostat=ios) content |
| 111 |
if (ios /= 0) then |
| 112 |
close(iu) |
| 113 |
ierr = json_err_io |
| 114 |
deallocate(content) |
| 115 |
allocate(character(len=0) :: content) |
| 116 |
return |
| 117 |
endif |
| 118 |
endif |
| 119 |
|
| 120 |
close(iu) |
| 121 |
ierr = json_success |
| 122 |
|
| 123 |
end subroutine json_read_file |
| 124 |
|
| 125 |
!----------------------------------------------------------------- |
| 126 |
! get a string value from a JSON text |
| 127 |
!----------------------------------------------------------------- |
| 128 |
subroutine json_get_string_or_array(json_text, key, value, ierr) |
| 129 |
character(len=*), intent(in) :: json_text |
| 130 |
character(len=*), intent(in) :: key |
| 131 |
character(:), allocatable, intent(out) :: value |
| 132 |
integer, intent(out) :: ierr |
| 133 |
integer :: kind |
| 134 |
|
| 135 |
call json_get_raw_value(json_text,key,value,kind,ierr) |
| 136 |
if (ierr /= json_success) return |
| 137 |
|
| 138 |
if (kind /= json_kind_string .and. kind /= json_kind_array) then |
| 139 |
if (allocated(value)) deallocate(value) |
| 140 |
ierr = json_err_type |
| 141 |
allocate(character(len=0) :: value) |
| 142 |
else |
| 143 |
ierr = json_success |
| 144 |
endif |
| 145 |
|
| 146 |
end subroutine json_get_string_or_array |
| 147 |
|
| 148 |
!----------------------------------------------------------------- |
| 149 |
! get a real value from a JSON text |
| 150 |
!----------------------------------------------------------------- |
| 151 |
subroutine json_get_real(json_text, key, value, ierr) |
| 152 |
character(len=*), intent(in) :: json_text |
| 153 |
character(len=*), intent(in) :: key |
| 154 |
real(kind=doub_prec), intent(out) :: value |
| 155 |
integer, intent(out) :: ierr |
| 156 |
character(:), allocatable :: raw |
| 157 |
integer :: kind |
| 158 |
|
| 159 |
call json_get_raw_value(json_text,key,raw,kind,ierr) |
| 160 |
if (ierr /= json_success) return |
| 161 |
|
| 162 |
if (kind /= json_kind_number) then |
| 163 |
ierr = json_err_type |
| 164 |
value = 0.0d0 |
| 165 |
if (allocated(raw)) deallocate(raw) |
| 166 |
return |
| 167 |
endif |
| 168 |
|
| 169 |
read(raw,*,iostat=ierr) value |
| 170 |
if (ierr /= 0) then |
| 171 |
ierr = json_err_parse |
| 172 |
value = 0.0d0 |
| 173 |
else |
| 174 |
ierr = json_success |
| 175 |
endif |
| 176 |
if (allocated(raw)) deallocate(raw) |
| 177 |
|
| 178 |
end subroutine json_get_real |
| 179 |
|
| 180 |
!----------------------------------------------------------------- |
| 181 |
! get an integer value from a JSON text |
| 182 |
!----------------------------------------------------------------- |
| 183 |
subroutine json_get_integer(json_text, key, value, ierr) |
| 184 |
character(len=*), intent(in) :: json_text |
| 185 |
character(len=*), intent(in) :: key |
| 186 |
integer, intent(out) :: value |
| 187 |
integer, intent(out) :: ierr |
| 188 |
character(:), allocatable :: raw |
| 189 |
integer :: kind |
| 190 |
|
| 191 |
call json_get_raw_value(json_text,key,raw,kind,ierr) |
| 192 |
if (ierr /= json_success) return |
| 193 |
|
| 194 |
if (kind /= json_kind_number) then |
| 195 |
ierr = json_err_type |
| 196 |
value = 0 |
| 197 |
if (allocated(raw)) deallocate(raw) |
| 198 |
return |
| 199 |
endif |
| 200 |
|
| 201 |
read(raw,*,iostat=ierr) value |
| 202 |
if (ierr /= 0) then |
| 203 |
ierr = json_err_parse |
| 204 |
value = 0 |
| 205 |
else |
| 206 |
ierr = json_success |
| 207 |
endif |
| 208 |
if (allocated(raw)) deallocate(raw) |
| 209 |
|
| 210 |
end subroutine json_get_integer |
| 211 |
|
| 212 |
!----------------------------------------------------------------- |
| 213 |
! get an integer(kind=int64) array from a JSON text |
| 214 |
!----------------------------------------------------------------- |
| 215 |
subroutine json_get_array_int64(json_text, key, values, ierr) |
| 216 |
character(len=*), intent(in) :: json_text |
| 217 |
character(len=*), intent(in) :: key |
| 218 |
integer(kind=int64), allocatable, intent(out) :: values(:) |
| 219 |
integer, intent(out) :: ierr |
| 220 |
character(:), allocatable :: raw |
| 221 |
integer :: kind, parse_err |
| 222 |
|
| 223 |
ierr = json_success |
| 224 |
call json_get_raw_value(json_text, key, raw, kind, ierr) |
| 225 |
if (ierr /= json_success) then |
| 226 |
allocate(values(0)) |
| 227 |
return |
| 228 |
endif |
| 229 |
|
| 230 |
if (kind /= json_kind_array) then |
| 231 |
ierr = json_err_type |
| 232 |
allocate(values(0)) |
| 233 |
if (allocated(raw)) deallocate(raw) |
| 234 |
return |
| 235 |
endif |
| 236 |
|
| 237 |
call parse_int64_array(raw, values, parse_err) |
| 238 |
if (allocated(raw)) deallocate(raw) |
| 239 |
if (parse_err /= 0) then |
| 240 |
ierr = json_err_parse |
| 241 |
if (.not. allocated(values)) allocate(values(0)) |
| 242 |
endif |
| 243 |
|
| 244 |
end subroutine json_get_array_int64 |
| 245 |
|
| 246 |
!----------------------------------------------------------------- |
| 247 |
! get an integer array from a JSON text |
| 248 |
!----------------------------------------------------------------- |
| 249 |
subroutine json_get_array_int(json_text, key, values, ierr) |
| 250 |
character(len=*), intent(in) :: json_text |
| 251 |
character(len=*), intent(in) :: key |
| 252 |
integer, allocatable, intent(out) :: values(:) |
| 253 |
integer, intent(out) :: ierr |
| 254 |
character(:), allocatable :: raw |
| 255 |
integer :: kind, parse_err |
| 256 |
|
| 257 |
ierr = json_success |
| 258 |
call json_get_raw_value(json_text, key, raw, kind, ierr) |
| 259 |
if (ierr /= json_success) then |
| 260 |
allocate(values(0)) |
| 261 |
return |
| 262 |
endif |
| 263 |
|
| 264 |
if (kind /= json_kind_array) then |
| 265 |
ierr = json_err_type |
| 266 |
allocate(values(0)) |
| 267 |
if (allocated(raw)) deallocate(raw) |
| 268 |
return |
| 269 |
endif |
| 270 |
|
| 271 |
call parse_int_array(raw, values, parse_err) |
| 272 |
if (allocated(raw)) deallocate(raw) |
| 273 |
if (parse_err /= 0) then |
| 274 |
ierr = json_err_parse |
| 275 |
if (.not. allocated(values)) allocate(values(0)) |
| 276 |
endif |
| 277 |
|
| 278 |
end subroutine json_get_array_int |
| 279 |
|
| 280 |
!----------------------------------------------------------------- |
| 281 |
! get a logical value from a JSON text |
| 282 |
!----------------------------------------------------------------- |
| 283 |
subroutine json_get_logical(json_text, key, value, ierr) |
| 284 |
character(len=*), intent(in) :: json_text |
| 285 |
character(len=*), intent(in) :: key |
| 286 |
logical, intent(out) :: value |
| 287 |
integer, intent(out) :: ierr |
| 288 |
character(:), allocatable :: raw |
| 289 |
integer :: kind |
| 290 |
|
| 291 |
call json_get_raw_value(json_text,key,raw,kind,ierr) |
| 292 |
if (ierr /= json_success) return |
| 293 |
|
| 294 |
if (kind /= json_kind_boolean) then |
| 295 |
ierr = json_err_type |
| 296 |
value = .false. |
| 297 |
if (allocated(raw)) deallocate(raw) |
| 298 |
return |
| 299 |
endif |
| 300 |
|
| 301 |
select case (trim(raw)) |
| 302 |
case('true') |
| 303 |
value = .true. |
| 304 |
ierr = json_success |
| 305 |
case('false') |
| 306 |
value = .false. |
| 307 |
ierr = json_success |
| 308 |
case default |
| 309 |
value = .false. |
| 310 |
ierr = json_err_parse |
| 311 |
end select |
| 312 |
if (allocated(raw)) deallocate(raw) |
| 313 |
|
| 314 |
end subroutine json_get_logical |
| 315 |
|
| 316 |
!----------------------------------------------------------------- |
| 317 |
! get a value from a JSON text by specified path |
| 318 |
!----------------------------------------------------------------- |
| 319 |
subroutine json_get_value_by_path(json_text, path, value_text, kind, ierr) |
| 320 |
character(len=*), intent(in) :: json_text |
| 321 |
character(len=*), intent(in) :: path |
| 322 |
character(:), allocatable, intent(out) :: value_text |
| 323 |
integer, intent(out) :: kind |
| 324 |
integer, intent(out) :: ierr |
| 325 |
type(json_path_token), allocatable :: tokens(:) |
| 326 |
character(:), allocatable :: current |
| 327 |
character(:), allocatable :: raw |
| 328 |
integer :: current_kind |
| 329 |
integer :: i, ntokens |
| 330 |
|
| 331 |
ierr = json_success |
| 332 |
current_kind = detect_value_kind(json_text) |
| 333 |
current = json_text |
| 334 |
|
| 335 |
call parse_path(path,tokens,ntokens,ierr) |
| 336 |
if (ierr /= json_success) then |
| 337 |
if (allocated(current)) deallocate(current) |
| 338 |
allocate(character(len=0) :: value_text) |
| 339 |
kind = json_kind_unknown |
| 340 |
return |
| 341 |
endif |
| 342 |
|
| 343 |
do i = 1, ntokens |
| 344 |
if (len_trim(tokens(i)%key) > 0) then |
| 345 |
call json_get_raw_value(current,trim(tokens(i)%key),raw,kind,ierr) |
| 346 |
if (ierr /= json_success) exit |
| 347 |
else |
| 348 |
raw = current |
| 349 |
kind = current_kind |
| 350 |
endif |
| 351 |
|
| 352 |
if (tokens(i)%has_index) then |
| 353 |
call json_array_get_element(raw,tokens(i)%index,current,current_kind,ierr) |
| 354 |
if (allocated(raw)) deallocate(raw) |
| 355 |
if (ierr /= json_success) exit |
| 356 |
else |
| 357 |
if (allocated(current)) deallocate(current) |
| 358 |
call move_alloc(raw,current) |
| 359 |
current_kind = kind |
| 360 |
endif |
| 361 |
enddo |
| 362 |
|
| 363 |
if (allocated(raw)) then |
| 364 |
deallocate(raw) |
| 365 |
end if |
| 366 |
|
| 367 |
if (ierr == json_success) then |
| 368 |
kind = current_kind |
| 369 |
call move_alloc(current,value_text) |
| 370 |
else |
| 371 |
if (allocated(current)) deallocate(current) |
| 372 |
allocate(character(len=0) :: value_text) |
| 373 |
end if |
| 374 |
|
| 375 |
if (allocated(tokens)) deallocate(tokens) |
| 376 |
|
| 377 |
end subroutine json_get_value_by_path |
| 378 |
|
| 379 |
!----------------------------------------------------------------- |
| 380 |
! get an element from a JSON array by giving the index |
| 381 |
!----------------------------------------------------------------- |
| 382 |
subroutine json_array_get_element(array_text, index, value_text, kind, ierr) |
| 383 |
character(len=*), intent(in) :: array_text |
| 384 |
integer, intent(in) :: index |
| 385 |
character(:), allocatable, intent(out) :: value_text |
| 386 |
integer, intent(out) :: kind |
| 387 |
integer, intent(out) :: ierr |
| 388 |
integer :: len_arr, i, depth, current_index |
| 389 |
integer :: start_pos, end_pos |
| 390 |
logical :: in_string, escaped |
| 391 |
character :: ch |
| 392 |
|
| 393 |
ierr = json_success |
| 394 |
kind = json_kind_unknown |
| 395 |
if (index < 0) then |
| 396 |
ierr = json_err_not_found |
| 397 |
allocate(character(len=0) :: value_text) |
| 398 |
return |
| 399 |
endif |
| 400 |
|
| 401 |
len_arr = len_trim(array_text) |
| 402 |
if (len_arr < 2) then |
| 403 |
ierr = json_err_parse |
| 404 |
allocate(character(len=0) :: value_text) |
| 405 |
return |
| 406 |
endif |
| 407 |
if (array_text(1:1) /= '[') then |
| 408 |
ierr = json_err_type |
| 409 |
allocate(character(len=0) :: value_text) |
| 410 |
return |
| 411 |
endif |
| 412 |
|
| 413 |
in_string = .false. |
| 414 |
escaped = .false. |
| 415 |
depth = 0 |
| 416 |
current_index = -1 |
| 417 |
start_pos = 0 |
| 418 |
end_pos = 0 |
| 419 |
|
| 420 |
do i = 2, len_arr |
| 421 |
if (start_pos == 0) then |
| 422 |
if (.not. is_whitespace_char(array_text(i:i))) start_pos = i |
| 423 |
endif |
| 424 |
|
| 425 |
ch = array_text(i:i) |
| 426 |
if (in_string) then |
| 427 |
if (escaped) then |
| 428 |
escaped = .false. |
| 429 |
else if (ch == '\') then |
| 430 |
escaped = .true. |
| 431 |
else if (ch == '"') then |
| 432 |
in_string = .false. |
| 433 |
endif |
| 434 |
else |
| 435 |
select case (ch) |
| 436 |
case('"') |
| 437 |
in_string = .true. |
| 438 |
case('[') |
| 439 |
depth = depth + 1 |
| 440 |
case('{') |
| 441 |
depth = depth + 1 |
| 442 |
case(']') |
| 443 |
if (depth == 0) then |
| 444 |
if (start_pos /= 0) then |
| 445 |
end_pos = i - 1 |
| 446 |
current_index = current_index + 1 |
| 447 |
if (current_index == index) exit |
| 448 |
start_pos = 0 |
| 449 |
endif |
| 450 |
exit |
| 451 |
else |
| 452 |
depth = depth - 1 |
| 453 |
endif |
| 454 |
case('}') |
| 455 |
depth = depth - 1 |
| 456 |
case(',') |
| 457 |
if (depth == 0) then |
| 458 |
if (start_pos /= 0) then |
| 459 |
end_pos = i - 1 |
| 460 |
current_index = current_index + 1 |
| 461 |
if (current_index == index) exit |
| 462 |
start_pos = 0 |
| 463 |
endif |
| 464 |
endif |
| 465 |
end select |
| 466 |
endif |
| 467 |
enddo |
| 468 |
|
| 469 |
if (current_index /= index) then |
| 470 |
if (end_pos == 0 .and. start_pos /= 0) then |
| 471 |
end_pos = len_arr - 1 |
| 472 |
current_index = current_index + 1 |
| 473 |
endif |
| 474 |
endif |
| 475 |
|
| 476 |
if (current_index /= index .or. start_pos == 0) then |
| 477 |
ierr = json_err_not_found |
| 478 |
allocate(character(len=0) :: value_text) |
| 479 |
kind = json_kind_unknown |
| 480 |
return |
| 481 |
endif |
| 482 |
|
| 483 |
call extract_trimmed_span(array_text,start_pos,end_pos,value_text) |
| 484 |
kind = detect_value_kind(value_text) |
| 485 |
|
| 486 |
end subroutine json_array_get_element |
| 487 |
|
| 488 |
!----------------------------------------------------------------- |
| 489 |
! get the number of items in a JSON array |
| 490 |
!----------------------------------------------------------------- |
| 491 |
subroutine json_array_length(array_text, nitems, ierr) |
| 492 |
character(len=*), intent(in) :: array_text |
| 493 |
integer, intent(out) :: nitems |
| 494 |
integer, intent(out) :: ierr |
| 495 |
integer :: len_arr, i, depth |
| 496 |
logical :: in_string, escaped |
| 497 |
character :: ch |
| 498 |
logical :: expecting_value |
| 499 |
|
| 500 |
ierr = json_success |
| 501 |
nitems = 0 |
| 502 |
|
| 503 |
len_arr = len_trim(array_text) |
| 504 |
if (len_arr < 2 .or. array_text(1:1) /= '[') then |
| 505 |
ierr = json_err_type |
| 506 |
return |
| 507 |
endif |
| 508 |
|
| 509 |
in_string = .false. |
| 510 |
escaped = .false. |
| 511 |
depth = 0 |
| 512 |
expecting_value = .true. |
| 513 |
|
| 514 |
do i = 2, len_arr |
| 515 |
ch = array_text(i:i) |
| 516 |
if (in_string) then |
| 517 |
if (escaped) then |
| 518 |
escaped = .false. |
| 519 |
else if (ch == '\') then |
| 520 |
escaped = .true. |
| 521 |
else if (ch == '"') then |
| 522 |
in_string = .false. |
| 523 |
endif |
| 524 |
cycle |
| 525 |
endif |
| 526 |
|
| 527 |
select case (ch) |
| 528 |
case('"') |
| 529 |
in_string = .true. |
| 530 |
if (depth == 0 .and. expecting_value) then |
| 531 |
nitems = nitems + 1 |
| 532 |
expecting_value = .false. |
| 533 |
endif |
| 534 |
case('[','{') |
| 535 |
if (depth == 0 .and. expecting_value) then |
| 536 |
nitems = nitems + 1 |
| 537 |
expecting_value = .false. |
| 538 |
endif |
| 539 |
depth = depth + 1 |
| 540 |
case(']', '}') |
| 541 |
depth = depth - 1 |
| 542 |
case(',') |
| 543 |
if (depth == 0) expecting_value = .true. |
| 544 |
case default |
| 545 |
if (depth == 0 .and. expecting_value .and. .not. is_whitespace_char(ch)) then |
| 546 |
nitems = nitems + 1 |
| 547 |
expecting_value = .false. |
| 548 |
endif |
| 549 |
end select |
| 550 |
enddo |
| 551 |
|
| 552 |
end subroutine json_array_length |
| 553 |
|
| 554 |
!----------------------------------------------------------------- |
| 555 |
! get the raw value from a JSON key : value pair |
| 556 |
!----------------------------------------------------------------- |
| 557 |
subroutine json_get_raw_value(json_text, key, raw_value, kind, ierr) |
| 558 |
character(len=*), intent(in) :: json_text |
| 559 |
character(len=*), intent(in) :: key |
| 560 |
character(:), allocatable, intent(out) :: raw_value |
| 561 |
integer, intent(out) :: kind |
| 562 |
integer, intent(out) :: ierr |
| 563 |
integer :: key_pos, colon_pos, value_start, value_end |
| 564 |
character :: first_char |
| 565 |
character(:), allocatable :: string_value |
| 566 |
|
| 567 |
ierr = json_success |
| 568 |
kind = json_kind_unknown |
| 569 |
key_pos = find_key_position(json_text,trim(key)) |
| 570 |
if (key_pos <= 0) then |
| 571 |
ierr = json_err_not_found |
| 572 |
allocate(character(len=0) :: raw_value) |
| 573 |
return |
| 574 |
endif |
| 575 |
|
| 576 |
colon_pos = find_colon_after(json_text,key_pos,len_trim(key)) |
| 577 |
if (colon_pos <= 0) then |
| 578 |
ierr = json_err_parse |
| 579 |
allocate(character(len=0) :: raw_value) |
| 580 |
return |
| 581 |
endif |
| 582 |
|
| 583 |
value_start = colon_pos + 1 |
| 584 |
call skip_whitespace(json_text,value_start) |
| 585 |
if (value_start > len(json_text)) then |
| 586 |
ierr = json_err_parse |
| 587 |
allocate(character(len=0) :: raw_value) |
| 588 |
return |
| 589 |
endif |
| 590 |
|
| 591 |
first_char = json_text(value_start:value_start) |
| 592 |
select case (first_char) |
| 593 |
case('"') |
| 594 |
value_end = value_start |
| 595 |
call parse_string(json_text,value_start,string_value,ierr) |
| 596 |
if (ierr /= json_success) then |
| 597 |
allocate(character(len=0) :: raw_value) |
| 598 |
return |
| 599 |
endif |
| 600 |
call move_alloc(string_value,raw_value) |
| 601 |
kind = json_kind_string |
| 602 |
return |
| 603 |
case('{') |
| 604 |
value_end = find_matching_delimiter(json_text,value_start,'{','}') |
| 605 |
if (value_end <= 0) then |
| 606 |
ierr = json_err_parse |
| 607 |
allocate(character(len=0) :: raw_value) |
| 608 |
return |
| 609 |
endif |
| 610 |
call extract_trimmed_span(json_text,value_start,value_end,raw_value) |
| 611 |
kind = json_kind_object |
| 612 |
return |
| 613 |
case('[') |
| 614 |
value_end = find_matching_delimiter(json_text,value_start,'[',']') |
| 615 |
if (value_end <= 0) then |
| 616 |
ierr = json_err_parse |
| 617 |
allocate(character(len=0) :: raw_value) |
| 618 |
return |
| 619 |
endif |
| 620 |
call extract_trimmed_span(json_text,value_start,value_end,raw_value) |
| 621 |
kind = json_kind_array |
| 622 |
return |
| 623 |
case('t','f') |
| 624 |
call parse_literal(json_text,value_start,raw_value,value_end,ierr) |
| 625 |
if (ierr /= json_success) return |
| 626 |
kind = json_kind_boolean |
| 627 |
return |
| 628 |
case('n') |
| 629 |
call parse_literal(json_text,value_start,raw_value,value_end,ierr) |
| 630 |
if (ierr /= json_success) return |
| 631 |
kind = json_kind_null |
| 632 |
return |
| 633 |
case default |
| 634 |
call parse_number(json_text,value_start,raw_value,value_end,ierr) |
| 635 |
if (ierr /= json_success) return |
| 636 |
kind = json_kind_number |
| 637 |
return |
| 638 |
end select |
| 639 |
|
| 640 |
end subroutine json_get_raw_value |
| 641 |
|
| 642 |
!----------------------------------------------------------------- |
| 643 |
! parse a JSON path into a list of tokens |
| 644 |
!----------------------------------------------------------------- |
| 645 |
subroutine parse_path(path, tokens, ntokens, ierr) |
| 646 |
character(len=*), intent(in) :: path |
| 647 |
type(json_path_token), allocatable, intent(out) :: tokens(:) |
| 648 |
integer, intent(out) :: ntokens |
| 649 |
integer, intent(out) :: ierr |
| 650 |
integer :: i, n, start |
| 651 |
type(json_path_token) :: token |
| 652 |
character(len=:), allocatable :: segment |
| 653 |
integer :: last |
| 654 |
integer :: ios, idx_value |
| 655 |
|
| 656 |
ierr = json_success |
| 657 |
ntokens = 0 |
| 658 |
if (len_trim(path) == 0) then |
| 659 |
ierr = json_err_parse |
| 660 |
allocate(tokens(0)) |
| 661 |
return |
| 662 |
endif |
| 663 |
|
| 664 |
i = 1 |
| 665 |
n = len_trim(path) |
| 666 |
allocate(tokens(0)) |
| 667 |
|
| 668 |
do while (i <= n) |
| 669 |
call skip_whitespace(path,i) |
| 670 |
if (i > n) exit |
| 671 |
start = i |
| 672 |
do while (i <= n) |
| 673 |
if (path(i:i) == '.' .or. path(i:i) == '[') exit |
| 674 |
i = i + 1 |
| 675 |
enddo |
| 676 |
last = i - 1 |
| 677 |
if (last >= start) then |
| 678 |
segment = path(start:last) |
| 679 |
token%key = trim(segment) |
| 680 |
token%has_index = .false. |
| 681 |
token%index = -1 |
| 682 |
call append_token(tokens,ntokens,token) |
| 683 |
else |
| 684 |
token%key = '' |
| 685 |
token%has_index = .false. |
| 686 |
token%index = -1 |
| 687 |
call append_token(tokens,ntokens,token) |
| 688 |
endif |
| 689 |
|
| 690 |
do while (i <= n) |
| 691 |
if (path(i:i) /= '[') exit |
| 692 |
i = i + 1 |
| 693 |
start = i |
| 694 |
do while (i <= n) |
| 695 |
if (path(i:i) == ']') exit |
| 696 |
i = i + 1 |
| 697 |
enddo |
| 698 |
if (i > n) then |
| 699 |
ierr = json_err_parse |
| 700 |
deallocate(tokens) |
| 701 |
allocate(tokens(0)) |
| 702 |
ntokens = 0 |
| 703 |
return |
| 704 |
endif |
| 705 |
read(path(start:i-1),*,iostat=ios) idx_value |
| 706 |
if (ios /= 0) then |
| 707 |
ierr = json_err_parse |
| 708 |
deallocate(tokens) |
| 709 |
allocate(tokens(0)) |
| 710 |
ntokens = 0 |
| 711 |
return |
| 712 |
endif |
| 713 |
if (.not. tokens(ntokens)%has_index) then |
| 714 |
tokens(ntokens)%has_index = .true. |
| 715 |
tokens(ntokens)%index = idx_value |
| 716 |
else |
| 717 |
token%key = '' |
| 718 |
token%has_index = .true. |
| 719 |
token%index = idx_value |
| 720 |
call append_token(tokens,ntokens,token) |
| 721 |
endif |
| 722 |
i = i + 1 |
| 723 |
enddo |
| 724 |
|
| 725 |
if (i <= n) then |
| 726 |
if (path(i:i) == '.') i = i + 1 |
| 727 |
endif |
| 728 |
enddo |
| 729 |
|
| 730 |
end subroutine parse_path |
| 731 |
|
| 732 |
!----------------------------------------------------------------- |
| 733 |
! append a token to a list of tokens |
| 734 |
!----------------------------------------------------------------- |
| 735 |
subroutine append_token(tokens, ntokens, token) |
| 736 |
type(json_path_token), allocatable, intent(inout) :: tokens(:) |
| 737 |
integer, intent(inout) :: ntokens |
| 738 |
type(json_path_token), intent(in) :: token |
| 739 |
type(json_path_token), allocatable :: tmp(:) |
| 740 |
|
| 741 |
allocate(tmp(ntokens+1)) |
| 742 |
if (ntokens > 0) tmp(1:ntokens) = tokens |
| 743 |
tmp(ntokens+1) = token |
| 744 |
ntokens = ntokens + 1 |
| 745 |
call move_alloc(tmp,tokens) |
| 746 |
|
| 747 |
end subroutine append_token |
| 748 |
|
| 749 |
!----------------------------------------------------------------- |
| 750 |
! parse a literal value from a JSON text |
| 751 |
!----------------------------------------------------------------- |
| 752 |
subroutine parse_literal(text, pos, value, end_pos, ierr) |
| 753 |
character(len=*), intent(in) :: text |
| 754 |
integer, intent(inout) :: pos |
| 755 |
character(:), allocatable, intent(out) :: value |
| 756 |
integer, intent(out) :: end_pos |
| 757 |
integer, intent(out) :: ierr |
| 758 |
integer :: len_text, start |
| 759 |
|
| 760 |
ierr = json_success |
| 761 |
len_text = len(text) |
| 762 |
start = pos |
| 763 |
|
| 764 |
do while (pos <= len_text) |
| 765 |
if (.not. is_identifier_char(text(pos:pos))) exit |
| 766 |
pos = pos + 1 |
| 767 |
enddo |
| 768 |
end_pos = pos - 1 |
| 769 |
call extract_trimmed_span(text,start,end_pos,value) |
| 770 |
|
| 771 |
select case (trim(value)) |
| 772 |
case('true','false','null') |
| 773 |
! valid literal |
| 774 |
case default |
| 775 |
ierr = json_err_parse |
| 776 |
end select |
| 777 |
|
| 778 |
end subroutine parse_literal |
| 779 |
|
| 780 |
!----------------------------------------------------------------- |
| 781 |
! parse a number value from a JSON text |
| 782 |
!----------------------------------------------------------------- |
| 783 |
subroutine parse_number(text, pos, value, end_pos, ierr) |
| 784 |
character(len=*), intent(in) :: text |
| 785 |
integer, intent(inout) :: pos |
| 786 |
character(:), allocatable, intent(out) :: value |
| 787 |
integer, intent(out) :: end_pos |
| 788 |
integer, intent(out) :: ierr |
| 789 |
integer :: len_text, start |
| 790 |
character :: ch |
| 791 |
|
| 792 |
ierr = json_success |
| 793 |
len_text = len(text) |
| 794 |
start = pos |
| 795 |
|
| 796 |
do while (pos <= len_text) |
| 797 |
ch = text(pos:pos) |
| 798 |
if (.not. is_number_char(ch)) exit |
| 799 |
pos = pos + 1 |
| 800 |
enddo |
| 801 |
end_pos = pos - 1 |
| 802 |
call extract_trimmed_span(text,start,end_pos,value) |
| 803 |
|
| 804 |
end subroutine parse_number |
| 805 |
|
| 806 |
!----------------------------------------------------------------- |
| 807 |
! parse a string value from a JSON text |
| 808 |
!----------------------------------------------------------------- |
| 809 |
subroutine parse_string(text, pos, value, ierr) |
| 810 |
character(len=*), intent(in) :: text |
| 811 |
integer, intent(inout) :: pos |
| 812 |
character(:), allocatable, intent(out) :: value |
| 813 |
integer, intent(out) :: ierr |
| 814 |
integer :: len_text, i, count |
| 815 |
logical :: escaped |
| 816 |
character :: ch |
| 817 |
character(:), allocatable :: buffer |
| 818 |
|
| 819 |
ierr = json_success |
| 820 |
len_text = len(text) |
| 821 |
if (pos > len_text .or. text(pos:pos) /= '"') then |
| 822 |
ierr = json_err_parse |
| 823 |
allocate(character(len=0) :: value) |
| 824 |
return |
| 825 |
endif |
| 826 |
|
| 827 |
pos = pos + 1 |
| 828 |
allocate(character(len=len_text) :: buffer) |
| 829 |
count = 0 |
| 830 |
escaped = .false. |
| 831 |
|
| 832 |
i = pos |
| 833 |
do while (i <= len_text) |
| 834 |
ch = text(i:i) |
| 835 |
if (escaped) then |
| 836 |
count = count + 1 |
| 837 |
select case (ch) |
| 838 |
case('"','\','/') |
| 839 |
buffer(count:count) = ch |
| 840 |
case('b') |
| 841 |
buffer(count:count) = char(8) |
| 842 |
case('f') |
| 843 |
buffer(count:count) = char(12) |
| 844 |
case('n') |
| 845 |
buffer(count:count) = char(10) |
| 846 |
case('r') |
| 847 |
buffer(count:count) = char(13) |
| 848 |
case('t') |
| 849 |
buffer(count:count) = char(9) |
| 850 |
case('u') |
| 851 |
! basic handling: skip unicode escape and insert placeholder |
| 852 |
buffer(count:count) = '?' |
| 853 |
if (i+4 <= len_text) i = i + 4 |
| 854 |
case default |
| 855 |
buffer(count:count) = ch |
| 856 |
end select |
| 857 |
escaped = .false. |
| 858 |
else |
| 859 |
select case (ch) |
| 860 |
case('\') |
| 861 |
escaped = .true. |
| 862 |
case('"') |
| 863 |
pos = i + 1 |
| 864 |
exit |
| 865 |
case default |
| 866 |
count = count + 1 |
| 867 |
buffer(count:count) = ch |
| 868 |
end select |
| 869 |
endif |
| 870 |
i = i + 1 |
| 871 |
enddo |
| 872 |
|
| 873 |
if (escaped) then |
| 874 |
ierr = json_err_parse |
| 875 |
deallocate(buffer) |
| 876 |
allocate(character(len=0) :: value) |
| 877 |
return |
| 878 |
endif |
| 879 |
|
| 880 |
allocate(character(len=count) :: value) |
| 881 |
if (count > 0) value = buffer(1:count) |
| 882 |
deallocate(buffer) |
| 883 |
|
| 884 |
end subroutine parse_string |
| 885 |
|
| 886 |
!----------------------------------------------------------------- |
| 887 |
! extract a trimmed span from a JSON text |
| 888 |
!----------------------------------------------------------------- |
| 889 |
subroutine extract_trimmed_span(text, start_pos, end_pos, span) |
| 890 |
character(len=*), intent(in) :: text |
| 891 |
integer, intent(in) :: start_pos, end_pos |
| 892 |
character(:), allocatable, intent(out) :: span |
| 893 |
integer :: s, e |
| 894 |
|
| 895 |
s = start_pos |
| 896 |
e = end_pos |
| 897 |
do while (s <= e .and. is_whitespace_char(text(s:s))) |
| 898 |
s = s + 1 |
| 899 |
enddo |
| 900 |
do while (e >= s .and. is_whitespace_char(text(e:e))) |
| 901 |
e = e - 1 |
| 902 |
enddo |
| 903 |
if (e < s) then |
| 904 |
allocate(character(len=0) :: span) |
| 905 |
else |
| 906 |
allocate(character(len=e-s+1) :: span) |
| 907 |
span = text(s:e) |
| 908 |
endif |
| 909 |
|
| 910 |
end subroutine extract_trimmed_span |
| 911 |
|
| 912 |
!----------------------------------------------------------------- |
| 913 |
! find the position of the matching delimiter in a JSON text |
| 914 |
!----------------------------------------------------------------- |
| 915 |
integer function find_matching_delimiter(text, start_pos, open_char, close_char) result(pos) |
| 916 |
character(len=*), intent(in) :: text |
| 917 |
integer, intent(in) :: start_pos |
| 918 |
character, intent(in) :: open_char, close_char |
| 919 |
integer :: depth, i, len_text |
| 920 |
logical :: in_string, escaped |
| 921 |
character :: ch |
| 922 |
|
| 923 |
len_text = len(text) |
| 924 |
depth = 0 |
| 925 |
in_string = .false. |
| 926 |
escaped = .false. |
| 927 |
pos = 0 |
| 928 |
|
| 929 |
do i = start_pos, len_text |
| 930 |
ch = text(i:i) |
| 931 |
if (in_string) then |
| 932 |
if (escaped) then |
| 933 |
escaped = .false. |
| 934 |
else if (ch == '\') then |
| 935 |
escaped = .true. |
| 936 |
else if (ch == '"') then |
| 937 |
in_string = .false. |
| 938 |
endif |
| 939 |
else |
| 940 |
if (ch == '"') then |
| 941 |
in_string = .true. |
| 942 |
else if (ch == open_char) then |
| 943 |
depth = depth + 1 |
| 944 |
else if (ch == close_char) then |
| 945 |
depth = depth - 1 |
| 946 |
if (depth == 0) then |
| 947 |
pos = i |
| 948 |
return |
| 949 |
endif |
| 950 |
endif |
| 951 |
endif |
| 952 |
enddo |
| 953 |
|
| 954 |
end function find_matching_delimiter |
| 955 |
|
| 956 |
!----------------------------------------------------------------- |
| 957 |
! find the position of a key in a JSON text |
| 958 |
!----------------------------------------------------------------- |
| 959 |
integer function find_key_position(text, key) result(pos) |
| 960 |
character(len=*), intent(in) :: text |
| 961 |
character(len=*), intent(in) :: key |
| 962 |
integer :: len_text, len_key, i |
| 963 |
logical :: in_string, escaped |
| 964 |
character :: ch |
| 965 |
|
| 966 |
len_text = len(text) |
| 967 |
len_key = len_trim(key) |
| 968 |
pos = 0 |
| 969 |
in_string = .false. |
| 970 |
escaped = .false. |
| 971 |
|
| 972 |
do i = 1, len_text - len_key - 1 |
| 973 |
ch = text(i:i) |
| 974 |
if (in_string) then |
| 975 |
if (escaped) then |
| 976 |
escaped = .false. |
| 977 |
else if (ch == '\') then |
| 978 |
escaped = .true. |
| 979 |
else if (ch == '"') then |
| 980 |
in_string = .false. |
| 981 |
endif |
| 982 |
else |
| 983 |
if (ch == '"') then |
| 984 |
if (i + len_key < len_text) then |
| 985 |
if (text(i+1:i+len_key) == key .and. text(i+len_key+1:i+len_key+1) == '"') then |
| 986 |
pos = i |
| 987 |
return |
| 988 |
endif |
| 989 |
endif |
| 990 |
in_string = .true. |
| 991 |
endif |
| 992 |
endif |
| 993 |
enddo |
| 994 |
end function find_key_position |
| 995 |
|
| 996 |
!----------------------------------------------------------------- |
| 997 |
! find the position of the colon after a key in a JSON text |
| 998 |
!----------------------------------------------------------------- |
| 999 |
integer function find_colon_after(text, key_pos, key_len) result(pos) |
| 1000 |
character(len=*), intent(in) :: text |
| 1001 |
integer, intent(in) :: key_pos, key_len |
| 1002 |
integer :: i, len_text |
| 1003 |
|
| 1004 |
len_text = len(text) |
| 1005 |
pos = 0 |
| 1006 |
i = key_pos + key_len + 2 |
| 1007 |
|
| 1008 |
do while (i <= len_text) |
| 1009 |
if (.not. is_whitespace_char(text(i:i))) then |
| 1010 |
if (text(i:i) == ':') then |
| 1011 |
pos = i |
| 1012 |
return |
| 1013 |
else |
| 1014 |
return |
| 1015 |
endif |
| 1016 |
endif |
| 1017 |
i = i + 1 |
| 1018 |
enddo |
| 1019 |
|
| 1020 |
end function find_colon_after |
| 1021 |
|
| 1022 |
!----------------------------------------------------------------- |
| 1023 |
! skip whitespace in a JSON text |
| 1024 |
!----------------------------------------------------------------- |
| 1025 |
subroutine skip_whitespace(text, pos) |
| 1026 |
character(len=*), intent(in) :: text |
| 1027 |
integer, intent(inout) :: pos |
| 1028 |
integer :: len_text |
| 1029 |
|
| 1030 |
len_text = len(text) |
| 1031 |
do while (pos <= len_text) |
| 1032 |
if (.not. is_whitespace_char(text(pos:pos))) exit |
| 1033 |
pos = pos + 1 |
| 1034 |
enddo |
| 1035 |
|
| 1036 |
end subroutine skip_whitespace |
| 1037 |
|
| 1038 |
!----------------------------------------------------------------- |
| 1039 |
! check if a character is a whitespace character |
| 1040 |
!----------------------------------------------------------------- |
| 1041 |
logical function is_whitespace_char(ch) result(res) |
| 1042 |
character, intent(in) :: ch |
| 1043 |
|
| 1044 |
res = (ch == ' ' .or. ch == char(9) .or. ch == char(10) .or. ch == char(13)) |
| 1045 |
|
| 1046 |
end function is_whitespace_char |
| 1047 |
|
| 1048 |
!----------------------------------------------------------------- |
| 1049 |
! check if a character is an identifier character |
| 1050 |
!----------------------------------------------------------------- |
| 1051 |
logical function is_identifier_char(ch) result(res) |
| 1052 |
character, intent(in) :: ch |
| 1053 |
|
| 1054 |
res = ((ch >= 'a' .and. ch <= 'z') .or. (ch >= 'A' .and. ch <= 'Z') .or. (ch >= '0' .and. ch <= '9') .or. ch == '_') |
| 1055 |
|
| 1056 |
end function is_identifier_char |
| 1057 |
|
| 1058 |
!----------------------------------------------------------------- |
| 1059 |
! check if a character is a number character |
| 1060 |
!----------------------------------------------------------------- |
| 1061 |
logical function is_number_char(ch) result(res) |
| 1062 |
character, intent(in) :: ch |
| 1063 |
|
| 1064 |
res = (ch == '+' .or. ch == '-' .or. ch == '.' .or. ch == 'e' .or. ch == 'E' .or. (ch >= '0' .and. ch <= '9')) |
| 1065 |
|
| 1066 |
end function is_number_char |
| 1067 |
|
| 1068 |
!----------------------------------------------------------------- |
| 1069 |
! detect the kind of a value from a JSON text |
| 1070 |
!----------------------------------------------------------------- |
| 1071 |
integer function detect_value_kind(value_text) result(kind) |
| 1072 |
character(len=*), intent(in) :: value_text |
| 1073 |
character :: first_char |
| 1074 |
integer :: i, n |
| 1075 |
|
| 1076 |
n = len(value_text) |
| 1077 |
first_char = ' ' |
| 1078 |
do i = 1, n |
| 1079 |
if (.not. is_whitespace_char(value_text(i:i))) then |
| 1080 |
first_char = value_text(i:i) |
| 1081 |
exit |
| 1082 |
endif |
| 1083 |
enddo |
| 1084 |
|
| 1085 |
if (first_char == ' ') then |
| 1086 |
kind = json_kind_unknown |
| 1087 |
return |
| 1088 |
endif |
| 1089 |
select case (first_char) |
| 1090 |
case('{') |
| 1091 |
kind = json_kind_object |
| 1092 |
case('[') |
| 1093 |
kind = json_kind_array |
| 1094 |
case('"') |
| 1095 |
kind = json_kind_string |
| 1096 |
case('t','f') |
| 1097 |
if (trim(value_text) == 'true' .or. trim(value_text) == 'false') then |
| 1098 |
kind = json_kind_boolean |
| 1099 |
else |
| 1100 |
kind = json_kind_unknown |
| 1101 |
endif |
| 1102 |
case('n') |
| 1103 |
if (trim(value_text) == 'null') then |
| 1104 |
kind = json_kind_null |
| 1105 |
else |
| 1106 |
kind = json_kind_unknown |
| 1107 |
endif |
| 1108 |
case default |
| 1109 |
kind = json_kind_number |
| 1110 |
end select |
| 1111 |
|
| 1112 |
end function detect_value_kind |
| 1113 |
|
| 1114 |
!----------------------------------------------------------------- |
| 1115 |
! parse an array of 64 bit integers from a JSON text |
| 1116 |
!----------------------------------------------------------------- |
| 1117 |
subroutine parse_int64_array(array_text,values,ierr) |
| 1118 |
character(len=*), intent(in) :: array_text |
| 1119 |
integer(kind=int64), allocatable, intent(out) :: values(:) |
| 1120 |
integer, intent(out) :: ierr |
| 1121 |
integer :: n,i,value_kind |
| 1122 |
character(len=:), allocatable :: elem |
| 1123 |
|
| 1124 |
ierr = 0 |
| 1125 |
call json_array_length(array_text,n,ierr) |
| 1126 |
if (ierr /= json_success) then |
| 1127 |
ierr = 1 |
| 1128 |
allocate(values(0)) |
| 1129 |
return |
| 1130 |
endif |
| 1131 |
allocate(values(n)) |
| 1132 |
do i = 1, n |
| 1133 |
call json_array_get_element(array_text,i-1,elem,value_kind,ierr) |
| 1134 |
if (ierr == json_success) then |
| 1135 |
read(elem,*,iostat=ierr) values(i) |
| 1136 |
else |
| 1137 |
ierr = 1 |
| 1138 |
endif |
| 1139 |
if (allocated(elem)) deallocate(elem) |
| 1140 |
if (ierr /= 0) then |
| 1141 |
values = 0_int64 |
| 1142 |
return |
| 1143 |
endif |
| 1144 |
enddo |
| 1145 |
|
| 1146 |
end subroutine parse_int64_array |
| 1147 |
|
| 1148 |
!----------------------------------------------------------------- |
| 1149 |
! parse an array of integers from a JSON text |
| 1150 |
!----------------------------------------------------------------- |
| 1151 |
subroutine parse_int_array(array_text,values,ierr) |
| 1152 |
character(len=*), intent(in) :: array_text |
| 1153 |
integer, allocatable, intent(out) :: values(:) |
| 1154 |
integer, intent(out) :: ierr |
| 1155 |
integer :: n,i,value_kind |
| 1156 |
character(len=:), allocatable :: elem |
| 1157 |
|
| 1158 |
ierr = 0 |
| 1159 |
call json_array_length(array_text,n,ierr) |
| 1160 |
if (ierr /= json_success) then |
| 1161 |
ierr = 1 |
| 1162 |
allocate(values(0)) |
| 1163 |
return |
| 1164 |
endif |
| 1165 |
allocate(values(n)) |
| 1166 |
do i = 1, n |
| 1167 |
call json_array_get_element(array_text,i-1,elem,value_kind,ierr) |
| 1168 |
if (ierr == json_success) then |
| 1169 |
read(elem,*,iostat=ierr) values(i) |
| 1170 |
else |
| 1171 |
ierr = 1 |
| 1172 |
endif |
| 1173 |
if (allocated(elem)) deallocate(elem) |
| 1174 |
if (ierr /= 0) then |
| 1175 |
values = 0 |
| 1176 |
return |
| 1177 |
endif |
| 1178 |
enddo |
| 1179 |
|
| 1180 |
end subroutine parse_int_array |
| 1181 |
|
| 1182 |
end module json_utils |
| 1183 |
|