Skip to content

Commit 1129010

Browse files
Merge pull request #317 from jacobwilliams/rename
added routines to rename a json variable by specifying the path.
2 parents 91bcbaa + c78da93 commit 1129010

File tree

3 files changed

+271
-20
lines changed

3 files changed

+271
-20
lines changed

src/json_file_module.F90

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ module json_file_module
9292
json_file_print_1, &
9393
json_file_print_2
9494

95+
96+
!>
97+
! Rename a variable, specifying it by path
98+
generic,public :: rename => MAYBEWRAP(json_file_rename)
99+
#ifdef USE_UCS4
100+
generic,public :: rename => json_file_rename_path_ascii, &
101+
json_file_rename_name_ascii
102+
#endif
103+
95104
!>
96105
! Get a variable from a [[json_file(type)]], by specifying the path.
97106
generic,public :: get => MAYBEWRAP(json_file_get_object), &
@@ -173,6 +182,13 @@ module json_file_module
173182
procedure :: MAYBEWRAP(json_file_variable_info)
174183
procedure :: MAYBEWRAP(json_file_variable_matrix_info)
175184

185+
!rename:
186+
procedure :: MAYBEWRAP(json_file_rename)
187+
#ifdef USE_UCS4
188+
procedure :: json_file_rename_path_ascii
189+
procedure :: json_file_rename_name_ascii
190+
#endif
191+
176192
!get:
177193
procedure :: MAYBEWRAP(json_file_get_object)
178194
procedure :: MAYBEWRAP(json_file_get_integer)
@@ -850,6 +866,82 @@ subroutine json_file_get_root(me,p)
850866
end subroutine json_file_get_root
851867
!*****************************************************************************************
852868

869+
!*****************************************************************************************
870+
!> author: Jacob Williams
871+
!
872+
! Rename a variable in a JSON file.
873+
874+
subroutine json_file_rename(me,path,name,found)
875+
876+
implicit none
877+
878+
class(json_file),intent(inout) :: me
879+
character(kind=CK,len=*),intent(in) :: path !! the path to the variable
880+
character(kind=CK,len=*),intent(in) :: name !! the new name
881+
logical(LK),intent(out),optional :: found !! if the variable was found
882+
883+
call me%core%rename(me%p, path, name, found)
884+
885+
end subroutine json_file_rename
886+
!*****************************************************************************************
887+
888+
!*****************************************************************************************
889+
!> author: Jacob Williams
890+
!
891+
! Alternate version of [[json_file_rename]], where "path" and "name" are kind=CDK.
892+
893+
subroutine wrap_json_file_rename(me,path,name,found)
894+
895+
implicit none
896+
897+
class(json_file),intent(inout) :: me
898+
character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
899+
character(kind=CDK,len=*),intent(in) :: name !! the new name
900+
logical(LK),intent(out),optional :: found !! if the variable was found
901+
902+
call me%json_file_rename(to_unicode(path),to_unicode(name),found)
903+
904+
end subroutine wrap_json_file_rename
905+
!*****************************************************************************************
906+
907+
!*****************************************************************************************
908+
!> author: Jacob Williams
909+
!
910+
! Wrapper for [[json_file_rename]] where "path" is kind=CDK).
911+
912+
subroutine json_file_rename_path_ascii(me,path,name,found)
913+
914+
implicit none
915+
916+
class(json_file),intent(inout) :: me
917+
character(kind=CDK,len=*),intent(in) :: path !! the path to the variable
918+
character(kind=CK,len=*),intent(in) :: name !! the new name
919+
logical(LK),intent(out),optional :: found !! if the variable was found
920+
921+
call me%json_file_rename(to_unicode(path),name,found)
922+
923+
end subroutine json_file_rename_path_ascii
924+
!*****************************************************************************************
925+
926+
!*****************************************************************************************
927+
!> author: Jacob Williams
928+
!
929+
! Wrapper for [[json_file_rename]] where "name" is kind=CDK).
930+
931+
subroutine json_file_rename_name_ascii(me,path,name,found)
932+
933+
implicit none
934+
935+
class(json_file),intent(inout) :: me
936+
character(kind=CK,len=*),intent(in) :: path !! the path to the variable
937+
character(kind=CDK,len=*),intent(in) :: name !! the new name
938+
logical(LK),intent(out),optional :: found !! if the variable was found
939+
940+
call me%json_file_rename(path,to_unicode(name),found)
941+
942+
end subroutine json_file_rename_name_ascii
943+
!*****************************************************************************************
944+
853945
!*****************************************************************************************
854946
!> author: Jacob Williams
855947
! date: 2/3/2014

