Skip to content

Commit ac6642e

Browse files
committed
Merge branch 'master' of https://github.com/jacobwilliams/json-fortran into speed
2 parents ea28d41 + 765daf2 commit ac6642e

File tree

4 files changed

+73
-11
lines changed

4 files changed

+73
-11
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ if ( ENABLE_TESTS )
242242
enable_testing()
243243

244244
# emulate GNU Autotools `make check`
245-
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} -C $<CONFIG>)
245+
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} -C $<CONFIG> --output-on-failure)
246+
add_custom_target(build_tests) # Make target to build all tests
247+
add_dependencies(build_tests ${LIB_NAME} ${LIB_NAME}-static)
246248

247249
find_program ( JSONLINT jsonlint )
248250
find_program ( DIFF diff )
@@ -290,6 +292,7 @@ if ( ENABLE_TESTS )
290292
add_executable ( ${TEST} EXCLUDE_FROM_ALL ${UNIT_TEST} )
291293
target_link_libraries ( ${TEST} ${LIB_NAME} )
292294
add_dependencies ( check ${TEST} )
295+
add_dependencies ( build_tests ${TEST} )
293296
set_target_properties ( ${TEST}
294297
PROPERTIES
295298
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )

files/inputs/invalid4.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"a": "blah",
3+
"b": 2
4+
}
5+
6+
7+
8+
9+
10+
11+
,

src/json_value_module.F90

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ module json_value_module
732732
procedure :: json_value_print
733733
procedure :: string_to_int
734734
procedure :: string_to_dble
735+
procedure :: parse_end => json_parse_end
735736
procedure :: parse_value
736737
procedure :: parse_number
737738
procedure :: parse_string
@@ -8719,7 +8720,7 @@ subroutine json_parse_file(json, file, p, unit)
87198720
logical(LK) :: has_duplicate !! if checking for duplicate keys
87208721
character(kind=CK,len=:),allocatable :: path !! path to any duplicate key
87218722

8722-
!clear any exceptions and initialize:
8723+
! clear any exceptions and initialize:
87238724
call json%initialize()
87248725

87258726
if ( present(unit) ) then
@@ -8731,7 +8732,7 @@ subroutine json_parse_file(json, file, p, unit)
87318732

87328733
iunit = unit
87338734

8734-
!check to see if the file is already open
8735+
! check to see if the file is already open
87358736
! if it is, then use it, otherwise open the file with the name given.
87368737
inquire(unit=iunit, opened=is_open, iostat=istat)
87378738
if (istat==0 .and. .not. is_open) then
@@ -8745,7 +8746,7 @@ subroutine json_parse_file(json, file, p, unit)
87458746
iostat = istat &
87468747
FILE_ENCODING )
87478748
else
8748-
!if the file is already open, then we need to make sure
8749+
! if the file is already open, then we need to make sure
87498750
! that it is open with the correct form/access/etc...
87508751
end if
87518752

@@ -8779,6 +8780,7 @@ subroutine json_parse_file(json, file, p, unit)
87798780

87808781
! parse as a value
87818782
call json%parse_value(unit=iunit, str=CK_'', value=p)
8783+
call json%parse_end(unit=iunit, str=CK_'')
87828784

87838785
! check for errors:
87848786
if (json%exception_thrown) then
@@ -8828,7 +8830,7 @@ subroutine json_parse_string(json, p, str)
88288830
logical(LK) :: has_duplicate !! if checking for duplicate keys
88298831
character(kind=CK,len=:),allocatable :: path !! path to any duplicate key
88308832

8831-
!clear any exceptions and initialize:
8833+
! clear any exceptions and initialize:
88328834
call json%initialize()
88338835

88348836
! create the value and associate the pointer
@@ -8840,6 +8842,7 @@ subroutine json_parse_string(json, p, str)
88408842

88418843
! parse as a value
88428844
call json%parse_value(unit=iunit, str=str, value=p)
8845+
call json%parse_end(unit=iunit, str=str)
88438846

88448847
if (json%exception_thrown) then
88458848
call json%annotate_invalid_json(iunit,str)
@@ -8858,6 +8861,41 @@ subroutine json_parse_string(json, p, str)
88588861
end subroutine json_parse_string
88598862
!*****************************************************************************************
88608863

8864+
!*****************************************************************************************
8865+
!>
8866+
! An error checking routine to call after a file (or string) has been parsed.
8867+
! It will throw an exception if there are any other non-whitespace characters
8868+
! in the file.
8869+
8870+
subroutine json_parse_end(json, unit, str)
8871+
8872+
implicit none
8873+
8874+
class(json_core),intent(inout) :: json
8875+
integer(IK),intent(in) :: unit !! file unit number
8876+
character(kind=CK,len=*),intent(in) :: str !! string containing JSON
8877+
!! data (only used if `unit=0`)
8878+
8879+
logical(LK) :: eof !! end-of-file flag
8880+
character(kind=CK,len=1) :: c !! character read from file
8881+
!! (or string) by [[pop_char]]
8882+
8883+
! first check for exceptions:
8884+
if (json%exception_thrown) return
8885+
8886+
! pop the next non whitespace character off the file
8887+
call json%pop_char(unit, str=str, eof=eof, skip_ws=.true., &
8888+
skip_comments=json%allow_comments, popped=c)
8889+
8890+
if (.not. eof) then
8891+
call json%throw_exception('Error in json_parse_end:'//&
8892+
' Unexpected character found after parsing value. "'//&
8893+
c//'"')
8894+
end if
8895+
8896+
end subroutine json_parse_end
8897+
!*****************************************************************************************
8898+
88618899
!*****************************************************************************************
88628900
!>
88638901
! Alternate version of [[json_parse_string]], where `str` is kind=CDK.

src/tests/jf_test_06.F90

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
! Module for the sixth unit test.
44
!
55
!# HISTORY
6-
! * Izaak Beekman : 2/18/2015 : Created (refactoried original json_example.f90 file)
6+
! * Izaak Beekman : 2/18/2015 : Created (refactored original json_example.f90 file)
77

88
module jf_test_6_mod
99

@@ -34,9 +34,13 @@ subroutine test_6(error_cnt)
3434
character(kind=CK,len=:),allocatable :: expected_error_msg
3535
logical(LK) :: status_ok
3636

37-
character(len=*),dimension(3),parameter :: files = ['invalid.json ',&
37+
character(len=*),dimension(5),parameter :: files = ['invalid.json ',&
3838
'invalid2.json',&
39-
'invalid3.json']
39+
'invalid3.json',&
40+
'invalid4.json',&
41+
' ']
42+
43+
character(len=*),parameter :: invalid_str = '{"a":1} "b": 2}' !! invalid JSON string
4044

4145
error_cnt = 0
4246
call json%initialize()
@@ -55,9 +59,15 @@ subroutine test_6(error_cnt)
5559

5660
! parse the json file:
5761
write(error_unit,'(A)') ''
58-
write(error_unit,'(A)') 'load file: '//trim(files(i))
59-
write(error_unit,'(A)') ''
60-
call json%load_file(filename = dir//trim(files(i)))
62+
if (files(i)=='') then
63+
write(error_unit,'(A)') 'load string: '//invalid_str
64+
write(error_unit,'(A)') ''
65+
call json%load_from_string(str = invalid_str)
66+
else
67+
write(error_unit,'(A)') 'load file: '//trim(files(i))
68+
write(error_unit,'(A)') ''
69+
call json%load_file(filename = dir//trim(files(i)))
70+
end if
6171
if (json%failed()) then
6272

6373
if (i==1) then

0 commit comments

Comments
 (0)