Skip to content

Commit 6b426df

Browse files
committed
first cut at added traverse() to json_file.
1 parent fc322be commit 6b426df

File tree

2 files changed

+72
-44
lines changed

2 files changed

+72
-44
lines changed

src/json_file_module.F90

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ module json_file_module
6161

6262
private
6363

64-
type(json_core) :: json !! the instance of the [[json_core]] factory used for this file
64+
type(json_core),public :: core !! The instance of the [[json_core]]
65+
!! factory used for this file.
66+
!! Note that this is public, so it can
67+
!! also be used by the user.
6568

6669
type(json_value),pointer :: p => null() !! the JSON structure read from the file
6770

@@ -142,6 +145,9 @@ module json_file_module
142145
procedure :: json_file_print_1
143146
procedure :: json_file_print_2
144147

148+
!traverse
149+
procedure,public :: traverse => json_file_traverse
150+
145151
end type json_file
146152
!*********************************************************
147153

@@ -188,7 +194,7 @@ pure function json_file_failed(me) result(failed)
188194
class(json_file),intent(in) :: me
189195
logical(LK) :: failed !! will be true if there has been an error.
190196

191-
failed = me%json%failed()
197+
failed = me%core%failed()
192198

193199
end function json_file_failed
194200
!*****************************************************************************************
@@ -205,7 +211,7 @@ subroutine json_file_check_for_errors(me,status_ok,error_msg)
205211
logical(LK),intent(out) :: status_ok !! true if there were no errors
206212
character(kind=CK,len=:),allocatable,intent(out) :: error_msg !! the error message (if there were errors)
207213

208-
call me%json%check_for_errors(status_ok,error_msg)
214+
call me%core%check_for_errors(status_ok,error_msg)
209215

210216
end subroutine json_file_check_for_errors
211217
!*****************************************************************************************
@@ -220,7 +226,7 @@ pure subroutine json_file_clear_exceptions(me)
220226

221227
class(json_file),intent(inout) :: me
222228

223-
call me%json%clear_exceptions()
229+
call me%core%clear_exceptions()
224230

225231
end subroutine json_file_clear_exceptions
226232
!*****************************************************************************************
@@ -236,7 +242,7 @@ subroutine json_file_print_error_message(me,io_unit)
236242
class(json_file),intent(inout) :: me
237243
integer, intent(in), optional :: io_unit
238244

239-
call me%json%print_error_message(io_unit)
245+
call me%core%print_error_message(io_unit)
240246

241247
end subroutine json_file_print_error_message
242248
!*****************************************************************************************
@@ -274,7 +280,7 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
274280
logical(LK),intent(in),optional :: case_sensitive_keys !! for name and path comparisons, are they
275281
!! case sensitive.
276282

