Skip to content

Commit 7274ff5

Browse files
Merge pull request #411 from jacobwilliams/410-character-json-file-assignment
Added a character=json_file assignment operator.
2 parents 4172ec5 + 7077ea1 commit 7274ff5

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

src/json_file_module.F90

+43-2
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,10 @@ module json_file_module
244244
generic,public :: operator(.in.) => MAYBEWRAP(json_file_valid_path_op)
245245
procedure,pass(me) :: MAYBEWRAP(json_file_valid_path_op)
246246

247-
generic,public :: assignment(=) => assign_json_file
247+
generic,public :: assignment(=) => assign_json_file,&
248+
assign_json_file_to_string
248249
procedure :: assign_json_file
250+
procedure,pass(me) :: assign_json_file_to_string
249251

250252
! ***************************************************
251253
! private routines
@@ -1126,7 +1128,7 @@ end subroutine json_file_get_root
11261128
!*****************************************************************************************
11271129
!> author: Jacob Williams
11281130
!
1129-
! Assignment operator for [[json_core(type)]].
1131+
! Assignment operator for [[json_core(type)]] = [[json_core(type)]].
11301132
! This will duplicate the [[json_core(type)]] and also
11311133
! perform a deep copy of the [[json_value(type)]] data structure.
11321134

@@ -1143,6 +1145,45 @@ subroutine assign_json_file(me,f)
11431145
end subroutine assign_json_file
11441146
!*****************************************************************************************
11451147

1148+
!*****************************************************************************************
1149+
!> author: Jacob Williams
1150+
!
1151+
! Assignment operator for character = [[json_core(type)]].
1152+
! This is just a wrapper for the [[json_value_to_string]] routine.
1153+
!
1154+
!### Note
1155+
! * If an exception is raised or the file contains no data,
1156+
! this will return an empty string.
1157+
1158+
subroutine assign_json_file_to_string(str,me)
1159+
1160+
implicit none
1161+
1162+
character(kind=CK,len=:),allocatable,intent(out) :: str
1163+
class(json_file),intent(in) :: me
1164+
1165+
type(json_core) :: core_copy !! a copy of `core` from `me`
1166+
1167+
if (me%core%failed() .or. .not. associated(me%p)) then
1168+
str = ''
1169+
else
1170+
1171+
! This is sort of a hack. Since `me` has to have `intent(in)`
1172+
! for the assignment to work, we need to make a copy of `me%core`
1173+
! so we can call the low level routine (since it needs it to
1174+
! be `intent(inout)`) because it's possible for this
1175+
! function to raise an exception.
1176+
1177+
core_copy = me%core ! copy the parser settings
1178+
1179+
call core_copy%serialize(me%p,str)
1180+
if (me%core%failed()) str = ''
1181+
1182+
end if
1183+
1184+
end subroutine assign_json_file_to_string
1185+
!*****************************************************************************************
1186+
11461187
!*****************************************************************************************
11471188
!> author: Jacob Williams
11481189
!

src/tests/jf_test_41.F90

+40-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ subroutine test_41(error_cnt)
2424

2525
type(json_value),pointer :: p, p2
2626
type(json_core) :: json
27-
type(json_file) :: f, f2
27+
type(json_file) :: f, f2, f3, f4
28+
character(kind=CK,len=:),allocatable :: str
2829

2930
character(kind=CK,len=*),parameter :: json_str = &
30-
'{"str_array": ["1","22","333","55555"]}'
31+
'{"str_array": ["1","22","333"]}'
3132

3233
error_cnt = 0
3334

@@ -37,6 +38,8 @@ subroutine test_41(error_cnt)
3738
write(error_unit,'(A)') '================================='
3839
write(error_unit,'(A)') ''
3940

41+
call json%initialize(no_whitespace=.true.)
42+
4043
write(error_unit,'(A)') 'parsing...'
4144
call json%deserialize(p,json_str)
4245
call json%deserialize(p2,json_str)
@@ -50,6 +53,7 @@ subroutine test_41(error_cnt)
5053
write(error_unit,'(A)') 'printing...'
5154
call json%print(p,int(output_unit,IK))
5255

56+
write(error_unit,'(A)') ''
5357
write(error_unit,'(A)') ''
5458
write(error_unit,'(A)') 'copying to json_file...'
5559

@@ -64,10 +68,44 @@ subroutine test_41(error_cnt)
6468
else
6569
write(error_unit,'(A)') ''
6670
write(error_unit,'(A)') 'printing...'
71+
call f%initialize(no_whitespace=.true.)
6772
call f%print() ! print to console
6873
if (f%failed()) then
6974
call f%print_error_message(error_unit)
7075
error_cnt = error_cnt + 1
76+
else
77+
78+
write(error_unit,'(A)') ''
79+
write(error_unit,'(A)') ''
80+
write(error_unit,'(A)') 'make two deep copies and print...'
81+
82+
f3 = f
83+
f4 = f
84+
85+
call f%print()
86+
call f3%print()
87+
call f4%print()
88+
89+
write(error_unit,'(A)') ''
90+
write(error_unit,'(A)') ''
91+
write(error_unit,'(A)') 'string assignment...'
92+
93+
str = f3
94+
write(error_unit,'(A)') str
95+
96+
if (f%failed()) then
97+
call f%print_error_message(error_unit)
98+
error_cnt = error_cnt + 1
99+
end if
100+
if (f3%failed()) then
101+
call f3%print_error_message(error_unit)
102+
error_cnt = error_cnt + 1
103+
end if
104+
if (f4%failed()) then
105+
call f4%print_error_message(error_unit)
106+
error_cnt = error_cnt + 1
107+
end if
108+
71109
end if
72110
end if
73111

0 commit comments

Comments
 (0)