Skip to content

Commit d2622a8

Browse files
committed
Added update method to the json_file class.
1 parent f1d87af commit d2622a8

File tree

2 files changed

+153
-12
lines changed

2 files changed

+153
-12
lines changed

src/json_example.f90

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,10 @@ subroutine test_1()
766766
!call json%get('data(2)', p)
767767
!call json_update(p,'real',[1.0_wp, 2.0_wp, 3.0_wp],found) !don't have one like this yet...
768768

769+
!use the json_file procedure to update a variable:
770+
call json%update('version.svn',999,found)
771+
if (json_failed()) call print_error_message()
772+
769773
write(*,'(A)') ''
770774
write(*,'(A)') 'printing the modified structure...'
771775
call json%print_file()

src/json_module.f90

Lines changed: 149 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,24 @@ module json_module
277277
json_file_get_double_vec, &
278278
json_file_get_logical_vec, &
279279
json_file_get_string_vec
280-
281-
!scalars:
280+
281+
generic,public :: update => json_file_update_integer, &
282+
json_file_update_logical, &
283+
json_file_update_real, &
284+
json_file_update_string
285+
286+
!get:
282287
procedure :: json_file_get_object
283-
procedure :: json_file_get_integer
284-
procedure :: json_file_get_double
285-
procedure :: json_file_get_logical
286-
procedure :: json_file_get_string
287-
288-
!vectors:
289-
procedure :: json_file_get_integer_vec
290-
procedure :: json_file_get_double_vec
291-
procedure :: json_file_get_logical_vec
292-
procedure :: json_file_get_string_vec
288+
procedure :: json_file_get_integer, json_file_get_integer_vec
289+
procedure :: json_file_get_double, json_file_get_double_vec
290+
procedure :: json_file_get_logical, json_file_get_logical_vec
291+
procedure :: json_file_get_string, json_file_get_string_vec
292+
293+
!update:
294+
procedure :: json_file_update_integer
295+
procedure :: json_file_update_logical
296+
procedure :: json_file_update_real
297+
procedure :: json_file_update_string
293298

294299
!*********************************************************
295300
end type json_file
@@ -361,6 +366,10 @@ end subroutine array_callback_func
361366
! These routines can also change the variable's type (but an error will be
362367
! thrown if the existing variable is not a scalar).
363368
!
369+
! NOTES
370+
! It should not be used to change the type of a variable in an array,
371+
! or it will produce an invalid JSON file.
372+
!
364373
! SOURCE
365374
interface json_update
366375
module procedure :: json_update_logical,&
@@ -1464,6 +1473,134 @@ subroutine json_value_remove_if_present(p,name)
14641473
end subroutine json_value_remove_if_present
14651474
!*****************************************************************************************
14661475

1476+
!*****************************************************************************************
1477+
!****f* json_module/json_file_update_integer
1478+
!
1479+
! NAME
1480+
! json_file_update_integer
1481+
!
1482+
! DESCRIPTION
1483+
! Given the path string, if the variable is present in the file,
1484+
! and is a scalar, then update its value.
1485+
! If it is not present, then create it and set its value.
1486+
!
1487+
! SEE ALSO
1488+
! json_update_integer
1489+
!
1490+
! AUTHOR
1491+
! Jacob Williams : 1/10/2015
1492+
!
1493+
! SOURCE
1494+
1495+
subroutine json_file_update_integer(me,name,val,found)
1496+
implicit none
1497+
1498+
class(json_file),intent(inout) :: me
1499+
character(kind=CK,len=*),intent(in) :: name
1500+
integer(IK),intent(in) :: val
1501+
logical(LK),intent(out) :: found
1502+
1503+
if (.not. exception_thrown) call json_update(me%p,name,val,found)
1504+
1505+
end subroutine json_file_update_integer
1506+
!*****************************************************************************************
1507+
1508+
!*****************************************************************************************
1509+
!****f* json_module/json_file_update_logical
1510+
!
1511+
! NAME
1512+
! json_file_update_logical
1513+
!
1514+
! DESCRIPTION
1515+
! Given the path string, if the variable is present in the file,
1516+
! and is a scalar, then update its value.
1517+
! If it is not present, then create it and set its value.
1518+
!
1519+
! SEE ALSO
1520+
! json_update_logical
1521+
!
1522+
! AUTHOR
1523+
! Jacob Williams : 1/10/2015
1524+
!
1525+
! SOURCE
1526+
1527+
subroutine json_file_update_logical(me,name,val,found)
1528+
implicit none
1529+
1530+
class(json_file),intent(inout) :: me
1531+
character(kind=CK,len=*),intent(in) :: name
1532+
logical(LK),intent(in) :: val
1533+
logical(LK),intent(out) :: found
1534+
1535+
if (.not. exception_thrown) call json_update(me%p,name,val,found)
1536+
1537+
end subroutine json_file_update_logical
1538+
!*****************************************************************************************
1539+
1540+
!*****************************************************************************************
1541+
!****f* json_module/json_file_update_real
1542+
!
1543+
! NAME
1544+
! json_file_update_real
1545+
!
1546+
! DESCRIPTION
1547+
! Given the path string, if the variable is present in the file,
1548+
! and is a scalar, then update its value.
1549+
! If it is not present, then create it and set its value.
1550+
!
1551+
! SEE ALSO
1552+
! json_update_real
1553+
!
1554+
! AUTHOR
1555+
! Jacob Williams : 1/10/2015
1556+
!
1557+
! SOURCE
1558+
1559+
subroutine json_file_update_real(me,name,val,found)
1560+
implicit none
1561+
1562+
class(json_file),intent(inout) :: me
1563+
character(kind=CK,len=*),intent(in) :: name
1564+
real(RK),intent(in) :: val
1565+
logical(LK),intent(out) :: found
1566+
1567+
if (.not. exception_thrown) call json_update(me%p,name,val,found)
1568+
1569+
end subroutine json_file_update_real
1570+
!*****************************************************************************************
1571+
1572+
!*****************************************************************************************
1573+
!****f* json_module/json_file_update_string
1574+
!
1575+
! NAME
1576+
! json_file_update_string
1577+
!
1578+
! DESCRIPTION
1579+
! Given the path string, if the variable is present in the file,
1580+
! and is a scalar, then update its value.
1581+
! If it is not present, then create it and set its value.
1582+
!
1583+
! SEE ALSO
1584+
! json_update_string
1585+
!
1586+
! AUTHOR
1587+
! Jacob Williams : 1/10/2015
1588+
!
1589+
! SOURCE
1590+
1591+
subroutine json_file_update_string(me,name,val,found)
1592+
implicit none
1593+
1594+
class(json_file),intent(inout) :: me
1595+
character(kind=CK,len=*),intent(in) :: name
1596+
character(kind=CK,len=*),intent(in) :: val
1597+
logical(LK),intent(out) :: found
1598+
1599+
if (.not. exception_thrown) call json_update(me%p,name,val,found)
1600+
1601+
end subroutine json_file_update_string
1602+
!*****************************************************************************************
1603+
14671604
!*****************************************************************************************
14681605
!****f* json_module/json_update_logical
14691606
!

0 commit comments

Comments
 (0)