Skip to content

Commit 78a8fec

Browse files
committed
fixed a few bugs.
various other updates.
1 parent 6e77eb9 commit 78a8fec

File tree

1 file changed

+79
-35
lines changed

1 file changed

+79
-35
lines changed

src/json_module.f90

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ module json_module
101101
logical,parameter :: debug = .false. !for printing the debug messages
102102

103103
! 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
112112

113113
type :: json_data_non_polymorphic
114114

@@ -230,7 +230,7 @@ subroutine array_callback_func(element, i, count)
230230
end subroutine array_callback_func
231231
end interface
232232

233-
interface json_value_get
233+
interface json_value_get !consider renaming this json_value_get_child
234234
module procedure get_by_index
235235
module procedure get_by_name_chars
236236
end interface json_value_get
@@ -278,6 +278,7 @@ end subroutine array_callback_func
278278
public :: json_print_to_string !write the JSON structure to a string
279279
public :: json_value_create !initialize a json_value pointer
280280
public :: json_value_count !count the number of children
281+
public :: json_info !get info about a json_value
281282
public :: to_logical !set the data type of a json_value
282283
public :: to_integer !
283284
public :: to_string !
@@ -355,7 +356,7 @@ subroutine destroy_json_file(me)
355356

356357
class(json_file),intent(inout) :: me
357358

358-
call json_value_destroy(me%p)
359+
if (associated(me%p)) call json_value_destroy(me%p)
359360

360361
!********************************************************************************
361362
end subroutine destroy_json_file
@@ -468,9 +469,8 @@ subroutine variable_info_in_file(me,path,found,var_type,n_children)
468469

469470
if (found) then
470471

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)
474474

475475
else
476476

@@ -487,6 +487,38 @@ subroutine variable_info_in_file(me,path,found,var_type,n_children)
487487
end subroutine variable_info_in_file
488488
!********************************************************************************
489489

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+
490522
!********************************************************************************
491523
subroutine get_object_from_json_file(me, path, p, found)
492524
!********************************************************************************
@@ -1671,20 +1703,24 @@ function json_value_count(this) result(count)
16711703
if (.not. exception_thrown) then
16721704

16731705
count = 0
1706+
1707+
if (associated(this)) then
16741708

1675-
if (associated(this%children)) then
1709+
if (associated(this%children)) then
16761710

1677-
p => this%children
1711+
p => this%children
16781712

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
16831717

1684-
nullify(p)
1718+
nullify(p)
16851719

1720+
end if
1721+
16861722
end if
1687-
1723+
16881724
end if
16891725

16901726
!********************************************************************************
@@ -1700,7 +1736,7 @@ subroutine get_by_index(this, idx, p)
17001736
! get_by_index
17011737
!
17021738
! DESCRIPTION
1703-
!
1739+
! Returns a child in the object given the index.
17041740
!
17051741
!********************************************************************************
17061742

@@ -1752,12 +1788,7 @@ subroutine get_by_name_chars(this, name, p)
17521788
! get_by_name_chars
17531789
!
17541790
! 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.
17611792
!
17621793
!********************************************************************************
17631794

@@ -1809,7 +1840,7 @@ subroutine json_value_to_string(me,str)
18091840
! Print the JSON structure to an allocatable string.
18101841
!
18111842
! AUTHOR
1812-
! Jacob Williams : 2/13/2014
1843+
! Jacob Williams : 2/12/2014
18131844
!
18141845
!********************************************************************************
18151846
implicit none
@@ -1886,7 +1917,7 @@ recursive subroutine json_value_print(this,iunit,indent,need_comma,colon,str)
18861917
else
18871918
spaces = 0
18881919
end if
1889-
1920+
18901921
nullify(element)
18911922

18921923
associate (d => this%data)
@@ -2128,7 +2159,10 @@ recursive subroutine json_get_by_path(this, path, p, found) !JW : Does thi
21282159
cycle
21292160
end if
21302161

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
21322166

21332167
child_i = i+1
21342168

@@ -2150,8 +2184,10 @@ recursive subroutine json_get_by_path(this, path, p, found) !JW : Does thi
21502184
child_i = i + 1
21512185
cycle
21522186
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
21552191
child_i = i + 1
21562192

21572193
case (']',')')
@@ -2190,8 +2226,16 @@ recursive subroutine json_get_by_path(this, path, p, found) !JW : Does thi
21902226
p => tmp
21912227
nullify(tmp)
21922228
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+
21952239
end if
21962240

21972241
else

0 commit comments

Comments
 (0)