Skip to content

Commit 5bd5153

Browse files
committed
Merge pull request #95 from jacobwilliams/devel
minor formatting and addition of a unit test.
2 parents 0b64bce + 4eee204 commit 5bd5153

File tree

2 files changed

+123
-65
lines changed

2 files changed

+123
-65
lines changed

src/json_module.F90

Lines changed: 113 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -299,71 +299,70 @@ module json_module
299299
! use_unformatted_stream
300300
!
301301
! DESCRIPTION
302-
! If using GFortran and Unicode is enabled, then
302+
! If Unicode is not enabled, then
303303
! JSON files are opened using access='STREAM' and
304304
! form='UNFORMATTED'. This allows the file to
305305
! be read faster.
306306
!
307+
! SEE ALSO
308+
! * access_spec
309+
! * form_spec
310+
!
307311
! SOURCE
308-
#if defined __GFORTRAN__ && defined USE_UCS4
312+
#ifdef USE_UCS4
309313
logical,parameter :: use_unformatted_stream = .false.
310-
character(kind=CDK,len=*),parameter :: access_spec = 'SEQUENTIAL'
311-
character(kind=CDK,len=*),parameter :: form_spec = 'FORMATTED'
312314
#else
313315
logical,parameter :: use_unformatted_stream = .true.
314-
character(kind=CDK,len=*),parameter :: access_spec = 'STREAM'
315-
character(kind=CDK,len=*),parameter :: form_spec = 'UNFORMATTED'
316316
#endif
317317
!*********************************************************
318318

319-
!JSON file extension
320-
character(kind=CDK,len=*),parameter,public :: json_ext = '.json' !JSON file extension
321-
322-
!special JSON characters
323-
character(kind=CK,len=*),parameter :: space = ' '
324-
character(kind=CK,len=*),parameter :: start_object = '{'
325-
character(kind=CK,len=*),parameter :: end_object = '}'
326-
character(kind=CK,len=*),parameter :: start_array = '['
327-
character(kind=CK,len=*),parameter :: end_array = ']'
328-
character(kind=CK,len=*),parameter :: delimiter = ','
329-
character(kind=CK,len=*),parameter :: colon_char = ':'
330-
character(kind=CK,len=*),parameter :: bspace = achar(8)
331-
character(kind=CK,len=*),parameter :: horizontal_tab = achar(9)
332-
character(kind=CK,len=*),parameter :: newline = achar(10)
333-
character(kind=CK,len=*),parameter :: formfeed = achar(12)
334-
character(kind=CK,len=*),parameter :: carriage_return = achar(13)
335-
character(kind=CK,len=*),parameter :: quotation_mark = achar(34)
336-
character(kind=CK,len=*),parameter :: slash = achar(47)
337-
character(kind=CK,len=*),parameter :: backslash = achar(92)
338-
339-
! Control characters, possibly in unicode
340-
integer, private :: i
341-
character(kind=CK,len=*),parameter :: control_chars(32) = [(achar(i),i=1,31), achar(127)]
342-
343-
!for indenting (Note: jsonlint.com uses 4 spaces)
344-
integer(IK),parameter :: spaces_per_tab = 2
345-
346-
!find out the precision of the floating point number system
347-
!and set safety factors
348-
integer(IK),parameter :: rp_safety_factor = 1
349-
integer(IK),parameter :: rp_addl_safety = 1
350-
integer(IK),parameter :: real_precision = rp_safety_factor*precision(1.0_RK) + &
351-
rp_addl_safety
352-
353-
!Get the number of possible digits in the exponent when using decimal number system
354-
integer(IK),parameter :: real_exponent_digits = floor( 1 + log10( &
355-
real(max(maxexponent(1.0_RK),abs(minexponent(1.0_RK))),&
356-
kind=RK) ) )
357-
358-
!6 = sign + leading 0 + decimal + 'E' + exponent sign + 1 extra
359-
integer(IK),parameter :: max_numeric_str_len = real_precision + real_exponent_digits + 6
360-
! real format set by library initialization
361-
character(kind=CDK,len=*),parameter :: int_fmt = '(I0)' !minimum width format for integers
362-
character(kind=CK, len=*),parameter :: star = '*' !for invalid numbers
319+
!*********************************************************
320+
!****d* json_module/access_spec
321+
!
322+
! NAME
323+
! access_spec
324+
!
325+
! DESCRIPTION
326+
! If Unicode is not enabled, then
327+
! JSON files are opened using access='STREAM' and
328+
! form='UNFORMATTED'. This allows the file to
329+
! be read faster.
330+
!
331+
! SEE ALSO
332+
! * use_unformatted_stream
333+
! * form_spec
334+
!
335+
! SOURCE
336+
#ifdef USE_UCS4
337+
character(kind=CDK,len=*),parameter :: access_spec = 'SEQUENTIAL'
338+
#else
339+
character(kind=CDK,len=*),parameter :: access_spec = 'STREAM'
340+
#endif
341+
!*********************************************************
363342

