josephalevin / fson Goto Github PK
View Code? Open in Web Editor NEWFortran 95 JSON Parser
Fortran 95 JSON Parser
Perhaps a stupid question, but is the json file closed after reading?
Hi,
I was using fson for a while in my project ifort 2024, and it was working great. Today I was debugging some unrelated issue and I turned on all the runtime error checking (/check:all option). Suddenly I am getting runtime errors at fson.f90 line 98:
Error "forrtl: severe (408): fort: (8): Attempt to fetch from allocatable variable STRBUFFER when it is not allocated". Adding "strBuffer = file" around line 76 seems to fix the "issue" and the function no longer crashes.
Thanks for providing a great tool.
[10/23] /opt/local/bin/gfortran-mp-12 -Ilibfson.dylib.p -I. -I../josephalevin-fson-16731d9 -fdiagnostics-color=always -Wall -O0 -g -pipe -Os -m64 -Wno-maybe-uninitialized -Jlibfson.dylib.p -o libfson.dylib.p/src_fson_path_m.f90.o -c ../josephalevin-fson-16731d9/src/fson_path_m.f90
FAILED: libfson.dylib.p/src_fson_path_m.f90.o libfson.dylib.p/fson_path_m.mod
/opt/local/bin/gfortran-mp-12 -Ilibfson.dylib.p -I. -I../josephalevin-fson-16731d9 -fdiagnostics-color=always -Wall -O0 -g -pipe -Os -m64 -Wno-maybe-uninitialized -Jlibfson.dylib.p -o libfson.dylib.p/src_fson_path_m.f90.o -c ../josephalevin-fson-16731d9/src/fson_path_m.f90
clang: warning: argument unused during compilation: '-I libfson.dylib.p'
clang: warning: argument unused during compilation: '-I .'
clang: warning: argument unused during compilation: '-I ../josephalevin-fson-16731d9'
<stdin>:3356:2: error: invalid instruction mnemonic 'cvttss2sil'
cvttss2sil 32(%rax), %eax
^~~~~~~~~~
ninja: build stopped: subcommand failed.
Log: https://build.macports.org/builders/ports-10.7_x86_64-builder/builds/130836/steps/install-port/logs/stdio
Elsewhere it builds fine.
Numbers in this format (e.g. 2e-3) are always returning zero. It appears that parse_number() is not setting the 'decimal' variable to .true. in this case.
an empty array seems to make parsing impossible.
In my version I solved it by
adding call push_char(c)
lik this
recursive subroutine parse_value(unit, value)
...
select case (c)
...
case ("]")
! end an empty array
call push_char(c)
nullify(value)
The library hangs on on the parsing of one simple integer.
Here is the code to reproduce the issue:
subroutine test_parsing()
type(fson_value), pointer :: jsonValue
jsonValue => fson_parse(str='42') ! infinite loop
call fson_destroy(jsonValue)
end subroutine
The problem seems to come from parse_integer.
I bumped into a case in which the parser crashed due to the presence of an empty string
I changed the code slightly and initialized last. Somehow the character last had the value "/".
This is my new code
function parse_string(unit) result(string)
integer, intent(inout) :: unit
type(fson_string), pointer :: string
logical :: eof
character :: c, last
string => fson_string_create()
last = " "
do
...
end do
end function parse_string
I am not sure that this project is still maintained: If yes, I have added unit tests based on fruit (http://sourceforge.net/apps/mediawiki/fortranxunit/index.php?title=Main_Page) in the branch https://github.com/jmozmoz/fson/tree/fruit_tests
(I also added the feature to parse a string instead of a file)
The first tests show e.g. that too long double precision values (in the sense of too many digits) are parsed incorrectly.
Also reals in scientific notation are not parsed correctly.
Hi Josephalevin,
I can not compile your code. I would appreciate some help:
gfortran -J build -c -g -o build/Debug/GNU-Linux-x86/src/fson_path_m.o src/fson_path_m.f95
src/fson_path_m.f95:28.21:
use fson_value_m
1
Fatal Error: Can't open module file 'fson_value_m.mod' for reading at (1)
Thanks in advance
If I am correct, there might be an issue with strings that contain escaped character.
According to JSON spec:
So, for example, a string containing only a single reverse solidus character may be represented as "\".
In the current implementation (and the forked ones), this valid JSON string:
"\\"
gives an error because the second double quote seems to be escaped (and end of string is missing).
Then the string:
"\\ "
doesn't produce an error but the resulting string is incorrect, as the backslash is not escaped.
Dear all,
I want to read geojson file and get the coordinations.
Can you help me to read coordinations array?
Thanks
IH Lee
Hi,
I've just found your project very useful for a f90 code I'm developing and that will interact with a webinterface through JSON file exchanges. I'd like to be able to use fson_get calls, involving JSON arrays. I am looking forward for having an extract example involving the callback routine. The example below fails for the 2nd and 3rd fson_get calls. For the 2nd, I understand it's far from the syntax you've anticipated with the callback placeholder, but for the 3rd case, I get a segfault!
Did I miss anything?
thanks again for releasing this very nice piece of code!
Example file that fails
program example1 ! Typical usage should only require an explicit use of the fson module. ! The other modules will be used privatley by fson as required. use fson ! declare a pointer variable. Always use a pointer with fson_value. type(fson_value), pointer :: value, p character(len=100) :: fn integer :: vv,vvv(2) character(len=50) :: fff real(4) :: d call get_command_argument(1,fn) ! parse the json file value => fson_parse(fn) ! print the parsed data to the console call fson_print(value) ! extract data from the parsed value print *,"--------------------------" call fson_get( this=value, value=vv, path="Correl.ID2" ) print*,"vv= ",vv call fson_get( this=value, value=vvv, path="Correl.ID1" ) print*,"vvv= ",vvv call fson_get( this=value, value=d, path="Prior[3].mode" ) print*,"d= ",d ! clean up call fson_destroy(value) end program example1
Associated json input file.
{ "Prior" : [ { "ID" : [1,3] , "type" : "cyclic" , "period" : 180 }, { "ID" : [3,4] , "type" : "uniform", "LowBound" : 0, "HighBound" : 1 }, { "ID" : [3,1] , "type" : "Normal" , "mode" : 0.3 , "width" : 40. , "LowBound" : null , "HighBound" : null }, { "ID" : [3,3] , "type" : "LogNormal", "mode" : 100. , "width" : 0.2, "LowBound" : null, "HighBound" : null }, { "ID" : [2,2] , "type" : "ImgPos2", "x" : 0.4 , "y" : 0.5 , "a" : 0.3 , "b" : 0.2 , "theta" : 40 }, { "ID" : [1,1] , "type" : "SrcPos1", "x" : 0.4, "y" : 0.5, "a" : 0.4 }, { "ID" : [1,4] , "type" : "Ellip2" , "width" : 0.3 , "highBound" : 1.0 } ] , "Correl" : { "ID1" : [1,2] , "ID2" : 3 , "correl" : 0.99, } }
Are there any examples available of extracting array information? Thanks.
Just wanted to mention that I have forked the FSON code and significantly rewritten/improved it. It can be found at: https://github.com/jacobwilliams/json-fortran. It fixes many of the bugs found in FSON (memory leaks, not being able to read certain numerical values, not handling escape characters in strings, etc.). Check it out if you are interested. Note that the new code requires a Fortran compiler that supports some Fortran 2003/2008 features (I'm using ifort).
Hi,
I wanted to know if FSON can create a JSON object, i know that json-fortran can create a json object i.e. make a json file from scratch, which can be then saved as a json file. I am curious as Fson is said to be parser.
Hi,
I am using the FSON library since a couple of days in my Fortran projects and simply love it.
I was however wondering if I am actually allowed to (re)use it, as your project does not contain any LICENSE file so far.
Would be great if you could add one or add a COPYRIGHT statement at least.
Best, Alex
Can you add the appropriate license for this project? I would like to use this in an academic setting, but I am afraid I cannot until a license is uploaded.
[ 58%] Linking Fortran executable zofu-driver
/opt/local/bin/cmake -E cmake_link_script CMakeFiles/zofu-driver.dir/link.txt --verbose=ON
/opt/local/bin/gfortran-mp-12 -L/opt/local/lib -Wl,-headerpad_max_install_names -Wl,-rpath,/opt/local/lib/libgcc -pipe -Os -m32 -mmacosx-version-min=10.6 "CMakeFiles/zofu-driver.dir/_deps/zofu-src/src/zofu_driver.F90.o" -o zofu-driver -Wl,-rpath,/opt/local/lib libzofu.dylib
make[2]: Leaving directory `/opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/josephalevin-fson-16731d9'
[ 58%] Built target zofu-driver
/usr/bin/make -f CMakeFiles/fson_test.dir/build.make CMakeFiles/fson_test.dir/depend
/usr/bin/make -f CMakeFiles/fson_test2.dir/build.make CMakeFiles/fson_test2.dir/depend
make[2]: Entering directory `/opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/josephalevin-fson-16731d9'
make[2]: Entering directory `/opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/josephalevin-fson-16731d9'
/opt/local/bin/cmake -E cmake_copy_f90_mod fson_test.mod CMakeFiles/fson_test_lib.dir/fson_test.mod.stamp GNU
[ 62%] Generating fson_test_driver.f90
./zofu-driver /opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/fson-1.0.5/src/tests/fson_test_zofu.f90 /opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/fson-1.0.5/fson_test_driver.f90
[ 66%] Generating fson_test2_driver.f90
./zofu-driver /opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/fson-1.0.5/src/tests/fson_test2_zofu.f90 /opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/fson-1.0.5/fson_test2_driver.f90
dyld: Library not loaded: /opt/local/lib/libzofu.dylib
Referenced from: /opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/josephalevin-fson-16731d9/zofu-driver
Reason: image not found
dyld: Library not loaded: /opt/local/lib/libzofu.dylib
Referenced from: /opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/josephalevin-fson-16731d9/zofu-driver
Reason: image not found
[ 70%] Linking Fortran shared library libfson_test_lib.dylib
/opt/local/bin/cmake -E cmake_link_script CMakeFiles/fson_test_lib.dir/link.txt --verbose=ON
/opt/local/bin/cmake -E touch CMakeFiles/fson_test_lib.dir/src/tests/fson_test_zofu.f90.o.provides.build
make[2]: *** [fson_test_driver.f90] Trace/BPT trap
make[2]: Leaving directory `/opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/josephalevin-fson-16731d9'
make[1]: *** [CMakeFiles/fson_test.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
make[2]: *** [fson_test2_driver.f90] Trace/BPT trap
make[2]: Leaving directory `/opt/local/var/macports/build/_opt_PPCRosettaPorts_devel_fson/fson/work/josephalevin-fson-16731d9'
make[1]: *** [CMakeFiles/fson_test2.dir/all] Error 2
CMakeLists have the following:
Line 133 in 16731d9
zofu
does have CMake implementation.zofu
, so that it does not need to built twice. (Moreover, on macOS internal build of zofu
just fails, at least on my system. External works perfectly and passes all tests.)Is there a trick to read a list of strings ?
{ "char_array" : ["name1", "name22"] }
I have a trouble with installation. I got following errors but there is no documentation for that. Is there a minimum requirement for gfortran? (I'm using GNU Fortran (GCC) 4.4.6 20120305
) Moreover, how I link fson with my code? Using .mod
and .so
files or adding source to my program directly? I think adding some documentation for that kind of things will helps.
gfortran -O2 -fpic -Jbuild -c src/fson_path_m.f90 -o build/fson_path_m.o
src/fson_path_m.f90:138.132:
1
Warning: Line truncated at (1)
src/fson_path_m.f90:658.36:
call get_array_2d(this, path, array_callback_2d_logical)
1
Error: Internal procedure 'array_callback_2d_logical' is not allowed as an actual argument at (1)
src/fson_path_m.f90:633.36:
call get_array_2d(this, path, array_callback_2d_double)
1
Error: Internal procedure 'array_callback_2d_double' is not allowed as an actual argument at (1)
src/fson_path_m.f90:608.36:
call get_array_2d(this, path, array_callback_2d_real)
1
Error: Internal procedure 'array_callback_2d_real' is not allowed as an actual argument at (1)
src/fson_path_m.f90:583.36:
call get_array_2d(this, path, array_callback_2d_integer)
1
Error: Internal procedure 'array_callback_2d_integer' is not allowed as an actual argument at (1)
src/fson_path_m.f90:499.36:
call get_array_1d(this, path, array_callback_1d_logical)
1
Error: Internal procedure 'array_callback_1d_logical' is not allowed as an actual argument at (1)
src/fson_path_m.f90:474.36:
call get_array_1d(this, path, array_callback_1d_double)
1
Error: Internal procedure 'array_callback_1d_double' is not allowed as an actual argument at (1)
src/fson_path_m.f90:449.36:
call get_array_1d(this, path, array_callback_1d_real)
1
Error: Internal procedure 'array_callback_1d_real' is not allowed as an actual argument at (1)
src/fson_path_m.f90:424.36:
call get_array_1d(this, path, array_callback_1d_integer)
1
Error: Internal procedure 'array_callback_1d_integer' is not allowed as an actual argument at (1)
make: *** [build/fson_path_m.o] Error 1
Hi,
I reinstalled and compiled fson-master on a different machine and I got the following error message after running my code:
ERROR: Unexpected character while parsing value. 'E' ASCII= 69.
I was able to run my code and parse JSON files with no errors before.
What can cause this error?
Many Thanks
Can you correct the error in file src/fson.f95 line 487?
Greeting.
Dear @josephalevin,
your json parser is extremely helpful for my. Many thanks for sharing.
This issue is just to let you know (if you do not already) that it does not work with double precision variables.
double precision a1
...
call fson_get(value,"a1value",a1)
This does not work. Of course it is easily solvable with a real variable and then converted to double precision
double precision a1
real a2
...
call fson_get(value,"a1value",a2)
a1 = dble(a2)
Today, I downIoaded the zip from your site and it is exactly what I am looking for, but...
I tried to parse an array only consisting of numbers
like 1.0E-01 and 1.0E+01. This did not work well.
I had to change 3 small things
1)
The first character of the numbers were ignored. This is because in the routine pop_char
ios can equal eor
I set ios to zero if pushed_index > 0. I think ios is only relevant if you actually had to read.
In my version 10**exp does not work if exp<0, so I changed the routine parse_number
if(exp>=0)then
frac = frac * (10 ** exp)
else
frac = frac / (10 ** (-exp))
end if
Also I had to change the routine parse_integer to
integer function parse_integer(unit, digit_count) result(integral)
integer, intent(in) :: unit
integer, optional, intent(inout) :: digit_count
logical :: eof
character :: c
integer :: tmp, count
logical :: signread
integer :: sign
count = 0
integral = 0
sign = 1
signread = .false.
do
c = pop_char(unit, eof = eof, skip_ws = .true.)
if (eof) then
print *, "ERROR: Unexpected end of file while parsing digit."
call exit (1)
else
select case(c)
case ("+","-")
if(signread)then
! sign is read twice
print *, "ERROR: Unexpected sign, because sign is already read."
call exit (1)
end if
signread = .true.
if(c.eq."-")sign=-1
count = count + 1
case ("0":"9")
! digit
read (c, '(i1)') tmp
! shift
if (count > 0) then
integral = integral * 10
end if
! add
integral = integral + tmp
! increase the count
count = count + 1
case default
if (present(digit_count)) then
digit_count = count
end if
integral=integral*sign
call push_char(c)
return
end select
end if
end do
end function parse_integer
The library accept invalid json strings.
Here is the code to reproduce the issue:
subroutine test_parsing()
type(fson_value), pointer :: jsonValue
jsonValue => fson_parse(str=' [ - 4 y 2 ] ') ! invalid json, but works fine !
call fson_destroy(jsonValue)
end subroutine
There appears to be an error in parsing real numbers with negative exponents (e.g. 4.14112E-1). I have a test case which parses 4.14112E1 correctly but dies if the exponent is changed from 1 to -1.
A test file which can invoke this issue is posted at https://gist.github.com/apthorpe/d27f2eddbffd3a747b0d
What are your thoughts on providing a CMake build either along with or instead of the Meson build? The primary advantage would be allowing users to call the FSON library with
find_package(FSON)
target_link_libraries(client FSON)
in their CMakeLists.txt and get a "standard" version if they use FSON in many projects.
I put together a wrapper project that provides this functionality if you want to take a look (I need to use FSON in a larger project that is already managed using CMake so this was the easy way for me to incorporate the library). If you would like to incorporate those into the main project, let me know and I can redo them as a pull request to the main project.
so I have data coming to me from an external site that appears similar to the below. Is there a way to reference this array with FSON?
[ {
"a" : "string",
"b" : "string"
}
, {
"a" : "string",
"b" : "string"
}
]
I can hack the file if absolutely necessary to add this around everything but would prefer not to if possible
{ "label":
....
}
Running the example code against a JSON sample showed spaces being stripped from string values.
A test file which can invoke this issue (same as link in previous issue) is posted at https://gist.github.com/apthorpe/d27f2eddbffd3a747b0d
Also, it seems that carriage returns may be prepended to parsed strings, though that may just be a printing artifact and not a parser issue; I haven't looked into it in depth yet and didn't want to open a separate issue for it until I do.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.