@@ -101,14 +101,14 @@ module json_module
101
101
logical ,parameter :: debug = .false. ! for printing the debug messages
102
102
103
103
! The types of data:
104
- integer ,parameter :: json_unknown = 0
105
- integer ,parameter :: json_null = 1
106
- integer ,parameter :: json_object = 2
107
- integer ,parameter :: json_array = 3
108
- integer ,parameter :: json_logical = 4
109
- integer ,parameter :: json_integer = 5
110
- integer ,parameter :: json_real = 6
111
- integer ,parameter :: json_string = 7
104
+ integer ,parameter , public :: json_unknown = 0
105
+ integer ,parameter , public :: json_null = 1
106
+ integer ,parameter , public :: json_object = 2
107
+ integer ,parameter , public :: json_array = 3
108
+ integer ,parameter , public :: json_logical = 4
109
+ integer ,parameter , public :: json_integer = 5
110
+ integer ,parameter , public :: json_real = 6
111
+ integer ,parameter , public :: json_string = 7
112
112
113
113
type :: json_data_non_polymorphic
114
114
@@ -230,7 +230,7 @@ subroutine array_callback_func(element, i, count)
230
230
end subroutine array_callback_func
231
231
end interface
232
232
233
- interface json_value_get
233
+ interface json_value_get ! consider renaming this json_value_get_child
234
234
module procedure get_by_index
235
235
module procedure get_by_name_chars
236
236
end interface json_value_get
@@ -278,6 +278,7 @@ end subroutine array_callback_func
278
278
public :: json_print_to_string ! write the JSON structure to a string
279
279
public :: json_value_create ! initialize a json_value pointer
280
280
public :: json_value_count ! count the number of children
281
+ public :: json_info ! get info about a json_value
281
282
public :: to_logical ! set the data type of a json_value
282
283
public :: to_integer !
283
284
public :: to_string !
@@ -355,7 +356,7 @@ subroutine destroy_json_file(me)
355
356
356
357
class(json_file),intent (inout ) :: me
357
358
358
- call json_value_destroy(me% p)
359
+ if ( associated (me % p)) call json_value_destroy(me% p)
359
360
360
361
! ********************************************************************************
361
362
end subroutine destroy_json_file
@@ -468,9 +469,8 @@ subroutine variable_info_in_file(me,path,found,var_type,n_children)
468
469
469
470
if (found) then
470
471
471
- ! get other info:
472
- var_type = p% data % var_type
473
- n_children = json_value_count(p) ! number of children
472
+ ! get info:
473
+ call json_info(p,var_type,n_children)
474
474
475
475
else
476
476
@@ -487,6 +487,38 @@ subroutine variable_info_in_file(me,path,found,var_type,n_children)
487
487
end subroutine variable_info_in_file
488
488
! ********************************************************************************
489
489
490
+ ! ********************************************************************************
491
+ subroutine json_info (p ,var_type ,n_children )
492
+ ! ********************************************************************************
493
+ ! ****f* json_module/json_info
494
+ !
495
+ ! NAME
496
+ ! json_info
497
+ !
498
+ ! USAGE
499
+ ! call me%info(path,found,var_type,n_children)
500
+ !
501
+ ! DESCRIPTION
502
+ ! Returns information about a json_value
503
+ !
504
+ ! AUTHOR
505
+ ! Jacob Williams : 2/13/2014
506
+ !
507
+ ! ********************************************************************************
508
+
509
+ implicit none
510
+
511
+ type (json_value),pointer :: p
512
+ integer ,intent (out ),optional :: var_type
513
+ integer ,intent (out ),optional :: n_children
514
+
515
+ if (present (var_type)) var_type = p% data % var_type ! variable type
516
+ if (present (n_children)) n_children = json_value_count(p) ! number of children
517
+
518
+ ! ********************************************************************************
519
+ end subroutine json_info
520
+ ! ********************************************************************************
521
+
490
522
! ********************************************************************************
491
523
subroutine get_object_from_json_file (me , path , p , found )
492
524
! ********************************************************************************
@@ -1671,20 +1703,24 @@ function json_value_count(this) result(count)
1671
1703
if (.not. exception_thrown) then
1672
1704
1673
1705
count = 0
1706
+
1707
+ if (associated (this)) then
1674
1708
1675
- if (associated (this% children)) then
1709
+ if (associated (this% children)) then
1676
1710
1677
- p = > this% children
1711
+ p = > this% children
1678
1712
1679
- do while (associated (p))
1680
- count = count + 1
1681
- p = > p% next
1682
- end do
1713
+ do while (associated (p))
1714
+ count = count + 1
1715
+ p = > p% next
1716
+ end do
1683
1717
1684
- nullify(p)
1718
+ nullify(p)
1685
1719
1720
+ end if
1721
+
1686
1722
end if
1687
-
1723
+
1688
1724
end if
1689
1725
1690
1726
! ********************************************************************************
@@ -1700,7 +1736,7 @@ subroutine get_by_index(this, idx, p)
1700
1736
! get_by_index
1701
1737
!
1702
1738
! DESCRIPTION
1703
- !
1739
+ ! Returns a child in the object given the index.
1704
1740
!
1705
1741
! ********************************************************************************
1706
1742
@@ -1752,12 +1788,7 @@ subroutine get_by_name_chars(this, name, p)
1752
1788
! get_by_name_chars
1753
1789
!
1754
1790
! DESCRIPTION
1755
- !
1756
- ! NOTES
1757
- ! This is a case-sensitive match.
1758
- !
1759
- ! TODO
1760
- ! Maybe add an option for case insensitive files.
1791
+ ! Returns a child in the object given the name string.
1761
1792
!
1762
1793
! ********************************************************************************
1763
1794
@@ -1809,7 +1840,7 @@ subroutine json_value_to_string(me,str)
1809
1840
! Print the JSON structure to an allocatable string.
1810
1841
!
1811
1842
! AUTHOR
1812
- ! Jacob Williams : 2/13 /2014
1843
+ ! Jacob Williams : 2/12 /2014
1813
1844
!
1814
1845
! ********************************************************************************
1815
1846
implicit none
@@ -1886,7 +1917,7 @@ recursive subroutine json_value_print(this,iunit,indent,need_comma,colon,str)
1886
1917
else
1887
1918
spaces = 0
1888
1919
end if
1889
-
1920
+
1890
1921
nullify(element)
1891
1922
1892
1923
associate (d = > this% data )
@@ -2128,7 +2159,10 @@ recursive subroutine json_get_by_path(this, path, p, found) !JW : Does thi
2128
2159
cycle
2129
2160
end if
2130
2161
2131
- if (.not. associated (p)) return
2162
+ if (.not. associated (p)) then
2163
+ call throw_exception(' Error in json_get_by_path: Error getting child member.' )
2164
+ exit
2165
+ end if
2132
2166
2133
2167
child_i = i+1
2134
2168
@@ -2150,8 +2184,10 @@ recursive subroutine json_get_by_path(this, path, p, found) !JW : Does thi
2150
2184
child_i = i + 1
2151
2185
cycle
2152
2186
end if
2153
- if (.not. associated (p)) return
2154
-
2187
+ if (.not. associated (p)) then
2188
+ call throw_exception(' Error in json_get_by_path: Error getting array element' )
2189
+ exit
2190
+ end if
2155
2191
child_i = i + 1
2156
2192
2157
2193
case (' ]' ,' )' )
@@ -2190,8 +2226,16 @@ recursive subroutine json_get_by_path(this, path, p, found) !JW : Does thi
2190
2226
p = > tmp
2191
2227
nullify(tmp)
2192
2228
end if
2193
- if (present (found)) found = .true. ! everything seems to be ok
2194
-
2229
+ if (associated (p)) then
2230
+ if (present (found)) found = .true. ! everything seems to be ok
2231
+ else
2232
+ call throw_exception(' Error in json_get_by_path: variable not found: ' // trim (path))
2233
+ if (present (found)) then
2234
+ found = .false.
2235
+ call json_clear_exceptions()
2236
+ end if
2237
+ end if
2238
+
2195
2239
end if
2196
2240
2197
2241
else
0 commit comments