Skip to content

Commit

Permalink
Added update method to the json_file class.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwilliams committed Jan 10, 2015
1 parent f1d87af commit d2622a8
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/json_example.f90
Expand Up @@ -766,6 +766,10 @@ subroutine test_1()
!call json%get('data(2)', p)
!call json_update(p,'real',[1.0_wp, 2.0_wp, 3.0_wp],found) !don't have one like this yet...

!use the json_file procedure to update a variable:
call json%update('version.svn',999,found)
if (json_failed()) call print_error_message()

write(*,'(A)') ''
write(*,'(A)') 'printing the modified structure...'
call json%print_file()
Expand Down
161 changes: 149 additions & 12 deletions src/json_module.f90
Expand Up @@ -277,19 +277,24 @@ module json_module
json_file_get_double_vec, &
json_file_get_logical_vec, &
json_file_get_string_vec

!scalars:

generic,public :: update => json_file_update_integer, &
json_file_update_logical, &
json_file_update_real, &
json_file_update_string

!get:
procedure :: json_file_get_object
procedure :: json_file_get_integer
procedure :: json_file_get_double
procedure :: json_file_get_logical
procedure :: json_file_get_string

!vectors:
procedure :: json_file_get_integer_vec
procedure :: json_file_get_double_vec
procedure :: json_file_get_logical_vec
procedure :: json_file_get_string_vec
procedure :: json_file_get_integer, json_file_get_integer_vec

This comment has been minimized.

Copy link
@zbeekman

zbeekman Jan 10, 2015

Contributor

These lines broke compilation for me using the Intel compiler. I’m getting:
error #8259: The type bound procedure definition statement must contains only one binding name.
I’m using

/usr/bin/ifort -V
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 15.0.0.077 Build 20140716
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.
procedure :: json_file_get_double, json_file_get_double_vec
procedure :: json_file_get_logical, json_file_get_logical_vec
procedure :: json_file_get_string, json_file_get_string_vec
!update:
procedure :: json_file_update_integer
procedure :: json_file_update_logical
procedure :: json_file_update_real
procedure :: json_file_update_string

!*********************************************************
end type json_file
Expand Down Expand Up @@ -361,6 +366,10 @@ end subroutine array_callback_func
! These routines can also change the variable's type (but an error will be
! thrown if the existing variable is not a scalar).
!
! NOTES
! It should not be used to change the type of a variable in an array,
! or it will produce an invalid JSON file.
!
! SOURCE
interface json_update
module procedure :: json_update_logical,&
Expand Down Expand Up @@ -1464,6 +1473,134 @@ subroutine json_value_remove_if_present(p,name)
end subroutine json_value_remove_if_present
!*****************************************************************************************

!*****************************************************************************************
!****f* json_module/json_file_update_integer
!
! NAME
! json_file_update_integer
!
! DESCRIPTION
! Given the path string, if the variable is present in the file,
! and is a scalar, then update its value.
! If it is not present, then create it and set its value.
!
! SEE ALSO
! json_update_integer
!
! AUTHOR
! Jacob Williams : 1/10/2015
!
! SOURCE

subroutine json_file_update_integer(me,name,val,found)
implicit none

class(json_file),intent(inout) :: me
character(kind=CK,len=*),intent(in) :: name
integer(IK),intent(in) :: val
logical(LK),intent(out) :: found

if (.not. exception_thrown) call json_update(me%p,name,val,found)

end subroutine json_file_update_integer
!*****************************************************************************************

!*****************************************************************************************
!****f* json_module/json_file_update_logical
!
! NAME
! json_file_update_logical
!
! DESCRIPTION
! Given the path string, if the variable is present in the file,
! and is a scalar, then update its value.
! If it is not present, then create it and set its value.
!
! SEE ALSO
! json_update_logical
!
! AUTHOR
! Jacob Williams : 1/10/2015
!
! SOURCE

subroutine json_file_update_logical(me,name,val,found)
implicit none

class(json_file),intent(inout) :: me
character(kind=CK,len=*),intent(in) :: name
logical(LK),intent(in) :: val
logical(LK),intent(out) :: found

if (.not. exception_thrown) call json_update(me%p,name,val,found)

end subroutine json_file_update_logical
!*****************************************************************************************

!*****************************************************************************************
!****f* json_module/json_file_update_real
!
! NAME
! json_file_update_real
!
! DESCRIPTION
! Given the path string, if the variable is present in the file,
! and is a scalar, then update its value.
! If it is not present, then create it and set its value.
!
! SEE ALSO
! json_update_real
!
! AUTHOR
! Jacob Williams : 1/10/2015
!
! SOURCE

subroutine json_file_update_real(me,name,val,found)
implicit none

class(json_file),intent(inout) :: me
character(kind=CK,len=*),intent(in) :: name
real(RK),intent(in) :: val
logical(LK),intent(out) :: found

if (.not. exception_thrown) call json_update(me%p,name,val,found)

end subroutine json_file_update_real
!*****************************************************************************************

!*****************************************************************************************
!****f* json_module/json_file_update_string
!
! NAME
! json_file_update_string
!
! DESCRIPTION
! Given the path string, if the variable is present in the file,
! and is a scalar, then update its value.
! If it is not present, then create it and set its value.
!
! SEE ALSO
! json_update_string
!
! AUTHOR
! Jacob Williams : 1/10/2015
!
! SOURCE

subroutine json_file_update_string(me,name,val,found)
implicit none

class(json_file),intent(inout) :: me
character(kind=CK,len=*),intent(in) :: name
character(kind=CK,len=*),intent(in) :: val
logical(LK),intent(out) :: found

if (.not. exception_thrown) call json_update(me%p,name,val,found)

end subroutine json_file_update_string
!*****************************************************************************************

!*****************************************************************************************
!****f* json_module/json_update_logical
!
Expand Down

0 comments on commit d2622a8

Please sign in to comment.