Skip to content

Commit 276eea4

Browse files
committed
Merge branch 'master' of https://github.com/jacobwilliams/json-fortran into increase-coverage
2 parents 1fca289 + 923b707 commit 276eea4

File tree

5 files changed

+75
-40
lines changed

5 files changed

+75
-40
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@ A Fortran 2008 JSON API
2222
Status
2323
------
2424
[![Build Status](https://img.shields.io/travis/jacobwilliams/json-fortran/master.svg?style=plastic)](https://travis-ci.org/jacobwilliams/json-fortran)
25-
[![Codecov](https://codecov.io/gh/jacobwilliams/json-fortran/branch/master/graph/badge.svg)](https://codecov.io/gh/jacobwilliams/json-fortran)
26-
2725
[![GitHub issues](https://img.shields.io/github/issues/jacobwilliams/json-fortran.png?style=plastic)](https://github.com/jacobwilliams/json-fortran/issues)
28-
[![Blocked by Vendor Bug](https://badge.waffle.io/jacobwilliams/json-fortran.png?label=vendor%20bug&title=Blocked%20by%20Vendor%20Bug)](https://waffle.io/jacobwilliams/json-fortran)
29-
[![Ready in backlog](https://badge.waffle.io/jacobwilliams/json-fortran.png?label=Ready&title=Ready)](https://github.com/jacobwilliams/json-fortran/#contributing)
30-
[![In Progress](https://badge.waffle.io/jacobwilliams/json-fortran.png?label=In%20Progress&title=In%20Progress)](https://waffle.io/jacobwilliams/json-fortran)
31-
[![Needs Review](https://badge.waffle.io/jacobwilliams/json-fortran.png?label=Needs%20Review&title=Needs%20Review)](https://waffle.io/jacobwilliams/json-fortran)
26+
[![Codecov](https://codecov.io/gh/jacobwilliams/json-fortran/branch/master/graph/badge.svg)](https://codecov.io/gh/jacobwilliams/json-fortran)
3227

3328
Take a look at the
3429
[CHANGELOG](https://github.com/jacobwilliams/json-fortran/blob/master/CHANGELOG.md#unreleased)
@@ -47,7 +42,7 @@ Download
4742
--------------------
4843

4944
[![GitHub release](https://img.shields.io/github/release/jacobwilliams/json-fortran.svg?style=plastic)](https://github.com/jacobwilliams/json-fortran/releases)
50-
[![homebrew version](https://img.shields.io/homebrew/v/json-fortran.svg?maxAge=2592000)](http://braumeister.org/formula/json-fortran)
45+
[![homebrew version](https://img.shields.io/homebrew/v/json-fortran.svg?style=plastic)](http://braumeister.org/formula/json-fortran)
5146

5247
Download the official versioned releases
5348
[here](https://github.com/jacobwilliams/json-fortran/releases/latest).
@@ -158,6 +153,9 @@ Contributing
158153
------------
159154

160155
[![Ready in backlog](https://badge.waffle.io/jacobwilliams/json-fortran.png?label=Ready&title=Ready)](https://github.com/jacobwilliams/json-fortran/blob/master/.github/CONTRIBUTING.md)
156+
[![Blocked by Vendor Bug](https://badge.waffle.io/jacobwilliams/json-fortran.png?label=vendor%20bug&title=Blocked%20by%20Vendor%20Bug)](https://waffle.io/jacobwilliams/json-fortran)
157+
[![In Progress](https://badge.waffle.io/jacobwilliams/json-fortran.png?label=In%20Progress&title=In%20Progress)](https://waffle.io/jacobwilliams/json-fortran)
158+
[![Needs Review](https://badge.waffle.io/jacobwilliams/json-fortran.png?label=Needs%20Review&title=Needs%20Review)](https://waffle.io/jacobwilliams/json-fortran)
161159

162160
Want to help? Take a quick look at our [contributing guidelines](https://github.com/jacobwilliams/json-fortran/blob/master/.github/CONTRIBUTING.md) then claim something in [the "ready" column on our Waffle.io](https://waffle.io/jacobwilliams/json-fortran) and [Fork. Commit. Pull request.](https://help.github.com/articles/fork-a-repo/)
163161

pages/development-resources/release-checklist.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ remain accurate.
5252
1. Add `pages/releases/index.md` to the git index: `git add pages/releases/index.md`
5353
1. Update the version string in remaining files requiring manual edits:
5454
1. Edit the `.VERSION` file
55-
1. Edit the CMake example on line 129 of `README.md`
55+
1. Edit the CMake example on line 124 of `README.md`
5656
1. Add both files to the git index: `git add .VERSION
5757
README.md`
5858
1. Commit the changes to the master branch: `git commit`

src/json_value_module.F90

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,10 @@ module json_value_module
176176
!! when an error is thrown in the class.
177177
!! Many of the methods will check this
178178
!! and return immediately if it is true.
179-
character(kind=CK,len=:),allocatable :: err_message !! the error message
179+
character(kind=CK,len=:),allocatable :: err_message
180+
!! the error message.
181+
!! if `exception_thrown=False` then
182+
!! this variable is not allocated.
180183

181184
integer(IK) :: char_count = 0 !! character position in the current line
182185
integer(IK) :: line_count = 1 !! lines read counter
@@ -463,11 +466,11 @@ module json_value_module
463466
! type(json_core) :: json
464467
! type(json_value) :: p
465468
! !...
466-
! call json%print(p,'test.json') !this is [[json_print_2]]
469+
! call json%print(p,'test.json') !this is [[json_print_to_filename]]
467470
!````
468-
generic,public :: print => json_print_1,json_print_2
469-
procedure :: json_print_1
470-
procedure :: json_print_2
471+
generic,public :: print => json_print_to_unit,json_print_to_filename
472+
procedure :: json_print_to_unit
473+
procedure :: json_print_to_filename
471474

472475
!>
473476
! Destructor routine for a [[json_value]] pointer.
@@ -1804,7 +1807,7 @@ pure subroutine json_clear_exceptions(json)
18041807

18051808
!clear the flag and message:
18061809
json%exception_thrown = .false.
1807-
json%err_message = CK_''
1810+
if (allocated(json%err_message)) deallocate(json%err_message)
18081811

18091812
end subroutine json_clear_exceptions
18101813
!*****************************************************************************************
@@ -1908,25 +1911,27 @@ end subroutine wrap_json_throw_exception
19081911
!
19091912
!### See also
19101913
! * [[json_failed]]
1914+
! * [[json_throw_exception]]
19111915

1912-
subroutine json_check_for_errors(json,status_ok,error_msg)
1916+
pure subroutine json_check_for_errors(json,status_ok,error_msg)
19131917

19141918
implicit none
19151919

1916-
class(json_core),intent(inout) :: json
1917-
logical(LK),intent(out) :: status_ok !! true if there were no errors
1918-
character(kind=CK,len=:),allocatable,intent(out) :: error_msg !! the error message (if there were errors)
1920+
class(json_core),intent(in) :: json
1921+
logical(LK),intent(out),optional :: status_ok !! true if there were no errors
1922+
character(kind=CK,len=:),allocatable,intent(out),optional :: error_msg !! the error message.
1923+
!! (not allocated if
1924+
!! there were no errors)
19191925

1920-
status_ok = .not. json%exception_thrown
1926+
if (present(status_ok)) status_ok = .not. json%exception_thrown
19211927

1922-
if (.not. status_ok) then
1923-
if (allocated(json%err_message)) then
1928+
if (present(error_msg)) then
1929+
if (json%exception_thrown) then
1930+
! if an exception has been thrown,
1931+
! then this will always be allocated
1932+
! [see json_throw_exception]
19241933
error_msg = json%err_message
1925-
else
1926-
error_msg = 'Unknown error.'
19271934
end if
1928-
else
1929-
error_msg = CK_''
19301935
end if
19311936

19321937
end subroutine json_check_for_errors
@@ -5225,7 +5230,7 @@ end subroutine json_value_to_string
52255230
!
52265231
! Print the [[json_value]] structure to a file.
52275232

5228-
subroutine json_print_1(json,p,iunit)
5233+
subroutine json_print_to_unit(json,p,iunit)
52295234

52305235
implicit none
52315236

@@ -5234,15 +5239,16 @@ subroutine json_print_1(json,p,iunit)
52345239
integer(IK),intent(in) :: iunit !! the file unit (the file must
52355240
!! already have been opened, can't be -1).
52365241

5237-
character(kind=CK,len=:),allocatable :: dummy
5242+
character(kind=CK,len=:),allocatable :: dummy !! dummy for `str` argument
5243+
!! to [[json_value_print]]
52385244

52395245
if (iunit/=unit2str) then
52405246
call json%json_value_print(p,iunit,str=dummy, indent=1_IK, colon=.true.)
52415247
else
5242-
call json%throw_exception('Error in json_print_1: iunit must not be -1.')
5248+
call json%throw_exception('Error in json_print_to_unit: iunit must not be -1.')
52435249
end if
52445250

5245-
end subroutine json_print_1
5251+
end subroutine json_print_to_unit
52465252
!*****************************************************************************************
52475253

52485254
!*****************************************************************************************
@@ -5251,7 +5257,7 @@ end subroutine json_print_1
52515257
!
52525258
! Print the [[json_value]] structure to a file.
52535259

5254-
subroutine json_print_2(json,p,filename)
5260+
subroutine json_print_to_filename(json,p,filename)
52555261

52565262
implicit none
52575263

@@ -5260,18 +5266,19 @@ subroutine json_print_2(json,p,filename)
52605266
character(kind=CDK,len=*),intent(in) :: filename !! the filename to print to
52615267
!! (should not already be open)
52625268

5263-
integer(IK) :: iunit,istat
5269+
integer(IK) :: iunit !! file unit for `open` statement
5270+
integer(IK) :: istat !! `iostat` code for `open` statement
52645271

52655272
open(newunit=iunit,file=filename,status='REPLACE',iostat=istat FILE_ENCODING )
52665273
if (istat==0) then
52675274
call json%print(p,iunit)
52685275
close(iunit,iostat=istat)
52695276
else
5270-
call json%throw_exception('Error in json_print_2: could not open file: '//&
5277+
call json%throw_exception('Error in json_print_to_filename: could not open file: '//&
52715278
trim(filename))
52725279
end if
52735280

5274-
end subroutine json_print_2
5281+
end subroutine json_print_to_filename
52755282
!*****************************************************************************************
52765283

52775284
!*****************************************************************************************
@@ -5291,16 +5298,17 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
52915298

52925299
class(json_core),intent(inout) :: json
52935300
type(json_value),pointer,intent(in) :: p
5294-
integer(IK),intent(in) :: iunit !! file unit to write to (6=console)
5301+
integer(IK),intent(in) :: iunit !! file unit to write to (the
5302+
!! file is assumed to be open)
52955303
integer(IK),intent(in),optional :: indent !! indention level
52965304
logical(LK),intent(in),optional :: is_array_element !! if this is an array element
52975305
logical(LK),intent(in),optional :: need_comma !! if it needs a comma after it
52985306
logical(LK),intent(in),optional :: colon !! if the colon was just written
52995307
character(kind=CK,len=:),intent(inout),allocatable :: str
5300-
!! if `iunit==unit2str` (-1) then the structure is
5301-
!! printed to this string rather than
5302-
!! a file. This mode is used by
5303-
!! [[json_value_to_string]].
5308+
!! if `iunit==unit2str` (-1) then
5309+
!! the structure is printed to this
5310+
!! string rather than a file. This mode
5311+
!! is used by [[json_value_to_string]].
53045312
logical(LK),intent(in),optional :: is_compressed_vector !! if True, this is an element
53055313
!! from an array being printed
53065314
!! on one line [default is False]
@@ -5326,6 +5334,16 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
53265334

53275335
if (.not. json%exception_thrown) then
53285336

5337+
if (.not. associated(p)) then
5338+
! note: a null() pointer will trigger this error.
5339+
! However, if the pointer is undefined, then this will
5340+
! crash (if this wasn't here it would crash below when
5341+
! we try to access the contents)
5342+
call json%throw_exception('Error in json_value_print: '//&
5343+
'the pointer is not associated')
5344+
return
5345+
end if
5346+
53295347
if (present(is_compressed_vector)) then
53305348
is_vector = is_compressed_vector
53315349
else
@@ -5420,6 +5438,7 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
54205438
! recursive print of the element
54215439
call json%json_value_print(element, iunit=iunit, indent=tab + 1, &
54225440
need_comma=i<count, colon=.true., str=str)
5441+
if (json%exception_thrown) return
54235442

54245443
! get the next child the list:
54255444
element => element%next
@@ -5500,6 +5519,8 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
55005519
call json%json_value_print(element, iunit=iunit, indent=tab,&
55015520
need_comma=i<count, is_array_element=.true., str=str)
55025521
end if
5522+
if (json%exception_thrown) return
5523+
55035524
! get the next child the list:
55045525
element => element%next
55055526

@@ -8859,8 +8880,8 @@ subroutine annotate_invalid_json(json,iunit,str)
88598880
end if
88608881

88618882
!create the error message:
8862-
json%err_message = json%err_message//newline//&
8863-
'line: '//trim(adjustl(line_str))//', '//&
8883+
if (allocated(json%err_message)) json%err_message = json%err_message//newline
8884+
json%err_message = 'line: '//trim(adjustl(line_str))//', '//&
88648885
'character: '//trim(adjustl(char_str))//newline//&
88658886
trim(line)//newline//arrow_str
88668887

src/tests/jf_test_02.F90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ subroutine test_2(error_cnt)
5151
error_cnt = error_cnt + 1
5252
end if
5353

54+
! first we test an incorrect usage case:
55+
! [trying to print a null json_value pointer]
56+
nullify(p)
57+
call json%print(p,error_unit)
58+
if (.not. json%failed()) then
59+
write(error_unit,'(A)') 'Error: printing a null pointer should have raised an exception.'
60+
error_cnt = error_cnt + 1
61+
end if
62+
call json%initialize() ! clears exceptions
63+
5464
!root:
5565
call json%create_object(p,dir//filename2) ! create the value and associate the pointer
5666
! add the file name as the name of the overall structure

src/tests/jf_test_15.F90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ subroutine test_15(error_cnt)
5252
call json%get(p2,'logical',d)
5353
call json%get(p2,'integer',tf)
5454
call json%get(p2,'real', tf)
55+
5556
call json%check_for_errors(status_ok, error_msg) !error condition true
57+
call json%check_for_errors(status_ok) !error condition true
58+
call json%check_for_errors(error_msg=error_msg) !error condition true
59+
5660
call json%initialize(print_signs=.true.) !print signs flag
5761

5862
call json%check_for_errors(status_ok, error_msg) !error condition false
63+
call json%check_for_errors(status_ok) !error condition false
64+
call json%check_for_errors(error_msg=error_msg) !error condition false - not allocated
5965

6066
call file1%move(file2) !should throw an exception since points are not associated
6167
call file1%initialize()

0 commit comments

Comments
 (0)