@@ -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
@@ -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 )
@@ -5148,7 +5158,7 @@ subroutine write_it(s,advance,comma,space_after_comma)
5148
5158
! overrides input value:
5149
5159
add_line_break = .false.
5150
5160
else
5151
- add_line_break = advance
5161
+ add_line_break = advance
5152
5162
end if
5153
5163
else
5154
5164
add_line_break = .not. json% no_whitespace ! default is to advance if
@@ -6828,23 +6838,18 @@ subroutine json_get_string(json, me, value)
6828
6838
type (json_value),pointer ,intent (in ) :: me
6829
6839
character (kind= CK,len= :),allocatable ,intent (out ) :: value
6830
6840
6831
- character (kind= CK,len= :),allocatable :: error_message ! ! for [[unescape_string]]
6832
-
6833
6841
value = CK_' '
6834
6842
if (.not. json% exception_thrown) then
6835
6843
6836
6844
if (me% var_type == json_string) then
6837
6845
6838
6846
if (allocated (me% str_value)) then
6839
6847
if (json% unescaped_strings) then
6840
- call unescape_string(me% str_value, value, error_message)
6841
- if (allocated (error_message)) then
6842
- call json% throw_exception(error_message)
6843
- deallocate (error_message)
6844
- value = CK_' '
6845
- end if
6846
- else
6848
+ ! default: it is stored already unescaped:
6847
6849
value = me% str_value
6850
+ else
6851
+ ! return the escaped version:
6852
+ call escape_string(me% str_value, value)
6848
6853
end if
6849
6854
else
6850
6855
call json% throw_exception(' Error in json_get_string: ' // &
@@ -7829,11 +7834,13 @@ recursive subroutine parse_value(json, unit, str, value)
7829
7834
select case (value% var_type)
7830
7835
case (json_string)
7831
7836
#if defined __GFORTRAN__
7832
- call json% parse_string(unit,str,tmp) ! write to a tmp variable because of
7833
- value% str_value = tmp ! a bug in 4.9 gfortran compiler.
7834
- 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)
7835
7842
#else
7836
- call json% parse_string(unit, str, value% str_value)
7843
+ call json% parse_string(unit,tmp, value% str_value)
7837
7844
#endif
7838
7845
end select
7839
7846
@@ -8225,7 +8232,8 @@ subroutine to_logical(p,val,name)
8225
8232
implicit none
8226
8233
8227
8234
type (json_value),intent (inout ) :: p
8228
- 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).
8229
8237
character (kind= CK,len=* ),intent (in ),optional :: name ! ! if the name is also to be changed.
8230
8238
8231
8239
! set type and value:
@@ -8254,7 +8262,8 @@ subroutine to_integer(p,val,name)
8254
8262
implicit none
8255
8263
8256
8264
type (json_value),intent (inout ) :: p
8257
- 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).
8258
8267
character (kind= CK,len=* ),intent (in ),optional :: name ! ! if the name is also to be changed.
8259
8268
8260
8269
! set type and value:
@@ -8283,7 +8292,8 @@ subroutine to_double(p,val,name)
8283
8292
implicit none
8284
8293
8285
8294
type (json_value),intent (inout ) :: p
8286
- 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).
8287
8297
character (kind= CK,len=* ),intent (in ),optional :: name ! ! if the name is also to be changed.
8288
8298
8289
8299
! set type and value:
@@ -8577,14 +8587,16 @@ subroutine parse_string(json, unit, str, string)
8577
8587
class(json_core),intent (inout ) :: json
8578
8588
integer (IK),intent (in ) :: unit ! ! file unit number (if parsing from a file)
8579
8589
character (kind= CK,len=* ),intent (in ) :: str ! ! JSON string (if parsing from a string)
8580
- character (kind= CK,len= :),allocatable ,intent (out ) :: string
8590
+ character (kind= CK,len= :),allocatable ,intent (out ) :: string ! ! the string (unescaped if necessary)
8581
8591
8582
8592
logical (LK) :: eof, is_hex, escape
8583
8593
character (kind= CK,len= 1 ) :: c
8584
8594
character (kind= CK,len= 4 ) :: hex
8585
8595
integer (IK) :: i
8586
8596
integer (IK) :: ip ! ! index to put next character,
8587
8597
! ! to speed up by reducing the number of character string reallocations.
8598
+ character (kind= CK,len= :),allocatable :: string_unescaped ! ! temp variable
8599
+ character (kind= CK,len= :),allocatable :: error_message ! ! for string unescaping
8588
8600
8589
8601
! at least return a blank string if there is a problem:
8590
8602
string = repeat (space, chunk_size)
@@ -8665,6 +8677,18 @@ subroutine parse_string(json, unit, str, string)
8665
8677
end if
8666
8678
end if
8667
8679
8680
+ ! string is returned unescaped:
8681
+ call unescape_string(string,string_unescaped,error_message)
8682
+ if (allocated (error_message)) then
8683
+ call json% throw_exception(error_message)
8684
+ else
8685
+ string = string_unescaped
8686
+ end if
8687
+
8688
+ ! cleanup:
8689
+ if (allocated (error_message)) deallocate (error_message)
8690
+ if (allocated (string_unescaped)) deallocate (string_unescaped)
8691
+
8668
8692
end if
8669
8693
8670
8694
end subroutine parse_string
0 commit comments