@@ -68,6 +68,13 @@ subroutine test_1(error_cnt)
68
68
error_cnt = error_cnt + 1
69
69
end if
70
70
71
+ ! print each variable:
72
+ write (error_unit,' (A)' ) ' '
73
+ write (error_unit,' (A)' ) ' printing each variable...'
74
+ call core% initialize()
75
+ call json% get(p) ! get root
76
+ call core% traverse(p,print_json_variable) ! print all the variables
77
+
71
78
! extract data from the parsed value
72
79
write (error_unit,' (A)' ) ' '
73
80
write (error_unit,' (A)' ) ' get some data from the file...'
@@ -276,6 +283,62 @@ subroutine test_1(error_cnt)
276
283
277
284
end subroutine test_1
278
285
286
+ subroutine print_json_variable (json ,p ,finished )
287
+
288
+ ! ! A `traverse` routine for printing out all
289
+ ! ! the variables in a JSON structure.
290
+
291
+ implicit none
292
+
293
+ class(json_core),intent (inout ) :: json
294
+ type (json_value),pointer ,intent (in ) :: p
295
+ logical (json_LK),intent (out ) :: finished ! ! set true to stop traversing
296
+
297
+ character (kind= json_CK,len= :),allocatable :: path ! ! path to the variable
298
+ logical (json_LK) :: found ! ! error flag
299
+ type (json_value),pointer :: child ! ! variable's first child
300
+ character (kind= json_CK,len= :),allocatable :: value ! ! variable value as a string
301
+ integer (json_IK) :: var_type ! ! JSON variable type
302
+
303
+ call json% get_child(p,child)
304
+ finished = .false.
305
+
306
+ ! only print the leafs:
307
+ if (.not. associated (child)) then
308
+ call json% get_path(p,path,found,&
309
+ use_alt_array_tokens= .true. ,&
310
+ path_sep= json_CK_' %' ) ! fortran-style
311
+ if (found) then
312
+
313
+ call json% info(p,var_type= var_type)
314
+ select case (var_type)
315
+ case (json_array)
316
+ ! an empty array
317
+ value = json_CK_' ()'
318
+ case (json_object)
319
+ ! an empty object
320
+ value = json_CK_' {}'
321
+ case default
322
+ ! get the value as a string
323
+ ! [assumes strict_type_checking=false]
324
+ ! note: strings are returned escaped without quotes
325
+ call json% get(p,value)
326
+ end select
327
+
328
+ ! check for errors:
329
+ if (json% failed()) then
330
+ finished = .true.
331
+ else
332
+ write (output_unit,' (A)' ) path// json_CK_' = ' // value
333
+ end if
334
+
335
+ else
336
+ finished = .true.
337
+ end if
338
+ end if
339
+
340
+ end subroutine print_json_variable
341
+
279
342
end module jf_test_1_mod
280
343
! *****************************************************************************************
281
344
0 commit comments