src/json_value_module.F90

Lines changed: 124 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,21 @@ module json_value_module
278278
json_value_add_string_vec_val_ascii
279279
#endif
280280

281-
procedure,private :: json_value_add_member
282-
procedure,private :: MAYBEWRAP(json_value_add_integer)
283-
procedure,private :: MAYBEWRAP(json_value_add_null)
284-
procedure,private :: MAYBEWRAP(json_value_add_integer_vec)
285-
procedure,private :: MAYBEWRAP(json_value_add_double)
286-
procedure,private :: MAYBEWRAP(json_value_add_double_vec)
287-
procedure,private :: MAYBEWRAP(json_value_add_logical)
288-
procedure,private :: MAYBEWRAP(json_value_add_logical_vec)
289-
procedure,private :: MAYBEWRAP(json_value_add_string)
290-
procedure,private :: MAYBEWRAP(json_value_add_string_vec)
281+
procedure,private :: json_value_add_member
282+
procedure,private :: MAYBEWRAP(json_value_add_integer)
283+
procedure,private :: MAYBEWRAP(json_value_add_null)
284+
procedure,private :: MAYBEWRAP(json_value_add_integer_vec)
285+
procedure,private :: MAYBEWRAP(json_value_add_double)
286+
procedure,private :: MAYBEWRAP(json_value_add_double_vec)
287+
procedure,private :: MAYBEWRAP(json_value_add_logical)
288+
procedure,private :: MAYBEWRAP(json_value_add_logical_vec)
289+
procedure,private :: MAYBEWRAP(json_value_add_string)
290+
procedure,private :: MAYBEWRAP(json_value_add_string_vec)
291291
#ifdef USE_UCS4
292-
procedure,private :: json_value_add_string_name_ascii
293-
procedure,private :: json_value_add_string_val_ascii
294-
procedure,private :: json_value_add_string_vec_name_ascii
295-
procedure,private :: json_value_add_string_vec_val_ascii
292+
procedure,private :: json_value_add_string_name_ascii
293+
procedure,private :: json_value_add_string_val_ascii
294+
procedure,private :: json_value_add_string_vec_name_ascii
295+
procedure,private :: json_value_add_string_vec_val_ascii
296296
#endif
297297

298298
!>
@@ -625,8 +625,16 @@ module json_value_module
625625

626626
!>
627627
! Rename a [[json_value]] variable.
628-
generic,public :: rename => MAYBEWRAP(json_value_rename)
628+
generic,public :: rename => MAYBEWRAP(json_value_rename),&
629+
MAYBEWRAP(json_rename_by_path)
629630
procedure :: MAYBEWRAP(json_value_rename)
631+
procedure :: MAYBEWRAP(json_rename_by_path)
632+
#ifdef USE_UCS4
633+
generic,public :: rename => json_rename_by_path_name_ascii,&
634+
json_rename_by_path_path_ascii
635+
procedure :: json_rename_by_path_name_ascii
636+
procedure :: json_rename_by_path_path_ascii
637+
#endif
630638

631639
!>
632640
! get info about a [[json_value]]
@@ -5678,6 +5686,107 @@ subroutine wrap_json_create_by_path(json,me,path,p,found,was_created)
56785686
end subroutine wrap_json_create_by_path
56795687
!*****************************************************************************************
56805688