364-
!for allocatable strings:
365-
integer(IK),parameter :: chunk_size = 100 !allocate chunks of this size
366-
integer(IK) :: ipos = 1 !next character to read
343+
!*********************************************************
344+
!****d* json_module/form_spec
345+
!
346+
! NAME
347+
! form_spec
348+
!
349+
! DESCRIPTION
350+
! If Unicode is not enabled, then
351+
! JSON files are opened using access='STREAM' and
352+
! form='UNFORMATTED'. This allows the file to
353+
! be read faster.
354+
!
355+
! SEE ALSO
356+
! * use_unformatted_stream
357+
! * access_spec
358+
!
359+
! SOURCE
360+
#ifdef USE_UCS4
361+
character(kind=CDK,len=*),parameter :: form_spec = 'FORMATTED'
362+
#else
363+
character(kind=CDK,len=*),parameter :: form_spec = 'UNFORMATTED'
364+
#endif
365+
!*********************************************************
367366

368367
!*********************************************************
369368
!****d* json_module/var_type
@@ -1075,15 +1074,66 @@ end subroutine array_callback_func
10751074
public :: operator(==)
10761075
# endif
10771076

1078-
!
1079-
! Note: the following global variables make this module non thread safe.
1080-
!
1077+
!JSON file extension
1078+
character(kind=CDK,len=*),parameter,public :: json_ext = '.json' !JSON file extension
1079+
1080+
!special JSON characters
1081+
character(kind=CK,len=*),parameter :: space = ' '
1082+
character(kind=CK,len=*),parameter :: start_object = '{'
1083+
character(kind=CK,len=*),parameter :: end_object = '}'
1084+
character(kind=CK,len=*),parameter :: start_array = '['
1085+
character(kind=CK,len=*),parameter :: end_array = ']'
1086+
character(kind=CK,len=*),parameter :: delimiter = ','
1087+
character(kind=CK,len=*),parameter :: colon_char = ':'
1088+
character(kind=CK,len=*),parameter :: bspace = achar(8)
1089+
character(kind=CK,len=*),parameter :: horizontal_tab = achar(9)
1090+
character(kind=CK,len=*),parameter :: newline = achar(10)
1091+
character(kind=CK,len=*),parameter :: formfeed = achar(12)
1092+
character(kind=CK,len=*),parameter :: carriage_return = achar(13)
1093+
character(kind=CK,len=*),parameter :: quotation_mark = achar(34)
1094+
character(kind=CK,len=*),parameter :: slash = achar(47)
1095+
character(kind=CK,len=*),parameter :: backslash = achar(92)
1096+
1097+
!These were parameters, but gfortran bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65141)
1098+
!necessitates moving them here to be variables
1099+
character(kind=CK,len=4) :: null_str = 'null'
1100+
character(kind=CK,len=4) :: true_str = 'true'
1101+
character(kind=CK,len=5) :: false_str = 'false'
1102+
1103+
! Control characters, possibly in unicode
1104+
integer, private :: i_
1105+
character(kind=CK,len=*),parameter :: control_chars(32) = [(achar(i_),i_=1,31), achar(127)]
1106+
1107+
!for indenting (Note: jsonlint.com uses 4 spaces)
1108+
integer(IK),parameter :: spaces_per_tab = 2
1109+
1110+
!find out the precision of the floating point number system
1111+
!and set safety factors
1112+
integer(IK),parameter :: rp_safety_factor = 1
1113+
integer(IK),parameter :: rp_addl_safety = 1
1114+
integer(IK),parameter :: real_precision = rp_safety_factor*precision(1.0_RK) + &
1115+
rp_addl_safety
1116+
1117+
!Get the number of possible digits in the exponent when using decimal number system
1118+
integer(IK),parameter :: real_exponent_digits = floor( 1 + log10( &
1119+
real(max(maxexponent(1.0_RK),abs(minexponent(1.0_RK))),&
1120+
kind=RK) ) )
1121+
1122+
!6 = sign + leading 0 + decimal + 'E' + exponent sign + 1 extra
1123+
integer(IK),parameter :: max_numeric_str_len = real_precision + real_exponent_digits + 6
1124+
! real format set by library initialization
1125+
character(kind=CDK,len=*),parameter :: int_fmt = '(I0)' !minimum width format for integers
1126+
character(kind=CK, len=*),parameter :: star = '*' !for invalid numbers
10811127

