Skip to content

Commit 319f94e

Browse files
committed
added routines to rename a json variable by specifying the path.
1 parent 91bcbaa commit 319f94e

File tree

3 files changed

+275
-19
lines changed

3 files changed

+275
-19
lines changed

src/json_file_module.F90

Lines changed: 94 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,84 @@ 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_by_path(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+
if (.not. associated(me%p)) call me%core%create_object(me%p,ck_'') ! create root
922+
923+
call me%json_file_rename(to_unicode(path),name,found)
924+
925+
end subroutine json_file_rename_path_ascii
926+
!*****************************************************************************************
927+
928+
!*****************************************************************************************
929+
!> author: Jacob Williams
930+
!
931+
! Wrapper for [[json_file_rename]] where "name" is kind=CDK).
932+
933+
subroutine json_file_rename_name_ascii(me,path,name,found)
934+
935+
implicit none
936+
937+
class(json_file),intent(inout) :: me
938+
character(kind=CK,len=*),intent(in) :: path !! the path to the variable
939+
character(kind=CDK,len=*),intent(in) :: name !! the new name
940+
logical(LK),intent(out),optional :: found !! if the variable was found
941+
942+
call me%json_file_rename(path,to_unicode(name),found)
943+
944+
end subroutine json_file_rename_name_ascii
945+
!*****************************************************************************************
946+
853947
!*****************************************************************************************
854948
!> author: Jacob Williams
855949
! date: 2/3/2014

src/json_value_module.F90

Lines changed: 126 additions & 14 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
!>
@@ -386,6 +386,17 @@ module json_value_module
386386
procedure :: json_add_string_vec_by_path_path_ascii
387387
#endif
388388

389+
!>
390+
! Rename a variable, by specifying its path.
391+
generic,public :: rename_by_path => MAYBEWRAP(json_rename_by_path)
392+
procedure :: MAYBEWRAP(json_rename_by_path)
393+
#ifdef USE_UCS4
394+
generic,public :: rename_by_path => json_rename_by_path_name_ascii,&
395+
json_rename_by_path_path_ascii
396+
procedure :: json_rename_by_path_name_ascii
397+
procedure :: json_rename_by_path_path_ascii
398+
#endif
399+
389400
!>
390401
! Create a [[json_value]] linked list using the
391402
! path to the variables. Optionally return a
@@ -5678,6 +5689,107 @@ subroutine wrap_json_create_by_path(json,me,path,p,found,was_created)
56785689
end subroutine wrap_json_create_by_path
56795690
!*****************************************************************************************
56805691

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