@@ -61,13 +61,15 @@ module json_file_module
61
61
62
62
private
63
63
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.
66
65
type (json_value),pointer :: p = > null () ! ! the JSON structure read from the file
67
66
68
67
contains
69
68
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
71
73
72
74
procedure ,public :: load_file = > json_file_load
73
75
@@ -109,9 +111,20 @@ module json_file_module
109
111
json_file_update_string_val_ascii
110
112
#endif
111
113
114
+ ! traverse
115
+ procedure ,public :: traverse = > json_file_traverse
116
+
117
+ ! ***************************************************
118
+ ! private routines
119
+ ! ***************************************************
120
+
112
121
! load from string:
113
122
procedure :: MAYBEWRAP(json_file_load_from_string)
114
123
124
+ ! initialize
125
+ procedure :: initialize_json_core_in_file
126
+ procedure :: set_json_core_in_file
127
+
115
128
! git info:
116
129
procedure :: MAYBEWRAP(json_file_variable_info)
117
130
@@ -188,7 +201,7 @@ pure function json_file_failed(me) result(failed)
188
201
class(json_file),intent (in ) :: me
189
202
logical (LK) :: failed ! ! will be true if there has been an error.
190
203
191
- failed = me% json % failed()
204
+ failed = me% core % failed()
192
205
193
206
end function json_file_failed
194
207
! *****************************************************************************************
@@ -205,7 +218,7 @@ subroutine json_file_check_for_errors(me,status_ok,error_msg)
205
218
logical (LK),intent (out ) :: status_ok ! ! true if there were no errors
206
219
character (kind= CK,len= :),allocatable ,intent (out ) :: error_msg ! ! the error message (if there were errors)
207
220
208
- call me% json % check_for_errors(status_ok,error_msg)
221
+ call me% core % check_for_errors(status_ok,error_msg)
209
222
210
223
end subroutine json_file_check_for_errors
211
224
! *****************************************************************************************
@@ -220,7 +233,7 @@ pure subroutine json_file_clear_exceptions(me)
220
233
221
234
class(json_file),intent (inout ) :: me
222
235
223
- call me% json % clear_exceptions()
236
+ call me% core % clear_exceptions()
224
237
225
238
end subroutine json_file_clear_exceptions
226
239
! *****************************************************************************************
@@ -236,7 +249,7 @@ subroutine json_file_print_error_message(me,io_unit)
236
249
class(json_file),intent (inout ) :: me
237
250
integer , intent (in ), optional :: io_unit
238
251
239
- call me% json % print_error_message(io_unit)
252
+ call me% core % print_error_message(io_unit)
240
253
241
254
end subroutine json_file_print_error_message
242
255
! *****************************************************************************************
@@ -274,7 +287,7 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
274
287
logical (LK),intent (in ),optional :: case_sensitive_keys ! ! for name and path comparisons, are they
275
288
! ! case sensitive.
276
289
277
- call me% json % initialize(verbose,compact_reals,&
290
+ call me% core % initialize(verbose,compact_reals,&
278
291
print_signs,real_format,spaces_per_tab,&
279
292
strict_type_checking,&
280
293
trailing_spaces_significant,&
@@ -283,6 +296,44 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
283
296
end subroutine initialize_json_core_in_file
284
297
! *****************************************************************************************
285
298
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
+
286
337
! *****************************************************************************************
287
338
! > author: Izaak Beekman
288
339
! date: 07/23/2015
@@ -346,7 +397,7 @@ function initialize_json_file_v2(json_value_object, json_core_object) &
346
397
type (json_core),intent (in ) :: json_core_object
347
398
348
399
file_object% p = > json_value_object
349
- file_object% json = json_core_object
400
+ file_object% core = json_core_object
350
401
351
402
end function initialize_json_file_v2
352
403
! *****************************************************************************************
@@ -375,10 +426,10 @@ subroutine json_file_destroy(me,destroy_core)
375
426
logical ,intent (in ),optional :: destroy_core ! ! to also destroy the [[json_core]].
376
427
! ! default is to leave it as is.
377
428
378
- if (associated (me% p)) call me% json % destroy(me% p)
429
+ if (associated (me% p)) call me% core % destroy(me% p)
379
430
380
431
if (present (destroy_core)) then
381
- if (destroy_core) call me% json % destroy()
432
+ if (destroy_core) call me% core % destroy()
382
433
end if
383
434
384
435
end subroutine json_file_destroy
@@ -405,7 +456,7 @@ subroutine json_file_move_pointer(to,from)
405
456
if (from% failed()) then
406
457
! Don't get the data if the FROM file has an
407
458
! 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: ' // &
409
460
' error exception in FROM file.' )
410
461
else
411
462
call to % initialize() ! initialize and clear any exceptions that may be present
@@ -414,7 +465,7 @@ subroutine json_file_move_pointer(to,from)
414
465
end if
415
466
416
467
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: ' // &
418
469
' pointer is not associated.' )
419
470
end if
420
471
@@ -448,7 +499,7 @@ subroutine json_file_load(me, filename, unit)
448
499
character (kind= CDK,len=* ),intent (in ) :: filename ! ! the filename to open
449
500
integer (IK),intent (in ),optional :: unit ! ! the unit number to use (if not present, a newunit is used)
450
501
451
- call me% json % parse(file= filename, p= me% p, unit= unit)
502
+ call me% core % parse(file= filename, p= me% p, unit= unit)
452
503
453
504
end subroutine json_file_load
454
505
! *****************************************************************************************
@@ -474,7 +525,7 @@ subroutine json_file_load_from_string(me, str)
474
525
class(json_file),intent (inout ) :: me
475
526
character (kind= CK,len=* ),intent (in ) :: str ! ! string to load JSON data from
476
527
477
- call me% json % parse(str= str, p= me% p)
528
+ call me% core % parse(str= str, p= me% p)
478
529
479
530
end subroutine json_file_load_from_string
480
531
! *****************************************************************************************
@@ -507,7 +558,7 @@ subroutine json_file_print_to_console(me)
507
558
508
559
class(json_file),intent (inout ) :: me
509
560
510
- call me% json % print (me% p,iunit= output_unit)
561
+ call me% core % print (me% p,iunit= output_unit)
511
562
512
563
end subroutine json_file_print_to_console
513
564
! *****************************************************************************************
@@ -526,9 +577,9 @@ subroutine json_file_print_1(me, iunit)
526
577
integer (IK),intent (in ) :: iunit ! ! file unit number (must not be -1)
527
578
528
579
if (iunit/= unit2str) then
529
- call me% json % print (me% p,iunit= iunit)
580
+ call me% core % print (me% p,iunit= iunit)
530
581
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.' )
532
583
end if
533
584
534
585
end subroutine json_file_print_1
@@ -559,7 +610,7 @@ subroutine json_file_print_2(me,filename)
559
610
class(json_file),intent (inout ) :: me
560
611
character (kind= CDK,len=* ),intent (in ) :: filename ! ! filename to print to
561
612
562
- call me% json % print (me% p,filename)
613
+ call me% core % print (me% p,filename)
563
614
564
615
end subroutine json_file_print_2
565
616
! *****************************************************************************************
@@ -587,7 +638,7 @@ subroutine json_file_print_to_string(me,str)
587
638
class(json_file),intent (inout ) :: me
588
639
character (kind= CK,len= :),allocatable ,intent (out ) :: str ! ! string to print JSON data to
589
640
590
- call me% json % print_to_string(me% p,str)
641
+ call me% core % print_to_string(me% p,str)
591
642
592
643
end subroutine json_file_print_to_string
593
644
! *****************************************************************************************
@@ -619,7 +670,7 @@ subroutine json_file_variable_info(me,path,found,var_type,n_children)
619
670
if (found) then
620
671
621
672
! get info:
622
- call me% json % info(p,var_type,n_children)
673
+ call me% core % info(p,var_type,n_children)
623
674
624
675
else
625
676
@@ -689,7 +740,7 @@ subroutine json_file_get_object(me, path, p, found)
689
740
type (json_value),pointer ,intent (out ) :: p ! ! pointer to the variable
690
741
logical (LK),intent (out ),optional :: found ! ! if it was really found
691
742
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)
693
744
694
745
end subroutine json_file_get_object
695
746
! *****************************************************************************************
@@ -727,7 +778,7 @@ subroutine json_file_get_integer(me, path, val, found)
727
778
integer (IK),intent (out ) :: val ! ! value
728
779
logical (LK),intent (out ),optional :: found ! ! if it was really found
729
780
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)
731
782
732
783
end subroutine json_file_get_integer
733
784
! *****************************************************************************************
@@ -765,7 +816,7 @@ subroutine json_file_get_integer_vec(me, path, vec, found)
765
816
integer (IK),dimension (:),allocatable ,intent (out ) :: vec ! ! the value vector
766
817
logical (LK),intent (out ),optional :: found ! ! if it was really found
767
818
768
- call me% json % get(me% p, path, vec, found)
819
+ call me% core % get(me% p, path, vec, found)
769
820
770
821
end subroutine json_file_get_integer_vec
771
822
! *****************************************************************************************
@@ -803,7 +854,7 @@ subroutine json_file_get_double (me, path, val, found)
803
854
real (RK),intent (out ) :: val
804
855
logical (LK),intent (out ),optional :: found
805
856
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)
807
858
808
859
end subroutine json_file_get_double
809
860
! *****************************************************************************************
@@ -841,7 +892,7 @@ subroutine json_file_get_double_vec(me, path, vec, found)
841
892
real (RK),dimension (:),allocatable ,intent (out ) :: vec
842
893
logical (LK),intent (out ),optional :: found
843
894
844
- call me% json % get(me% p, path, vec, found)
895
+ call me% core % get(me% p, path, vec, found)
845
896
846
897
end subroutine json_file_get_double_vec
847
898
! *****************************************************************************************
@@ -879,7 +930,7 @@ subroutine json_file_get_logical(me,path,val,found)
879
930
logical (LK),intent (out ) :: val
880
931
logical (LK),intent (out ),optional :: found
881
932
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)
883
934
884
935
end subroutine json_file_get_logical
885
936
! *****************************************************************************************
@@ -917,7 +968,7 @@ subroutine json_file_get_logical_vec(me, path, vec, found)
917
968
logical (LK),dimension (:),allocatable ,intent (out ) :: vec
918
969
logical (LK),intent (out ),optional :: found
919
970
920
- call me% json % get(me% p, path, vec, found)
971
+ call me% core % get(me% p, path, vec, found)
921
972
922
973
end subroutine json_file_get_logical_vec
923
974
! *****************************************************************************************
@@ -956,7 +1007,7 @@ subroutine json_file_get_string(me, path, val, found)
956
1007
character (kind= CK,len= :),allocatable ,intent (out ) :: val
957
1008
logical (LK),intent (out ),optional :: found
958
1009
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)
960
1011
961
1012
end subroutine json_file_get_string
962
1013
! *****************************************************************************************
@@ -994,7 +1045,7 @@ subroutine json_file_get_string_vec(me, path, vec, found)
994
1045
character (kind= CK,len=* ),dimension (:),allocatable ,intent (out ) :: vec
995
1046
logical (LK),intent (out ),optional :: found
996
1047
997
- call me% json % get(me% p, path, vec, found)
1048
+ call me% core % get(me% p, path, vec, found)
998
1049
999
1050
end subroutine json_file_get_string_vec
1000
1051
! *****************************************************************************************
@@ -1037,7 +1088,7 @@ subroutine json_file_update_integer(me,name,val,found)
1037
1088
integer (IK),intent (in ) :: val
1038
1089
logical (LK),intent (out ) :: found
1039
1090
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)
1041
1092
1042
1093
end subroutine json_file_update_integer
1043
1094
! *****************************************************************************************
@@ -1080,7 +1131,7 @@ subroutine json_file_update_logical(me,name,val,found)
1080
1131
logical (LK),intent (in ) :: val
1081
1132
logical (LK),intent (out ) :: found
1082
1133
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)
1084
1135
1085
1136
end subroutine json_file_update_logical
1086
1137
! *****************************************************************************************
@@ -1123,7 +1174,7 @@ subroutine json_file_update_real(me,name,val,found)
1123
1174
real (RK),intent (in ) :: val
1124
1175
logical (LK),intent (out ) :: found
1125
1176
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)
1127
1178
1128
1179
end subroutine json_file_update_real
1129
1180
! *****************************************************************************************
@@ -1166,7 +1217,7 @@ subroutine json_file_update_string(me,name,val,found)
1166
1217
character (kind= CK,len=* ),intent (in ) :: val
1167
1218
logical (LK),intent (out ) :: found
1168
1219
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)
1170
1221
1171
1222
end subroutine json_file_update_string
1172
1223
! *****************************************************************************************
@@ -1225,6 +1276,26 @@ subroutine json_file_update_string_val_ascii(me,name,val,found)
1225
1276
end subroutine json_file_update_string_val_ascii
1226
1277
! *****************************************************************************************
1227
1278
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
+
1228
1299
! *****************************************************************************************
1229
1300
end module json_file_module
1230
1301
! *****************************************************************************************
0 commit comments