Skip to content

Commit 1688a11

Browse files
committed
Fix #85
- Unit number -1 instead of 0 denotes print to string, since 0 is usually stderr
1 parent c35fd75 commit 1688a11

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/json_module.F90

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,9 @@ end subroutine array_callback_func
11461146
integer(IK),parameter :: chunk_size = 100 !allocate chunks of this size
11471147
integer(IK) :: ipos = 1 !next character to read
11481148

1149+
!unit number to cause stuff to be output to strings rather than files
1150+
!See 9.5.6.12 in the F2003/08 standard
1151+
integer(IK),parameter :: unit2str = -1
11491152
contains
11501153
!*****************************************************************************************
11511154

@@ -1386,15 +1389,15 @@ subroutine json_file_print_1(me, iunit)
13861389
implicit none
13871390

13881391
class(json_file),intent(inout) :: me
1389-
integer(IK),intent(in) :: iunit !must be non-zero
1392+
integer(IK),intent(in) :: iunit !must not be -1
13901393

13911394
integer(IK) :: i
13921395
character(kind=CK,len=:),allocatable :: dummy
13931396

1394-
if (iunit/=0) then
1397+
if (iunit/=unit2str) then
13951398
i = iunit
13961399
else
1397-
call throw_exception('Error in json_file_print_1: iunit must be nonzero.')
1400+
call throw_exception('Error in json_file_print_1: iunit must not be -1.')
13981401
return
13991402
end if
14001403

@@ -4217,7 +4220,7 @@ subroutine json_value_to_string(me,str)
42174220
character(kind=CK,len=:),intent(out),allocatable :: str
42184221

42194222
str = ''
4220-
call json_value_print(me, iunit=0, str=str, indent=1, colon=.true.)
4223+
call json_value_print(me, iunit=unit2str, str=str, indent=1, colon=.true.)
42214224

42224225
end subroutine json_value_to_string
42234226
!*****************************************************************************************
@@ -4232,7 +4235,7 @@ end subroutine json_value_to_string
42324235
! Print the JSON structure to a file.
42334236
!
42344237
! INPUT
4235-
! * iunit is the nonzero file unit (the file must already have been opened).
4238+
! * iunit is the file unit (the file must already have been opened, can't be -1).
42364239
!
42374240
! AUTHOR
42384241
! Jacob Williams, 6/20/2014
@@ -4244,13 +4247,13 @@ subroutine json_print_1(me,iunit)
42444247
implicit none
42454248

42464249
type(json_value),pointer,intent(in) :: me
4247-
integer(IK),intent(in) :: iunit !must be non-zero
4250+
integer(IK),intent(in) :: iunit !must not be -1
42484251
character(kind=CK,len=:),allocatable :: dummy
42494252

4250-
if (iunit/=0) then
4253+
if (iunit/=unit2str) then
42514254
call json_value_print(me,iunit,str=dummy, indent=1, colon=.true.)
42524255
else
4253-
call throw_exception('Error in json_print: iunit must be nonzero.')
4256+
call throw_exception('Error in json_print: iunit must not be -1.')
42544257
end if
42554258

42564259
end subroutine json_print_1
@@ -4321,7 +4324,7 @@ recursive subroutine json_value_print(me,iunit,str,indent,need_comma,colon,is_ar
43214324
logical(LK),intent(in),optional :: need_comma !if it needs a comma after it
43224325
logical(LK),intent(in),optional :: colon !if the colon was just written
43234326
character(kind=CK,len=:),intent(inout),allocatable :: str
4324-
!if iunit==0, then the structure is
4327+
!if iunit==unit2str (-1) then the structure is
43254328
! printed to this string rather than
43264329
! a file. This mode is used by
43274330
! json_value_to_string.
@@ -4337,7 +4340,7 @@ recursive subroutine json_value_print(me,iunit,str,indent,need_comma,colon,is_ar
43374340
if (.not. exception_thrown) then
43384341

43394342
!whether to write a string or a file (one or the other):
4340-
write_string = (iunit==0)
4343+
write_string = (iunit==unit2str)
43414344
write_file = .not. write_string
43424345

43434346
!if the comma will be printed after the value

0 commit comments

Comments
 (0)