@@ -196,6 +196,11 @@ module json_value_module
196
196
! ! If true, the entire structure will be
197
197
! ! printed on one line.
198
198
199
+ logical (LK) :: unescaped_strings = .true. ! ! If false, then the raw escaped
200
+ ! ! string is returned from [[json_get_string]]
201
+ ! ! and similar routines. If true [default],
202
+ ! ! then the string is returned unescaped.
203
+
199
204
contains
200
205
201
206
private
@@ -632,7 +637,8 @@ function initialize_json_core(verbose,compact_reals,&
632
637
strict_type_checking ,&
633
638
trailing_spaces_significant ,&
634
639
case_sensitive_keys ,&
635
- no_whitespace ) result(json_core_object)
640
+ no_whitespace ,&
641
+ unescape_strings ) result(json_core_object)
636
642
637
643
implicit none
638
644
@@ -654,13 +660,18 @@ function initialize_json_core(verbose,compact_reals,&
654
660
logical (LK),intent (in ),optional :: no_whitespace ! ! if true, printing the JSON structure is
655
661
! ! done without adding any non-significant
656
662
! ! spaces or linebreaks (default is false)
663
+ logical (LK),intent (in ),optional :: unescape_strings ! ! If false, then the raw escaped
664
+ ! ! string is returned from [[json_get_string]]
665
+ ! ! and similar routines. If true [default],
666
+ ! ! then the string is returned unescaped.
657
667
658
668
call json_core_object% initialize(verbose,compact_reals,&
659
669
print_signs,real_format,spaces_per_tab,&
660
670
strict_type_checking,&
661
671
trailing_spaces_significant,&
662
672
case_sensitive_keys,&
663
- no_whitespace)
673
+ no_whitespace,&
674
+ unescape_strings)
664
675
665
676
end function initialize_json_core
666
677
! *****************************************************************************************
@@ -689,7 +700,8 @@ subroutine json_initialize(json,verbose,compact_reals,&
689
700
strict_type_checking ,&
690
701
trailing_spaces_significant ,&
691
702
case_sensitive_keys ,&
692
- no_whitespace )
703
+ no_whitespace ,&
704
+ unescape_strings )
693
705
694
706
implicit none
695
707
@@ -710,6 +722,10 @@ subroutine json_initialize(json,verbose,compact_reals,&
710
722
logical (LK),intent (in ),optional :: no_whitespace ! ! if true, printing the JSON structure is
711
723
! ! done without adding any non-significant
712
724
! ! spaces or linebreaks (default is false)
725
+ logical (LK),intent (in ),optional :: unescape_strings ! ! If false, then the raw escaped
726
+ ! ! string is returned from [[json_get_string]]
727
+ ! ! and similar routines. If true [default],
728
+ ! ! then the string is returned unescaped.
713
729
714
730
character (kind= CDK,len= 10 ) :: w,d,e
715
731
character (kind= CDK,len= 2 ) :: sgn, rl_edit_desc
@@ -745,6 +761,8 @@ subroutine json_initialize(json,verbose,compact_reals,&
745
761
json% case_sensitive_keys = case_sensitive_keys
746
762
if (present (no_whitespace)) &
747
763
json% no_whitespace = no_whitespace
764
+ if (present (unescape_strings)) &
765
+ json% unescaped_strings = unescape_strings
748
766
749
767
! Set the format for real numbers:
750
768
! [if not changing it, then it remains the same]
@@ -5001,31 +5019,83 @@ subroutine json_get_string(json, me, value)
5001
5019
value = ' '
5002
5020
if (.not. json% exception_thrown) then
5003
5021
5004
- select case (me% var_type)
5005
-
5006
- case (json_string)
5022
+ if (me% var_type == json_string) then
5007
5023
5008
5024
if (allocated (me% str_value)) then
5009
- call unescape_string(me% str_value, value, error_message)
5010
- if (allocated (error_message)) then
5011
- call json% throw_exception(error_message)
5012
- deallocate (error_message)
5013
- value = ' '
5025
+ if (json% unescaped_strings) then
5026
+ call unescape_string(me% str_value, value, error_message)
5027
+ if (allocated (error_message)) then
5028
+ call json% throw_exception(error_message)
5029
+ deallocate (error_message)
5030
+ value = ' '
5031
+ end if
5032
+ else
5033
+ value = me% str_value
5014
5034
end if
5015
5035
else
5016
5036
call json% throw_exception(' Error in json_get_string: ' // &
5017
5037
' me%str_value not allocated' )
5018
5038
end if
5019
5039
5020
- case default
5040
+ else
5021
5041
5022
- call json% throw_exception(' Error in json_get_string: ' // &
5023
- ' Unable to resolve value to characters: ' // &
5024
- me% name)
5042
+ if (json% strict_type_checking) then
5043
+ call json% throw_exception(' Error in json_get_string:' // &
5044
+ ' Unable to resolve value to string: ' // me% name)
5045
+ else
5025
5046
5026
- ! Note: for the other cases, we could do val to string conversions.
5047
+ select case (me % var_type)
5027
5048
5028
- end select
5049
+ case (json_integer)
5050
+
5051
+ if (allocated (me% int_value)) then
5052
+ value = repeat (' ' , max_integer_str_len)
5053
+ call integer_to_string(me% int_value,int_fmt,value)
5054
+ value = trim (value)
5055
+ else
5056
+ call json% throw_exception(' Error in json_get_string: ' // &
5057
+ ' me%int_value not allocated' )
5058
+ end if
5059
+
5060
+ case (json_double)
5061
+
5062
+ if (allocated (me% dbl_value)) then
5063
+ value = repeat (' ' , max_numeric_str_len)
5064
+ call real_to_string(me% dbl_value,json% real_fmt,&
5065
+ json% compact_real,value)
5066
+ value = trim (value)
5067
+ else
5068
+ call json% throw_exception(' Error in dbl_value: ' // &
5069
+ ' me%int_value not allocated' )
5070
+ end if
5071
+
5072
+ case (json_logical)
5073
+
5074
+ if (allocated (me% log_value)) then
5075
+ if (me% log_value) then
5076
+ value = true_str
5077
+ else
5078
+ value = false_str
5079
+ end if
5080
+ else
5081
+ call json% throw_exception(' Error in json_get_string: ' // &
5082
+ ' me%log_value not allocated' )
5083
+ end if
5084
+
5085
+ case (json_null)
5086
+
5087
+ value = null_str
5088
+
5089
+ case default
5090
+
5091
+ call json% throw_exception(' Error in json_get_string: ' // &
5092
+ ' Unable to resolve value to characters: ' // &
5093
+ me% name)
5094
+
5095
+ end select
5096
+
5097
+ end if
5098
+ end if
5029
5099
5030
5100
end if
5031
5101
0 commit comments