277-
call me%json%initialize(verbose,compact_reals,&
283+
call me%core%initialize(verbose,compact_reals,&
278284
print_signs,real_format,spaces_per_tab,&
279285
strict_type_checking,&
280286
trailing_spaces_significant,&
@@ -346,7 +352,7 @@ function initialize_json_file_v2(json_value_object, json_core_object) &
346352
type(json_core),intent(in) :: json_core_object
347353

348354
file_object%p => json_value_object
349-
file_object%json = json_core_object
355+
file_object%core = json_core_object
350356

351357
end function initialize_json_file_v2
352358
!*****************************************************************************************
@@ -375,10 +381,10 @@ subroutine json_file_destroy(me,destroy_core)
375381
logical,intent(in),optional :: destroy_core !! to also destroy the [[json_core]].
376382
!! default is to leave it as is.
377383

378-
if (associated(me%p)) call me%json%destroy(me%p)
384+
if (associated(me%p)) call me%core%destroy(me%p)
379385

380386
if (present(destroy_core)) then
381-
if (destroy_core) call me%json%destroy()
387+
if (destroy_core) call me%core%destroy()
382388
end if
383389

384390
end subroutine json_file_destroy
@@ -405,7 +411,7 @@ subroutine json_file_move_pointer(to,from)
405411
if (from%failed()) then
406412
!Don't get the data if the FROM file has an
407413
!active exception, since it may not be valid.
408-
call to%json%throw_exception('Error in json_file_move_pointer: '//&
414+
call to%core%throw_exception('Error in json_file_move_pointer: '//&
409415
'error exception in FROM file.')
410416
else
411417
call to%initialize() !initialize and clear any exceptions that may be present
@@ -414,7 +420,7 @@ subroutine json_file_move_pointer(to,from)
414420
end if
415421

416422
else
417-
call to%json%throw_exception('Error in json_file_move_pointer: '//&
423+
call to%core%throw_exception('Error in json_file_move_pointer: '//&
418424
'pointer is not associated.')
419425
end if
420426

@@ -448,7 +454,7 @@ subroutine json_file_load(me, filename, unit)
448454
character(kind=CDK,len=*),intent(in) :: filename !! the filename to open
449455
integer(IK),intent(in),optional :: unit !! the unit number to use (if not present, a newunit is used)
450456

451-
call me%json%parse(file=filename, p=me%p, unit=unit)
457+
call me%core%parse(file=filename, p=me%p, unit=unit)
452458

453459
end subroutine json_file_load
454460
!*****************************************************************************************
@@ -474,7 +480,7 @@ subroutine json_file_load_from_string(me, str)
474480
class(json_file),intent(inout) :: me
475481
character(kind=CK,len=*),intent(in) :: str !! string to load JSON data from
476482

477-
call me%json%parse(str=str, p=me%p)
483+
call me%core%parse(str=str, p=me%p)
478484

479485
end subroutine json_file_load_from_string
480486
!*****************************************************************************************
@@ -507,7 +513,7 @@ subroutine json_file_print_to_console(me)
507513

508514
class(json_file),intent(inout) :: me
509515

510-
call me%json%print(me%p,iunit=output_unit)
516+
call me%core%print(me%p,iunit=output_unit)
511517

512518
end subroutine json_file_print_to_console
513519
!*****************************************************************************************
@@ -526,9 +532,9 @@ subroutine json_file_print_1(me, iunit)
526532
integer(IK),intent(in) :: iunit !! file unit number (must not be -1)
527533

528534
if (iunit/=unit2str) then
529-
call me%json%print(me%p,iunit=iunit)
535+
call me%core%print(me%p,iunit=iunit)
530536
else
531-
call me%json%throw_exception('Error in json_file_print_1: iunit must not be -1.')
537+
call me%core%throw_exception('Error in json_file_print_1: iunit must not be -1.')
532538
end if
533539

534540
end subroutine json_file_print_1
@@ -559,7 +565,7 @@ subroutine json_file_print_2(me,filename)
559565
class(json_file),intent(inout) :: me
560566
character(kind=CDK,len=*),intent(in) :: filename !! filename to print to
561567

562-
call me%json%print(me%p,filename)
568+
call me%core%print(me%p,filename)
563569

564570
end subroutine json_file_print_2
565571
!*****************************************************************************************
@@ -587,7 +593,7 @@ subroutine json_file_print_to_string(me,str)
587593
class(json_file),intent(inout) :: me
588594
character(kind=CK,len=:),allocatable,intent(out) :: str !! string to print JSON data to
589595

590-
call me%json%print_to_string(me%p,str)
596+
call me%core%print_to_string(me%p,str)
591597

592598
end subroutine json_file_print_to_string
593599
!*****************************************************************************************
@@ -619,7 +625,7 @@ subroutine json_file_variable_info(me,path,found,var_type,n_children)
619625
if (found) then
620626

621627
!get info:
622-
call me%json%info(p,var_type,n_children)
628+
call me%core%info(p,var_type,n_children)
623629

624630
else
625631

@@ -689,7 +695,7 @@ subroutine json_file_get_object(me, path, p, found)
689695
type(json_value),pointer,intent(out) :: p !! pointer to the variable
690696
logical(LK),intent(out),optional :: found !! if it was really found
691697

692-
call me%json%get(me%p, path=path, p=p, found=found)
698+
call me%core%get(me%p, path=path, p=p, found=found)
693699

694700
end subroutine json_file_get_object
695701
!*****************************************************************************************
@@ -727,7 +733,7 @@ subroutine json_file_get_integer(me, path, val, found)
727733
integer(IK),intent(out) :: val !! value
728734
logical(LK),intent(out),optional :: found !! if it was really found
729735

730-
call me%json%get(me%p, path=path, value=val, found=found)
736+
call me%core%get(me%p, path=path, value=val, found=found)
731737

732738
end subroutine json_file_get_integer
733739
!*****************************************************************************************
@@ -765,7 +771,7 @@ subroutine json_file_get_integer_vec(me, path, vec, found)
765771
integer(IK),dimension(:),allocatable,intent(out) :: vec !! the value vector
766772
logical(LK),intent(out),optional :: found !! if it was really found
767773

768-
call me%json%get(me%p, path, vec, found)
774+
call me%core%get(me%p, path, vec, found)
769775

770776
end subroutine json_file_get_integer_vec
771777
!*****************************************************************************************
@@ -803,7 +809,7 @@ subroutine json_file_get_double (me, path, val, found)
803809
real(RK),intent(out) :: val
804810
logical(LK),intent(out),optional :: found
805811

806-
call me%json%get(me%p, path=path, value=val, found=found)
812+
call me%core%get(me%p, path=path, value=val, found=found)
807813

808814
end subroutine json_file_get_double
809815
!*****************************************************************************************
@@ -841,7 +847,7 @@ subroutine json_file_get_double_vec(me, path, vec, found)
841847
real(RK),dimension(:),allocatable,intent(out) :: vec
842848
logical(LK),intent(out),optional :: found
843849

844-
call me%json%get(me%p, path, vec, found)
850+
call me%core%get(me%p, path, vec, found)
845851

846852
end subroutine json_file_get_double_vec
847853
!*****************************************************************************************
@@ -879,7 +885,7 @@ subroutine json_file_get_logical(me,path,val,found)
879885
logical(LK),intent(out) :: val
880886
logical(LK),intent(out),optional :: found
881887

882-
call me%json%get(me%p, path=path, value=val, found=found)
888+
call me%core%get(me%p, path=path, value=val, found=found)
883889

884890
end subroutine json_file_get_logical
885891
!*****************************************************************************************
@@ -917,7 +923,7 @@ subroutine json_file_get_logical_vec(me, path, vec, found)
917923
logical(LK),dimension(:),allocatable,intent(out) :: vec
918924
logical(LK),intent(out),optional :: found
919925

920-
call me%json%get(me%p, path, vec, found)
926+
call me%core%get(me%p, path, vec, found)
921927

922928
end subroutine json_file_get_logical_vec
923929
!*****************************************************************************************
@@ -956,7 +962,7 @@ subroutine json_file_get_string(me, path, val, found)
956962
character(kind=CK,len=:),allocatable,intent(out) :: val
957963
logical(LK),intent(out),optional :: found
958964

959-
call me%json%get(me%p, path=path, value=val, found=found)
965+
call me%core%get(me%p, path=path, value=val, found=found)
960966

961967
end subroutine json_file_get_string
962968
!*****************************************************************************************
@@ -994,7 +1000,7 @@ subroutine json_file_get_string_vec(me, path, vec, found)
9941000
character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec
9951001
logical(LK),intent(out),optional :: found
9961002

997-
call me%json%get(me%p, path, vec, found)
1003+
call me%core%get(me%p, path, vec, found)
9981004

9991005
end subroutine json_file_get_string_vec
10001006
!*****************************************************************************************
@@ -1037,7 +1043,7 @@ subroutine json_file_update_integer(me,name,val,found)
10371043
integer(IK),intent(in) :: val
10381044
logical(LK),intent(out) :: found
10391045

1040-
if (.not. me%json%failed()) call me%json%update(me%p,name,val,found)
1046+
if (.not. me%core%failed()) call me%core%update(me%p,name,val,found)
10411047

10421048
end subroutine json_file_update_integer
10431049
!*****************************************************************************************
@@ -1080,7 +1086,7 @@ subroutine json_file_update_logical(me,name,val,found)
10801086
logical(LK),intent(in) :: val
10811087
logical(LK),intent(out) :: found
10821088

1083-
if (.not. me%json%failed()) call me%json%update(me%p,name,val,found)
1089+
if (.not. me%core%failed()) call me%core%update(me%p,name,val,found)
10841090

10851091
end subroutine json_file_update_logical
10861092
!*****************************************************************************************
@@ -1123,7 +1129,7 @@ subroutine json_file_update_real(me,name,val,found)
11231129
real(RK),intent(in) :: val
11241130
logical(LK),intent(out) :: found
11251131

1126-
if (.not. me%json%failed()) call me%json%update(me%p,name,val,found)
1132+
if (.not. me%core%failed()) call me%core%update(me%p,name,val,found)
11271133

11281134
end subroutine json_file_update_real
11291135
!*****************************************************************************************
@@ -1166,7 +1172,7 @@ subroutine json_file_update_string(me,name,val,found)
11661172
character(kind=CK,len=*),intent(in) :: val
11671173
logical(LK),intent(out) :: found
11681174

1169-
if (.not. me%json%failed()) call me%json%update(me%p,name,val,found)
1175+
if (.not. me%core%failed()) call me%core%update(me%p,name,val,found)
11701176

11711177
end subroutine json_file_update_string
11721178
!*****************************************************************************************
@@ -1225,6 +1231,26 @@ subroutine json_file_update_string_val_ascii(me,name,val,found)
12251231
end subroutine json_file_update_string_val_ascii
12261232
!*****************************************************************************************
12271233

1234+
!*****************************************************************************************
1235+
!> author: Jacob Williams
1236+
! date: 6/11/2016
1237+
!
1238+
! Traverse the JSON structure in the file.
1239+
! This routine calls the user-specified [[json_traverse_callback_func]]
1240+
! for each element of the structure.
1241+
1242+
subroutine json_file_traverse(me,traverse_callback)
1243+
1244+
implicit none
1245+
1246+
class(json_file),intent(inout) :: me
1247+
procedure(json_traverse_callback_func) :: traverse_callback
1248+
1249+
call me%core%traverse(me%p,traverse_callback)
1250+
1251+
end subroutine json_file_traverse
1252+
!*****************************************************************************************
1253+
12281254
!*****************************************************************************************
12291255
end module json_file_module
12301256
!*****************************************************************************************

0 commit comments

Comments
 (0)