Skip to content

Commit 976d5d6

Browse files
committed
added a string to json_file assignment operator.
Fixes #463 also fixed typos in unit test 46
1 parent a83481c commit 976d5d6

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

src/json_file_module.F90

+23-1
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,11 @@ module json_file_module
245245
procedure,pass(me) :: MAYBEWRAP(json_file_valid_path_op)
246246

247247
generic,public :: assignment(=) => assign_json_file,&
248-
assign_json_file_to_string
248+
assign_json_file_to_string,&
249+
assign_string_to_json_file
249250
procedure :: assign_json_file
250251
procedure,pass(me) :: assign_json_file_to_string
252+
procedure :: assign_string_to_json_file
251253

252254
! ***************************************************
253255
! private routines
@@ -1184,6 +1186,26 @@ subroutine assign_json_file_to_string(str,me)
11841186
end subroutine assign_json_file_to_string
11851187
!*****************************************************************************************
11861188

1189+
!*****************************************************************************************
1190+
!> author: Jacob Williams
1191+
!
1192+
! Assignment operator for [[json_core(type)]] = character.
1193+
! This is just a wrapper for the [[json_file_load_from_string]] routine.
1194+
1195+
subroutine assign_string_to_json_file(me,str)
1196+
1197+
implicit none
1198+
1199+
class(json_file),intent(inout) :: me
1200+
character(kind=CK,len=*),intent(in) :: str
1201+
1202+
if (associated(me%p)) call me%destroy()
1203+
if (me%core%failed()) call me%core%clear_exceptions()
1204+
call me%deserialize(str)
1205+
1206+
end subroutine assign_string_to_json_file
1207+
!*****************************************************************************************
1208+
11871209
!*****************************************************************************************
11881210
!> author: Jacob Williams
11891211
!

src/tests/jf_test_46.F90

+35-16
Original file line numberDiff line numberDiff line change
@@ -170,94 +170,113 @@ subroutine test_46(error_cnt)
170170

171171
! unicode:
172172
call json_f%get(CK_'not_there', ival, found, default=99_IK)
173-
if (json%failed() .or. found .or. ival /= 99_IK) then
173+
if (json_f%failed() .or. found .or. ival /= 99_IK) then
174174
write(error_unit,'(A)') 'Error using json_get_integer_by_path default'
175175
error_cnt = error_cnt + 1
176176
end if
177177

178178
call json_f%get(CK_'not_there', rval, found, default=99.0_RK)
179-
if (json%failed() .or. found .or. rval-99.0_RK>0.0_RK) then
179+
if (json_f%failed() .or. found .or. rval-99.0_RK>0.0_RK) then
180180
write(error_unit,'(A)') 'Error using json_get_real_by_path default'
181181
error_cnt = error_cnt + 1
182182
end if
183183

184184
call json_f%get(CK_'not_there', lval, found, default=.true.)
185-
if (json%failed() .or. found .or. lval .neqv. .true.) then
185+
if (json_f%failed() .or. found .or. lval .neqv. .true.) then
186186
write(error_unit,'(A)') 'Error using json_get_logical_by_path default'
187187
error_cnt = error_cnt + 1
188188
end if
189189

190190
call json_f%get(CK_'not_there', cval, found, default=CK_'default')
191-
if (json%failed() .or. found .or. cval /= CK_'default') then
191+
if (json_f%failed() .or. found .or. cval /= CK_'default') then
192192
write(error_unit,'(A)') 'Error using json_get_string_by_path default'
193193
error_cnt = error_cnt + 1
194194
end if
195195

196196
call json_f%get(CK_'not_there', cvec, found, default=cvec_default)
197-
if (json%failed() .or. found .or. any(cvec /= cvec_default)) then
197+
if (json_f%failed() .or. found .or. any(cvec /= cvec_default)) then
198198
write(error_unit,'(A)') 'Error using json_get_string_vec_by_path default'
199199
error_cnt = error_cnt + 1
200200
end if
201201
call json_f%get(CK_'not_there', cvec2, ilen, found, default=cvec_default)
202-
if (json%failed() .or. found .or. any(cvec2 /= cvec_default) .or. any(ilen/=1_IK)) then
202+
if (json_f%failed() .or. found .or. any(cvec2 /= cvec_default) .or. any(ilen/=1_IK)) then
203203
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
204204
error_cnt = error_cnt + 1
205205
end if
206206
call json_f%get(CK_'not_there', cvec2, ilen, found, default=cvec_default, default_ilen=ilen_default)
207-
if (json%failed() .or. found .or. any(cvec2 /= cvec_default) .or. any(ilen/=1_IK)) then
207+
if (json_f%failed() .or. found .or. any(cvec2 /= cvec_default) .or. any(ilen/=1_IK)) then
208208
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
209209
error_cnt = error_cnt + 1
210210
end if
211211

