Skip to content

Commit e42a156

Browse files
committed
Merge branch 'master' of github.com:zbeekman/json-fortran into CMake-build
This incorporates upstream changes from github.com:jacobwilliams/json-fortran made by @jacobwilliams. Conflicts: README.md
2 parents 4a2925e + aa7a6a5 commit e42a156

File tree

4 files changed

+94
-35
lines changed

4 files changed

+94
-35
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
json-fortran
22
============
33

4-
A Fortran 2003/2008 JSON API
4+
A Fortran 2008 JSON API
55

66
Brief Description
77
---------------
88

9-
A mostly-complete API for reading and writing JSON files, written in
10-
modern Fortran. The code requires a Fortran compiler that supports
11-
various Fortran 2003 and Fortran 2008 features such as: allocatable
12-
strings, associate, newunit, generic, class, and abstract interface.
13-
I am using the Intel Fortran compiler 13.1.0 on Linux (the Mac and PC
14-
versions should also work fine). It also currently compiles under
15-
recent experimental 4.9 release of the gnu gfortran compiler. The
16-
source code is a single Fortran module file (json_module.f90).
9+
A mostly-complete API for reading and writing JSON files, written in
10+
modern Fortran. The code requires a Fortran compiler that supports
11+
various Fortran 2003 and Fortran 2008 features such as: allocatable
12+
strings, associate, newunit, generic, class, and abstract interface.
13+
It has been successfully compiled with the Intel Fortran compiler
14+
13.1.0 (and greater) and the recent [4.9 release of the GNU gfortran
15+
compiler](http://gcc.gnu.org/wiki/GFortran/News#GCC4.9). The source
16+
code is a single Fortran module file (json_module.f90).
1717

1818
Building the Library
1919
--------------------
@@ -22,14 +22,14 @@ Currently two ways are provided to build the jsonfortran library
2222
(libjsonfortran). A build script, build.sh is provided in the project
2323
root directory. Additionally, a [CMake](http://www.cmake.org) build
2424
system is provided. This build system has been tested on Mac and Linux
25-
using the Intel Fortran Compiler. It has not been tested on
26-
Windows. This CMake based build provides an install target, and
27-
exports from both the install location and the build location so that
28-
building and using json-fortran in another CMake based project is
25+
using the Intel Fortran Compiler and gfortran 4.9. It has not been
26+
tested on Windows. This CMake based build provides an install target,
27+
and exports from both the install location and the build location so
28+
that building and using json-fortran in another CMake based project is
2929
trivial. To get started with the CMake based build, set the
3030
environment variable `FC` to point to your Fortran compiler, and
3131
create a build directory. Then `(cmake-gui|ccmake|cmake)
32-
/path/to/json-fortran` to configure, `make` to build and `make
32+
/path/to/json-fortran-root` to configure, `make` to build and `make
3333
install` to optionally install. As long as the project is built with
3434
CMake other CMake projects can find it and link against it:
3535

@@ -134,10 +134,10 @@ of pointers. See the json_example.f90 file for more examples.
134134
Other Comments
135135
---------------
136136

137-
This code is a fork and extensive upgrade of the FSON code that can be
138-
found at: <https://github.com/josephalevin/fson>. It includes many
139-
features that the original code did not have, and fixes many of that
140-
code's bugs.
137+
This code is a fork and extensive upgrade of the Fortran 95 FSON code
138+
that can be found at: <https://github.com/josephalevin/fson>. It
139+
includes many features that the original code did not have, and fixes
140+
many of that code's bugs.
141141

142142
More About JSON
143143
------------

build.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/bin/sh
22

33
#
4-
# Build the json library and example program on Linux using ifort
4+
# This is just a simple script to
5+
# build the json-fortran library and
6+
# example program on Linux and Mac.
57
#
68
# Jacob Williams : 2/8/2014
79
#
@@ -35,6 +37,8 @@ ARCHIVERFLAGS='-cq'
3537
FEXT='.f90'
3638
OBJEXT='.o'
3739
LIBEXT='.a'
40+
MODEXT='.mod'
41+
WC='*'
3842

3943
LIBOUT='libjson'
4044
EXEOUT='json'
@@ -46,6 +50,11 @@ EXAMPLECODE='json_example'
4650
mkdir -p $BUILDDIR
4751
mkdir -p $BINDIR
4852

53+
#clean build:
54+
rm -f $BUILDDIR$WC$OBJEXT
55+
rm -f $BUILDDIR$WC$MODEXT
56+
rm -f $BUILDDIR$WC$LIBEXT
57+
4958
#build library:
5059
$FCOMPILER $FCOMPILERFLAGS -c $SRCDIR$MODCODE$FEXT $FCMODULEPATHFLAG$BUILDDIR
5160
mv $MODCODE$OBJEXT $BUILDDIR

src/json_example.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ subroutine print_error_message()
605605

606606
!print it if there is one:
607607
if (.not. status_ok) then
608-
write(*,*) error_msg
608+
write(*,'(A)') error_msg
609609
deallocate(error_msg)
610610
call json_clear_exceptions()
611611
end if

src/json_module.f90

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module json_module
77
! json_module
88
!
99
! DESCRIPTION
10-
! JSON-FORTRAN: A Fortran 2003/2008 JSON (JavaScript Object Notation) API.
10+
! JSON-FORTRAN: A Fortran 2008 JSON (JavaScript Object Notation) API.
1111
!
1212
! NOTES
1313
! -Based on fson by Joseph A. Levin (see LICENSE below)
@@ -2855,22 +2855,20 @@ subroutine json_parse(file, p)
28552855
character(len=*),intent(in) :: file
28562856
type(json_value),pointer :: p
28572857

2858-
integer :: iunit
2859-
integer :: istat
2860-
2861-
character(len=256) :: line
2858+
integer :: iunit, istat
2859+
character(len=:),allocatable :: line
28622860

28632861
!clean any exceptions and initialize:
28642862
call json_initialize()
28652863

28662864
! open the file
2867-
open ( newunit = iunit, &
2868-
file = file, &
2869-
status = 'OLD', &
2870-
action = 'READ', &
2871-
form = 'FORMATTED', &
2872-
position = 'REWIND', &
2873-
iostat = istat)
2865+
open ( newunit = iunit, &
2866+
file = file, &
2867+
status = 'OLD', &
2868+
action = 'READ', &
2869+
form = 'FORMATTED', &
2870+
position = 'REWIND', &
2871+
iostat = istat)
28742872

28752873
if (istat==0) then
28762874

@@ -2888,10 +2886,10 @@ subroutine json_parse(file, p)
28882886
! can print the line where the error occurred:
28892887
!
28902888
if (exception_thrown) then
2891-
backspace(iunit)
2892-
read(iunit, fmt='(A256)',iostat=istat) line
2889+
call get_current_line_from_file(iunit,line)
28932890
if (istat==0) err_message = err_message//new_line(' ')//&
2894-
' Error in line: '//trim(line)
2891+
'Offending line: '//trim(line)
2892+
if (allocated(line)) deallocate(line)
28952893
end if
28962894

28972895
! close the file
@@ -2909,6 +2907,58 @@ subroutine json_parse(file, p)
29092907
end subroutine json_parse
29102908
!********************************************************************************
29112909

2910+
!********************************************************************************
2911+
subroutine get_current_line_from_file(iunit,line)
2912+
!********************************************************************************
2913+
!****f* json_module/get_current_line_from_file
2914+
!
2915+
! NAME
2916+
! get_current_line_from_file
2917+
!
2918+
! DESCRIPTION
2919+
! Rewind the file to the beginning of the current line, and return this line.
2920+
! The file is assumed to be opened.
2921+
!
2922+
! AUTHOR
2923+
! Jacob Williams
2924+
!
2925+
!********************************************************************************
2926+
2927+
implicit none
2928+
2929+
integer,intent(in) :: iunit
2930+
character(len=:),allocatable,intent(out) :: line
2931+
2932+
integer,parameter :: n_chunk = 256 !chunk size [arbitrary]
2933+
character(len=*),parameter :: nfmt = '(A256)' !corresponding format statement
2934+
2935+
character(len=n_chunk) :: chunk
2936+
integer :: istat,isize
2937+
2938+
!initialize:
2939+
line = ''
2940+
2941+
!rewind to beginning of the current record:
2942+
backspace(iunit, iostat=istat)
2943+
2944+
!loop to read in all the characters in the current record.
2945+
![the line is read in chunks until the end of the line is reached]
2946+
if (istat==0) then
2947+
do
2948+
read(iunit,fmt=nfmt,advance='NO',size=isize,iostat=istat) chunk
2949+
if (istat==0) then
2950+
line = line//chunk
2951+
else
2952+
if (isize>0) line = line//chunk(1:isize)
2953+
exit
2954+
end if
2955+
end do
2956+
end if
2957+
2958+
!********************************************************************************
2959+
end subroutine get_current_line_from_file
2960+
!********************************************************************************
2961+
29122962
!********************************************************************************
29132963
recursive subroutine parse_value(unit, value)
29142964
!********************************************************************************

0 commit comments

Comments
 (0)