@@ -12,20 +12,13 @@ A Fortran 2008 JSON API
1212 - [ Brief description] ( #brief-description )
1313 - [ Download] ( #download )
1414 - [ Building the library] ( #building-the-library )
15- - [ Example Usage] ( #example-usage )
16- - [ Reading JSON from a file] ( #reading-json-from-a-file )
17- - [ Reading JSON from a string] ( #reading-json-from-a-string )
18- - [ Modifying variables in a JSON file] ( #modifying-variables-in-a-json-file )
19- - [ Writing a JSON file] ( #writing-a-json-file )
20- - [ Building a JSON file from scratch] ( #building-a-json-file-from-scratch )
2115 - [ Documentation] ( #documentation )
2216 - [ Contributing] ( #contributing )
2317 - [ License] ( #license )
2418 - [ Miscellaneous] ( #miscellaneous )
2519
2620<!-- markdown-toc end -->
2721
28-
2922Status
3023------
3124[ ![ Build Status] ( https://img.shields.io/travis/jacobwilliams/json-fortran/master.svg?style=plastic )] ( https://travis-ci.org/jacobwilliams/json-fortran )
4033Take a look at the
4134[ CHANGELOG] ( https://github.com/jacobwilliams/json-fortran/blob/master/CHANGELOG.md#unreleased )
4235for a list of changes since the latest release.
36+
4337[ top] ( #json-fortran )
4438
4539Brief description
@@ -135,161 +129,6 @@ endforeach()
135129
136130[ top] ( #json-fortran )
137131
138- Example Usage
139- ---------------
140- In this section the basic functionality of the JSON-Fortran library is illustrated.
141-
142- ### Reading JSON from a file
143-
144- Reading a JSON file and getting data from it is fairly
145- straightforward using the ` json_file ` class. Here is an example. See unit tests 1 and 3-6
146- for more examples. The source files may be found in ` src/tests/ ` .
147-
148- ``` fortran
149- program example1
150-
151- use json_module
152-
153- type(json_file) :: json
154- logical :: found
155- integer :: i,j,k
156-
157- ! initialize the module
158- call json_initialize()
159-
160- ! read the file
161- call json%load_file(filename = '../files/inputs/test1.json')
162-
163- ! print the file to the console
164- call json%print_file()
165-
166- ! extract data from the file
167- ! [found can be used to check if the data was really there]
168- call json%get('version.major', i, found)
169- if ( .not. found ) stop 1
170- call json%get('version.minor', j, found)
171- if ( .not. found ) stop 1
172- call json%get('data(1).number', k, found)
173- if ( .not. found ) stop 1
174-
175- ! clean up
176- call json%destroy()
177- if (json_failed()) stop 1
178-
179- end program example1
180- ```
181-
182- [ top] ( #json-fortran )
183-
184- ### Reading JSON from a string
185-
186- JSON can also be read directly from a character string like so:
187- ``` fortran
188- call json%load_from_string('{"name": "Leonidas"}')
189- ```
190-
191- [ top] ( #json-fortran )
192-
193- ### Modifying variables in a JSON file
194-
195- After reading a JSON file, if you want to change the values of some of the variables, you can use the ` update ` method. For the example above:
196-
197- ``` fortran
198- ! [found can be used to check if the data was really there]
199- call json%update('version.major',9,found) !change major version to 9
200- call json%update('version.minor',0,found) !change minor version to 0
201- call json%update('version.patch',0,found) !change patch to 0
202- ```
203-
204- [ top] ( #json-fortran )
205-
206- ### Writing a JSON file
207-
208- To print the JSON file (either to a file or the console), the ` print_file ` method can be used. For the above example:
209-
210- ``` fortran
211- call json%print_file() !prints to the console
212- call json%print_file(iunit) !prints to the file connected to iunit
213- ```
214-
215- [ top] ( #json-fortran )
216-
217- ### Building a JSON file from scratch
218-
219- Constructing a JSON file element by element is slightly more complicated and involves the use
220- of ` json_value ` pointers. For more examples see unit tests 2, 4 and 7 in ` src/tests/ ` .
221-
222- ``` fortran
223- program example2
224-
225- use,intrinsic :: iso_fortran_env, only: wp => real64
226-
227- use json_module
228-
229- type(json_value),pointer :: p, inp
230-
231- ! initialize the module
232- call json_initialize()
233-
234- ! initialize the structure:
235- call json_create_object(p,'')
236-
237- ! add an "inputs" object to the structure:
238- call json_create_object(inp,'inputs')
239- call json_add(p, inp) !add it to the root
240-
241- ! add some data to inputs:
242- call json_add(inp, 't0', 0.1_wp)
243- call json_add(inp, 'tf', 1.1_wp)
244- call json_add(inp, 'x0', 9999.0000d0)
245- call json_add(inp, 'integer_scalar', 787)
246- call json_add(inp, 'integer_array', [2,4,99])
247- call json_add(inp, 'names', ['aaa','bbb','ccc'])
248- call json_add(inp, 'logical_scalar', .true.)
249- call json_add(inp, 'logical_vector', [.true., .false., .true.])
250- nullify(inp) !don't need this anymore
251-
252- ! write the file:
253- call json_print(p,'../files/example2.json')
254-
255- !cleanup:
256- call json_destroy(p)
257- if (json_failed()) stop 1
258-
259- end program example2
260- ```
261-
262- The code above produces the file:
263-
264- ``` JSON
265- {
266- "inputs" : {
267- "t0" : 0.1E+0 ,
268- "tf" : 0.11E+1 ,
269- "x0" : 0.9999E+4 ,
270- "integer_scalar" : 787 ,
271- "integer_array" : [
272- 2 ,
273- 4 ,
274- 99
275- ],
276- "names" : [
277- " aaa" ,
278- " bbb" ,
279- " ccc"
280- ],
281- "logical_scalar" : true ,
282- "logical_vector" : [
283- true ,
284- false ,
285- true
286- ]
287- }
288- }
289- ```
290-
291- [ top] ( #json-fortran )
292-
293132Documentation
294133--------------
295134
@@ -299,6 +138,8 @@ documentation can also be generated by processing the source files
299138with [ FORD] ( https://github.com/cmacmackin/ford ) . Note that both the
300139shell script and CMake will also generate these files automatically in the documentation folder, assuming you have FORD installed.
301140
141+ Some examples can also be found on the [ wiki] ( https://github.com/jacobwilliams/json-fortran/wiki/Example-Usage ) .
142+
302143[ top] ( #json-fortran )
303144
304145Contributing
0 commit comments