Skip to content

Commit 73d046f

Browse files
Merge pull request #206 from jacobwilliams/json-file-traverse
JSON file traverse
2 parents fc322be + 6fe76f7 commit 73d046f

File tree

4 files changed

+175
-50
lines changed

4 files changed

+175
-50
lines changed

src/json_file_module.F90

Lines changed: 105 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ module json_file_module
6161

6262
private
6363

64-
type(json_core) :: json !! the instance of the [[json_core]] factory used for this file
65-
64+
type(json_core) :: core !! The instance of the [[json_core]] factory used for this file.
6665
type(json_value),pointer :: p => null() !! the JSON structure read from the file
6766

6867
contains
6968

70-
procedure,public :: initialize => initialize_json_core_in_file
69+
generic,public :: initialize => initialize_json_core_in_file,&
70+
set_json_core_in_file
71+
72+
procedure,public :: get_core => get_json_core_in_file
7173

7274
procedure,public :: load_file => json_file_load
7375

@@ -109,9 +111,20 @@ module json_file_module
109111
json_file_update_string_val_ascii
110112
#endif
111113

114+
!traverse
115+
procedure,public :: traverse => json_file_traverse
116+
117+
! ***************************************************
118+
! private routines
119+
! ***************************************************
120+
112121
!load from string:
113122
procedure :: MAYBEWRAP(json_file_load_from_string)
114123

124+
!initialize
125+
procedure :: initialize_json_core_in_file
126+
procedure :: set_json_core_in_file
127+
115128
!git info:
116129
procedure :: MAYBEWRAP(json_file_variable_info)
117130

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

191-
failed = me%json%failed()
204+
failed = me%core%failed()
192205

193206
end function json_file_failed
194207
!*****************************************************************************************
@@ -205,7 +218,7 @@ subroutine json_file_check_for_errors(me,status_ok,error_msg)
205218
logical(LK),intent(out) :: status_ok !! true if there were no errors
206219
character(kind=CK,len=:),allocatable,intent(out) :: error_msg !! the error message (if there were errors)
207220

208-
call me%json%check_for_errors(status_ok,error_msg)
221+
call me%core%check_for_errors(status_ok,error_msg)
209222

210223
end subroutine json_file_check_for_errors
211224
!*****************************************************************************************
@@ -220,7 +233,7 @@ pure subroutine json_file_clear_exceptions(me)
220233

221234
class(json_file),intent(inout) :: me
222235

223-
call me%json%clear_exceptions()
236+
call me%core%clear_exceptions()
224237

225238
end subroutine json_file_clear_exceptions
226239
!*****************************************************************************************
@@ -236,7 +249,7 @@ subroutine json_file_print_error_message(me,io_unit)
236249
class(json_file),intent(inout) :: me
237250
integer, intent(in), optional :: io_unit
238251

239-
call me%json%print_error_message(io_unit)
252+
call me%core%print_error_message(io_unit)
240253

241254
end subroutine json_file_print_error_message
242255
!*****************************************************************************************
@@ -274,7 +287,7 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
274287
logical(LK),intent(in),optional :: case_sensitive_keys !! for name and path comparisons, are they
275288
!! case sensitive.
276289