10821128
!real string printing:
10831129
character(kind=CDK,len=:),allocatable :: real_fmt !the format string to use for real numbers
10841130
! [set in json_initialize]
10851131
logical(LK) :: compact_real = .true. !to use the "compact" form of real numbers for output
10861132

1133+
!
1134+
! Note: the following global variables make this module non thread safe.
1135+
!
1136+
10871137
!exception handling [private variables]
10881138
logical(LK) :: is_verbose = .false. !if true, all exceptions are immediately printed to console
10891139
logical(LK) :: exception_thrown = .false. !the error flag
@@ -1095,11 +1145,9 @@ end subroutine array_callback_func
10951145
integer(IK) :: pushed_index = 0
10961146
character(kind=CK,len=10) :: pushed_char = '' !JW : what is this magic number 10??
10971147

1098-
!These were parameters, but gfortran bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65141)
1099-
!necessitates moving them here to be variables
1100-
character(kind=CK,len=4) :: null_str = 'null'
1101-
character(kind=CK,len=4) :: true_str = 'true'
1102-
character(kind=CK,len=5) :: false_str = 'false'
1148+
!for allocatable strings:
1149+
integer(IK),parameter :: chunk_size = 100 !allocate chunks of this size
1150+
integer(IK) :: ipos = 1 !next character to read
11031151

11041152
contains
11051153
!*****************************************************************************************
@@ -6110,10 +6158,10 @@ end subroutine wrap_json_get_array_with_path
61106158
! calls will automatically be cleared.
61116159
!
61126160
! HISTORY
6113-
! Jacob Williams : 01/13/2015 : added read from string option.
6114-
! Izaak Beekman : 03/08/2015 : moved read from string to separate
6115-
! subroutine, and error annotation
6116-
! to separate subroutine
6161+
! * Jacob Williams : 01/13/2015 : added read from string option.
6162+
! * Izaak Beekman : 03/08/2015 : moved read from string to separate
6163+
! subroutine, and error annotation
6164+
! to separate subroutine.
61176165
!
61186166
! SOURCE
61196167

src/tests/jf_test_10.f90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,16 @@ subroutine test_10(error_cnt)
247247
else
248248
if (found) then
249249
write(error_unit,'(A)') '...success'
250+
251+
write(error_unit,'(A)') 'json_info...'
252+
call json_info(p,var_type,n_children)
253+
if (json_failed()) then
254+
call json_print_error_message(error_unit)
255+
error_cnt = error_cnt + 1
256+
else
257+
write(error_unit,'(A)') '...success'
258+
end if
259+
250260
write(error_unit,'(A)') 'json_remove_if_present...'
251261
call json_remove_if_present(p,'version.patch')
252262
if (json_failed()) then

0 commit comments

Comments
 (0)