Simulation Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3041 lines
119 KiB

  1. !*****************************************************************************************
  2. !> author: Jacob Williams
  3. ! license: BSD
  4. !
  5. ! Higher-level [[json_file]] interface for the [[json_value]] type.
  6. !
  7. !### License
  8. ! * JSON-Fortran is released under a BSD-style license.
  9. ! See the [LICENSE](https://github.com/jacobwilliams/json-fortran/blob/master/LICENSE)
  10. ! file for details.
  11. module json_file_module
  12. use,intrinsic :: iso_fortran_env
  13. use json_kinds
  14. use json_parameters, only: unit2str
  15. use json_string_utilities
  16. use json_value_module
  17. implicit none
  18. private
  19. #include "json_macros.inc"
  20. !*********************************************************
  21. !> author: Jacob Williams
  22. ! date: 12/9/2013
  23. !
  24. ! The `json_file` is the main public class that is
  25. ! used to open a file and get data from it.
  26. !
  27. ! A `json_file` contains only two items: an instance of a [[json_core(type)]],
  28. ! which is used for all data manipulation, and a [[json_value]] pointer,
  29. ! which is used to construct the linked-list data structure.
  30. ! Note that most methods in the `json_file` class are simply wrappers
  31. ! to the lower-level routines in the [[json_value_module]].
  32. !
  33. !### Example
  34. !
  35. !```fortran
  36. ! program test
  37. ! use json_module
  38. ! implicit none
  39. ! type(json_file) :: json
  40. ! integer :: ival
  41. ! real(real64) :: rval
  42. ! character(len=:),allocatable :: cval
  43. ! logical :: found
  44. ! call json%initialize(compact_reals=.true.)
  45. ! call json%load(filename='myfile.json')
  46. ! call json%print() !print to the console
  47. ! call json%get('var.i',ival,found)
  48. ! call json%get('var.r(3)',rval,found)
  49. ! call json%get('var.c',cval,found)
  50. ! call json%destroy()
  51. ! end program test
  52. !```
  53. !
  54. !@note The `destroy()` method may be called to free the memory if necessary.
  55. ! [[json_file(type)]] includes a finalizer that also calls
  56. ! `destroy()` when the variable goes out of scope.
  57. type,public :: json_file
  58. private
  59. type(json_core) :: core !! The instance of the [[json_core(type)]]
  60. !! factory used for this file.
  61. type(json_value),pointer :: p => null() !! the JSON structure read from the file
  62. contains
  63. generic,public :: initialize => initialize_json_core_in_file,&
  64. set_json_core_in_file
  65. procedure,public :: get_core => get_json_core_in_file
  66. !>
  67. ! Load JSON from a file.
  68. procedure,public :: load => json_file_load
  69. !>
  70. ! The same as `load`, but only here for backward compatibility
  71. procedure,public :: load_file => json_file_load
  72. !>
  73. ! Load JSON from a string.
  74. generic,public :: deserialize => MAYBEWRAP(json_file_load_from_string)
  75. !>
  76. ! The same as `deserialize`, but only here for backward compatibility
  77. generic,public :: load_from_string => MAYBEWRAP(json_file_load_from_string)
  78. !>
  79. ! Print the [[json_value]] structure to an allocatable string
  80. procedure,public :: serialize => json_file_print_to_string
  81. !>
  82. ! The same as `serialize`, but only here for backward compatibility
  83. procedure,public :: print_to_string => json_file_print_to_string
  84. procedure,public :: destroy => json_file_destroy
  85. procedure,public :: nullify => json_file_nullify
  86. procedure,public :: move => json_file_move_pointer
  87. generic,public :: info => MAYBEWRAP(json_file_variable_info)
  88. generic,public :: matrix_info => MAYBEWRAP(json_file_variable_matrix_info)
  89. !error checking:
  90. procedure,public :: failed => json_file_failed
  91. procedure,public :: print_error_message => json_file_print_error_message
  92. procedure,public :: check_for_errors => json_file_check_for_errors
  93. procedure,public :: clear_exceptions => json_file_clear_exceptions
  94. generic,public :: print => json_file_print_to_console, &
  95. json_file_print_to_unit, &
  96. json_file_print_to_filename
  97. !>
  98. ! The same as `print`, but only here for backward compatibility
  99. generic,public :: print_file => json_file_print_to_console, &
  100. json_file_print_to_unit, &
  101. json_file_print_to_filename
  102. !>
  103. ! Rename a variable, specifying it by path
  104. generic,public :: rename => MAYBEWRAP(json_file_rename)
  105. #ifdef USE_UCS4
  106. generic,public :: rename => json_file_rename_path_ascii, &
  107. json_file_rename_name_ascii
  108. #endif
  109. !>
  110. ! Verify that a path is valid
  111. ! (i.e., a variable with this path exists in the file).
  112. generic,public :: valid_path => MAYBEWRAP(json_file_valid_path)
  113. !>
  114. ! Get a variable from a [[json_file(type)]], by specifying the path.
  115. generic,public :: get => MAYBEWRAP(json_file_get_object), &
  116. MAYBEWRAP(json_file_get_integer), &
  117. #ifndef REAL32
  118. MAYBEWRAP(json_file_get_real32), &
  119. #endif
  120. MAYBEWRAP(json_file_get_real), &
  121. #ifdef REAL128
  122. MAYBEWRAP(json_file_get_real64), &
  123. #endif
  124. MAYBEWRAP(json_file_get_logical), &
  125. MAYBEWRAP(json_file_get_string), &
  126. MAYBEWRAP(json_file_get_integer_vec), &
  127. #ifndef REAL32
  128. MAYBEWRAP(json_file_get_real32_vec), &
  129. #endif
  130. MAYBEWRAP(json_file_get_real_vec), &
  131. #ifdef REAL128
  132. MAYBEWRAP(json_file_get_real64_vec), &
  133. #endif
  134. MAYBEWRAP(json_file_get_logical_vec), &
  135. MAYBEWRAP(json_file_get_string_vec), &
  136. MAYBEWRAP(json_file_get_alloc_string_vec), &
  137. json_file_get_root
  138. !>
  139. ! Add a variable to a [[json_file(type)]], by specifying the path.
  140. !
  141. !### Example
  142. !
  143. !```fortran
  144. ! program test
  145. ! use json_module, rk=>json_rk, ik=>json_ik
  146. ! implicit none
  147. ! type(json_file) :: f
  148. ! call f%initialize() ! specify whatever init options you want.
  149. ! call f%add('inputs.t', 0.0_rk)
  150. ! call f%add('inputs.x', [1.0_rk,2.0_rk,3.0_rk])
  151. ! call f%add('inputs.flag', .true.)
  152. ! call f%print() ! print to the console
  153. ! end program test
  154. !```
  155. generic,public :: add => json_file_add, &
  156. MAYBEWRAP(json_file_add_object), &
  157. MAYBEWRAP(json_file_add_integer), &
  158. #ifndef REAL32
  159. MAYBEWRAP(json_file_add_real32), &
  160. #endif
  161. MAYBEWRAP(json_file_add_real), &
  162. #ifdef REAL128
  163. MAYBEWRAP(json_file_add_real64), &
  164. #endif
  165. MAYBEWRAP(json_file_add_logical), &
  166. MAYBEWRAP(json_file_add_string), &
  167. MAYBEWRAP(json_file_add_integer_vec), &
  168. #ifndef REAL32
  169. MAYBEWRAP(json_file_add_real32_vec), &
  170. #endif
  171. MAYBEWRAP(json_file_add_real_vec), &
  172. #ifdef REAL128
  173. MAYBEWRAP(json_file_add_real64_vec), &
  174. #endif
  175. MAYBEWRAP(json_file_add_logical_vec), &
  176. MAYBEWRAP(json_file_add_string_vec)
  177. #ifdef USE_UCS4
  178. generic,public :: add => json_file_add_string_path_ascii, &
  179. json_file_add_string_value_ascii,&
  180. json_file_add_string_vec_path_ascii,&
  181. json_file_add_string_vec_vec_ascii
  182. #endif
  183. !>
  184. ! Update a scalar variable in a [[json_file(type)]],
  185. ! by specifying the path.
  186. !
  187. !@note These have been mostly supplanted by the `add`
  188. ! methods, which do a similar thing (and can be used for
  189. ! scalars and vectors, etc.)
  190. generic,public :: update => MAYBEWRAP(json_file_update_integer), &
  191. MAYBEWRAP(json_file_update_logical), &
  192. #ifndef REAL32
  193. MAYBEWRAP(json_file_update_real32), &
  194. #endif
  195. MAYBEWRAP(json_file_update_real), &
  196. #ifdef REAL128
  197. MAYBEWRAP(json_file_update_real64), &
  198. #endif
  199. MAYBEWRAP(json_file_update_string)
  200. #ifdef USE_UCS4
  201. generic,public :: update => json_file_update_string_name_ascii, &
  202. json_file_update_string_val_ascii
  203. #endif
  204. !>
  205. ! Remove a variable from a [[json_file(type)]]
  206. ! by specifying the path.
  207. generic,public :: remove => MAYBEWRAP(json_file_remove)
  208. !traverse
  209. procedure,public :: traverse => json_file_traverse
  210. ! ***************************************************
  211. ! operators
  212. ! ***************************************************
  213. generic,public :: operator(.in.) => MAYBEWRAP(json_file_valid_path_op)
  214. procedure,pass(me) :: MAYBEWRAP(json_file_valid_path_op)
  215. generic,public :: assignment(=) => assign_json_file,&
  216. assign_json_file_to_string,&
  217. MAYBEWRAP(assign_string_to_json_file)
  218. procedure :: assign_json_file
  219. procedure,pass(me) :: assign_json_file_to_string
  220. procedure :: MAYBEWRAP(assign_string_to_json_file)
  221. ! ***************************************************
  222. ! private routines
  223. ! ***************************************************
  224. !load from string:
  225. procedure :: MAYBEWRAP(json_file_load_from_string)
  226. !initialize
  227. procedure :: initialize_json_core_in_file
  228. procedure :: set_json_core_in_file
  229. !get info:
  230. procedure :: MAYBEWRAP(json_file_variable_info)
  231. procedure :: MAYBEWRAP(json_file_variable_matrix_info)
  232. !rename:
  233. procedure :: MAYBEWRAP(json_file_rename)
  234. #ifdef USE_UCS4
  235. procedure :: json_file_rename_path_ascii
  236. procedure :: json_file_rename_name_ascii
  237. #endif
  238. !validate path:
  239. procedure :: MAYBEWRAP(json_file_valid_path)
  240. !get:
  241. procedure :: MAYBEWRAP(json_file_get_object)
  242. procedure :: MAYBEWRAP(json_file_get_integer)
  243. #ifndef REAL32
  244. procedure :: MAYBEWRAP(json_file_get_real32)
  245. #endif
  246. procedure :: MAYBEWRAP(json_file_get_real)
  247. #ifdef REAL128
  248. procedure :: MAYBEWRAP(json_file_get_real64)
  249. #endif
  250. procedure :: MAYBEWRAP(json_file_get_logical)
  251. procedure :: MAYBEWRAP(json_file_get_string)
  252. procedure :: MAYBEWRAP(json_file_get_integer_vec)
  253. #ifndef REAL32
  254. procedure :: MAYBEWRAP(json_file_get_real32_vec)
  255. #endif
  256. procedure :: MAYBEWRAP(json_file_get_real_vec)
  257. #ifdef REAL128
  258. procedure :: MAYBEWRAP(json_file_get_real64_vec)
  259. #endif
  260. procedure :: MAYBEWRAP(json_file_get_logical_vec)
  261. procedure :: MAYBEWRAP(json_file_get_string_vec)
  262. procedure :: MAYBEWRAP(json_file_get_alloc_string_vec)
  263. procedure :: json_file_get_root
  264. !add:
  265. procedure :: json_file_add
  266. procedure :: MAYBEWRAP(json_file_add_object)
  267. procedure :: MAYBEWRAP(json_file_add_integer)
  268. #ifndef REAL32
  269. procedure :: MAYBEWRAP(json_file_add_real32)
  270. #endif
  271. procedure :: MAYBEWRAP(json_file_add_real)
  272. #ifdef REAL128
  273. procedure :: MAYBEWRAP(json_file_add_real64)
  274. #endif
  275. procedure :: MAYBEWRAP(json_file_add_logical)
  276. procedure :: MAYBEWRAP(json_file_add_string)
  277. procedure :: MAYBEWRAP(json_file_add_integer_vec)
  278. #ifndef REAL32
  279. procedure :: MAYBEWRAP(json_file_add_real32_vec)
  280. #endif
  281. procedure :: MAYBEWRAP(json_file_add_real_vec)
  282. #ifdef REAL128
  283. procedure :: MAYBEWRAP(json_file_add_real64_vec)
  284. #endif
  285. procedure :: MAYBEWRAP(json_file_add_logical_vec)
  286. procedure :: MAYBEWRAP(json_file_add_string_vec)
  287. #ifdef USE_UCS4
  288. procedure :: json_file_add_string_path_ascii
  289. procedure :: json_file_add_string_value_ascii
  290. procedure :: json_file_add_string_vec_path_ascii
  291. procedure :: json_file_add_string_vec_vec_ascii
  292. #endif
  293. !update:
  294. procedure :: MAYBEWRAP(json_file_update_integer)
  295. procedure :: MAYBEWRAP(json_file_update_logical)
  296. #ifndef REAL32
  297. procedure :: MAYBEWRAP(json_file_update_real32)
  298. #endif
  299. procedure :: MAYBEWRAP(json_file_update_real)
  300. #ifdef REAL128
  301. procedure :: MAYBEWRAP(json_file_update_real64)
  302. #endif
  303. procedure :: MAYBEWRAP(json_file_update_string)
  304. #ifdef USE_UCS4
  305. procedure :: json_file_update_string_name_ascii
  306. procedure :: json_file_update_string_val_ascii
  307. #endif
  308. !remove:
  309. procedure :: MAYBEWRAP(json_file_remove)
  310. !print:
  311. procedure :: json_file_print_to_console
  312. procedure :: json_file_print_to_unit
  313. procedure :: json_file_print_to_filename
  314. final :: finalize_json_file
  315. end type json_file
  316. !*********************************************************
  317. !*********************************************************
  318. !> author: Izaak Beekman
  319. ! date: 07/23/2015
  320. !
  321. ! Structure constructor to initialize a [[json_file(type)]]
  322. ! object with an existing [[json_value]] object or a JSON
  323. ! string, and either the [[json_core(type)]] settings or a
  324. ! [[json_core(type)]] instance.
  325. !
  326. !### Example
  327. !
  328. !```fortran
  329. ! ...
  330. ! type(json_file) :: my_file
  331. ! type(json_value),pointer :: json_object
  332. ! type(json_core) :: json_core_object
  333. ! ...
  334. ! ! Construct a json_object:
  335. ! !could do this:
  336. ! my_file = json_file(json_object)
  337. ! !or:
  338. ! my_file = json_file(json_object,verbose=.true.)
  339. ! !or:
  340. ! my_file = json_file('{"x": [1]}',verbose=.true.)
  341. ! !or:
  342. ! my_file = json_file(json_object,json_core_object)
  343. ! !or:
  344. ! my_file = json_file('{"x": [1]}',json_core_object)
  345. !```
  346. interface json_file
  347. module procedure initialize_json_file, &
  348. initialize_json_file_v2, &
  349. MAYBEWRAP(initialize_json_file_from_string), &
  350. MAYBEWRAP(initialize_json_file_from_string_v2)
  351. end interface
  352. !*************************************************************************************
  353. contains
  354. !*****************************************************************************************
  355. !*****************************************************************************************
  356. !>
  357. ! Finalizer for [[json_file]] class.
  358. !
  359. ! Just a wrapper for [[json_file_destroy]].
  360. subroutine finalize_json_file(me)
  361. implicit none
  362. type(json_file),intent(inout) :: me
  363. call me%destroy(destroy_core=.true.)
  364. end subroutine finalize_json_file
  365. !*****************************************************************************************
  366. !*****************************************************************************************
  367. !>
  368. ! Check error status in the file.
  369. pure function json_file_failed(me) result(failed)
  370. implicit none
  371. class(json_file),intent(in) :: me
  372. logical(LK) :: failed !! will be true if there has been an error.
  373. failed = me%core%failed()
  374. end function json_file_failed
  375. !*****************************************************************************************
  376. !*****************************************************************************************
  377. !>
  378. ! Retrieve error status and message from the class.
  379. subroutine json_file_check_for_errors(me,status_ok,error_msg)
  380. implicit none
  381. class(json_file),intent(inout) :: me
  382. logical(LK),intent(out),optional :: status_ok !! true if there were no errors
  383. character(kind=CK,len=:),allocatable,intent(out),optional :: error_msg !! the error message
  384. !! (if there were errors)
  385. #if defined __GFORTRAN__
  386. character(kind=CK,len=:),allocatable :: tmp !! workaround for gfortran bugs
  387. call me%core%check_for_errors(status_ok,tmp)
  388. if (present(error_msg)) error_msg = tmp
  389. #else
  390. call me%core%check_for_errors(status_ok,error_msg)
  391. #endif
  392. end subroutine json_file_check_for_errors
  393. !*****************************************************************************************
  394. !*****************************************************************************************
  395. !>
  396. ! Clear exceptions in the class.
  397. pure subroutine json_file_clear_exceptions(me)
  398. implicit none
  399. class(json_file),intent(inout) :: me
  400. call me%core%clear_exceptions()
  401. end subroutine json_file_clear_exceptions
  402. !*****************************************************************************************
  403. !*****************************************************************************************
  404. !>
  405. ! This is a wrapper for [[json_print_error_message]].
  406. subroutine json_file_print_error_message(me,io_unit)
  407. implicit none
  408. class(json_file),intent(inout) :: me
  409. integer, intent(in), optional :: io_unit
  410. call me%core%print_error_message(io_unit)
  411. end subroutine json_file_print_error_message
  412. !*****************************************************************************************
  413. !*****************************************************************************************
  414. !>
  415. ! Initialize the [[json_core(type)]] for this [[json_file]].
  416. ! This is just a wrapper for [[json_initialize]].
  417. !
  418. !@note This does not destroy the data in the file.
  419. !
  420. !@note [[initialize_json_core]], [[json_initialize]],
  421. ! [[initialize_json_core_in_file]], [[initialize_json_file]],
  422. ! [[initialize_json_file_v2]], [[initialize_json_file_from_string]],
  423. ! and [[initialize_json_file_from_string_v2]]
  424. ! all have a similar interface.
  425. subroutine initialize_json_core_in_file(me,&
  426. #include "json_initialize_dummy_arguments.inc"
  427. )
  428. implicit none
  429. class(json_file),intent(inout) :: me
  430. #include "json_initialize_arguments.inc"
  431. call me%core%initialize(&
  432. #include "json_initialize_dummy_arguments.inc"
  433. )
  434. end subroutine initialize_json_core_in_file
  435. !*****************************************************************************************
  436. !*****************************************************************************************
  437. !>
  438. ! Set the [[json_core(type)]] for this [[json_file]].
  439. !
  440. !@note This does not destroy the data in the file.
  441. !
  442. !@note This one is used if you want to initialize the file with
  443. ! an already-existing [[json_core(type)]] (presumably, this was already
  444. ! initialized by a call to [[initialize_json_core]] or similar).
  445. subroutine set_json_core_in_file(me,core)
  446. implicit none
  447. class(json_file),intent(inout) :: me
  448. type(json_core),intent(in) :: core
  449. me%core = core
  450. end subroutine set_json_core_in_file
  451. !*****************************************************************************************
  452. !*****************************************************************************************
  453. !>
  454. ! Get a copy of the [[json_core(type)]] in this [[json_file]].
  455. subroutine get_json_core_in_file(me,core)
  456. implicit none
  457. class(json_file),intent(in) :: me
  458. type(json_core),intent(out) :: core
  459. core = me%core
  460. end subroutine get_json_core_in_file
  461. !*****************************************************************************************
  462. !*****************************************************************************************
  463. !> author: Izaak Beekman
  464. ! date: 07/23/2015
  465. !
  466. ! Cast a [[json_value]] object as a [[json_file(type)]] object.
  467. ! It also calls the `initialize()` method.
  468. !
  469. !@note [[initialize_json_core]], [[json_initialize]],
  470. ! [[initialize_json_core_in_file]], [[initialize_json_file]],
  471. ! [[initialize_json_file_v2]], [[initialize_json_file_from_string]],
  472. ! and [[initialize_json_file_from_string_v2]]
  473. ! all have a similar interface.
  474. function initialize_json_file(p,&
  475. #include "json_initialize_dummy_arguments.inc"
  476. ) result(file_object)
  477. implicit none
  478. type(json_file) :: file_object
  479. type(json_value),pointer,optional :: p !! `json_value` object to cast
  480. !! as a `json_file` object. This
  481. !! will be nullified.
  482. #include "json_initialize_arguments.inc"
  483. call file_object%initialize(&
  484. #include "json_initialize_dummy_arguments.inc"
  485. )
  486. if (present(p)) then
  487. file_object%p => p
  488. ! we have to nullify it to avoid
  489. ! a dangling pointer when the file
  490. ! goes out of scope
  491. nullify(p)
  492. end if
  493. end function initialize_json_file
  494. !*****************************************************************************************
  495. !*****************************************************************************************
  496. !> author: Jacob Williams
  497. ! date: 4/26/2016
  498. !
  499. ! Cast a [[json_value]] pointer and a [[json_core(type)]] object
  500. ! as a [[json_file(type)]] object.
  501. function initialize_json_file_v2(json_value_object,json_core_object) &
  502. result(file_object)
  503. implicit none
  504. type(json_file) :: file_object
  505. type(json_value),pointer,intent(in) :: json_value_object
  506. type(json_core),intent(in) :: json_core_object
  507. file_object%p => json_value_object
  508. file_object%core = json_core_object
  509. end function initialize_json_file_v2
  510. !*****************************************************************************************
  511. !*****************************************************************************************
  512. !> author: Jacob Williams
  513. ! date: 01/19/2019
  514. !
  515. ! Cast a JSON string as a [[json_file(type)]] object.
  516. ! It also calls the `initialize()` method.
  517. !
  518. !### Example
  519. !
  520. !```fortran
  521. ! type(json_file) :: f
  522. ! f = json_file('{"key ": 1}', trailing_spaces_significant=.true.)
  523. !```
  524. !
  525. !@note [[initialize_json_core]], [[json_initialize]],
  526. ! [[initialize_json_core_in_file]], [[initialize_json_file]],
  527. ! [[initialize_json_file_v2]], [[initialize_json_file_from_string]],
  528. ! and [[initialize_json_file_from_string_v2]]
  529. ! all have a similar interface.
  530. function initialize_json_file_from_string(str,&
  531. #include "json_initialize_dummy_arguments.inc"
  532. ) result(file_object)
  533. implicit none
  534. type(json_file) :: file_object
  535. character(kind=CK,len=*),intent(in) :: str !! string to load JSON data from
  536. #include "json_initialize_arguments.inc"
  537. call file_object%initialize(&
  538. #include "json_initialize_dummy_arguments.inc"
  539. )
  540. call file_object%deserialize(str)
  541. end function initialize_json_file_from_string
  542. !*****************************************************************************************
  543. !*****************************************************************************************
  544. !>
  545. ! Alternate version of [[initialize_json_file_from_string]], where "str" is kind=CDK.
  546. function wrap_initialize_json_file_from_string(str,&
  547. #include "json_initialize_dummy_arguments.inc"
  548. ) result(file_object)
  549. implicit none
  550. type(json_file) :: file_object
  551. character(kind=CDK,len=*),intent(in) :: str !! string to load JSON data from
  552. #include "json_initialize_arguments.inc"
  553. file_object = initialize_json_file_from_string(&
  554. to_unicode(str),&
  555. #include "json_initialize_dummy_arguments.inc"
  556. )
  557. end function wrap_initialize_json_file_from_string
  558. !*****************************************************************************************
  559. !*****************************************************************************************
  560. !> author: Jacob Williams
  561. ! date: 1/19/2019
  562. !
  563. ! Cast a JSON string and a [[json_core(type)]] object
  564. ! as a [[json_file(type)]] object.
  565. function initialize_json_file_from_string_v2(str, json_core_object) &
  566. result(file_object)
  567. implicit none
  568. type(json_file) :: file_object
  569. character(kind=CK,len=*),intent(in) :: str !! string to load JSON data from
  570. type(json_core),intent(in) :: json_core_object
  571. file_object%core = json_core_object
  572. call file_object%deserialize(str)
  573. end function initialize_json_file_from_string_v2
  574. !*****************************************************************************************
  575. !*****************************************************************************************
  576. !>
  577. ! Alternate version of [[initialize_json_file_from_string_v2]], where "str" is kind=CDK.
  578. function wrap_initialize_json_file_from_string_v2(str,json_core_object) &
  579. result(file_object)
  580. implicit none
  581. type(json_file) :: file_object
  582. character(kind=CDK,len=*),intent(in) :: str !! string to load JSON data from
  583. type(json_core),intent(in) :: json_core_object
  584. file_object = initialize_json_file_from_string_v2(to_unicode(str),json_core_object)
  585. end function wrap_initialize_json_file_from_string_v2
  586. !*****************************************************************************************
  587. !*****************************************************************************************
  588. !> author: Jacob Williams
  589. !
  590. ! Nullify the [[json_value]] pointer in a [[json_file(type)]],
  591. ! but do not destroy it.
  592. !
  593. ! This should normally only be done if the pointer is the target of
  594. ! another pointer outside the class that is still intended to be in
  595. ! scope after the [[json_file(type)]] has gone out of scope.
  596. ! Otherwise, this would result in a memory leak.
  597. !
  598. !### See also
  599. ! * [[json_file_destroy]]
  600. !
  601. !### History
  602. ! * 6/30/2019 : Created
  603. subroutine json_file_nullify(me)
  604. implicit none
  605. class(json_file),intent(inout) :: me
  606. nullify(me%p)
  607. end subroutine json_file_nullify
  608. !*****************************************************************************************
  609. !*****************************************************************************************
  610. !> author: Jacob Williams
  611. !
  612. ! Destroy the [[json_value]] data in a [[json_file(type)]].
  613. ! This may be done when the variable is no longer needed,
  614. ! or will be reused to open a different file.
  615. ! Otherwise a memory leak will occur.
  616. !
  617. ! Optionally, also destroy the [[json_core(type)]] instance (this
  618. ! is not necessary to prevent memory leaks, since a [[json_core(type)]]
  619. ! does not use pointers).
  620. !
  621. !### See also
  622. ! * [[json_file_nullify]]
  623. !
  624. !### History
  625. ! * 12/9/2013 : Created
  626. ! * 4/26/2016 : Added optional `destroy_core` argument
  627. !
  628. !@note This routine will be called automatically when the variable
  629. ! goes out of scope.
  630. subroutine json_file_destroy(me,destroy_core)
  631. implicit none
  632. class(json_file),intent(inout) :: me
  633. logical,intent(in),optional :: destroy_core !! to also destroy the [[json_core(type)]].
  634. !! default is to leave it as is.
  635. if (associated(me%p)) call me%core%destroy(me%p)
  636. if (present(destroy_core)) then
  637. if (destroy_core) call me%core%destroy()
  638. end if
  639. end subroutine json_file_destroy
  640. !*****************************************************************************************
  641. !*****************************************************************************************
  642. !> author: Jacob Williams
  643. ! date: 12/5/2014
  644. !
  645. ! Move the [[json_value]] pointer from one [[json_file(type)]] to another.
  646. ! The "from" pointer is then nullified, but not destroyed.
  647. !
  648. !@note If "from%p" is not associated, then an error is thrown.
  649. subroutine json_file_move_pointer(to,from)
  650. implicit none
  651. class(json_file),intent(inout) :: to
  652. class(json_file),intent(inout) :: from
  653. if (associated(from%p)) then
  654. if (from%failed()) then
  655. !Don't get the data if the FROM file has an
  656. !active exception, since it may not be valid.
  657. call to%core%throw_exception('Error in json_file_move_pointer: '//&
  658. 'error exception in FROM file.')
  659. else
  660. call to%initialize() !initialize and clear any exceptions that may be present
  661. to%p => from%p
  662. nullify(from%p)
  663. end if
  664. else
  665. call to%core%throw_exception('Error in json_file_move_pointer: '//&
  666. 'pointer is not associated.')
  667. end if
  668. end subroutine json_file_move_pointer
  669. !*****************************************************************************************
  670. !*****************************************************************************************
  671. !> author: Jacob Williams
  672. ! date: 12/9/2013
  673. !
  674. ! Load the JSON data from a file.
  675. !
  676. !### Example
  677. !
  678. !```fortran
  679. ! program main
  680. ! use json_module
  681. ! implicit none
  682. ! type(json_file) :: f
  683. ! call f%load('my_file.json')
  684. ! !...
  685. ! call f%destroy()
  686. ! end program main
  687. !```
  688. subroutine json_file_load(me, filename, unit)
  689. implicit none
  690. class(json_file),intent(inout) :: me
  691. character(kind=CDK,len=*),intent(in) :: filename !! the filename to open
  692. integer(IK),intent(in),optional :: unit !! the unit number to use
  693. !! (if not present, a newunit
  694. !! is used)
  695. call me%core%load(file=filename, p=me%p, unit=unit)
  696. end subroutine json_file_load
  697. !*****************************************************************************************
  698. !*****************************************************************************************
  699. !> author: Jacob Williams
  700. ! date: 1/13/2015
  701. !
  702. ! Load the JSON data from a string.
  703. !
  704. !### Example
  705. !
  706. ! Load JSON from a string:
  707. !```fortran
  708. ! type(json_file) :: f
  709. ! call f%deserialize('{ "name": "Leonidas" }')
  710. !```
  711. subroutine json_file_load_from_string(me, str)
  712. implicit none
  713. class(json_file),intent(inout) :: me
  714. character(kind=CK,len=*),intent(in) :: str !! string to load JSON data from
  715. call me%core%deserialize(me%p, str)
  716. end subroutine json_file_load_from_string
  717. !*****************************************************************************************
  718. !*****************************************************************************************
  719. !>
  720. ! Alternate version of [[json_file_load_from_string]], where "str" is kind=CDK.
  721. subroutine wrap_json_file_load_from_string(me, str)
  722. implicit none
  723. class(json_file),intent(inout) :: me
  724. character(kind=CDK,len=*),intent(in) :: str
  725. call me%deserialize(to_unicode(str))
  726. end subroutine wrap_json_file_load_from_string
  727. !*****************************************************************************************
  728. !*****************************************************************************************
  729. !> author: Jacob Williams
  730. ! date: 1/11/2015
  731. !
  732. ! Print the JSON file to the console.
  733. subroutine json_file_print_to_console(me)
  734. implicit none
  735. class(json_file),intent(inout) :: me
  736. call me%core%print(me%p,iunit=int(output_unit,IK))
  737. end subroutine json_file_print_to_console
  738. !*****************************************************************************************
  739. !*****************************************************************************************
  740. !> author: Jacob Williams
  741. ! date: 12/9/2013
  742. !
  743. ! Prints the JSON file to the specified file unit number.
  744. subroutine json_file_print_to_unit(me, iunit)
  745. implicit none
  746. class(json_file),intent(inout) :: me
  747. integer(IK),intent(in) :: iunit !! file unit number (must not be -1)
  748. if (iunit/=unit2str) then
  749. call me%core%print(me%p,iunit=iunit)
  750. else
  751. call me%core%throw_exception('Error in json_file_print_to_unit: iunit must not be -1.')
  752. end if
  753. end subroutine json_file_print_to_unit
  754. !*****************************************************************************************
  755. !*****************************************************************************************
  756. !> author: Jacob Williams
  757. ! date: 1/11/2015
  758. !
  759. ! Print the JSON structure to the specified filename.
  760. ! The file is opened, printed, and then closed.
  761. !
  762. !### Example
  763. ! Example loading a JSON file, changing a value, and then printing
  764. ! result to a new file:
  765. !```fortran
  766. ! type(json_file) :: f
  767. ! logical :: found
  768. ! call f%load('my_file.json') !open the original file
  769. ! call f%update('version',4,found) !change the value of a variable
  770. ! call f%print('my_file_2.json') !save file as new name
  771. !```
  772. subroutine json_file_print_to_filename(me,filename)
  773. implicit none
  774. class(json_file),intent(inout) :: me
  775. character(kind=CDK,len=*),intent(in) :: filename !! filename to print to
  776. call me%core%print(me%p,filename)
  777. end subroutine json_file_print_to_filename
  778. !*****************************************************************************************
  779. !*****************************************************************************************
  780. !> author: Jacob Williams
  781. ! date: 1/11/2015
  782. !
  783. ! Print the JSON file to a string.
  784. !
  785. !### Example
  786. !
  787. ! Open a JSON file, and then print the contents to a string:
  788. !```fortran
  789. ! type(json_file) :: f
  790. ! character(kind=CK,len=:),allocatable :: str
  791. ! call f%load('my_file.json')
  792. ! call f%serialize(str)
  793. !```
  794. subroutine json_file_print_to_string(me,str)
  795. implicit none
  796. class(json_file),intent(inout) :: me
  797. character(kind=CK,len=:),allocatable,intent(out) :: str !! string to print JSON data to
  798. call me%core%serialize(me%p,str)
  799. end subroutine json_file_print_to_string
  800. !*****************************************************************************************
  801. !*****************************************************************************************
  802. !> author: Jacob Williams
  803. ! date: 2/3/2014
  804. !
  805. ! Returns information about a variable in a [[json_file(type)]].
  806. !
  807. !@note If `found` is present, no exceptions will be thrown if an
  808. ! error occurs. Otherwise, an exception will be thrown if the
  809. ! variable is not found.
  810. subroutine json_file_variable_info(me,path,found,var_type,n_children,name)
  811. implicit none
  812. class(json_file),intent(inout) :: me
  813. character(kind=CK,len=*),intent(in) :: path !! path to the variable
  814. logical(LK),intent(out),optional :: found !! the variable exists in the structure
  815. integer(IK),intent(out),optional :: var_type !! variable type
  816. integer(IK),intent(out),optional :: n_children !! number of children
  817. character(kind=CK,len=:),allocatable,intent(out),optional :: name !! variable name
  818. call me%core%info(me%p,path,found,var_type,n_children,name)
  819. end subroutine json_file_variable_info
  820. !*****************************************************************************************
  821. !*****************************************************************************************
  822. !>
  823. ! Alternate version of [[json_file_variable_info]], where "path" is kind=CDK.
  824. !
  825. !@note If `found` is present, no exceptions will be thrown if an
  826. ! error occurs. Otherwise, an exception will be thrown if the
  827. ! variable is not found.
  828. subroutine wrap_json_file_variable_info(me,path,found,var_type,n_children,name)
  829. implicit none
  830. class(json_file),intent(inout) :: me
  831. character(kind=CDK,len=*),intent(in) :: path
  832. logical(LK),intent(out),optional :: found
  833. integer(IK),intent(out),optional :: var_type
  834. integer(IK),intent(out),optional :: n_children
  835. character(kind=CK,len=:),allocatable,intent(out),optional :: name !! variable name
  836. call me%info(to_unicode(path),found,var_type,n_children,name)
  837. end subroutine wrap_json_file_variable_info
  838. !*****************************************************************************************
  839. !*****************************************************************************************
  840. !> author: Jacob Williams
  841. ! date: 6/26/2016
  842. !
  843. ! Returns matrix information about a variable in a [[json_file(type)]].
  844. !
  845. !@note If `found` is present, no exceptions will be thrown if an
  846. ! error occurs. Otherwise, an exception will be thrown if the
  847. ! variable is not found.
  848. subroutine json_file_variable_matrix_info(me,path,is_matrix,found,&
  849. var_type,n_sets,set_size,name)
  850. implicit none
  851. class(json_file),intent(inout) :: me
  852. character(kind=CK,len=*),intent(in) :: path !! path to the variable
  853. logical(LK),intent(out) :: is_matrix !! true if it is a valid matrix
  854. logical(LK),intent(out),optional :: found !! true if it was found
  855. integer(IK),intent(out),optional :: var_type !! variable type of data in
  856. !! the matrix (if all elements have
  857. !! the same type)
  858. integer(IK),intent(out),optional :: n_sets !! number of data sets (i.e., matrix
  859. !! rows if using row-major order)
  860. integer(IK),intent(out),optional :: set_size !! size of each data set (i.e., matrix
  861. !! cols if using row-major order)
  862. character(kind=CK,len=:),allocatable,intent(out),optional :: name !! variable name
  863. call me%core%matrix_info(me%p,path,is_matrix,found,var_type,n_sets,set_size,name)
  864. end subroutine json_file_variable_matrix_info
  865. !*****************************************************************************************
  866. !*****************************************************************************************
  867. !>
  868. ! Alternate version of [[json_file_variable_matrix_info]], where "path" is kind=CDK.
  869. !
  870. !@note If `found` is present, no exceptions will be thrown if an
  871. ! error occurs. Otherwise, an exception will be thrown if the
  872. ! variable is not found.
  873. subroutine wrap_json_file_variable_matrix_info(me,path,is_matrix,found,&
  874. var_type,n_sets,set_size,name)
  875. implicit none
  876. class(json_file),intent(inout) :: me
  877. character(kind=CDK,len=*),intent(in) :: path !! path to the variable
  878. logical(LK),intent(out) :: is_matrix !! true if it is a valid matrix
  879. logical(LK),intent(out),optional :: found !! true if it was found
  880. integer(IK),intent(out),optional :: var_type !! variable type of data in
  881. !! the matrix (if all elements have
  882. !! the same type)
  883. integer(IK),intent(out),optional :: n_sets !! number of data sets (i.e., matrix
  884. !! rows if using row-major order)
  885. integer(IK),intent(out),optional :: set_size !! size of each data set (i.e., matrix
  886. !! cols if using row-major order)
  887. character(kind=CK,len=:),allocatable,intent(out),optional :: name !! variable name
  888. call me%matrix_info(to_unicode(path),is_matrix,found,var_type,n_sets,set_size,name)
  889. end subroutine wrap_json_file_variable_matrix_info
  890. !*****************************************************************************************
  891. !*****************************************************************************************
  892. !> author: Izaak Beekman
  893. ! date: 7/23/2015
  894. !
  895. ! Get a [[json_value]] pointer to the JSON file root.
  896. !
  897. !@note This is equivalent to calling ```[[json_file]]%get('$',p)```
  898. subroutine json_file_get_root(me,p)
  899. implicit none
  900. class(json_file),intent(inout) :: me
  901. type(json_value),pointer,intent(out) :: p !! pointer to the variable
  902. p => me%p
  903. end subroutine json_file_get_root
  904. !*****************************************************************************************
  905. !*****************************************************************************************
  906. !> author: Jacob Williams
  907. !
  908. ! Assignment operator for [[json_core(type)]] = [[json_core(type)]].
  909. ! This will duplicate the [[json_core(type)]] and also
  910. ! perform a deep copy of the [[json_value(type)]] data structure.
  911. subroutine assign_json_file(me,f)
  912. implicit none
  913. class(json_file),intent(out) :: me
  914. type(json_file),intent(in) :: f
  915. me%core = f%core ! no pointers here so OK to copy
  916. call me%core%clone(f%p,me%p)
  917. end subroutine assign_json_file
  918. !*****************************************************************************************
  919. !*****************************************************************************************
  920. !> author: Jacob Williams
  921. !
  922. ! Assignment operator for character = [[json_core(type)]].
  923. ! This is just a wrapper for the [[json_value_to_string]] routine.
  924. !
  925. !### Note
  926. ! * If an exception is raised or the file contains no data,
  927. ! this will return an empty string.
  928. subroutine assign_json_file_to_string(str,me)
  929. implicit none
  930. character(kind=CK,len=:),allocatable,intent(out) :: str
  931. class(json_file),intent(in) :: me
  932. type(json_core) :: core_copy !! a copy of `core` from `me`
  933. if (me%core%failed() .or. .not. associated(me%p)) then
  934. str = CK_''
  935. else
  936. ! This is sort of a hack. Since `me` has to have `intent(in)`
  937. ! for the assignment to work, we need to make a copy of `me%core`
  938. ! so we can call the low level routine (since it needs it to
  939. ! be `intent(inout)`) because it's possible for this
  940. ! function to raise an exception.
  941. core_copy = me%core ! copy the parser settings
  942. call core_copy%serialize(me%p,str)
  943. if (me%core%failed()) str = CK_''
  944. end if
  945. end subroutine assign_json_file_to_string
  946. !*****************************************************************************************
  947. !*****************************************************************************************
  948. !> author: Jacob Williams
  949. !
  950. ! Assignment operator for [[json_core(type)]] = character.
  951. ! This is just a wrapper for the [[json_file_load_from_string]] routine.
  952. subroutine assign_string_to_json_file(me,str)
  953. implicit none
  954. class(json_file),intent(inout) :: me
  955. character(kind=CK,len=*),intent(in) :: str
  956. if (associated(me%p)) call me%destroy()
  957. if (me%core%failed()) call me%core%clear_exceptions()
  958. call me%deserialize(str)
  959. end subroutine assign_string_to_json_file
  960. !*****************************************************************************************
  961. !*****************************************************************************************
  962. !> author: Jacob Williams
  963. !
  964. ! Alternate version of [[assign_string_to_json_file]], where "str" is kind=CDK.
  965. subroutine wrap_assign_string_to_json_file(me,str)
  966. implicit none
  967. class(json_file),intent(inout) :: me
  968. character(kind=CDK,len=*),intent(in) :: str
  969. call me%assign_string_to_json_file(to_unicode(str))
  970. end subroutine wrap_assign_string_to_json_file
  971. !*****************************************************************************************
  972. !*****************************************************************************************
  973. !> author: Jacob Williams
  974. !
  975. ! A wrapper for [[json_file_valid_path]] for the `.in.` operator
  976. function json_file_valid_path_op(path,me) result(found)
  977. implicit none
  978. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  979. class(json_file),intent(in) :: me !! the JSON file
  980. logical(LK) :: found !! if the variable was found
  981. type(json_core) :: core_copy !! a copy of `core` from `me`
  982. ! This is sort of a hack. Since `me` has to have `intent(in)`
  983. ! for the operator to work, we need to make a copy of `me%core`
  984. ! so we can call the low level routine (since it needs it to
  985. ! be `intent(inout)`) because it's technically possible for this
  986. ! function to raise an exception. This normally should never
  987. ! happen here unless the JSON structure is malformed.
  988. core_copy = me%core ! copy the settings (need them to know
  989. ! how to interpret the path)
  990. found = core_copy%valid_path(me%p, path) ! call the low-level routine
  991. call core_copy%destroy() ! just in case (but not really necessary)
  992. end function json_file_valid_path_op
  993. !*****************************************************************************************
  994. !*****************************************************************************************
  995. !> author: Jacob Williams
  996. !
  997. ! Alternate version of [[json_file_valid_path_op]], where "path" is kind=CDK.
  998. function wrap_json_file_valid_path_op(path,me) result(found)
  999. implicit none
  1000. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1001. class(json_file),intent(in) :: me !! the JSON file
  1002. logical(LK) :: found !! if the variable was found
  1003. found = to_unicode(path) .in. me
  1004. end function wrap_json_file_valid_path_op
  1005. !*****************************************************************************************
  1006. !*****************************************************************************************
  1007. !> author: Jacob Williams
  1008. !
  1009. ! Returns true if the `path` is present in the JSON file.
  1010. function json_file_valid_path(me,path) result(found)
  1011. implicit none
  1012. class(json_file),intent(inout) :: me
  1013. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1014. logical(LK) :: found !! if the variable was found
  1015. found = me%core%valid_path(me%p, path)
  1016. end function json_file_valid_path
  1017. !*****************************************************************************************
  1018. !*****************************************************************************************
  1019. !> author: Jacob Williams
  1020. !
  1021. ! Alternate version of [[json_file_valid_path]], where "path" is kind=CDK.
  1022. function wrap_json_file_valid_path(me,path) result(found)
  1023. implicit none
  1024. class(json_file),intent(inout) :: me
  1025. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1026. logical(LK) :: found !! if the variable was found
  1027. found = me%valid_path(to_unicode(path))
  1028. end function wrap_json_file_valid_path
  1029. !*****************************************************************************************
  1030. !*****************************************************************************************
  1031. !> author: Jacob Williams
  1032. !
  1033. ! Rename a variable in a JSON file.
  1034. subroutine json_file_rename(me,path,name,found)
  1035. implicit none
  1036. class(json_file),intent(inout) :: me
  1037. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1038. character(kind=CK,len=*),intent(in) :: name !! the new name
  1039. logical(LK),intent(out),optional :: found !! if the variable was found
  1040. call me%core%rename(me%p, path, name, found)
  1041. end subroutine json_file_rename
  1042. !*****************************************************************************************
  1043. !*****************************************************************************************
  1044. !> author: Jacob Williams
  1045. !
  1046. ! Alternate version of [[json_file_rename]], where "path" and "name" are kind=CDK.
  1047. subroutine wrap_json_file_rename(me,path,name,found)
  1048. implicit none
  1049. class(json_file),intent(inout) :: me
  1050. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1051. character(kind=CDK,len=*),intent(in) :: name !! the new name
  1052. logical(LK),intent(out),optional :: found !! if the variable was found
  1053. call me%json_file_rename(to_unicode(path),to_unicode(name),found)
  1054. end subroutine wrap_json_file_rename
  1055. !*****************************************************************************************
  1056. !*****************************************************************************************
  1057. !> author: Jacob Williams
  1058. !
  1059. ! Wrapper for [[json_file_rename]] where "path" is kind=CDK).
  1060. subroutine json_file_rename_path_ascii(me,path,name,found)
  1061. implicit none
  1062. class(json_file),intent(inout) :: me
  1063. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1064. character(kind=CK,len=*),intent(in) :: name !! the new name
  1065. logical(LK),intent(out),optional :: found !! if the variable was found
  1066. call me%json_file_rename(to_unicode(path),name,found)
  1067. end subroutine json_file_rename_path_ascii
  1068. !*****************************************************************************************
  1069. !*****************************************************************************************
  1070. !> author: Jacob Williams
  1071. !
  1072. ! Wrapper for [[json_file_rename]] where "name" is kind=CDK).
  1073. subroutine json_file_rename_name_ascii(me,path,name,found)
  1074. implicit none
  1075. class(json_file),intent(inout) :: me
  1076. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1077. character(kind=CDK,len=*),intent(in) :: name !! the new name
  1078. logical(LK),intent(out),optional :: found !! if the variable was found
  1079. call me%json_file_rename(path,to_unicode(name),found)
  1080. end subroutine json_file_rename_name_ascii
  1081. !*****************************************************************************************
  1082. !*****************************************************************************************
  1083. !> author: Jacob Williams
  1084. ! date: 2/3/2014
  1085. !
  1086. ! Get a [[json_value]] pointer to an object from a JSON file.
  1087. subroutine json_file_get_object(me, path, p, found)
  1088. implicit none
  1089. class(json_file),intent(inout) :: me
  1090. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1091. type(json_value),pointer,intent(out) :: p !! pointer to the variable
  1092. logical(LK),intent(out),optional :: found !! if it was really found
  1093. call me%core%get(me%p, path, p, found)
  1094. end subroutine json_file_get_object
  1095. !*****************************************************************************************
  1096. !*****************************************************************************************
  1097. !>
  1098. ! Alternate version of [[json_file_get_object]], where "path" is kind=CDK.
  1099. subroutine wrap_json_file_get_object(me, path, p, found)
  1100. implicit none
  1101. class(json_file),intent(inout) :: me
  1102. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1103. type(json_value),pointer,intent(out) :: p !! pointer to the variable
  1104. logical(LK),intent(out),optional :: found !! if it was really found
  1105. call me%get(to_unicode(path), p, found)
  1106. end subroutine wrap_json_file_get_object
  1107. !*****************************************************************************************
  1108. !*****************************************************************************************
  1109. !> author: Jacob Williams
  1110. ! date: 12/9/2013
  1111. !
  1112. ! Get an integer value from a JSON file.
  1113. subroutine json_file_get_integer(me, path, val, found, default)
  1114. implicit none
  1115. class(json_file),intent(inout) :: me
  1116. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1117. integer(IK),intent(out) :: val !! value
  1118. logical(LK),intent(out),optional :: found !! if it was really found
  1119. integer(IK),intent(in),optional :: default
  1120. call me%core%get(me%p, path, val, found, default)
  1121. end subroutine json_file_get_integer
  1122. !*****************************************************************************************
  1123. !*****************************************************************************************
  1124. !>
  1125. ! Alternate version of [[json_file_get_integer]], where "path" is kind=CDK.
  1126. subroutine wrap_json_file_get_integer(me, path, val, found, default)
  1127. implicit none
  1128. class(json_file),intent(inout) :: me
  1129. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1130. integer(IK),intent(out) :: val !! value
  1131. logical(LK),intent(out),optional :: found !! if it was really found
  1132. integer(IK),intent(in),optional :: default
  1133. call me%get(to_unicode(path), val, found, default)
  1134. end subroutine wrap_json_file_get_integer
  1135. !*****************************************************************************************
  1136. !*****************************************************************************************
  1137. !> author: Jacob Williams
  1138. ! date: 1/20/2014
  1139. !
  1140. ! Get an integer vector from a JSON file.
  1141. subroutine json_file_get_integer_vec(me, path, vec, found, default)
  1142. implicit none
  1143. class(json_file),intent(inout) :: me
  1144. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1145. integer(IK),dimension(:),allocatable,intent(out) :: vec !! the value vector
  1146. logical(LK),intent(out),optional :: found !! if it was really found
  1147. integer(IK),dimension(:),intent(in),optional :: default
  1148. call me%core%get(me%p, path, vec, found, default)
  1149. end subroutine json_file_get_integer_vec
  1150. !*****************************************************************************************
  1151. !*****************************************************************************************
  1152. !>
  1153. ! Alternate version of [[json_file_get_integer_vec]], where "path" is kind=CDK.
  1154. subroutine wrap_json_file_get_integer_vec(me, path, vec, found, default)
  1155. implicit none
  1156. class(json_file),intent(inout) :: me
  1157. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1158. integer(IK),dimension(:),allocatable,intent(out) :: vec !! the value vector
  1159. logical(LK),intent(out),optional :: found !! if it was really found
  1160. integer(IK),dimension(:),intent(in),optional :: default
  1161. call me%get(to_unicode(path), vec, found, default)
  1162. end subroutine wrap_json_file_get_integer_vec
  1163. !*****************************************************************************************
  1164. !*****************************************************************************************
  1165. !> author: Jacob Williams
  1166. ! date: 12/9/2013
  1167. !
  1168. ! Get a real(RK) variable value from a JSON file.
  1169. subroutine json_file_get_real (me, path, val, found, default)
  1170. implicit none
  1171. class(json_file),intent(inout) :: me
  1172. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1173. real(RK),intent(out) :: val !! value
  1174. logical(LK),intent(out),optional :: found !! if it was really found
  1175. real(RK),intent(in),optional :: default
  1176. call me%core%get(me%p, path, val, found, default)
  1177. end subroutine json_file_get_real
  1178. !*****************************************************************************************
  1179. !*****************************************************************************************
  1180. !>
  1181. ! Alternate version of [[json_file_get_real]], where "path" is kind=CDK.
  1182. subroutine wrap_json_file_get_real (me, path, val, found, default)
  1183. implicit none
  1184. class(json_file),intent(inout) :: me
  1185. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1186. real(RK),intent(out) :: val !! value
  1187. logical(LK),intent(out),optional :: found !! if it was really found
  1188. real(RK),intent(in),optional :: default
  1189. call me%get(to_unicode(path), val, found, default)
  1190. end subroutine wrap_json_file_get_real
  1191. !*****************************************************************************************
  1192. !*****************************************************************************************
  1193. !> author: Jacob Williams
  1194. ! date: 1/19/2014
  1195. !
  1196. ! Get a real(RK) vector from a JSON file.
  1197. subroutine json_file_get_real_vec(me, path, vec, found, default)
  1198. implicit none
  1199. class(json_file),intent(inout) :: me
  1200. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1201. real(RK),dimension(:),allocatable,intent(out) :: vec !! the value vector
  1202. logical(LK),intent(out),optional :: found !! if it was really found
  1203. real(RK),dimension(:),intent(in),optional :: default
  1204. call me%core%get(me%p, path, vec, found, default)
  1205. end subroutine json_file_get_real_vec
  1206. !*****************************************************************************************
  1207. !*****************************************************************************************
  1208. !>
  1209. ! Alternate version of [[json_file_get_real_vec]], where "path" is kind=CDK.
  1210. subroutine wrap_json_file_get_real_vec(me, path, vec, found, default)
  1211. implicit none
  1212. class(json_file),intent(inout) :: me
  1213. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1214. real(RK),dimension(:),allocatable,intent(out) :: vec !! the value vector
  1215. logical(LK),intent(out),optional :: found !! if it was really found
  1216. real(RK),dimension(:),intent(in),optional :: default
  1217. call me%get(to_unicode(path), vec, found, default)
  1218. end subroutine wrap_json_file_get_real_vec
  1219. !*****************************************************************************************
  1220. #ifndef REAL32
  1221. !*****************************************************************************************
  1222. !> author: Jacob Williams
  1223. ! date: 1/21/2019
  1224. !
  1225. ! Alternate version of [[json_file_get_real]] where `val` is `real32`.
  1226. subroutine json_file_get_real32 (me, path, val, found, default)
  1227. implicit none
  1228. class(json_file),intent(inout) :: me
  1229. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1230. real(real32),intent(out) :: val !! value
  1231. logical(LK),intent(out),optional :: found !! if it was really found
  1232. real(real32),intent(in),optional :: default
  1233. call me%core%get(me%p, path, val, found, default)
  1234. end subroutine json_file_get_real32
  1235. !*****************************************************************************************
  1236. !*****************************************************************************************
  1237. !>
  1238. ! Alternate version of [[json_file_get_real32]], where "path" is kind=CDK.
  1239. subroutine wrap_json_file_get_real32 (me, path, val, found, default)
  1240. implicit none
  1241. class(json_file),intent(inout) :: me
  1242. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1243. real(real32),intent(out) :: val !! value
  1244. logical(LK),intent(out),optional :: found !! if it was really found
  1245. real(real32),intent(in),optional :: default
  1246. call me%get(to_unicode(path), val, found, default)
  1247. end subroutine wrap_json_file_get_real32
  1248. !*****************************************************************************************
  1249. !*****************************************************************************************
  1250. !> author: Jacob Williams
  1251. ! date: 1/21/2019
  1252. !
  1253. ! Alternate version of [[json_file_get_real_vec]] where `vec` is `real32`.
  1254. subroutine json_file_get_real32_vec(me, path, vec, found, default)
  1255. implicit none
  1256. class(json_file),intent(inout) :: me
  1257. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1258. real(real32),dimension(:),allocatable,intent(out) :: vec !! the value vector
  1259. logical(LK),intent(out),optional :: found !! if it was really found
  1260. real(real32),dimension(:),intent(in),optional :: default
  1261. call me%core%get(me%p, path, vec, found, default)
  1262. end subroutine json_file_get_real32_vec
  1263. !*****************************************************************************************
  1264. !*****************************************************************************************
  1265. !>
  1266. ! Alternate version of [[json_file_get_real32_vec]], where "path" is kind=CDK.
  1267. subroutine wrap_json_file_get_real32_vec(me, path, vec, found, default)
  1268. implicit none
  1269. class(json_file),intent(inout) :: me
  1270. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1271. real(real32),dimension(:),allocatable,intent(out) :: vec !! the value vector
  1272. logical(LK),intent(out),optional :: found !! if it was really found
  1273. real(real32),dimension(:),intent(in),optional :: default
  1274. call me%get(to_unicode(path), vec, found, default)
  1275. end subroutine wrap_json_file_get_real32_vec
  1276. !*****************************************************************************************
  1277. #endif
  1278. #ifdef REAL128
  1279. !*****************************************************************************************
  1280. !> author: Jacob Williams
  1281. ! date: 1/21/2019
  1282. !
  1283. ! Alternate version of [[json_file_get_real]] where `val` is `real64`.
  1284. subroutine json_file_get_real64 (me, path, val, found, default)
  1285. implicit none
  1286. class(json_file),intent(inout) :: me
  1287. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1288. real(real64),intent(out) :: val !! value
  1289. logical(LK),intent(out),optional :: found !! if it was really found
  1290. real(real64),intent(in),optional :: default
  1291. call me%core%get(me%p, path, val, found, default)
  1292. end subroutine json_file_get_real64
  1293. !*****************************************************************************************
  1294. !*****************************************************************************************
  1295. !>
  1296. ! Alternate version of [[json_file_get_real64]], where "path" is kind=CDK.
  1297. subroutine wrap_json_file_get_real64 (me, path, val, found, default)
  1298. implicit none
  1299. class(json_file),intent(inout) :: me
  1300. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1301. real(real64),intent(out) :: val !! value
  1302. logical(LK),intent(out),optional :: found !! if it was really found
  1303. real(real64),intent(in),optional :: default
  1304. call me%get(to_unicode(path), val, found, default)
  1305. end subroutine wrap_json_file_get_real64
  1306. !*****************************************************************************************
  1307. !*****************************************************************************************
  1308. !> author: Jacob Williams
  1309. ! date: 1/21/2019
  1310. !
  1311. ! Alternate version of [[json_file_get_real_vec]] where `vec` is `real64`.
  1312. subroutine json_file_get_real64_vec(me, path, vec, found, default)
  1313. implicit none
  1314. class(json_file),intent(inout) :: me
  1315. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1316. real(real64),dimension(:),allocatable,intent(out) :: vec !! the value vector
  1317. logical(LK),intent(out),optional :: found !! if it was really found
  1318. real(real64),dimension(:),intent(in),optional :: default
  1319. call me%core%get(me%p, path, vec, found, default)
  1320. end subroutine json_file_get_real64_vec
  1321. !*****************************************************************************************
  1322. !*****************************************************************************************
  1323. !>
  1324. ! Alternate version of [[json_file_get_real64_vec]], where "path" is kind=CDK.
  1325. subroutine wrap_json_file_get_real64_vec(me, path, vec, found, default)
  1326. implicit none
  1327. class(json_file),intent(inout) :: me
  1328. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1329. real(real64),dimension(:),allocatable,intent(out) :: vec !! the value vector
  1330. logical(LK),intent(out),optional :: found !! if it was really found
  1331. real(real64),dimension(:),intent(in),optional :: default
  1332. call me%get(to_unicode(path), vec, found, default)
  1333. end subroutine wrap_json_file_get_real64_vec
  1334. !*****************************************************************************************
  1335. #endif
  1336. !*****************************************************************************************
  1337. !> author: Jacob Williams
  1338. ! date: 12/9/2013
  1339. !
  1340. ! Get a logical(LK) value from a JSON file.
  1341. subroutine json_file_get_logical(me,path,val,found,default)
  1342. implicit none
  1343. class(json_file),intent(inout) :: me
  1344. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1345. logical(LK),intent(out) :: val !! value
  1346. logical(LK),intent(out),optional :: found !! if it was really found
  1347. logical(LK),intent(in),optional :: default
  1348. call me%core%get(me%p, path, val, found, default)
  1349. end subroutine json_file_get_logical
  1350. !*****************************************************************************************
  1351. !*****************************************************************************************
  1352. !>
  1353. ! Alternate version of [[json_file_get_logical]], where "path" is kind=CDK.
  1354. subroutine wrap_json_file_get_logical(me,path,val,found,default)
  1355. implicit none
  1356. class(json_file),intent(inout) :: me
  1357. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1358. logical(LK),intent(out) :: val !! value
  1359. logical(LK),intent(out),optional :: found !! if it was really found
  1360. logical(LK),intent(in),optional :: default
  1361. call me%get(to_unicode(path), val, found, default)
  1362. end subroutine wrap_json_file_get_logical
  1363. !*****************************************************************************************
  1364. !*****************************************************************************************
  1365. !> author: Jacob Williams
  1366. ! date: 1/20/2014
  1367. !
  1368. ! Get a logical(LK) vector from a JSON file.
  1369. subroutine json_file_get_logical_vec(me, path, vec, found, default)
  1370. implicit none
  1371. class(json_file),intent(inout) :: me
  1372. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1373. logical(LK),dimension(:),allocatable,intent(out) :: vec !! the value vector
  1374. logical(LK),intent(out),optional :: found !! if it was really found
  1375. logical(LK),dimension(:),intent(in),optional :: default
  1376. call me%core%get(me%p, path, vec, found, default)
  1377. end subroutine json_file_get_logical_vec
  1378. !*****************************************************************************************
  1379. !*****************************************************************************************
  1380. !>
  1381. ! Alternate version of [[json_file_get_logical_vec]], where "path" is kind=CDK.
  1382. subroutine wrap_json_file_get_logical_vec(me, path, vec, found, default)
  1383. implicit none
  1384. class(json_file),intent(inout) :: me
  1385. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1386. logical(LK),dimension(:),allocatable,intent(out) :: vec !! the value vector
  1387. logical(LK),intent(out),optional :: found !! if it was really found
  1388. logical(LK),dimension(:),intent(in),optional :: default
  1389. call me%get(to_unicode(path), vec, found, default)
  1390. end subroutine wrap_json_file_get_logical_vec
  1391. !*****************************************************************************************
  1392. !*****************************************************************************************
  1393. !> author: Jacob Williams
  1394. ! date: 12/9/2013
  1395. !
  1396. ! Get a character string from a json file.
  1397. ! The output val is an allocatable character string.
  1398. subroutine json_file_get_string(me, path, val, found, default)
  1399. implicit none
  1400. class(json_file),intent(inout) :: me
  1401. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1402. character(kind=CK,len=:),allocatable,intent(out) :: val !! value
  1403. logical(LK),intent(out),optional :: found !! if it was really found
  1404. character(kind=CK,len=*),intent(in),optional :: default
  1405. call me%core%get(me%p, path, val, found, default)
  1406. end subroutine json_file_get_string
  1407. !*****************************************************************************************
  1408. !*****************************************************************************************
  1409. !>
  1410. ! Alternate version of [[json_file_get_string]], where "path" is kind=CDK.
  1411. subroutine wrap_json_file_get_string(me, path, val, found, default)
  1412. implicit none
  1413. class(json_file),intent(inout) :: me
  1414. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1415. character(kind=CK,len=:),allocatable,intent(out) :: val !! value
  1416. logical(LK),intent(out),optional :: found !! if it was really found
  1417. character(kind=CK,len=*),intent(in),optional :: default
  1418. call me%get(to_unicode(path), val, found, default)
  1419. end subroutine wrap_json_file_get_string
  1420. !*****************************************************************************************
  1421. !*****************************************************************************************
  1422. !> author: Jacob Williams
  1423. ! date: 1/19/2014
  1424. !
  1425. ! Get a string vector from a JSON file.
  1426. subroutine json_file_get_string_vec(me, path, vec, found, default)
  1427. implicit none
  1428. class(json_file),intent(inout) :: me
  1429. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1430. character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec !! value vector
  1431. logical(LK),intent(out),optional :: found !! if it was really found
  1432. character(kind=CK,len=*),dimension(:),intent(in),optional :: default
  1433. call me%core%get(me%p, path, vec, found, default)
  1434. end subroutine json_file_get_string_vec
  1435. !*****************************************************************************************
  1436. !*****************************************************************************************
  1437. !>
  1438. ! Alternate version of [[json_file_get_string_vec]], where "path" is kind=CDK.
  1439. subroutine wrap_json_file_get_string_vec(me, path, vec, found, default)
  1440. implicit none
  1441. class(json_file),intent(inout) :: me
  1442. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1443. character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec !! value vector
  1444. logical(LK),intent(out),optional :: found !! if it was really found
  1445. character(kind=CK,len=*),dimension(:),intent(in),optional :: default
  1446. call me%get(to_unicode(path), vec, found, default)
  1447. end subroutine wrap_json_file_get_string_vec
  1448. !*****************************************************************************************
  1449. !*****************************************************************************************
  1450. !> author: Jacob Williams
  1451. ! date: 12/17/2016
  1452. !
  1453. ! Get an (allocatable length) string vector from a JSON file.
  1454. ! This is just a wrapper for [[json_get_alloc_string_vec_by_path]].
  1455. subroutine json_file_get_alloc_string_vec(me, path, vec, ilen, found, default, default_ilen)
  1456. implicit none
  1457. class(json_file),intent(inout) :: me
  1458. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1459. character(kind=CK,len=:),dimension(:),allocatable,intent(out) :: vec !! value vector
  1460. integer(IK),dimension(:),allocatable,intent(out) :: ilen !! the actual length
  1461. !! of each character
  1462. !! string in the array
  1463. logical(LK),intent(out),optional :: found
  1464. character(kind=CK,len=*),dimension(:),intent(in),optional :: default
  1465. integer(IK),dimension(:),intent(in),optional :: default_ilen !! the actual
  1466. !! length of `default`
  1467. call me%core%get(me%p, path, vec, ilen, found, default, default_ilen)
  1468. end subroutine json_file_get_alloc_string_vec
  1469. !*****************************************************************************************
  1470. !*****************************************************************************************
  1471. !>
  1472. ! Alternate version of [[json_file_get_alloc_string_vec]], where "path" is kind=CDK.
  1473. ! This is just a wrapper for [[wrap_json_get_alloc_string_vec_by_path]].
  1474. subroutine wrap_json_file_get_alloc_string_vec(me, path, vec, ilen, found, default, default_ilen)
  1475. implicit none
  1476. class(json_file),intent(inout) :: me
  1477. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1478. character(kind=CK,len=:),dimension(:),allocatable,intent(out) :: vec !! value vector
  1479. integer(IK),dimension(:),allocatable,intent(out) :: ilen !! the actual length
  1480. !! of each character
  1481. !! string in the array
  1482. logical(LK),intent(out),optional :: found
  1483. character(kind=CK,len=*),dimension(:),intent(in),optional :: default
  1484. integer(IK),dimension(:),intent(in),optional :: default_ilen !! the actual
  1485. !! length of `default`
  1486. call me%get(to_unicode(path), vec, ilen, found, default, default_ilen)
  1487. end subroutine wrap_json_file_get_alloc_string_vec
  1488. !*****************************************************************************************
  1489. !*****************************************************************************************
  1490. !> author: Jacob Williams
  1491. !
  1492. ! Add a [[json_value]] pointer as the root object to a JSON file.
  1493. !
  1494. !### Note
  1495. !
  1496. ! This is mostly equivalent to:
  1497. !```fortran
  1498. ! f = [[json_file]](p)
  1499. !```
  1500. ! But without the finalization calls.
  1501. !
  1502. ! And:
  1503. !```fortran
  1504. ! if (destroy_original) call [[json_file]]%destroy()
  1505. ! call [[json_file]]%add('$',p)
  1506. !```
  1507. subroutine json_file_add(me,p,destroy_original)
  1508. implicit none
  1509. class(json_file),intent(inout) :: me
  1510. type(json_value),pointer,intent(in) :: p !! pointer to the variable to add
  1511. logical(LK),intent(in),optional :: destroy_original !! if the file currently contains
  1512. !! an associated pointer, it is
  1513. !! destroyed. [Default is True]
  1514. logical(LK) :: destroy !! if `me%p` is to be destroyed
  1515. if (present(destroy_original)) then
  1516. destroy = destroy_original
  1517. else
  1518. destroy = .true. ! default
  1519. end if
  1520. if (destroy) call me%core%destroy(me%p)
  1521. me%p => p
  1522. end subroutine json_file_add
  1523. !*****************************************************************************************
  1524. !*****************************************************************************************
  1525. !> author: Jacob Williams
  1526. !
  1527. ! Add a [[json_value]] pointer to an object to a JSON file.
  1528. subroutine json_file_add_object(me,path,p,found,was_created)
  1529. implicit none
  1530. class(json_file),intent(inout) :: me
  1531. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1532. type(json_value),pointer,intent(in) :: p !! pointer to the variable to add
  1533. logical(LK),intent(out),optional :: found !! if the variable was found
  1534. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1535. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1536. call me%core%add_by_path(me%p,path,p,found,was_created)
  1537. end subroutine json_file_add_object
  1538. !*****************************************************************************************
  1539. !*****************************************************************************************
  1540. !> author: Jacob Williams
  1541. !
  1542. ! Alternate version of [[json_file_add_object]], where "path" is kind=CDK.
  1543. subroutine wrap_json_file_add_object(me,path,p,found,was_created)
  1544. implicit none
  1545. class(json_file),intent(inout) :: me
  1546. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1547. type(json_value),pointer,intent(in) :: p !! pointer to the variable to add
  1548. logical(LK),intent(out),optional :: found !! if the variable was found
  1549. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1550. call me%json_file_add_object(to_unicode(path),p,found,was_created)
  1551. end subroutine wrap_json_file_add_object
  1552. !*****************************************************************************************
  1553. !*****************************************************************************************
  1554. !> author: Jacob Williams
  1555. !
  1556. ! Add an integer value to a JSON file.
  1557. subroutine json_file_add_integer(me,path,val,found,was_created)
  1558. implicit none
  1559. class(json_file),intent(inout) :: me
  1560. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1561. integer(IK),intent(in) :: val !! value
  1562. logical(LK),intent(out),optional :: found !! if the variable was found
  1563. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1564. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1565. call me%core%add_by_path(me%p,path,val,found,was_created)
  1566. end subroutine json_file_add_integer
  1567. !*****************************************************************************************
  1568. !*****************************************************************************************
  1569. !> author: Jacob Williams
  1570. !
  1571. ! Alternate version of [[json_file_add_integer]], where "path" is kind=CDK.
  1572. subroutine wrap_json_file_add_integer(me,path,val,found,was_created)
  1573. implicit none
  1574. class(json_file),intent(inout) :: me
  1575. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1576. integer(IK),intent(in) :: val !! value
  1577. logical(LK),intent(out),optional :: found !! if the variable was found
  1578. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1579. call me%json_file_add_integer(to_unicode(path),val,found,was_created)
  1580. end subroutine wrap_json_file_add_integer
  1581. !*****************************************************************************************
  1582. !*****************************************************************************************
  1583. !> author: Jacob Williams
  1584. !
  1585. ! Add an integer vector to a JSON file.
  1586. subroutine json_file_add_integer_vec(me,path,vec,found,was_created)
  1587. implicit none
  1588. class(json_file),intent(inout) :: me
  1589. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1590. integer(IK),dimension(:),intent(in) :: vec !! the value vector
  1591. logical(LK),intent(out),optional :: found !! if the variable was found
  1592. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1593. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1594. call me%core%add_by_path(me%p,path,vec,found,was_created)
  1595. end subroutine json_file_add_integer_vec
  1596. !*****************************************************************************************
  1597. !*****************************************************************************************
  1598. !> author: Jacob Williams
  1599. !
  1600. ! Alternate version of [[json_file_add_integer_vec]], where "path" is kind=CDK.
  1601. subroutine wrap_json_file_add_integer_vec(me,path,vec,found,was_created)
  1602. implicit none
  1603. class(json_file),intent(inout) :: me
  1604. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1605. integer(IK),dimension(:),intent(in) :: vec !! the value vector
  1606. logical(LK),intent(out),optional :: found !! if the variable was found
  1607. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1608. call me%json_file_add_integer_vec(to_unicode(path),vec,found,was_created)
  1609. end subroutine wrap_json_file_add_integer_vec
  1610. !*****************************************************************************************
  1611. !*****************************************************************************************
  1612. !> author: Jacob Williams
  1613. !
  1614. ! Add a real(RK) variable value to a JSON file.
  1615. subroutine json_file_add_real(me,path,val,found,was_created)
  1616. implicit none
  1617. class(json_file),intent(inout) :: me
  1618. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1619. real(RK),intent(in) :: val !! value
  1620. logical(LK),intent(out),optional :: found !! if the variable was found
  1621. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1622. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1623. call me%core%add_by_path(me%p,path,val,found,was_created)
  1624. end subroutine json_file_add_real
  1625. !*****************************************************************************************
  1626. !*****************************************************************************************
  1627. !> author: Jacob Williams
  1628. !
  1629. ! Alternate version of [[json_file_add_real]], where "path" is kind=CDK.
  1630. subroutine wrap_json_file_add_real(me,path,val,found,was_created)
  1631. implicit none
  1632. class(json_file),intent(inout) :: me
  1633. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1634. real(RK),intent(in) :: val !! value
  1635. logical(LK),intent(out),optional :: found !! if the variable was found
  1636. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1637. call me%json_file_add_real(to_unicode(path),val,found,was_created)
  1638. end subroutine wrap_json_file_add_real
  1639. !*****************************************************************************************
  1640. !*****************************************************************************************
  1641. !> author: Jacob Williams
  1642. !
  1643. ! Add a real(RK) vector to a JSON file.
  1644. subroutine json_file_add_real_vec(me,path,vec,found,was_created)
  1645. implicit none
  1646. class(json_file),intent(inout) :: me
  1647. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1648. real(RK),dimension(:),intent(in) :: vec !! the value vector
  1649. logical(LK),intent(out),optional :: found !! if the variable was found
  1650. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1651. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1652. call me%core%add_by_path(me%p,path,vec,found,was_created)
  1653. end subroutine json_file_add_real_vec
  1654. !*****************************************************************************************
  1655. !*****************************************************************************************
  1656. !> author: Jacob Williams
  1657. !
  1658. ! Alternate version of [[json_file_add_real_vec]], where "path" is kind=CDK.
  1659. subroutine wrap_json_file_add_real_vec(me,path,vec,found,was_created)
  1660. implicit none
  1661. class(json_file),intent(inout) :: me
  1662. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1663. real(RK),dimension(:),intent(in) :: vec !! the value vector
  1664. logical(LK),intent(out),optional :: found !! if the variable was found
  1665. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1666. call me%json_file_add_real_vec(to_unicode(path),vec,found,was_created)
  1667. end subroutine wrap_json_file_add_real_vec
  1668. !*****************************************************************************************
  1669. #ifndef REAL32
  1670. !*****************************************************************************************
  1671. !> author: Jacob Williams
  1672. !
  1673. ! Alternate version of [[json_file_add_real]] where `val` is `real32`.
  1674. subroutine json_file_add_real32(me,path,val,found,was_created)
  1675. implicit none
  1676. class(json_file),intent(inout) :: me
  1677. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1678. real(real32),intent(in) :: val !! value
  1679. logical(LK),intent(out),optional :: found !! if the variable was found
  1680. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1681. call me%core%add_by_path(me%p,path,val,found,was_created)
  1682. end subroutine json_file_add_real32
  1683. !*****************************************************************************************
  1684. !*****************************************************************************************
  1685. !> author: Jacob Williams
  1686. !
  1687. ! Alternate version of [[json_file_add_real32]], where "path" is kind=CDK.
  1688. subroutine wrap_json_file_add_real32(me,path,val,found,was_created)
  1689. implicit none
  1690. class(json_file),intent(inout) :: me
  1691. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1692. real(real32),intent(in) :: val !! value
  1693. logical(LK),intent(out),optional :: found !! if the variable was found
  1694. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1695. call me%json_file_add_real32(to_unicode(path),val,found,was_created)
  1696. end subroutine wrap_json_file_add_real32
  1697. !*****************************************************************************************
  1698. !*****************************************************************************************
  1699. !> author: Jacob Williams
  1700. !
  1701. ! Alternate version of [[json_file_add_real_vec]] where `vec` is `real32`.
  1702. subroutine json_file_add_real32_vec(me,path,vec,found,was_created)
  1703. implicit none
  1704. class(json_file),intent(inout) :: me
  1705. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1706. real(real32),dimension(:),intent(in) :: vec !! the value vector
  1707. logical(LK),intent(out),optional :: found !! if the variable was found
  1708. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1709. call me%core%add_by_path(me%p,path,vec,found,was_created)
  1710. end subroutine json_file_add_real32_vec
  1711. !*****************************************************************************************
  1712. !*****************************************************************************************
  1713. !> author: Jacob Williams
  1714. !
  1715. ! Alternate version of [[json_file_add_real32_vec]], where "path" is kind=CDK.
  1716. subroutine wrap_json_file_add_real32_vec(me,path,vec,found,was_created)
  1717. implicit none
  1718. class(json_file),intent(inout) :: me
  1719. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1720. real(real32),dimension(:),intent(in) :: vec !! the value vector
  1721. logical(LK),intent(out),optional :: found !! if the variable was found
  1722. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1723. call me%json_file_add_real32_vec(to_unicode(path),vec,found,was_created)
  1724. end subroutine wrap_json_file_add_real32_vec
  1725. !*****************************************************************************************
  1726. #endif
  1727. #ifdef REAL128
  1728. !*****************************************************************************************
  1729. !> author: Jacob Williams
  1730. !
  1731. ! Alternate version of [[json_file_add_real]] where `val` is `real64`.
  1732. subroutine json_file_add_real64(me,path,val,found,was_created)
  1733. implicit none
  1734. class(json_file),intent(inout) :: me
  1735. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1736. real(real64),intent(in) :: val !! value
  1737. logical(LK),intent(out),optional :: found !! if the variable was found
  1738. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1739. call me%core%add_by_path(me%p,path,val,found,was_created)
  1740. end subroutine json_file_add_real64
  1741. !*****************************************************************************************
  1742. !*****************************************************************************************
  1743. !> author: Jacob Williams
  1744. !
  1745. ! Alternate version of [[json_file_add_real64]], where "path" is kind=CDK.
  1746. subroutine wrap_json_file_add_real64(me,path,val,found,was_created)
  1747. implicit none
  1748. class(json_file),intent(inout) :: me
  1749. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1750. real(real64),intent(in) :: val !! value
  1751. logical(LK),intent(out),optional :: found !! if the variable was found
  1752. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1753. call me%json_file_add_real64(to_unicode(path),val,found,was_created)
  1754. end subroutine wrap_json_file_add_real64
  1755. !*****************************************************************************************
  1756. !*****************************************************************************************
  1757. !> author: Jacob Williams
  1758. !
  1759. ! Alternate version of [[json_file_add_real_vec]] where `vec` is `real64`.
  1760. subroutine json_file_add_real64_vec(me,path,vec,found,was_created)
  1761. implicit none
  1762. class(json_file),intent(inout) :: me
  1763. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1764. real(real64),dimension(:),intent(in) :: vec !! the value vector
  1765. logical(LK),intent(out),optional :: found !! if the variable was found
  1766. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1767. call me%core%add_by_path(me%p,path,vec,found,was_created)
  1768. end subroutine json_file_add_real64_vec
  1769. !*****************************************************************************************
  1770. !*****************************************************************************************
  1771. !> author: Jacob Williams
  1772. !
  1773. ! Alternate version of [[json_file_add_real64_vec]], where "path" is kind=CDK.
  1774. subroutine wrap_json_file_add_real64_vec(me,path,vec,found,was_created)
  1775. implicit none
  1776. class(json_file),intent(inout) :: me
  1777. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1778. real(real64),dimension(:),intent(in) :: vec !! the value vector
  1779. logical(LK),intent(out),optional :: found !! if the variable was found
  1780. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1781. call me%json_file_add_real64_vec(to_unicode(path),vec,found,was_created)
  1782. end subroutine wrap_json_file_add_real64_vec
  1783. !*****************************************************************************************
  1784. #endif
  1785. !*****************************************************************************************
  1786. !> author: Jacob Williams
  1787. !
  1788. ! Add a logical(LK) value to a JSON file.
  1789. subroutine json_file_add_logical(me,path,val,found,was_created)
  1790. implicit none
  1791. class(json_file),intent(inout) :: me
  1792. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1793. logical(LK),intent(in) :: val !! value
  1794. logical(LK),intent(out),optional :: found !! if the variable was found
  1795. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1796. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1797. call me%core%add_by_path(me%p,path,val,found,was_created)
  1798. end subroutine json_file_add_logical
  1799. !*****************************************************************************************
  1800. !*****************************************************************************************
  1801. !> author: Jacob Williams
  1802. !
  1803. ! Alternate version of [[json_file_add_logical]], where "path" is kind=CDK.
  1804. subroutine wrap_json_file_add_logical(me,path,val,found,was_created)
  1805. implicit none
  1806. class(json_file),intent(inout) :: me
  1807. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1808. logical(LK),intent(in) :: val !! value
  1809. logical(LK),intent(out),optional :: found !! if the variable was found
  1810. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1811. call me%json_file_add_logical(to_unicode(path),val,found,was_created)
  1812. end subroutine wrap_json_file_add_logical
  1813. !*****************************************************************************************
  1814. !*****************************************************************************************
  1815. !> author: Jacob Williams
  1816. !
  1817. ! Add a logical(LK) vector to a JSON file.
  1818. subroutine json_file_add_logical_vec(me,path,vec,found,was_created)
  1819. implicit none
  1820. class(json_file),intent(inout) :: me
  1821. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1822. logical(LK),dimension(:),intent(in) :: vec !! the value vector
  1823. logical(LK),intent(out),optional :: found !! if the variable was found
  1824. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1825. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1826. call me%core%add_by_path(me%p,path,vec,found,was_created)
  1827. end subroutine json_file_add_logical_vec
  1828. !*****************************************************************************************
  1829. !*****************************************************************************************
  1830. !> author: Jacob Williams
  1831. !
  1832. ! Alternate version of [[json_file_add_logical_vec]], where "path" is kind=CDK.
  1833. subroutine wrap_json_file_add_logical_vec(me,path,vec,found,was_created)
  1834. implicit none
  1835. class(json_file),intent(inout) :: me
  1836. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1837. logical(LK),dimension(:),intent(in) :: vec !! the value vector
  1838. logical(LK),intent(out),optional :: found !! if the variable was found
  1839. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1840. call me%json_file_add_logical_vec(to_unicode(path),vec,found,was_created)
  1841. end subroutine wrap_json_file_add_logical_vec
  1842. !*****************************************************************************************
  1843. !*****************************************************************************************
  1844. !> author: Jacob Williams
  1845. !
  1846. ! Add a character string to a json file.
  1847. subroutine json_file_add_string(me,path,val,found,was_created,trim_str,adjustl_str)
  1848. implicit none
  1849. class(json_file),intent(inout) :: me
  1850. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1851. character(kind=CK,len=*),intent(in) :: val !! value
  1852. logical(LK),intent(out),optional :: found !! if the variable was found
  1853. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1854. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for the `val`
  1855. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for the `val`
  1856. !! (note that ADJUSTL is done before TRIM)
  1857. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1858. call me%core%add_by_path(me%p,path,val,found,was_created,trim_str,adjustl_str)
  1859. end subroutine json_file_add_string
  1860. !*****************************************************************************************
  1861. !*****************************************************************************************
  1862. !> author: Jacob Williams
  1863. !
  1864. ! Alternate version of [[json_file_add_string]], where "path" and "val" are kind=CDK.
  1865. subroutine wrap_json_file_add_string(me,path,val,found,was_created,trim_str,adjustl_str)
  1866. implicit none
  1867. class(json_file),intent(inout) :: me
  1868. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1869. character(kind=CDK,len=*),intent(in) :: val !! value
  1870. logical(LK),intent(out),optional :: found !! if the variable was found
  1871. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1872. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for the `val`
  1873. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for the `val`
  1874. !! (note that ADJUSTL is done before TRIM)
  1875. call me%json_file_add_string(to_unicode(path),to_unicode(val),found,&
  1876. was_created,trim_str,adjustl_str)
  1877. end subroutine wrap_json_file_add_string
  1878. !*****************************************************************************************
  1879. !*****************************************************************************************
  1880. !> author: Jacob Williams
  1881. !
  1882. ! Wrapper for [[json_file_add_string]] where "path" is kind=CDK).
  1883. subroutine json_file_add_string_path_ascii(me,path,val,found,&
  1884. was_created,trim_str,adjustl_str)
  1885. implicit none
  1886. class(json_file),intent(inout) :: me
  1887. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1888. character(kind=CK,len=*),intent(in) :: val !! value
  1889. logical(LK),intent(out),optional :: found !! if the variable was found
  1890. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1891. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for the `val`
  1892. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for the `val`
  1893. !! (note that ADJUSTL is done before TRIM)
  1894. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1895. call me%json_file_add_string(to_unicode(path),val,found,&
  1896. was_created,trim_str,adjustl_str)
  1897. end subroutine json_file_add_string_path_ascii
  1898. !*****************************************************************************************
  1899. !*****************************************************************************************
  1900. !> author: Jacob Williams
  1901. !
  1902. ! Wrapper for [[json_file_add_string]] where "val" is kind=CDK).
  1903. subroutine json_file_add_string_value_ascii(me,path,val,found,&
  1904. was_created,trim_str,adjustl_str)
  1905. implicit none
  1906. class(json_file),intent(inout) :: me
  1907. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1908. character(kind=CDK,len=*),intent(in) :: val !! value
  1909. logical(LK),intent(out),optional :: found !! if the variable was found
  1910. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1911. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for the `val`
  1912. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for the `val`
  1913. !! (note that ADJUSTL is done before TRIM)
  1914. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1915. call me%json_file_add_string(path,to_unicode(val),found,&
  1916. was_created,trim_str,adjustl_str)
  1917. end subroutine json_file_add_string_value_ascii
  1918. !*****************************************************************************************
  1919. !*****************************************************************************************
  1920. !> author: Jacob Williams
  1921. !
  1922. ! Add a string vector to a JSON file.
  1923. subroutine json_file_add_string_vec(me,path,vec,found,&
  1924. was_created,ilen,trim_str,adjustl_str)
  1925. implicit none
  1926. class(json_file),intent(inout) :: me
  1927. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1928. character(kind=CK,len=*),dimension(:),intent(in) :: vec !! the value vector
  1929. logical(LK),intent(out),optional :: found !! if the variable was found
  1930. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1931. integer(IK),dimension(:),intent(in),optional :: ilen !! the string lengths of each
  1932. !! element in `value`. If not present,
  1933. !! the full `len(value)` string is added
  1934. !! for each element.
  1935. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for each element
  1936. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for each element
  1937. !! (note that ADJUSTL is done before TRIM)
  1938. if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
  1939. call me%core%add_by_path(me%p,path,vec,found,was_created,ilen,trim_str,adjustl_str)
  1940. end subroutine json_file_add_string_vec
  1941. !*****************************************************************************************
  1942. !*****************************************************************************************
  1943. !> author: Jacob Williams
  1944. !
  1945. ! Alternate version of [[json_file_add_string_vec]], where "path" and "vec" are kind=CDK.
  1946. subroutine wrap_json_file_add_string_vec(me,path,vec,found,&
  1947. was_created,ilen,trim_str,adjustl_str)
  1948. implicit none
  1949. class(json_file),intent(inout) :: me
  1950. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1951. character(kind=CDK,len=*),dimension(:),intent(in):: vec !! the value vector
  1952. logical(LK),intent(out),optional :: found !! if the variable was found
  1953. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1954. integer(IK),dimension(:),intent(in),optional :: ilen !! the string lengths of each
  1955. !! element in `value`. If not present,
  1956. !! the full `len(value)` string is added
  1957. !! for each element.
  1958. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for each element
  1959. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for each element
  1960. !! (note that ADJUSTL is done before TRIM)
  1961. call me%json_file_add_string_vec(to_unicode(path),to_unicode(vec),found,&
  1962. was_created,ilen,trim_str,adjustl_str)
  1963. end subroutine wrap_json_file_add_string_vec
  1964. !*****************************************************************************************
  1965. !*****************************************************************************************
  1966. !> author: Jacob Williams
  1967. !
  1968. ! Alternate version of [[json_file_add_string_vec]], where "path" is kind=CDK.
  1969. subroutine json_file_add_string_vec_path_ascii(me,path,vec,found,&
  1970. was_created,ilen,trim_str,adjustl_str)
  1971. implicit none
  1972. class(json_file),intent(inout) :: me
  1973. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  1974. character(kind=CK,len=*),dimension(:),intent(in) :: vec !! the value vector
  1975. logical(LK),intent(out),optional :: found !! if the variable was found
  1976. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  1977. integer(IK),dimension(:),intent(in),optional :: ilen !! the string lengths of each
  1978. !! element in `value`. If not present,
  1979. !! the full `len(value)` string is added
  1980. !! for each element.
  1981. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for each element
  1982. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for each element
  1983. !! (note that ADJUSTL is done before TRIM)
  1984. call me%json_file_add_string_vec(to_unicode(path),vec,found,&
  1985. was_created,ilen,trim_str,adjustl_str)
  1986. end subroutine json_file_add_string_vec_path_ascii
  1987. !*****************************************************************************************
  1988. !*****************************************************************************************
  1989. !> author: Jacob Williams
  1990. !
  1991. ! Alternate version of [[json_file_add_string_vec]], where "vec" is kind=CDK.
  1992. subroutine json_file_add_string_vec_vec_ascii(me,path,vec,found,&
  1993. was_created,ilen,trim_str,adjustl_str)
  1994. implicit none
  1995. class(json_file),intent(inout) :: me
  1996. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  1997. character(kind=CDK,len=*),dimension(:),intent(in) :: vec !! the value vector
  1998. logical(LK),intent(out),optional :: found !! if the variable was found
  1999. logical(LK),intent(out),optional :: was_created !! if the variable had to be created
  2000. integer(IK),dimension(:),intent(in),optional :: ilen !! the string lengths of each
  2001. !! element in `value`. If not present,
  2002. !! the full `len(value)` string is added
  2003. !! for each element.
  2004. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for each element
  2005. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for each element
  2006. !! (note that ADJUSTL is done before TRIM)
  2007. call me%json_file_add_string_vec(path,to_unicode(vec),found,&
  2008. was_created,ilen,trim_str,adjustl_str)
  2009. end subroutine json_file_add_string_vec_vec_ascii
  2010. !*****************************************************************************************
  2011. !*****************************************************************************************
  2012. !> author: Jacob Williams
  2013. ! date: 1/10/2015
  2014. !
  2015. ! Given the path string, if the variable is present in the file,
  2016. ! and is a scalar, then update its value.
  2017. ! If it is not present, then create it and set its value.
  2018. !
  2019. !### See also
  2020. ! * [[json_update_integer]]
  2021. subroutine json_file_update_integer(me,path,val,found)
  2022. implicit none
  2023. class(json_file),intent(inout) :: me
  2024. character(kind=CK,len=*),intent(in) :: path
  2025. integer(IK),intent(in) :: val
  2026. logical(LK),intent(out) :: found
  2027. if (.not. me%core%failed()) call me%core%update(me%p,path,val,found)
  2028. end subroutine json_file_update_integer
  2029. !*****************************************************************************************
  2030. !*****************************************************************************************
  2031. !>
  2032. ! Alternate version of [[json_file_update_integer]], where "path" is kind=CDK.
  2033. subroutine wrap_json_file_update_integer(me,path,val,found)
  2034. implicit none
  2035. class(json_file),intent(inout) :: me
  2036. character(kind=CDK,len=*),intent(in) :: path
  2037. integer(IK),intent(in) :: val
  2038. logical(LK),intent(out) :: found
  2039. call me%update(to_unicode(path),val,found)
  2040. end subroutine wrap_json_file_update_integer
  2041. !*****************************************************************************************
  2042. !*****************************************************************************************
  2043. !> author: Jacob Williams
  2044. ! date: 1/10/2015
  2045. !
  2046. ! Given the path string, if the variable is present in the file,
  2047. ! and is a scalar, then update its value.
  2048. ! If it is not present, then create it and set its value.
  2049. !
  2050. !### See also
  2051. ! * [[json_update_logical]]
  2052. subroutine json_file_update_logical(me,path,val,found)
  2053. implicit none
  2054. class(json_file),intent(inout) :: me
  2055. character(kind=CK,len=*),intent(in) :: path
  2056. logical(LK),intent(in) :: val
  2057. logical(LK),intent(out) :: found
  2058. if (.not. me%core%failed()) call me%core%update(me%p,path,val,found)
  2059. end subroutine json_file_update_logical
  2060. !*****************************************************************************************
  2061. !*****************************************************************************************
  2062. !>
  2063. ! Alternate version of [[json_file_update_logical]], where "path" is kind=CDK.
  2064. subroutine wrap_json_file_update_logical(me,path,val,found)
  2065. implicit none
  2066. class(json_file),intent(inout) :: me
  2067. character(kind=CDK,len=*),intent(in) :: path
  2068. logical(LK),intent(in) :: val
  2069. logical(LK),intent(out) :: found
  2070. call me%update(to_unicode(path),val,found)
  2071. end subroutine wrap_json_file_update_logical
  2072. !*****************************************************************************************
  2073. !*****************************************************************************************
  2074. !> author: Jacob Williams
  2075. ! date: 1/10/2015
  2076. !
  2077. ! Given the path string, if the variable is present in the file,
  2078. ! and is a scalar, then update its value.
  2079. ! If it is not present, then create it and set its value.
  2080. subroutine json_file_update_real(me,path,val,found)
  2081. implicit none
  2082. class(json_file),intent(inout) :: me
  2083. character(kind=CK,len=*),intent(in) :: path
  2084. real(RK),intent(in) :: val
  2085. logical(LK),intent(out) :: found
  2086. if (.not. me%core%failed()) call me%core%update(me%p,path,val,found)
  2087. end subroutine json_file_update_real
  2088. !*****************************************************************************************
  2089. !*****************************************************************************************
  2090. !>
  2091. ! Alternate version of [[json_file_update_real]], where "path" is kind=CDK.
  2092. subroutine wrap_json_file_update_real(me,path,val,found)
  2093. implicit none
  2094. class(json_file),intent(inout) :: me
  2095. character(kind=CDK,len=*),intent(in) :: path
  2096. real(RK),intent(in) :: val
  2097. logical(LK),intent(out) :: found
  2098. call me%update(to_unicode(path),val,found)
  2099. end subroutine wrap_json_file_update_real
  2100. !*****************************************************************************************
  2101. #ifndef REAL32
  2102. !*****************************************************************************************
  2103. !> author: Jacob Williams
  2104. ! date: 1/21/2019
  2105. !
  2106. ! Alternate version of [[json_file_update_real]] where `val` is `real32`.
  2107. subroutine json_file_update_real32(me,path,val,found)
  2108. implicit none
  2109. class(json_file),intent(inout) :: me
  2110. character(kind=CK,len=*),intent(in) :: path
  2111. real(real32),intent(in) :: val
  2112. logical(LK),intent(out) :: found
  2113. call me%update(path,real(val,RK),found)
  2114. end subroutine json_file_update_real32
  2115. !*****************************************************************************************
  2116. !*****************************************************************************************
  2117. !>
  2118. ! Alternate version of [[json_file_update_real32]], where "path" is kind=CDK.
  2119. subroutine wrap_json_file_update_real32(me,path,val,found)
  2120. implicit none
  2121. class(json_file),intent(inout) :: me
  2122. character(kind=CDK,len=*),intent(in) :: path
  2123. real(real32),intent(in) :: val
  2124. logical(LK),intent(out) :: found
  2125. call me%update(to_unicode(path),val,found)
  2126. end subroutine wrap_json_file_update_real32
  2127. !*****************************************************************************************
  2128. #endif
  2129. #ifdef REAL128
  2130. !*****************************************************************************************
  2131. !> author: Jacob Williams
  2132. ! date: 1/21/2019
  2133. !
  2134. ! Alternate version of [[json_file_update_real]] where `val` is `real64`.
  2135. subroutine json_file_update_real64(me,path,val,found)
  2136. implicit none
  2137. class(json_file),intent(inout) :: me
  2138. character(kind=CK,len=*),intent(in) :: path
  2139. real(real64),intent(in) :: val
  2140. logical(LK),intent(out) :: found
  2141. call me%update(path,real(val,RK),found)
  2142. end subroutine json_file_update_real64
  2143. !*****************************************************************************************
  2144. !*****************************************************************************************
  2145. !>
  2146. ! Alternate version of [[json_file_update_real64]], where "path" is kind=CDK.
  2147. subroutine wrap_json_file_update_real64(me,path,val,found)
  2148. implicit none
  2149. class(json_file),intent(inout) :: me
  2150. character(kind=CDK,len=*),intent(in) :: path
  2151. real(real64),intent(in) :: val
  2152. logical(LK),intent(out) :: found
  2153. call me%update(to_unicode(path),val,found)
  2154. end subroutine wrap_json_file_update_real64
  2155. !*****************************************************************************************
  2156. #endif
  2157. !*****************************************************************************************
  2158. !> author: Jacob Williams
  2159. ! date: 1/10/2015
  2160. !
  2161. ! Given the path string, if the variable is present in the file,
  2162. ! and is a scalar, then update its value.
  2163. ! If it is not present, then create it and set its value.
  2164. !
  2165. !### See also
  2166. ! * [[json_update_string]]
  2167. subroutine json_file_update_string(me,path,val,found,trim_str,adjustl_str)
  2168. implicit none
  2169. class(json_file),intent(inout) :: me
  2170. character(kind=CK,len=*),intent(in) :: path
  2171. character(kind=CK,len=*),intent(in) :: val
  2172. logical(LK),intent(out) :: found
  2173. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for the `val`
  2174. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for the `val`
  2175. !! (note that ADJUSTL is done before TRIM)
  2176. if (.not. me%core%failed()) call me%core%update(me%p,path,val,found,trim_str,adjustl_str)
  2177. end subroutine json_file_update_string
  2178. !*****************************************************************************************
  2179. !*****************************************************************************************
  2180. !>
  2181. ! Alternate version of [[json_file_update_string]], where "path" and "val" are kind=CDK.
  2182. subroutine wrap_json_file_update_string(me,path,val,found,trim_str,adjustl_str)
  2183. implicit none
  2184. class(json_file),intent(inout) :: me
  2185. character(kind=CDK,len=*),intent(in) :: path
  2186. character(kind=CDK,len=*),intent(in) :: val
  2187. logical(LK),intent(out) :: found
  2188. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for the `val`
  2189. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for the `val`
  2190. !! (note that ADJUSTL is done before TRIM)
  2191. call me%update(to_unicode(path),to_unicode(val),found,trim_str,adjustl_str)
  2192. end subroutine wrap_json_file_update_string
  2193. !*****************************************************************************************
  2194. !*****************************************************************************************
  2195. !>
  2196. ! Alternate version of [[json_file_update_string]], where "path" is kind=CDK.
  2197. subroutine json_file_update_string_name_ascii(me,path,val,found,trim_str,adjustl_str)
  2198. implicit none
  2199. class(json_file),intent(inout) :: me
  2200. character(kind=CDK,len=*),intent(in) :: path
  2201. character(kind=CK, len=*),intent(in) :: val
  2202. logical(LK),intent(out) :: found
  2203. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for the `val`
  2204. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for the `val`
  2205. !! (note that ADJUSTL is done before TRIM)
  2206. call me%update(to_unicode(path),val,found,trim_str,adjustl_str)
  2207. end subroutine json_file_update_string_name_ascii
  2208. !*****************************************************************************************
  2209. !*****************************************************************************************
  2210. !>
  2211. ! Alternate version of [[json_file_update_string]], where "val" is kind=CDK.
  2212. subroutine json_file_update_string_val_ascii(me,path,val,found,trim_str,adjustl_str)
  2213. implicit none
  2214. class(json_file),intent(inout) :: me
  2215. character(kind=CK, len=*),intent(in) :: path
  2216. character(kind=CDK,len=*),intent(in) :: val
  2217. logical(LK),intent(out) :: found
  2218. logical(LK),intent(in),optional :: trim_str !! if TRIM() should be called for the `val`
  2219. logical(LK),intent(in),optional :: adjustl_str !! if ADJUSTL() should be called for the `val`
  2220. !! (note that ADJUSTL is done before TRIM)
  2221. call me%update(path,to_unicode(val),found,trim_str,adjustl_str)
  2222. end subroutine json_file_update_string_val_ascii
  2223. !*****************************************************************************************
  2224. !*****************************************************************************************
  2225. !> author: Jacob Williams
  2226. ! date: 6/11/2016
  2227. !
  2228. ! Traverse the JSON structure in the file.
  2229. ! This routine calls the user-specified [[json_traverse_callback_func]]
  2230. ! for each element of the structure.
  2231. subroutine json_file_traverse(me,traverse_callback)
  2232. implicit none
  2233. class(json_file),intent(inout) :: me
  2234. procedure(json_traverse_callback_func) :: traverse_callback
  2235. call me%core%traverse(me%p,traverse_callback)
  2236. end subroutine json_file_traverse
  2237. !*****************************************************************************************
  2238. !*****************************************************************************************
  2239. !> author: Jacob Williams
  2240. ! date: 7/7/2018
  2241. !
  2242. ! Remove a variable from a JSON file.
  2243. !
  2244. !@note This is just a wrapper to [[remove_if_present]].
  2245. subroutine json_file_remove(me,path)
  2246. implicit none
  2247. class(json_file),intent(inout) :: me
  2248. character(kind=CK,len=*),intent(in) :: path !! the path to the variable
  2249. call me%core%remove_if_present(me%p,path)
  2250. end subroutine json_file_remove
  2251. !*****************************************************************************************
  2252. !*****************************************************************************************
  2253. !>
  2254. ! Alternate version of [[json_file_remove]], where "path" is kind=CDK.
  2255. subroutine wrap_json_file_remove(me,path)
  2256. implicit none
  2257. class(json_file),intent(inout) :: me
  2258. character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
  2259. call me%remove(to_unicode(path))
  2260. end subroutine wrap_json_file_remove
  2261. !*****************************************************************************************
  2262. !*****************************************************************************************
  2263. end module json_file_module
  2264. !*****************************************************************************************