1
1
! *****************************************************************************************
2
- ! >author: Jacob Williams
2
+ ! > author: Jacob Williams
3
+ ! license: BSD
3
4
!
4
5
! JSON-FORTRAN: A Fortran 2008 JSON (JavaScript Object Notation) API.
5
6
!
6
- ! # See also
7
- ! * [json-fortran development site](http://github.com/jacobwilliams/json-fortran)
8
- ! * [json-fortran online documentation](http://jacobwilliams.github.io/json-fortran)
9
- ! * [JSON website](http://www.json.org/)
10
- ! * [JSON validator](http://jsonlint.com/)
7
+ ! This module provides an interface for reading and writing JSON files.
8
+ !
9
+ ! @note ```USE_UCS4``` is an optional preprocessor flag.
10
+ ! When present, Unicode support is enabled. Note that this
11
+ ! is currently only supported with the gfortran compiler.
12
+ ! Example: ```gfortran -DUSE_UCS4 ... ```
13
+ #ifdef USE_UCS4
14
+ ! The documentation given here assumes ```USE_UCS4``` **is** defined.
15
+ #else
16
+ ! The documentation given here assumes ```USE_UCS4``` **is not** defined.
17
+ #endif
18
+ !
19
+ ! @note ```CK``` and ```CDK``` are the json-fortran character kind and json-fortran default
20
+ ! character kind respectively. Client code must ensure characters of kind=CK
21
+ ! are used for all character variables and strings passed to the json-fortran
22
+ ! library *EXCEPT* for file names which must be of ```'DEFAULT'``` character kind,
23
+ ! provided here as ```CDK```. In particular, any: json path, character or string, or
24
+ ! object name passed to the json-fortran library *MUST* be of type ```CK```.
11
25
!
12
26
! # License
13
27
!
14
- ! json-fortran License:
28
+ ! ** json-fortran License:**
15
29
!
16
30
! JSON-FORTRAN: A Fortran 2008 JSON API
17
31
!
18
32
! http://github.com/jacobwilliams/json-fortran
19
33
!
20
- ! Copyright (c) 2014, Jacob Williams
34
+ ! Copyright (c) 2014-2015 , Jacob Williams
21
35
!
22
36
! All rights reserved.
23
37
!
41
55
! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42
56
! SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
57
!
44
- ! Original FSON License:
45
- !
46
- ! http://github.com/josephalevin/fson
58
+ ! **Original FSON License:**
47
59
!
48
60
! Copyright (c) 2012 Joseph A. Levin
49
61
!
73
85
! Various Fortran 2003/2008 features are now used
74
86
! (e.g., allocatable strings, newunit, generic, class, and abstract interface).
75
87
! * Development continues at: [Github](http://github.com/jacobwilliams/json-fortran)
88
+ !
89
+ ! # See also
90
+ ! * [json-fortran development site](http://github.com/jacobwilliams/json-fortran)
91
+ ! * [json-fortran online documentation](http://jacobwilliams.github.io/json-fortran)
92
+ ! * [JSON website](http://www.json.org/)
93
+ ! * [JSON validator](http://jsonlint.com/)
76
94
77
95
module json_module
78
96
@@ -82,17 +100,6 @@ module json_module
82
100
83
101
private
84
102
85
- ! *********************************************************
86
- !
87
- ! USE_UCS4 is an optional preprocessor flag.
88
- ! When present, Unicode support is enabled.
89
- !
90
- ! USAGE
91
- ! When compiling:
92
- ! * gfortran -DUSE_UCS4 ...
93
- !
94
- ! *********************************************************
95
-
96
103
integer ,parameter :: RK = real64 ! ! Default real kind [8 bytes]
97
104
98
105
integer ,parameter :: IK = int32 ! ! Default integer kind [4 bytes].
@@ -101,21 +108,7 @@ module json_module
101
108
! >
102
109
! Processor dependendant 'DEFAULT' character kind.
103
110
! This is 1 byte for the Intel and Gfortran compilers.
104
- !
105
- ! # Notes
106
- ! CK and CDK are the json-fortran character kind and json-fortran default
107
- ! character kind respectively. Client code must ensure characters of kind=CK
108
- ! are used for all character variables and strings passed to the json-fortran
109
- ! library *EXCEPT* for file names which must be of 'DEFAULT' character kind,
110
- ! provided here as CDK. In particular, any:
111
- ! * file name
112
- ! * format statement
113
- ! * file path
114
- ! passed to the json-fortran library *MUST* be of type CDK. This
115
- ! will be the case for all string literals nor prepended with CK_ and only
116
- ! if ISO 10646 is supported and enabled, will strings of kind CK be different
117
- ! than CDK
118
- !
111
+
119
112
integer ,parameter ,public :: CDK = selected_char_kind(' DEFAULT' )
120
113
! *********************************************************
121
114
@@ -152,17 +145,6 @@ module json_module
152
145
! Currently only gfortran >= 4.9.2 will correctly support
153
146
! UCS4 which is stored in 4 bytes.
154
147
! (and perhaps others).
155
- !
156
- ! # Notes
157
- ! CK and CDK are the json-fortran character kind and json-fortran default
158
- ! character kind respectively. Client code must ensure characters of kind=CK
159
- ! are used for all character variables and strings passed to the json-fortran
160
- ! library *EXCEPT* for file names which must be of 'DEFAULT' character kind,
161
- ! provided here as JDCK. In particular, any:
162
- ! * json path
163
- ! * character or string
164
- ! * object name
165
- ! passed to the json-fortran library *MUST* be of type CK.
166
148
167
149
integer ,parameter ,public :: CK = selected_char_kind(json_fortran_string_kind)
168
150
! *********************************************************
@@ -841,8 +823,10 @@ end subroutine array_callback_func
841
823
rp_addl_safety
842
824
843
825
! Get the number of possible digits in the exponent when using decimal number system
826
+ integer (IK),parameter :: maxexp = maxexponent (1.0_RK )
827
+ integer (IK),parameter :: minexp = minexponent (1.0_RK )
844
828
integer (IK),parameter :: real_exponent_digits = floor ( 1 + log10 ( &
845
- real (max (maxexponent ( 1.0_RK ) ,abs (minexponent ( 1.0_RK ) )),&
829
+ real (max (maxexp ,abs (maxexp )),&
846
830
kind= RK) ) )
847
831
848
832
! 6 = sign + leading 0 + decimal + 'E' + exponent sign + 1 extra
@@ -884,7 +868,7 @@ end subroutine array_callback_func
884
868
! *****************************************************************************************
885
869
! > author: Jacob Williams
886
870
!
887
- ! Destroy the [[json_value]] type.
871
+ ! Destroy the data within a [[json_value]], and rest type to json_unknown .
888
872
889
873
subroutine destroy_json_data (d )
890
874
@@ -964,7 +948,7 @@ subroutine json_file_load(me, filename, unit)
964
948
965
949
class(json_file),intent (inout ) :: me
966
950
character (kind= CDK,len=* ),intent (in ) :: filename ! ! the filename to open
967
- integer (IK),intent (in ),optional :: unit ! ! the unit to use
951
+ integer (IK),intent (in ),optional :: unit ! ! the unit number to use
968
952
969
953
call json_parse(file= filename, p= me% p, unit= unit)
970
954
@@ -2632,8 +2616,8 @@ subroutine wrap_json_value_add_integer(me, name, val)
2632
2616
implicit none
2633
2617
2634
2618
type (json_value),pointer :: me
2635
- character (kind= CDK,len=* ),intent (in ) :: name
2636
- integer (IK),intent (in ) :: val
2619
+ character (kind= CDK,len=* ),intent (in ) :: name ! ! name of the variable
2620
+ integer (IK),intent (in ) :: val ! ! value
2637
2621
2638
2622
call json_value_add_integer(me, to_unicode(name), val)
2639
2623
@@ -2654,8 +2638,8 @@ subroutine json_value_add_integer_vec(me, name, val)
2654
2638
implicit none
2655
2639
2656
2640
type (json_value),pointer :: me
2657
- character (kind= CK,len=* ),intent (in ) :: name
2658
- integer (IK),dimension (:),intent (in ) :: val
2641
+ character (kind= CK,len=* ),intent (in ) :: name ! ! name of the variable
2642
+ integer (IK),dimension (:),intent (in ) :: val ! ! value
2659
2643
2660
2644
type (json_value),pointer :: var
2661
2645
integer (IK) :: i ! counter
@@ -2687,8 +2671,8 @@ subroutine wrap_json_value_add_integer_vec(me, name, val)
2687
2671
implicit none
2688
2672
2689
2673
type (json_value),pointer :: me
2690
- character (kind= CDK,len=* ),intent (in ) :: name
2691
- integer (IK),dimension (:),intent (in ) :: val
2674
+ character (kind= CDK,len=* ),intent (in ) :: name ! ! name of the variable
2675
+ integer (IK),dimension (:),intent (in ) :: val ! ! value
2692
2676
2693
2677
call json_value_add_integer_vec(me, to_unicode(name), val)
2694
2678
@@ -2709,8 +2693,8 @@ subroutine json_value_add_logical(me, name, val)
2709
2693
implicit none
2710
2694
2711
2695
type (json_value),pointer :: me
2712
- character (kind= CK,len=* ),intent (in ) :: name
2713
- logical (LK),intent (in ) :: val
2696
+ character (kind= CK,len=* ),intent (in ) :: name ! ! name of the variable
2697
+ logical (LK),intent (in ) :: val ! ! value
2714
2698
2715
2699
type (json_value),pointer :: var
2716
2700
@@ -2736,8 +2720,8 @@ subroutine wrap_json_value_add_logical(me, name, val)
2736
2720
implicit none
2737
2721
2738
2722
type (json_value),pointer :: me
2739
- character (kind= CDK,len=* ),intent (in ) :: name
2740
- logical (LK),intent (in ) :: val
2723
+ character (kind= CDK,len=* ),intent (in ) :: name ! ! name of the variable
2724
+ logical (LK),intent (in ) :: val ! ! value
2741
2725
2742
2726
call json_value_add_logical(me, to_unicode(name), val)
2743
2727
@@ -2758,8 +2742,8 @@ subroutine json_value_add_logical_vec(me, name, val)
2758
2742
implicit none
2759
2743
2760
2744
type (json_value),pointer :: me
2761
- character (kind= CK,len=* ),intent (in ) :: name
2762
- logical (LK),dimension (:),intent (in ) :: val
2745
+ character (kind= CK,len=* ),intent (in ) :: name ! ! name of the vector
2746
+ logical (LK),dimension (:),intent (in ) :: val ! ! value
2763
2747
2764
2748
type (json_value),pointer :: var
2765
2749
integer (IK) :: i ! counter
@@ -2791,8 +2775,8 @@ subroutine wrap_json_value_add_logical_vec(me, name, val)
2791
2775
implicit none
2792
2776
2793
2777
type (json_value),pointer :: me
2794
- character (kind= CDK,len=* ),intent (in ) :: name
2795
- logical (LK),dimension (:),intent (in ) :: val
2778
+ character (kind= CDK,len=* ),intent (in ) :: name ! ! name of the variable
2779
+ logical (LK),dimension (:),intent (in ) :: val ! ! value
2796
2780
2797
2781
call json_value_add_logical_vec(me, to_unicode(name), val)
2798
2782
@@ -2813,8 +2797,8 @@ subroutine json_value_add_string(me, name, val)
2813
2797
implicit none
2814
2798
2815
2799
type (json_value),pointer :: me
2816
- character (kind= CK,len=* ),intent (in ) :: name
2817
- character (kind= CK,len=* ),intent (in ) :: val
2800
+ character (kind= CK,len=* ),intent (in ) :: name ! ! name of the variable
2801
+ character (kind= CK,len=* ),intent (in ) :: val ! ! value
2818
2802
2819
2803
type (json_value),pointer :: var
2820
2804
character (kind= CK,len= :),allocatable :: str
@@ -2844,8 +2828,8 @@ subroutine wrap_json_value_add_string(me, name, val)
2844
2828
implicit none
2845
2829
2846
2830
type (json_value),pointer :: me
2847
- character (kind= CDK,len=* ),intent (in ) :: name
2848
- character (kind= CDK,len=* ),intent (in ) :: val
2831
+ character (kind= CDK,len=* ),intent (in ) :: name ! ! name of the variable
2832
+ character (kind= CDK,len=* ),intent (in ) :: val ! ! value
2849
2833
2850
2834
call json_value_add_string(me, to_unicode(name), to_unicode(val))
2851
2835
@@ -2861,8 +2845,8 @@ subroutine json_value_add_string_name_ascii(me, name, val)
2861
2845
implicit none
2862
2846
2863
2847
type (json_value),pointer :: me
2864
- character (kind= CDK,len=* ),intent (in ) :: name
2865
- character (kind= CK, len=* ),intent (in ) :: val
2848
+ character (kind= CDK,len=* ),intent (in ) :: name ! ! name of the variable
2849
+ character (kind= CK, len=* ),intent (in ) :: val ! ! value
2866
2850
2867
2851
call json_value_add_string(me, to_unicode(name), val)
2868
2852
@@ -2878,8 +2862,8 @@ subroutine json_value_add_string_val_ascii(me, name, val)
2878
2862
implicit none
2879
2863
2880
2864
type (json_value),pointer :: me
2881
- character (kind= CK, len=* ),intent (in ) :: name
2882
- character (kind= CDK,len=* ),intent (in ) :: val
2865
+ character (kind= CK, len=* ),intent (in ) :: name ! ! name of the variable
2866
+ character (kind= CDK,len=* ),intent (in ) :: val ! ! value
2883
2867
2884
2868
call json_value_add_string(me, name, to_unicode(val))
2885
2869
@@ -5999,35 +5983,33 @@ end subroutine parse_array
5999
5983
!
6000
5984
! # History
6001
5985
! * Jacob Williams : 6/16/2014 : Added hex validation.
6002
- !
6003
5986
6004
5987
subroutine parse_string (unit , str , string )
6005
5988
6006
5989
implicit none
6007
5990
6008
- integer (IK), intent (in ) :: unit ! ! file unit number (if parsing from a file)
6009
- character (kind= CK,len=* ),intent (in ) :: str ! ! JSON string (if parsing from a string)
6010
- character (kind= CK,len= :),allocatable ,intent (out ) :: string
5991
+ integer (IK),intent (in ) :: unit ! ! file unit number (if parsing from a file)
5992
+ character (kind= CK,len=* ),intent (in ) :: str ! ! JSON string (if parsing from a string)
5993
+ character (kind= CK,len= :),allocatable ,intent (out ) :: string
6011
5994
6012
5995
logical (LK) :: eof, is_hex, escape
6013
5996
character (kind= CK,len= 1 ) :: c, last
6014
5997
character (kind= CK,len= 4 ) :: hex
6015
5998
integer (IK) :: i
6016
-
6017
- ! to speed up by reducing the number of character string reallocations:
6018
- integer (IK) :: ip ! index to put next character
5999
+ integer (IK) :: ip ! ! index to put next character,
6000
+ ! ! to speed up by reducing the number of character string reallocations.
6019
6001
6020
6002
! at least return a blank string if there is a problem:
6021
6003
string = repeat (space, chunk_size)
6022
6004
6023
6005
if (.not. exception_thrown) then
6024
6006
6025
6007
! initialize:
6026
- ip = 1
6027
- last = space
6008
+ ip = 1
6009
+ last = space
6028
6010
is_hex = .false.
6029
6011
escape = .false.
6030
- i = 0
6012
+ i = 0
6031
6013
6032
6014
do
6033
6015
@@ -6113,9 +6095,9 @@ subroutine parse_for_chars(unit, str, chars)
6113
6095
6114
6096
implicit none
6115
6097
6116
- integer (IK), intent (in ) :: unit ! ! file unit number (if parsing from a file)
6117
- character (kind= CK,len=* ),intent (in ) :: str ! ! JSON string (if parsing from a string)
6118
- character (kind= CK,len=* ), intent (in ) :: chars
6098
+ integer (IK),intent (in ) :: unit ! ! file unit number (if parsing from a file)
6099
+ character (kind= CK,len=* ),intent (in ) :: str ! ! JSON string (if parsing from a string)
6100
+ character (kind= CK,len=* ),intent (in ) :: chars ! ! the string to check for.
6119
6101
6120
6102
integer (IK) :: i, length
6121
6103
logical (LK) :: eof
@@ -6371,7 +6353,6 @@ end function pop_char
6371
6353
!
6372
6354
! # History
6373
6355
! * Jacob Williams : 5/3/2015 : replaced original version of this routine.
6374
- !
6375
6356
6376
6357
subroutine push_char (c )
6377
6358
@@ -6482,7 +6463,7 @@ subroutine compact_real_string(str)
6482
6463
6483
6464
implicit none
6484
6465
6485
- character (kind= CK,len=* ),intent (inout ) :: str
6466
+ character (kind= CK,len=* ),intent (inout ) :: str ! ! string representation of a real number.
6486
6467
6487
6468
character (kind= CK,len= len (str)) :: significand, expnt
6488
6469
character (kind= CK,len= 2 ) :: separator
0 commit comments