@@ -115,11 +115,12 @@ module json_value_module
115
115
type (json_value),pointer :: children = > null () ! ! first child item of this
116
116
type (json_value),pointer :: tail = > null () ! ! last child item of this
117
117
118
- character (kind= CK,len= :),allocatable :: name ! ! variable name
118
+ character (kind= CK,len= :),allocatable :: name ! ! variable name (unescaped)
119
119
120
120
real (RK),allocatable :: dbl_value ! ! real data for this variable
121
121
logical (LK),allocatable :: log_value ! ! logical data for this variable
122
122
character (kind= CK,len= :),allocatable :: str_value ! ! string data for this variable
123
+ ! ! (unescaped)
123
124
integer (IK),allocatable :: int_value ! ! integer data for this variable
124
125
125
126
integer (IK) :: var_type = json_unknown ! ! variable type
@@ -287,13 +288,13 @@ module json_value_module
287
288
! thrown if the existing variable is not a scalar).
288
289
!
289
290
! ### See also
290
- ! * [[add_by_path]] - this one can be used to change
291
+ ! * [[json_core(type): add_by_path]] - this one can be used to change
291
292
! arrays and objects to scalars if so desired.
292
293
!
293
294
! @note Unlike some routines, the `found` output is not optional,
294
295
! so it doesn't present exceptions from being thrown.
295
296
!
296
- ! @note These have been mostly supplanted by the [[add_by_path]]
297
+ ! @note These have been mostly supplanted by the [[json_core(type): add_by_path]]
297
298
! methods, which do a similar thing (and can be used for
298
299
! scalars and vectors, etc.)
299
300
generic,public :: update = > MAYBEWRAP(json_update_logical),&
@@ -378,7 +379,7 @@ module json_value_module
378
379
! (This will create a `null` variable)
379
380
!
380
381
! ### See also
381
- ! * [[add_by_path]]
382
+ ! * [[json_core(type): add_by_path]]
382
383
383
384
generic,public :: create = > MAYBEWRAP(json_create_by_path)
384
385
procedure :: MAYBEWRAP(json_create_by_path)
@@ -641,9 +642,12 @@ module json_value_module
641
642
generic,public :: get_path = > MAYBEWRAP(json_get_path)
642
643
procedure :: MAYBEWRAP(json_get_path)
643
644
644
- procedure ,public :: remove = > json_value_remove ! ! Remove a [[json_value]] from a linked-list structure.
645
- procedure ,public :: replace = > json_value_replace ! ! Replace a [[json_value]] in a linked-list structure.
646
- procedure ,public :: reverse = > json_value_reverse ! ! Reverse the order of the children of an array of object.
645
+ procedure ,public :: remove = > json_value_remove ! ! Remove a [[json_value]] from a
646
+ ! ! linked-list structure.
647
+ procedure ,public :: replace = > json_value_replace ! ! Replace a [[json_value]] in a
648
+ ! ! linked-list structure.
649
+ procedure ,public :: reverse = > json_value_reverse ! ! Reverse the order of the children
650
+ ! ! of an array of object.
647
651
procedure ,public :: check_for_errors = > json_check_for_errors ! ! check for error and get error message
648
652
procedure ,public :: clear_exceptions = > json_clear_exceptions ! ! clear exceptions
649
653
procedure ,public :: count = > json_count ! ! count the number of children
@@ -654,14 +658,19 @@ module json_value_module
654
658
procedure ,public :: get_previous = > json_get_previous ! ! get pointer to json_value previous
655
659
procedure ,public :: get_tail = > json_get_tail ! ! get pointer to json_value tail
656
660
procedure ,public :: initialize = > json_initialize ! ! to initialize some parsing parameters
657
- procedure ,public :: traverse = > json_traverse ! ! to traverse all elements of a JSON structure
658
- procedure ,public :: print_error_message = > json_print_error_message ! ! simply routine to print error messages
661
+ procedure ,public :: traverse = > json_traverse ! ! to traverse all elements of a JSON
662
+ ! ! structure
663
+ procedure ,public :: print_error_message = > json_print_error_message ! ! simply routine to print error
664
+ ! ! messages
659
665
procedure ,public :: swap = > json_value_swap ! ! Swap two [[json_value]] pointers
660
- ! ! in a structure (or two different structures).
661
- procedure ,public :: is_child_of = > json_value_is_child_of ! ! Check if a [[json_value]] is a descendant of another.
662
- procedure ,public :: validate = > json_value_validate ! ! Check that a [[json_value]] linked list is valid
663
- ! ! (i.e., is properly constructed). This may be
664
- ! ! useful if it has been constructed externally.
666
+ ! ! in a structure (or two different
667
+ ! ! structures).
668
+ procedure ,public :: is_child_of = > json_value_is_child_of ! ! Check if a [[json_value]] is a
669
+ ! ! descendant of another.
670
+ procedure ,public :: validate = > json_value_validate ! ! Check that a [[json_value]] linked
671
+ ! ! list is valid (i.e., is properly
672
+ ! ! constructed). This may be useful
673
+ ! ! if it has been constructed externally.
665
674
666
675
! other private routines:
667
676
procedure :: name_equal
@@ -4223,13 +4232,9 @@ subroutine json_value_add_string(json, p, name, val)
4223
4232
character (kind= CK,len=* ),intent (in ) :: val ! ! value
4224
4233
4225
4234
type (json_value),pointer :: var
4226
- character (kind= CK,len= :),allocatable :: str
4227
-
4228
- ! add escape characters if necessary:
4229
- call escape_string(val, str)
4230
4235
4231
4236
! create the variable:
4232
- call json% create_string(var,str ,name)
4237
+ call json% create_string(var,val ,name)
4233
4238
4234
4239
! add it:
4235
4240
call json% add(p, var)
@@ -4855,6 +4860,8 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4855
4860
integer (IK) :: var_type,var_type_prev
4856
4861
logical (LK) :: is_vector ! ! if all elements of a vector
4857
4862
! ! are scalars of the same type
4863
+ character (kind= CK,len= :),allocatable :: str_escaped ! ! escaped version of
4864
+ ! ! `name` or `str_value`
4858
4865
4859
4866
if (.not. json% exception_thrown) then
4860
4867
@@ -4931,19 +4938,20 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4931
4938
4932
4939
! print the name
4933
4940
if (allocated (element% name)) then
4941
+ call escape_string(element% name,str_escaped)
4934
4942
if (json% no_whitespace) then
4935
4943
! compact printing - no extra space
4936
4944
call write_it(repeat (space, spaces)// quotation_mark// &
4937
- element % name // quotation_mark// colon_char,&
4945
+ str_escaped // quotation_mark// colon_char,&
4938
4946
advance= .false. )
4939
4947
else
4940
4948
call write_it(repeat (space, spaces)// quotation_mark// &
4941
- element % name // quotation_mark// colon_char// space,&
4949
+ str_escaped // quotation_mark// colon_char// space,&
4942
4950
advance= .false. )
4943
4951
end if
4944
4952
else
4945
4953
call json% throw_exception(' Error in json_value_print:' // &
4946
- ' element%name not allocated' )
4954
+ ' element%name not allocated' )
4947
4955
nullify(element)
4948
4956
return
4949
4957
end if
@@ -5056,8 +5064,10 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
5056
5064
case (json_string)
5057
5065
5058
5066
if (allocated (p% str_value)) then
5067
+ ! have to escape the string for printing:
5068
+ call escape_string(p% str_value,str_escaped)
5059
5069
call write_it( s// quotation_mark// &
5060
- p % str_value // quotation_mark, &
5070
+ str_escaped // quotation_mark, &
5061
5071
comma= print_comma, &
5062
5072
advance= (.not. is_vector),&
5063
5073
space_after_comma= is_vector )
@@ -5144,7 +5154,12 @@ subroutine write_it(s,advance,comma,space_after_comma)
5144
5154
end if
5145
5155
end if
5146
5156
if (present (advance)) then
5147
- add_line_break = advance
5157
+ if (json% no_whitespace) then
5158
+ ! overrides input value:
5159
+ add_line_break = .false.
5160
+ else
5161
+ add_line_break = advance
5162
+ end if
5148
5163
else
5149
5164
add_line_break = .not. json% no_whitespace ! default is to advance if
5150
5165
! we are printing whitespace
@@ -6823,23 +6838,18 @@ subroutine json_get_string(json, me, value)
6823
6838
type (json_value),pointer ,intent (in ) :: me
6824
6839
character (kind= CK,len= :),allocatable ,intent (out ) :: value
6825
6840
6826
- character (kind= CK,len= :),allocatable :: error_message ! ! for [[unescape_string]]
6827
-
6828
6841
value = CK_' '
6829
6842
if (.not. json% exception_thrown) then
6830
6843
6831
6844
if (me% var_type == json_string) then
6832
6845
6833
6846
if (allocated (me% str_value)) then
6834
6847
if (json% unescaped_strings) then
6835
- call unescape_string(me% str_value, value, error_message)
6836
- if (allocated (error_message)) then
6837
- call json% throw_exception(error_message)
6838
- deallocate (error_message)
6839
- value = CK_' '
6840
- end if
6841
- else
6848
+ ! default: it is stored already unescaped:
6842
6849
value = me% str_value
6850
+ else
6851
+ ! return the escaped version:
6852
+ call escape_string(me% str_value, value)
6843
6853
end if
6844
6854
else
6845
6855
call json% throw_exception(' Error in json_get_string: ' // &
@@ -7824,11 +7834,13 @@ recursive subroutine parse_value(json, unit, str, value)
7824
7834
select case (value% var_type)
7825
7835
case (json_string)
7826
7836
#if defined __GFORTRAN__
7827
- call json% parse_string(unit,str,tmp) ! write to a tmp variable because of
7828
- value% str_value = tmp ! a bug in 4.9 gfortran compiler.
7829
- deallocate (tmp) !
7837
+ ! write to a tmp variable because of
7838
+ ! a bug in 4.9 gfortran compiler.
7839
+ call json% parse_string(unit,str,tmp)
7840
+ value% str_value = tmp
7841
+ if (allocated (tmp)) deallocate (tmp)
7830
7842
#else
7831
- call json% parse_string(unit, str, value% str_value)
7843
+ call json% parse_string(unit,str,value% str_value)
7832
7844
#endif
7833
7845
end select
7834
7846
@@ -8220,7 +8232,8 @@ subroutine to_logical(p,val,name)
8220
8232
implicit none
8221
8233
8222
8234
type (json_value),intent (inout ) :: p
8223
- logical (LK),intent (in ),optional :: val ! ! if the value is also to be set (if not present, then .false. is used).
8235
+ logical (LK),intent (in ),optional :: val ! ! if the value is also to be set
8236
+ ! ! (if not present, then .false. is used).
8224
8237
character (kind= CK,len=* ),intent (in ),optional :: name ! ! if the name is also to be changed.
8225
8238
8226
8239
! set type and value:
@@ -8249,7 +8262,8 @@ subroutine to_integer(p,val,name)
8249
8262
implicit none
8250
8263
8251
8264
type (json_value),intent (inout ) :: p
8252
- integer (IK),intent (in ),optional :: val ! ! if the value is also to be set (if not present, then 0 is used).
8265
+ integer (IK),intent (in ),optional :: val ! ! if the value is also to be set
8266
+ ! ! (if not present, then 0 is used).
8253
8267
character (kind= CK,len=* ),intent (in ),optional :: name ! ! if the name is also to be changed.
8254
8268
8255
8269
! set type and value:
@@ -8278,7 +8292,8 @@ subroutine to_double(p,val,name)
8278
8292
implicit none
8279
8293
8280
8294
type (json_value),intent (inout ) :: p
8281
- real (RK),intent (in ),optional :: val ! ! if the value is also to be set (if not present, then 0.0_rk is used).
8295
+ real (RK),intent (in ),optional :: val ! ! if the value is also to be set
8296
+ ! ! (if not present, then 0.0_rk is used).
8282
8297
character (kind= CK,len=* ),intent (in ),optional :: name ! ! if the name is also to be changed.
8283
8298
8284
8299
! set type and value:
@@ -8564,6 +8579,7 @@ end subroutine parse_array
8564
8579
! ### History
8565
8580
! * Jacob Williams : 6/16/2014 : Added hex validation.
8566
8581
! * Jacob Williams : 12/3/2015 : Fixed some bugs.
8582
+ ! * Jacob Williams : 8/23/2015 : `string` is now returned unescaped.
8567
8583
8568
8584
subroutine parse_string (json , unit , str , string )
8569
8585
@@ -8572,14 +8588,16 @@ subroutine parse_string(json, unit, str, string)
8572
8588
class(json_core),intent (inout ) :: json
8573
8589
integer (IK),intent (in ) :: unit ! ! file unit number (if parsing from a file)
8574
8590
character (kind= CK,len=* ),intent (in ) :: str ! ! JSON string (if parsing from a string)
8575
- character (kind= CK,len= :),allocatable ,intent (out ) :: string
8591
+ character (kind= CK,len= :),allocatable ,intent (out ) :: string ! ! the string (unescaped if necessary)
8576
8592
8577
8593
logical (LK) :: eof, is_hex, escape
8578
8594
character (kind= CK,len= 1 ) :: c
8579
8595
character (kind= CK,len= 4 ) :: hex
8580
8596
integer (IK) :: i
8581
8597
integer (IK) :: ip ! ! index to put next character,
8582
8598
! ! to speed up by reducing the number of character string reallocations.
8599
+ character (kind= CK,len= :),allocatable :: string_unescaped ! ! temp variable
8600
+ character (kind= CK,len= :),allocatable :: error_message ! ! for string unescaping
8583
8601
8584
8602
! at least return a blank string if there is a problem:
8585
8603
string = repeat (space, chunk_size)
@@ -8660,6 +8678,18 @@ subroutine parse_string(json, unit, str, string)
8660
8678
end if
8661
8679
end if
8662
8680
8681
+ ! string is returned unescaped:
8682
+ call unescape_string(string,string_unescaped,error_message)
8683
+ if (allocated (error_message)) then
8684
+ call json% throw_exception(error_message)
8685
+ else
8686
+ string = string_unescaped
8687
+ end if
8688
+
8689
+ ! cleanup:
8690
+ if (allocated (error_message)) deallocate (error_message)
8691
+ if (allocated (string_unescaped)) deallocate (string_unescaped)
8692
+
8663
8693
end if
8664
8694
8665
8695
end subroutine parse_string
0 commit comments