|
1 | 1 | json-fortran
|
2 | 2 | ============
|
3 | 3 |
|
| 4 | +Project Name |
| 5 | +--------------- |
| 6 | + |
4 | 7 | Fortran 2003/2008 JSON API
|
5 | 8 |
|
6 |
| -Coming soon... |
| 9 | +Brief Description |
| 10 | +--------------- |
| 11 | + |
| 12 | +A mostly complete API for reading and writing JSON files, written in modern Fortran. The code requires a Fortran compiler that supports various Fortran 2003 and Fortran 2008 features such as: allocatable strings, associate, newunit, generic, class, and abstract interface. I am using the Intel Fortran compiler 13.1.0 on Linux (the Mac and PC versions should also work fine). It does not currently compile with the gnu gfortran compiler. |
| 13 | + |
| 14 | +Reading a JSON file |
| 15 | +--------------- |
| 16 | + |
| 17 | +Reading a JSON file and getting data from it is fairly straightforward. Here is an exmaple. See the json_example.f90 file for more examples. |
| 18 | + |
| 19 | + program example1 |
| 20 | + |
| 21 | + use json_module |
| 22 | + |
| 23 | + type(json_file) :: json |
| 24 | + logical :: found |
| 25 | + integer :: ival |
| 26 | + |
| 27 | + ! initialize the module |
| 28 | + call json_initialize() |
| 29 | + |
| 30 | + ! read the file |
| 31 | + call json%load_file(filename = 'test1.json') |
| 32 | + |
| 33 | + ! print the file to the console |
| 34 | + call json%print_file() |
| 35 | + |
| 36 | + ! extract data from the parsed value |
| 37 | + ! [found can be used to check if the data was really there] |
| 38 | + call json%get('version.major', ival, found) |
| 39 | + call json%get('version.minor', ival, found) |
| 40 | + |
| 41 | + ! clean up |
| 42 | + call json%destroy() |
| 43 | + |
| 44 | + end program example1 |
| 45 | + |
| 46 | + |
| 47 | +Writing a JSON file |
| 48 | +--------------- |
| 49 | + |
| 50 | +Writing a json file is slightly more complicated and involves the use of pointers. See the json_example.f90 file for more examples. |
| 51 | + |
| 52 | + program example2 |
| 53 | + |
| 54 | + use json_module |
| 55 | + |
| 56 | + type(json_value),pointer :: p, inp |
| 57 | + logical :: found |
| 58 | + integer :: iunit |
| 59 | + |
| 60 | + ! initialize the module |
| 61 | + call json_initialize() |
| 62 | + |
| 63 | + ! initialize the structure: |
| 64 | + call json_value_create(p) |
| 65 | + call to_object(p,'test2.json') |
| 66 | + |
| 67 | + ! add an "inputs" object to the structure: |
| 68 | + call json_value_create(inp) |
| 69 | + call to_object(inp,'inputs') |
| 70 | + call json_value_add(p, inp) !add it to the root |
| 71 | + |
| 72 | + ! add some data to inputs: |
| 73 | + call json_value_add(inp, 't0', 0.1_wp) |
| 74 | + call json_value_add(inp, 'tf', 1.1_wp) |
| 75 | + call json_value_add(inp, 'x0', 9999.0000e0) |
| 76 | + call json_value_add(inp, 'integer_scalar',) |
| 77 | + call json_value_add(inp, 'integer_array', [2,4,99]) |
| 78 | + call json_value_add(inp, 'names', ['aaa','bbb','ccc']) |
| 79 | + call json_value_add(inp, 'logical_scalar', .true.) |
| 80 | + call json_value_add(inp, 'logical_vector', [.true., .false., .true.]) |
| 81 | + nullify(inp) !don't need this anymore |
| 82 | + |
| 83 | + ! write the file: |
| 84 | + open(newunit=iunit, file='test2.json', status='REPLACE') |
| 85 | + call json_print(p,iunit) |
| 86 | + close(iunit) |
| 87 | + |
| 88 | + !cleanup: |
| 89 | + call json_destroy(p) |
| 90 | + |
| 91 | + end program example2 |
| 92 | + |
| 93 | +Other Comments |
| 94 | +--------------- |
| 95 | + |
| 96 | +This code is a fork and extensive upgrade of the FSON code that can be found at: https://github.com/josephalevin/fson |
| 97 | + |
| 98 | +More About JSON |
| 99 | +------------ |
| 100 | +For more information about JSON, see: <http://www.json.org/> |
0 commit comments