212212
! default:
213213
call json_f%get('not_there', ival, found, default=99_IK)
214-
if (json%failed() .or. found .or. ival /= 99_IK) then
214+
if (json_f%failed() .or. found .or. ival /= 99_IK) then
215215
write(error_unit,'(A)') 'Error using json_get_integer_by_path default'
216216
error_cnt = error_cnt + 1
217217
end if
218218

219219
call json_f%get('not_there', rval, found, default=99.0_RK)
220-
if (json%failed() .or. found .or. rval-99.0_RK>0.0_RK) then
220+
if (json_f%failed() .or. found .or. rval-99.0_RK>0.0_RK) then
221221
write(error_unit,'(A)') 'Error using json_get_real_by_path default'
222222
error_cnt = error_cnt + 1
223223
end if
224224

225225
call json_f%get('not_there', lval, found, default=.true.)
226-
if (json%failed() .or. found .or. lval .neqv. .true.) then
226+
if (json_f%failed() .or. found .or. lval .neqv. .true.) then
227227
write(error_unit,'(A)') 'Error using json_get_logical_by_path default'
228228
error_cnt = error_cnt + 1
229229
end if
230230

231231
call json_f%get('not_there', cval, found, default=CK_'default')
232-
if (json%failed() .or. found .or. cval /= CK_'default') then
232+
if (json_f%failed() .or. found .or. cval /= CK_'default') then
233233
write(error_unit,'(A)') 'Error using json_get_string_by_path default'
234234
error_cnt = error_cnt + 1
235235
end if
236236

237237
call json_f%get('not_there', cvec, found, default=cvec_default)
238-
if (json%failed() .or. found .or. any(cvec /= cvec_default)) then
238+
if (json_f%failed() .or. found .or. any(cvec /= cvec_default)) then
239239
write(error_unit,'(A)') 'Error using json_get_string_vec_by_path default'
240240
error_cnt = error_cnt + 1
241241
end if
242242
call json_f%get('not_there', cvec2, ilen, found, default=cvec_default)
243-
if (json%failed() .or. found .or. any(cvec2 /= cvec_default) .or. any(ilen/=1_IK)) then
243+
if (json_f%failed() .or. found .or. any(cvec2 /= cvec_default) .or. any(ilen/=1_IK)) then
244244
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
245245
error_cnt = error_cnt + 1
246246
end if
247247
call json_f%get('not_there', cvec2, ilen, found, default=cvec_default, default_ilen=ilen_default)
248-
if (json%failed() .or. found .or. any(cvec2 /= cvec_default) .or. any(ilen/=1_IK)) then
248+
if (json_f%failed() .or. found .or. any(cvec2 /= cvec_default) .or. any(ilen/=1_IK)) then
249249
write(error_unit,'(A)') 'Error using json_get_alloc_string_vec_by_path default'
250250
error_cnt = error_cnt + 1
251251
end if
252252

253-
! now, we try them when an exception is active:
254253
call json_f%destroy()
255-
json_f = json_file(CK_'{"x": 1.0e.2.1}') ! this will raise an exception
254+
255+
json_f = str ! this should succeed
256+
if (json_f%failed()) then
257+
write(error_unit,'(A)') 'Error in json_file = string assignment operator'
258+
error_cnt = error_cnt + 1
259+
end if
260+
261+
! now, we try them when an exception is active:
262+
json_f = CK_'{"x": 1.0e.2.1}' ! this will raise an exception
263+
if (.not. json_f%failed()) then
264+
write(error_unit,'(A)') 'Error in json_file = string assignment operator : '//&
265+
'should have raised an exception'
266+
error_cnt = error_cnt + 1
267+
end if
268+
256269
call json_f%get(CK_'not_there', rval, found, default=99.0_RK)
257270
call json_f%get(CK_'not_there', cvec, found, default=[CK_'1'])
258271
call json_f%get(CK_'not_there', cvec2, ilen, found, default=cvec_default)
259272
call json_f%get(CK_'not_there', cvec2, ilen, found, default=cvec_default, default_ilen=ilen_default)
260273

274+
json_f = str ! now, try again after a failure
275+
if (json_f%failed()) then
276+
write(error_unit,'(A)') 'Error in json_file = string assignment operator'
277+
error_cnt = error_cnt + 1
278+
end if
279+
261280
if (error_cnt==0) then
262281
write(error_unit,'(A)') 'Success!'
263282
else

0 commit comments

Comments
 (0)