277-
call me%json%initialize(verbose,compact_reals,&
290+
call me%core%initialize(verbose,compact_reals,&
278291
print_signs,real_format,spaces_per_tab,&
279292
strict_type_checking,&
280293
trailing_spaces_significant,&
@@ -283,6 +296,44 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
283296
end subroutine initialize_json_core_in_file
284297
!*****************************************************************************************
285298

299+
!*****************************************************************************************
300+
!>
301+
! Set the [[json_core]] for this [[json_file]].
302+
!
303+
!@note: This does not destroy the data in the file.
304+
!
305+
!@note: This one is used if you want to initialize the file with
306+
! an already-existing [[json_core]] (presumably, this was already
307+
! initialized by a call to [[initialize_json_core]] or similar).
308+
309+
subroutine set_json_core_in_file(me,core)
310+
311+
implicit none
312+
313+
class(json_file),intent(inout) :: me
314+
type(json_core),intent(in) :: core
315+
316+
me%core = core
317+
318+
end subroutine set_json_core_in_file
319+
!*****************************************************************************************
320+
321+
!*****************************************************************************************
322+
!>
323+
! Get a copy of the [[json_core]] in this [[json_file]].
324+
325+
subroutine get_json_core_in_file(me,core)
326+
327+
implicit none
328+
329+
class(json_file),intent(in) :: me
330+
type(json_core),intent(out) :: core
331+
332+
core = me%core
333+
334+
end subroutine get_json_core_in_file
335+
!*****************************************************************************************
336+
286337
!*****************************************************************************************
287338
!> author: Izaak Beekman
288339
! date: 07/23/2015
@@ -346,7 +397,7 @@ function initialize_json_file_v2(json_value_object, json_core_object) &
346397
type(json_core),intent(in) :: json_core_object
347398

348399
file_object%p => json_value_object
349-
file_object%json = json_core_object
400+
file_object%core = json_core_object
350401

351402
end function initialize_json_file_v2
352403
!*****************************************************************************************
@@ -375,10 +426,10 @@ subroutine json_file_destroy(me,destroy_core)
375426
logical,intent(in),optional :: destroy_core !! to also destroy the [[json_core]].
376427
!! default is to leave it as is.
377428

378-
if (associated(me%p)) call me%json%destroy(me%p)
429+
if (associated(me%p)) call me%core%destroy(me%p)
379430

380431
if (present(destroy_core)) then
381-
if (destroy_core) call me%json%destroy()
432+
if (destroy_core) call me%core%destroy()
382433
end if
383434

384435
end subroutine json_file_destroy
@@ -405,7 +456,7 @@ subroutine json_file_move_pointer(to,from)
405456
if (from%failed()) then
406457
!Don't get the data if the FROM file has an
407458
!active exception, since it may not be valid.
408-
call to%json%throw_exception('Error in json_file_move_pointer: '//&
459+
call to%core%throw_exception('Error in json_file_move_pointer: '//&
409460
'error exception in FROM file.')
410461
else
411462
call to%initialize() !initialize and clear any exceptions that may be present
@@ -414,7 +465,7 @@ subroutine json_file_move_pointer(to,from)
414465
end if
415466

416467
else
417-
call to%json%throw_exception('Error in json_file_move_pointer: '//&
468+
call to%core%throw_exception('Error in json_file_move_pointer: '//&
418469
'pointer is not associated.')
419470
end if
420471

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

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

453504
end subroutine json_file_load
454505
!*****************************************************************************************
@@ -474,7 +525,7 @@ subroutine json_file_load_from_string(me, str)
474525
class(json_file),intent(inout) :: me
475526
character(kind=CK,len=*),intent(in) :: str !! string to load JSON data from
476527

477-
call me%json%parse(str=str, p=me%p)
528+
call me%core%parse(str=str, p=me%p)
478529

479530
end subroutine json_file_load_from_string
480531
!*****************************************************************************************
@@ -507,7 +558,7 @@ subroutine json_file_print_to_console(me)
507558

508559
class(json_file),intent(inout) :: me
509560

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

512563
end subroutine json_file_print_to_console
513564
!*****************************************************************************************
@@ -526,9 +577,9 @@ subroutine json_file_print_1(me, iunit)
526577
integer(IK),intent(in) :: iunit !! file unit number (must not be -1)
527578

528579
if (iunit/=unit2str) then
529-
call me%json%print(me%p,iunit=iunit)
580+
call me%core%print(me%p,iunit=iunit)
530581
else
531-
call me%json%throw_exception('Error in json_file_print_1: iunit must not be -1.')
582+
call me%core%throw_exception('Error in json_file_print_1: iunit must not be -1.')
532583
end if
533584

534585
end subroutine json_file_print_1
@@ -559,7 +610,7 @@ subroutine json_file_print_2(me,filename)
559610
class(json_file),intent(inout) :: me
560611
character(kind=CDK,len=*),intent(in) :: filename !! filename to print to
561612

562-
call me%json%print(me%p,filename)
613+
call me%core%print(me%p,filename)
563614

564615
end subroutine json_file_print_2
565616
!*****************************************************************************************
@@ -587,7 +638,7 @@ subroutine json_file_print_to_string(me,str)
587638
class(json_file),intent(inout) :: me
588639
character(kind=CK,len=:),allocatable,intent(out) :: str !! string to print JSON data to
589640

590-
call me%json%print_to_string(me%p,str)
641+
call me%core%print_to_string(me%p,str)
591642

592643
end subroutine json_file_print_to_string
593644
!*****************************************************************************************
@@ -619,7 +670,7 @@ subroutine json_file_variable_info(me,path,found,var_type,n_children)
619670
if (found) then
620671

621672
!get info:
622-
call me%json%info(p,var_type,n_children)
673+
call me%core%info(p,var_type,n_children)
623674

624675
else
625676

@@ -689,7 +740,7 @@ subroutine json_file_get_object(me, path, p, found)
689740
type(json_value),pointer,intent(out) :: p !! pointer to the variable
690741
logical(LK),intent(out),optional :: found !! if it was really found
691742

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

694745
end subroutine json_file_get_object
695746
!*****************************************************************************************
@@ -727,7 +778,7 @@ subroutine json_file_get_integer(me, path, val, found)
727778
integer(IK),intent(out) :: val !! value
728779
logical(LK),intent(out),optional :: found !! if it was really found
729780

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

732783
end subroutine json_file_get_integer
733784
!*****************************************************************************************
@@ -765,7 +816,7 @@ subroutine json_file_get_integer_vec(me, path, vec, found)
765816
integer(IK),dimension(:),allocatable,intent(out) :: vec !! the value vector
766817
logical(LK),intent(out),optional :: found !! if it was really found
767818

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

770821
end subroutine json_file_get_integer_vec
771822
!*****************************************************************************************
@@ -803,7 +854,7 @@ subroutine json_file_get_double (me, path, val, found)
803854
real(RK),intent(out) :: val
804855
logical(LK),intent(out),optional :: found
805856

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

808859
end subroutine json_file_get_double
809860
!*****************************************************************************************
@@ -841,7 +892,7 @@ subroutine json_file_get_double_vec(me, path, vec, found)
841892
real(RK),dimension(:),allocatable,intent(out) :: vec
842893
logical(LK),intent(out),optional :: found
843894

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

846897
end subroutine json_file_get_double_vec
847898
!*****************************************************************************************
@@ -879,7 +930,7 @@ subroutine json_file_get_logical(me,path,val,found)
879930
logical(LK),intent(out) :: val
880931
logical(LK),intent(out),optional :: found
881932

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

884935
end subroutine json_file_get_logical
885936
!*****************************************************************************************
@@ -917,7 +968,7 @@ subroutine json_file_get_logical_vec(me, path, vec, found)
917968
logical(LK),dimension(:),allocatable,intent(out) :: vec
918969
logical(LK),intent(out),optional :: found
919970

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

922973
end subroutine json_file_get_logical_vec
923974
!*****************************************************************************************
@@ -956,7 +1007,7 @@ subroutine json_file_get_string(me, path, val, found)
9561007
character(kind=CK,len=:),allocatable,intent(out) :: val
9571008
logical(LK),intent(out),optional :: found
9581009

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

9611012
end subroutine json_file_get_string
9621013
!*****************************************************************************************
@@ -994,7 +1045,7 @@ subroutine json_file_get_string_vec(me, path, vec, found)
9941045
character(kind=CK,len=*),dimension(:),allocatable,intent(out) :: vec
9951046
logical(LK),intent(out),optional :: found
9961047

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

9991050
end subroutine json_file_get_string_vec
10001051
!*****************************************************************************************
@@ -1037,7 +1088,7 @@ subroutine json_file_update_integer(me,name,val,found)
10371088
integer(IK),intent(in) :: val
10381089
logical(LK),intent(out) :: found
10391090

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

10421093
end subroutine json_file_update_integer
10431094
!*****************************************************************************************
@@ -1080,7 +1131,7 @@ subroutine json_file_update_logical(me,name,val,found)
10801131
logical(LK),intent(in) :: val
10811132
logical(LK),intent(out) :: found
10821133

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

10851136
end subroutine json_file_update_logical
10861137
!*****************************************************************************************
@@ -1123,7 +1174,7 @@ subroutine json_file_update_real(me,name,val,found)
11231174
real(RK),intent(in) :: val
11241175
logical(LK),intent(out) :: found
11251176

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

11281179
end subroutine json_file_update_real
11291180
!*****************************************************************************************
@@ -1166,7 +1217,7 @@ subroutine json_file_update_string(me,name,val,found)
11661217
character(kind=CK,len=*),intent(in) :: val
11671218
logical(LK),intent(out) :: found
11681219

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

11711222
end subroutine json_file_update_string
11721223
!*****************************************************************************************
@@ -1225,6 +1276,26 @@ subroutine json_file_update_string_val_ascii(me,name,val,found)
12251276
end subroutine json_file_update_string_val_ascii
12261277
!*****************************************************************************************
12271278

1279+
!*****************************************************************************************
1280+
!> author: Jacob Williams
1281+
! date: 6/11/2016
1282+
!
1283+
! Traverse the JSON structure in the file.
1284+
! This routine calls the user-specified [[json_traverse_callback_func]]
1285+
! for each element of the structure.
1286+
1287+
subroutine json_file_traverse(me,traverse_callback)
1288+
1289+
implicit none
1290+
1291+
class(json_file),intent(inout) :: me
1292+
procedure(json_traverse_callback_func) :: traverse_callback
1293+
1294+
call me%core%traverse(me%p,traverse_callback)
1295+
1296+
end subroutine json_file_traverse
1297+
!*****************************************************************************************
1298+
12281299
!*****************************************************************************************
12291300
end module json_file_module
12301301
!*****************************************************************************************

0 commit comments

Comments
 (0)