Skip to content

Commit e0329a5

Browse files
committed
wp no longer public. add exceptions to update routine if value is not a scalar. update documentation.
1 parent 750502b commit e0329a5

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/json_example.f90

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ program json_test
4848
! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4949
!
5050
!*******************************************************************************************************
51+
use,intrinsic :: iso_fortran_env, only: wp => real64 !double precision reals
5152

5253
use json_module
5354

@@ -606,10 +607,10 @@ subroutine test_1()
606607
write(*,'(A)') ' Test replacing data from the json structure:'
607608

608609
call json%get('data(1)', p)
609-
call json_value_update(p,'name','Cuthbert',found)
610+
call json_update(p,'name','Cuthbert',found)
610611

611612
!call json%get('data(2)', p)
612-
!call json_value_update(p,'real',[1.0_wp, 2.0_wp, 3.0_wp],found) !........ ??? .......
613+
!call json_update(p,'real',[1.0_wp, 2.0_wp, 3.0_wp],found) !don't have one like this yet...
613614

614615
write(*,'(A)') ''
615616
write(*,'(A)') 'printing the modified structure...'

src/json_module.f90

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ module json_module
9292
! DEALINGS IN THE SOFTWARE.
9393
!
9494
!*****************************************************************************************
95+
use,intrinsic :: iso_fortran_env, only: wp => real64 !double precision reals
9596

9697
implicit none
9798

9899
private
99100

100101
!parameters:
101-
integer,parameter,public :: wp = selected_real_kind(15,307) !double precision reals
102-
character(len=*),parameter,public :: json_ext = '.json' !JSON file extension
102+
character(len=*),parameter,public :: json_ext = '.json' !JSON file extension
103103

104104
character(len=1),parameter :: space = ' ' !special characters
105105
character(len=1),parameter :: bspace = achar(8)
@@ -295,12 +295,25 @@ end subroutine array_callback_func
295295
!*************************************************************************************
296296

297297
!*************************************************************************************
298-
interface json_value_update
298+
!****f* json_module/json_update
299+
!
300+
! NAME
301+
! json_update
302+
!
303+
! DESCRIPTION
304+
! These are like json_value_add, except if a child with the same name is
305+
! already present, then its value is simply updated.
306+
! Note that currently, these only work for scalar variables.
307+
! These routines can also change the variable's type (but an error will be
308+
! thrown if the existing variable is not a scalar).
309+
!
310+
! SOURCE
311+
interface json_update
299312
module procedure :: json_update_logical,&
300313
json_update_real,&
301314
json_update_integer,&
302315
json_update_chars
303-
end interface json_value_update
316+
end interface json_update
304317
!*************************************************************************************
305318

306319
!*************************************************************************************
@@ -351,7 +364,7 @@ end subroutine array_callback_func
351364
public :: json_value_get !use either a 1 based index or member
352365
! name to get a json_value.
353366
public :: json_value_add !add data to a JSON structure
354-
public :: json_value_update !update a value in a JSON structure
367+
public :: json_update !update a value in a JSON structure
355368
public :: json_get !get data from the JSON structure
356369
public :: json_print !print the JSON structure to a file
357370
public :: json_print_to_string !write the JSON structure to a string
@@ -1282,8 +1295,8 @@ subroutine json_update_logical(p,name,val,found)
12821295
call to_logical(p_var,val) !update the value
12831296
case default
12841297
found = .false.
1285-
write(*,*) 'Error in json_update_logical: '//&
1286-
'the variable is not a scalar value'
1298+
call throw_exception('Error in json_update_logical: '//&
1299+
'the variable is not a scalar value')
12871300
end select
12881301

12891302
else
@@ -1329,8 +1342,8 @@ subroutine json_update_real(p,name,val,found)
13291342
call to_real(p_var,val) !update the value
13301343
case default
13311344
found = .false.
1332-
write(*,*) 'Error in json_update_real: '//&
1333-
'the variable is not a scalar value'
1345+
call throw_exception('Error in json_update_real: '//&
1346+
'the variable is not a scalar value')
13341347
end select
13351348

13361349
else
@@ -1376,8 +1389,8 @@ subroutine json_update_integer(p,name,val,found)
13761389
call to_integer(p_var,val) !update the value
13771390
case default
13781391
found = .false.
1379-
write(*,*) 'Error in json_update_integer: '//&
1380-
'the variable is not a scalar value'
1392+
call throw_exception('Error in json_update_integer: '//&
1393+
'the variable is not a scalar value')
13811394
end select
13821395

13831396
else
@@ -1423,8 +1436,8 @@ subroutine json_update_chars(p,name,val,found)
14231436
call to_string(p_var,val) !update the value
14241437
case default
14251438
found = .false.
1426-
write(*,*) 'Error in json_update_chars: '//&
1427-
'the variable is not a scalar value'
1439+
call throw_exception('Error in json_update_chars: '//&
1440+
'the variable is not a scalar value')
14281441
end select
14291442

14301443
else

0 commit comments

Comments
 (0)