5689+
!*****************************************************************************************
5690+
!>
5691+
! Rename a [[json_value]], given the path.
5692+
5693+
subroutine json_rename_by_path(json, me, path, name, found)
5694+
5695+
implicit none
5696+
5697+
class(json_core),intent(inout) :: json
5698+
type(json_value),pointer,intent(in) :: me
5699+
character(kind=CK,len=*),intent(in) :: path
5700+
character(kind=CK,len=*),intent(in) :: name !! the new name
5701+
logical(LK),intent(out),optional :: found
5702+
5703+
type(json_value),pointer :: p
5704+
5705+
if ( json%exception_thrown ) then
5706+
if ( present(found) ) found = .false.
5707+
return
5708+
end if
5709+
5710+
nullify(p)
5711+
call json%get(me=me, path=path, p=p)
5712+
5713+
if (.not. associated(p)) then
5714+
call json%throw_exception('Error in json_rename_by_path:'//&
5715+
' Unable to resolve path: '//trim(path))
5716+
else
5717+
call json%rename(p,name)
5718+
nullify(p)
5719+
end if
5720+
5721+
if (json%exception_thrown) then
5722+
if (present(found)) then
5723+
found = .false.
5724+
call json%clear_exceptions()
5725+
end if
5726+
else
5727+
if (present(found)) found = .true.
5728+
end if
5729+
5730+
end subroutine json_rename_by_path
5731+
!*****************************************************************************************
5732+
5733+
!*****************************************************************************************
5734+
!>
5735+
! Alternate version of [[json_rename_by_path]], where "path" and "name" are kind=CDK
5736+
5737+
subroutine wrap_json_rename_by_path(json, me, path, name, found)
5738+
5739+
implicit none
5740+
5741+
class(json_core),intent(inout) :: json
5742+
type(json_value),pointer,intent(in) :: me
5743+
character(kind=CDK,len=*),intent(in) :: path
5744+
character(kind=CDK,len=*),intent(in) :: name
5745+
logical(LK),intent(out),optional :: found
5746+
5747+
call json%rename(me,to_unicode(path),to_unicode(name),found)
5748+
5749+
end subroutine wrap_json_rename_by_path
5750+
!*****************************************************************************************
5751+
5752+
!*****************************************************************************************
5753+
!>
5754+
! Alternate version of [[json_rename_by_path]], where "name" is kind=CDK
5755+
5756+
subroutine json_rename_by_path_name_ascii(json, me, path, name, found)
5757+
5758+
implicit none
5759+
5760+
class(json_core),intent(inout) :: json
5761+
type(json_value),pointer,intent(in) :: me
5762+
character(kind=CK,len=*),intent(in) :: path
5763+
character(kind=CDK,len=*),intent(in) :: name
5764+
logical(LK),intent(out),optional :: found
5765+
5766+
call json%rename(me,path,to_unicode(name),found)
5767+
5768+
end subroutine json_rename_by_path_name_ascii
5769+
!*****************************************************************************************
5770+
5771+
!*****************************************************************************************
5772+
!>
5773+
! Alternate version of [[json_rename_by_path]], where "path" is kind=CDK
5774+
5775+
subroutine json_rename_by_path_path_ascii(json, me, path, name, found)
5776+
5777+
implicit none
5778+
5779+
class(json_core),intent(inout) :: json
5780+
type(json_value),pointer,intent(in) :: me
5781+
character(kind=CDK,len=*),intent(in) :: path
5782+
character(kind=CK,len=*),intent(in) :: name
5783+
logical(LK),intent(out),optional :: found
5784+
5785+
call json%rename(me,to_unicode(path),name,found)
5786+
5787+
end subroutine json_rename_by_path_path_ascii
5788+
!*****************************************************************************************
5789+
56815790
!*****************************************************************************************
56825791
!>
56835792
! Returns the [[json_value]] pointer given the path string.

src/tests/jf_test_17.F90

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
module jf_test_17_mod
99

10-
use json_module, CK => json_CK
10+
use json_module, CK => json_CK, CDK => json_CDK
1111
use, intrinsic :: iso_fortran_env , only: error_unit,output_unit
1212

1313
implicit none
@@ -24,6 +24,11 @@ subroutine test_17(error_cnt)
2424

2525
type(json_core) :: json
2626
type(json_value),pointer :: p,q
27+
type(json_file) :: f
28+
29+
character(kind=CK,len=*),parameter :: json_string = &
30+
'{"city": ["New York","Los Angeles","Chicago"], '//&
31+
'"value": 1, "iflag": true, "struct":{"vec":[1,2,3]}}'
2732

2833
write(error_unit,'(A)') ''
2934
write(error_unit,'(A)') '================================='
@@ -35,8 +40,7 @@ subroutine test_17(error_cnt)
3540

3641
write(error_unit,'(A)') ''
3742
write(error_unit,'(A)') 'Original:'
38-
call json%parse(p, '{"city": ["New York","Los Angeles","Chicago"], '//&
39-
'"value": 1, "iflag": true, "struct":{"vec":[1,2,3]}}')
43+
call json%parse(p, json_string)
4044
if (json%failed()) then
4145
call json%print_error_message(error_unit)
4246
error_cnt = error_cnt + 1
@@ -46,13 +50,16 @@ subroutine test_17(error_cnt)
4650
write(error_unit,'(A)') ''
4751
write(error_unit,'(A)') 'Rename: "city" to "cities"'
4852
call json%get(p,'city',q)
49-
call json%rename(q,'cities')
53+
call json%rename(q,'cities') ! also test the unicode ones
54+
call json%rename(q,CK_'cities')
55+
call json%rename(q,CDK_'cities')
5056
call json%print(p,output_unit)
5157
if (json%failed()) then
5258
call json%print_error_message(error_unit)
5359
error_cnt = error_cnt + 1
5460
end if
5561
nullify(q)
62+
5663
!verify that it was renamed:
5764
call json%get(p,'cities',q)
5865
if (json%failed()) then
@@ -63,9 +70,52 @@ subroutine test_17(error_cnt)
6370
end if
6471
nullify(q)
6572

73+
! rename by specifying the path:
74+
write(error_unit,'(A)') ''
75+
write(error_unit,'(A)') 'Rename: "iflag" to "flag"'
76+
call json%rename(p,'iflag','flag')
77+
call json%print(p,output_unit)
78+
if (json%failed()) then
79+
call json%print_error_message(error_unit)
80+
error_cnt = error_cnt + 1
81+
end if
82+
call json%get(p,'flag',q)
83+
if (json%failed()) then
84+
call json%print_error_message(error_unit)
85+
error_cnt = error_cnt + 1
86+
else
87+
write(error_unit,'(A)') 'Success!'
88+
end if
89+
nullify(q)
90+
91+
! unicode wrappers:
92+
call json%rename(p,CK_'flag', CK_'iflag')
93+
call json%rename(p,CK_'iflag', CDK_'flag')
94+
call json%rename(p,CDK_'flag', CK_'iflag')
95+
call json%rename(p,CDK_'iflag',CDK_'flag')
96+
if (json%failed()) then
97+
call json%print_error_message(error_unit)
98+
error_cnt = error_cnt + 1
99+
else
100+
write(error_unit,'(A)') 'Success!'
101+
end if
102+
66103
!cleanup:
67104
call json%destroy(p)
68105

106+
! test the corresponding json_file version:
107+
call f%load_from_string(json_string)
108+
call f%rename(CK_'iflag', CK_'flag')
109+
call f%rename(CK_'flag', CDK_'iflag')
110+
call f%rename(CDK_'iflag', CK_'flag')
111+
call f%rename(CDK_'flag', CDK_'iflag')
112+
if (f%failed()) then
113+
call f%print_error_message(error_unit)
114+
error_cnt = error_cnt + 1
115+
else
116+
write(error_unit,'(A)') 'Success!'
117+
end if
118+
69119
end subroutine test_17
70120

71121
end module jf_test_17_mod
@@ -85,4 +135,4 @@ program jf_test_17
85135

86136
end program jf_test_17
87137
!*****************************************************************************************
88-
#endif
138+
#endif

0 commit comments

Comments
 (0)