Skip to content

Commit 7fca6e6

Browse files
committed
Added a new print routine where input is the filename. Updating the documentation with some examples.
1 parent e0329a5 commit 7fca6e6

File tree

2 files changed

+107
-20
lines changed

2 files changed

+107
-20
lines changed

src/json_example.f90

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ subroutine test_4()
189189

190190
integer :: i
191191
character(len=10) :: istr
192-
integer :: iunit
193192
character(len=:),allocatable :: string
194193

195194
write(*,'(A)') ''
@@ -220,9 +219,7 @@ subroutine test_4()
220219
write(*,'(A)') 'write to file'
221220

222221
!write the file:
223-
open(newunit=iunit, file=dir//filename4, status='REPLACE')
224-
call json_print(p,iunit)
225-
close(iunit)
222+
call json_print(p,trim(dir//filename4))
226223

227224
write(*,'(A)') ''
228225
write(*,'(A)') 'write to string'

src/json_module.f90

Lines changed: 106 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module json_module
3838
! -------------------------------------------------------------------------------------
3939
! json-fortran License:
4040
!
41-
! JSON-FORTRAN: A Fortran 2003/2008 JSON API
41+
! JSON-FORTRAN: A Fortran 2008 JSON API
4242
! http://github.com/jacobwilliams/json-fortran
4343
!
4444
! Copyright (c) 2014, Jacob Williams
@@ -160,6 +160,15 @@ module json_module
160160
! DESCRIPTION
161161
! Type used to construct the linked-list json structure
162162
!
163+
! EXAMPLE
164+
! type(json_value),pointer :: p
165+
! call json_value_create(p)
166+
! call to_object(p)
167+
! call json_value_add(p,'year',1805)
168+
! call json_value_add(p,'value',1.0_wp)
169+
! call json_print(p,'test.json')
170+
! call json_destroy(p)
171+
!
163172
! SOURCE
164173

165174
type,public :: json_value
@@ -200,7 +209,7 @@ module json_module
200209
! call json%load_file(filename='myfile.json')
201210
! call json%print_file()
202211
! call json%get('var.i',ival,found)
203-
! call json%get('var.d',rval,found)
212+
! call json%get('var.r(3)',rval,found)
204213
! call json%get('var.c',cval,found)
205214
! call json%destroy()
206215
!
@@ -339,7 +348,28 @@ end subroutine array_callback_func
339348
interface json_print_to_string
340349
module procedure :: json_value_to_string
341350
end interface
342-
351+
352+
!*************************************************************************************
353+
!****f* json_module/json_print
354+
!
355+
! NAME
356+
! json_print
357+
!
358+
! DESCRIPTION
359+
! Print the json_value to a file.
360+
!
361+
! EXAMPLE
362+
! type(json_value) :: p
363+
! ...
364+
! call json_print(p,'test.json') !this is json_print_2
365+
!
366+
! SOURCE
367+
interface json_print
368+
module procedure :: json_print_1 !input is unit number
369+
module procedure :: json_print_2 !input is file name
370+
end interface
371+
!*************************************************************************************
372+
343373
interface json_destroy
344374
module procedure :: json_value_destroy
345375
end interface
@@ -1028,6 +1058,21 @@ end subroutine throw_exception
10281058
! If an error is thrown, before using the module again, json_initialize
10291059
! should be called to clean up before it is used again.
10301060
!
1061+
! EXAMPLE
1062+
! type(json_file) :: json
1063+
! logical :: status_ok
1064+
! character(len=:),allocatable :: error_msg
1065+
! call json%load_file(filename='myfile.json')
1066+
! call json_check_for_errors(status_ok, error_msg)
1067+
! if (.not. status_ok) then
1068+
! write(*,*) 'Error: '//error_msg
1069+
! call json_clear_exceptions()
1070+
! call json%destroy()
1071+
! end if
1072+
!
1073+
! SEE ALSO
1074+
! json_failed
1075+
!
10311076
! AUTHOR
10321077
! Jacob Williams : 12/4/2013
10331078
!
@@ -1064,12 +1109,21 @@ end subroutine json_check_for_errors
10641109
! DESCRIPTION
10651110
! Logical function to indicate if an exception has been thrown.
10661111
!
1067-
! USAGE
1112+
! EXAMPLE
1113+
! type(json_file) :: json
1114+
! logical :: status_ok
1115+
! character(len=:),allocatable :: error_msg
1116+
! call json%load_file(filename='myfile.json')
10681117
! if (json_failed()) then
1069-
! !do something about it
1118+
! call json_check_for_errors(status_ok, error_msg)
1119+
! write(*,*) 'Error: '//error_msg
10701120
! call json_clear_exceptions()
1121+
! call json%destroy()
10711122
! end if
10721123
!
1124+
! SEE ALSO
1125+
! json_check_for_errors
1126+
!
10731127
! AUTHOR
10741128
! Jacob Williams : 12/5/2013
10751129
!
@@ -1095,10 +1149,11 @@ end function json_failed
10951149
! DESCRIPTION
10961150
! Allocate a json_value pointer variable.
10971151
! This should be called before adding data to it.
1098-
! Example:
1099-
! type(json_value),pointer :: var
1100-
! call json_value_create(var)
1101-
! call to_real(var,1.0d0)
1152+
!
1153+
! EXAMPLE
1154+
! type(json_value),pointer :: var
1155+
! call json_value_create(var)
1156+
! call to_real(var,1.0d0)
11021157
!
11031158
! NOTES
11041159
! This routine does not check for exceptions.
@@ -2102,20 +2157,21 @@ end subroutine json_value_to_string
21022157
!*****************************************************************************************
21032158

21042159
!*****************************************************************************************
2105-
!****f* json_module/json_print
2160+
!****f* json_module/json_print_1
21062161
!
21072162
! NAME
2108-
! json_print
2163+
! json_print_1
21092164
!
21102165
! DESCRIPTION
21112166
! Print the JSON structure to a file
2167+
! Input is the nonzero file unit (the file must already have been opened).
21122168
!
21132169
! AUTHOR
21142170
! Jacob Williams, 6/20/2014
21152171
!
21162172
! SOURCE
21172173

2118-
subroutine json_print(me,iunit)
2174+
subroutine json_print_1(me,iunit)
21192175

21202176
implicit none
21212177

@@ -2130,7 +2186,41 @@ subroutine json_print(me,iunit)
21302186
call throw_exception('Error in json_print: iunit must be nonzero.')
21312187
end if
21322188

2133-
end subroutine json_print
2189+
end subroutine json_print_1
2190+
2191+
!*****************************************************************************************
2192+
!****f* json_module/json_print_2
2193+
!
2194+
! NAME
2195+
! json_print_2
2196+
!
2197+
! DESCRIPTION
2198+
! Print the JSON structure to a file.
2199+
! Input is the filename.
2200+
!
2201+
! AUTHOR
2202+
! Jacob Williams, 12/23/2014
2203+
!
2204+
! SOURCE
2205+
2206+
subroutine json_print_2(me,filename)
2207+
2208+
implicit none
2209+
2210+
type(json_value),pointer,intent(in) :: me
2211+
character(len=*),intent(in) :: filename
2212+
2213+
integer :: iunit,istat
2214+
2215+
open(newunit=iunit,file=filename,status='REPLACE',iostat=istat)
2216+
if (istat==0) then
2217+
call json_print(me,iunit)
2218+
close(iunit,iostat=istat)
2219+
else
2220+
call throw_exception('Error in json_print: could not open file: '//trim(filename))
2221+
end if
2222+
2223+
end subroutine json_print_2
21342224

21352225
!*****************************************************************************************
21362226
!****if* json_module/json_value_print
@@ -2378,7 +2468,7 @@ end subroutine json_value_print
23782468
! json_get_by_path
23792469
!
23802470
! DESCRIPTION
2381-
!
2471+
! Returns the json_value pointer given the path string.
23822472
!
23832473
! NOTES
23842474
! $ root
@@ -3303,7 +3393,7 @@ end subroutine json_get_char_vec
33033393
! json_get_array
33043394
!
33053395
! DESCRIPTION
3306-
! Get an array from an json_value.
3396+
! Get an array from a json_value.
33073397
! This routine calls the user-supplied array_callback subroutine
33083398
! for each element in the array.
33093399
!
@@ -3543,7 +3633,7 @@ end subroutine get_current_line_from_file
35433633
! parse_value
35443634
!
35453635
! DESCRIPTION
3546-
!
3636+
! Core parsing routine.
35473637
!
35483638
! SOURCE
35493639

0 commit comments

Comments
 (0)