@@ -38,7 +38,7 @@ module json_module
38
38
! -------------------------------------------------------------------------------------
39
39
! json-fortran License:
40
40
!
41
- ! JSON-FORTRAN: A Fortran 2003/ 2008 JSON API
41
+ ! JSON-FORTRAN: A Fortran 2008 JSON API
42
42
! http://github.com/jacobwilliams/json-fortran
43
43
!
44
44
! Copyright (c) 2014, Jacob Williams
@@ -160,6 +160,15 @@ module json_module
160
160
! DESCRIPTION
161
161
! Type used to construct the linked-list json structure
162
162
!
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
+ !
163
172
! SOURCE
164
173
165
174
type,public :: json_value
@@ -200,7 +209,7 @@ module json_module
200
209
! call json%load_file(filename='myfile.json')
201
210
! call json%print_file()
202
211
! call json%get('var.i',ival,found)
203
- ! call json%get('var.d ',rval,found)
212
+ ! call json%get('var.r(3) ',rval,found)
204
213
! call json%get('var.c',cval,found)
205
214
! call json%destroy()
206
215
!
@@ -339,7 +348,28 @@ end subroutine array_callback_func
339
348
interface json_print_to_string
340
349
module procedure :: json_value_to_string
341
350
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
+
343
373
interface json_destroy
344
374
module procedure :: json_value_destroy
345
375
end interface
@@ -1028,6 +1058,21 @@ end subroutine throw_exception
1028
1058
! If an error is thrown, before using the module again, json_initialize
1029
1059
! should be called to clean up before it is used again.
1030
1060
!
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
+ !
1031
1076
! AUTHOR
1032
1077
! Jacob Williams : 12/4/2013
1033
1078
!
@@ -1064,12 +1109,21 @@ end subroutine json_check_for_errors
1064
1109
! DESCRIPTION
1065
1110
! Logical function to indicate if an exception has been thrown.
1066
1111
!
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')
1068
1117
! if (json_failed()) then
1069
- ! !do something about it
1118
+ ! call json_check_for_errors(status_ok, error_msg)
1119
+ ! write(*,*) 'Error: '//error_msg
1070
1120
! call json_clear_exceptions()
1121
+ ! call json%destroy()
1071
1122
! end if
1072
1123
!
1124
+ ! SEE ALSO
1125
+ ! json_check_for_errors
1126
+ !
1073
1127
! AUTHOR
1074
1128
! Jacob Williams : 12/5/2013
1075
1129
!
@@ -1095,10 +1149,11 @@ end function json_failed
1095
1149
! DESCRIPTION
1096
1150
! Allocate a json_value pointer variable.
1097
1151
! 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)
1102
1157
!
1103
1158
! NOTES
1104
1159
! This routine does not check for exceptions.
@@ -2102,20 +2157,21 @@ end subroutine json_value_to_string
2102
2157
! *****************************************************************************************
2103
2158
2104
2159
! *****************************************************************************************
2105
- ! ****f* json_module/json_print
2160
+ ! ****f* json_module/json_print_1
2106
2161
!
2107
2162
! NAME
2108
- ! json_print
2163
+ ! json_print_1
2109
2164
!
2110
2165
! DESCRIPTION
2111
2166
! Print the JSON structure to a file
2167
+ ! Input is the nonzero file unit (the file must already have been opened).
2112
2168
!
2113
2169
! AUTHOR
2114
2170
! Jacob Williams, 6/20/2014
2115
2171
!
2116
2172
! SOURCE
2117
2173
2118
- subroutine json_print (me ,iunit )
2174
+ subroutine json_print_1 (me ,iunit )
2119
2175
2120
2176
implicit none
2121
2177
@@ -2130,7 +2186,41 @@ subroutine json_print(me,iunit)
2130
2186
call throw_exception(' Error in json_print: iunit must be nonzero.' )
2131
2187
end if
2132
2188
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
2134
2224
2135
2225
! *****************************************************************************************
2136
2226
! ****if* json_module/json_value_print
@@ -2378,7 +2468,7 @@ end subroutine json_value_print
2378
2468
! json_get_by_path
2379
2469
!
2380
2470
! DESCRIPTION
2381
- !
2471
+ ! Returns the json_value pointer given the path string.
2382
2472
!
2383
2473
! NOTES
2384
2474
! $ root
@@ -3303,7 +3393,7 @@ end subroutine json_get_char_vec
3303
3393
! json_get_array
3304
3394
!
3305
3395
! DESCRIPTION
3306
- ! Get an array from an json_value.
3396
+ ! Get an array from a json_value.
3307
3397
! This routine calls the user-supplied array_callback subroutine
3308
3398
! for each element in the array.
3309
3399
!
@@ -3543,7 +3633,7 @@ end subroutine get_current_line_from_file
3543
3633
! parse_value
3544
3634
!
3545
3635
! DESCRIPTION
3546
- !
3636
+ ! Core parsing routine.
3547
3637
!
3548
3638
! SOURCE
3549
3639
0 commit comments