@@ -222,6 +222,12 @@ module json_value_module
222
222
! ! Note: if `path_mode/=1`
223
223
! ! then this is ignored.
224
224
225
+ logical (LK) :: compress_vectors = .false. ! ! If true, then arrays of integers,
226
+ ! ! nulls, doubles, & logicals are
227
+ ! ! printed all on one line.
228
+ ! ! [Note: `no_whitespace` will
229
+ ! ! override this option if necessary]
230
+
225
231
contains
226
232
227
233
private
@@ -757,7 +763,8 @@ function initialize_json_core(verbose,compact_reals,&
757
763
unescape_strings ,&
758
764
comment_char ,&
759
765
path_mode ,&
760
- path_separator ) result(json_core_object)
766
+ path_separator ,&
767
+ compress_vectors ) result(json_core_object)
761
768
762
769
implicit none
763
770
@@ -773,7 +780,8 @@ function initialize_json_core(verbose,compact_reals,&
773
780
unescape_strings,&
774
781
comment_char,&
775
782
path_mode,&
776
- path_separator)
783
+ path_separator,&
784
+ compress_vectors)
777
785
778
786
end function initialize_json_core
779
787
! *****************************************************************************************
@@ -806,7 +814,8 @@ subroutine json_initialize(json,verbose,compact_reals,&
806
814
unescape_strings ,&
807
815
comment_char ,&
808
816
path_mode ,&
809
- path_separator )
817
+ path_separator ,&
818
+ compress_vectors )
810
819
811
820
implicit none
812
821
@@ -873,6 +882,11 @@ subroutine json_initialize(json,verbose,compact_reals,&
873
882
json% path_separator = path_separator
874
883
end if
875
884
885
+ ! printing vectors in compressed form:
886
+ if (present (compress_vectors)) then
887
+ json% compress_vectors = compress_vectors
888
+ end if
889
+
876
890
! Set the format for real numbers:
877
891
! [if not changing it, then it remains the same]
878
892
@@ -4759,7 +4773,8 @@ end subroutine json_print_2
4759
4773
! bug in v4.9 of the gfortran compiler.
4760
4774
4761
4775
recursive subroutine json_value_print (json ,p ,iunit ,str ,indent ,&
4762
- need_comma ,colon ,is_array_element )
4776
+ need_comma ,colon ,is_array_element ,&
4777
+ is_compressed_vector )
4763
4778
4764
4779
implicit none
4765
4780
@@ -4775,17 +4790,29 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4775
4790
! ! printed to this string rather than
4776
4791
! ! a file. This mode is used by
4777
4792
! ! [[json_value_to_string]].
4793
+ logical (LK),intent (in ),optional :: is_compressed_vector ! ! if True, this is an element
4794
+ ! ! from an array being printed
4795
+ ! ! on one line [default is False]
4778
4796
4779
- character (kind= CK,len= max_numeric_str_len) :: tmp ! for val to string conversions
4797
+ character (kind= CK,len= max_numeric_str_len) :: tmp ! ! for val to string conversions
4780
4798
character (kind= CK,len= :),allocatable :: s
4781
4799
type (json_value),pointer :: element
4782
4800
integer (IK) :: tab, i, count, spaces
4783
4801
logical (LK) :: print_comma
4784
4802
logical (LK) :: write_file, write_string
4785
4803
logical (LK) :: is_array
4804
+ integer (IK) :: var_type,var_type_prev
4805
+ logical (LK) :: is_vector ! ! if all elements of a vector
4806
+ ! ! are scalars of the same type
4786
4807
4787
4808
if (.not. json% exception_thrown) then
4788
4809
4810
+ if (present (is_compressed_vector)) then
4811
+ is_vector = is_compressed_vector
4812
+ else
4813
+ is_vector = .false.
4814
+ end if
4815
+
4789
4816
! whether to write a string or a file (one or the other):
4790
4817
write_string = (iunit== unit2str)
4791
4818
write_file = .not. write_string
@@ -4890,13 +4917,42 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4890
4917
4891
4918
count = json% count (p)
4892
4919
4920
+ if (json% compress_vectors) then
4921
+ ! check to see if every child is the same type,
4922
+ ! and a scalar:
4923
+ is_vector = .true.
4924
+ var_type_prev = - 1 ! an invalid value
4925
+ nullify(element)
4926
+ element = > p% children
4927
+ do i = 1 , count
4928
+ if (.not. associated (element)) then
4929
+ call json% throw_exception(' Error in json_value_print: ' // &
4930
+ ' Malformed JSON linked list' )
4931
+ return
4932
+ end if
4933
+ ! check variable type of all the children.
4934
+ ! They must all be the same, and a scalar.
4935
+ call json% info(element,var_type= var_type)
4936
+ if (i> 1 .and. (var_type/= var_type_prev .or. &
4937
+ any (var_type==[json_object,json_array]))) then
4938
+ is_vector = .false.
4939
+ exit
4940
+ end if
4941
+ var_type_prev = var_type
4942
+ ! get the next child the list:
4943
+ element = > element% next
4944
+ end do
4945
+ else
4946
+ is_vector = .false.
4947
+ end if
4948
+
4893
4949
if (count== 0 ) then ! special case for empty array
4894
4950
4895
4951
call write_it( s// start_array// end_array, comma= print_comma )
4896
4952
4897
4953
else
4898
4954
4899
- call write_it( s// start_array )
4955
+ call write_it( s// start_array, advance = ( .not. is_vector) )
4900
4956
4901
4957
! if an array is in an array, there is an extra tab:
4902
4958
if (is_array) then
@@ -4915,30 +4971,44 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4915
4971
end if
4916
4972
4917
4973
! recursive print of the element
4918
- call json% json_value_print(element, iunit= iunit, indent= tab,&
4919
- need_comma= i< count, is_array_element= .true. , str= str)
4920
-
4974
+ if (is_vector) then
4975
+ call json% json_value_print(element, iunit= iunit, indent= 0 ,&
4976
+ need_comma= i< count, is_array_element= .false. , str= str,&
4977
+ is_compressed_vector = .true. )
4978
+ else
4979
+ call json% json_value_print(element, iunit= iunit, indent= tab,&
4980
+ need_comma= i< count, is_array_element= .true. , str= str)
4981
+ end if
4921
4982
! get the next child the list:
4922
4983
element = > element% next
4923
4984
4924
4985
end do
4925
4986
4926
4987
! indent the closing array character:
4927
- call write_it( repeat (space, max (0 ,spaces- json% spaces_per_tab))// end_array,&
4928
- comma= print_comma )
4988
+ if (is_vector) then
4989
+ call write_it( end_array,comma= print_comma )
4990
+ else
4991
+ call write_it( repeat (space, max (0 ,spaces- json% spaces_per_tab))// end_array,&
4992
+ comma= print_comma )
4993
+ end if
4929
4994
nullify(element)
4930
4995
4931
4996
end if
4932
4997
4933
4998
case (json_null)
4934
4999
4935
- call write_it( s// null_str, comma= print_comma )
5000
+ call write_it( s// null_str, comma= print_comma, &
5001
+ advance= (.not. is_vector),&
5002
+ space_after_comma= is_vector )
4936
5003
4937
5004
case (json_string)
4938
5005
4939
5006
if (allocated (p% str_value)) then
4940
5007
call write_it( s// quotation_mark// &
4941
- p% str_value// quotation_mark, comma= print_comma )
5008
+ p% str_value// quotation_mark, &
5009
+ comma= print_comma, &
5010
+ advance= (.not. is_vector),&
5011
+ space_after_comma= is_vector )
4942
5012
else
4943
5013
call json% throw_exception(' Error in json_value_print:' // &
4944
5014
' p%value_string not allocated' )
@@ -4948,16 +5018,22 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4948
5018
case (json_logical)
4949
5019
4950
5020
if (p% log_value) then
4951
- call write_it( s// true_str, comma= print_comma )
5021
+ call write_it( s// true_str, comma= print_comma, &
5022
+ advance= (.not. is_vector),&
5023
+ space_after_comma= is_vector )
4952
5024
else
4953
- call write_it( s// false_str, comma= print_comma )
5025
+ call write_it( s// false_str, comma= print_comma, &
5026
+ advance= (.not. is_vector),&
5027
+ space_after_comma= is_vector )
4954
5028
end if
4955
5029
4956
5030
case (json_integer)
4957
5031
4958
5032
call integer_to_string(p% int_value,int_fmt,tmp)
4959
5033
4960
- call write_it( s// trim (tmp), comma= print_comma )
5034
+ call write_it( s// trim (tmp), comma= print_comma, &
5035
+ advance= (.not. is_vector),&
5036
+ space_after_comma= is_vector )
4961
5037
4962
5038
case (json_double)
4963
5039
@@ -4968,7 +5044,9 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4968
5044
call real_to_string(p% dbl_value,default_real_fmt,json% compact_real,tmp)
4969
5045
end if
4970
5046
4971
- call write_it( s// trim (tmp), comma= print_comma )
5047
+ call write_it( s// trim (tmp), comma= print_comma, &
5048
+ advance= (.not. is_vector),&
5049
+ space_after_comma= is_vector )
4972
5050
4973
5051
case default
4974
5052
@@ -4983,26 +5061,36 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4983
5061
4984
5062
contains
4985
5063
4986
- subroutine write_it (s ,advance ,comma )
5064
+ subroutine write_it (s ,advance ,comma , space_after_comma )
4987
5065
4988
5066
! ! write the string to the file (or the output string)
4989
5067
4990
5068
implicit none
4991
5069
4992
- character (kind= CK,len=* ),intent (in ) :: s ! ! string to print
4993
- logical (LK),intent (in ),optional :: advance ! ! to add line break or not
4994
- logical (LK),intent (in ),optional :: comma ! ! print comma after the string
5070
+ character (kind= CK,len=* ),intent (in ) :: s ! ! string to print
5071
+ logical (LK),intent (in ),optional :: advance ! ! to add line break or not
5072
+ logical (LK),intent (in ),optional :: comma ! ! print comma after the string
5073
+ logical (LK),intent (in ),optional :: space_after_comma ! ! print a space after the comma
4995
5074
4996
5075
logical (LK) :: add_comma ! ! if a delimiter is to be added after string
4997
5076
logical (LK) :: add_line_break ! ! if a line break is to be added after string
5077
+ logical (LK) :: add_space ! ! if a space is to be added after the comma
4998
5078
character (kind= CK,len= :),allocatable :: s2 ! ! temporary string
4999
5079
5000
5080
if (present (comma)) then
5001
5081
add_comma = comma
5002
5082
else
5003
5083
add_comma = .false. ! default is not to add comma
5004
5084
end if
5005
-
5085
+ if (json% no_whitespace) then
5086
+ add_space = .false.
5087
+ else
5088
+ if (present (space_after_comma)) then
5089
+ add_space = space_after_comma
5090
+ else
5091
+ add_space = .false. ! default is not to add space
5092
+ end if
5093
+ end if
5006
5094
if (present (advance)) then
5007
5095
add_line_break = advance
5008
5096
else
@@ -5012,7 +5100,10 @@ subroutine write_it(s,advance,comma)
5012
5100
5013
5101
! string to print:
5014
5102
s2 = s
5015
- if (add_comma) s2 = s2 // delimiter
5103
+ if (add_comma) then
5104
+ s2 = s2 // delimiter
5105
+ if (add_space) s2 = s2 // space
5106
+ end if
5016
5107
5017
5108
if (write_file) then
5018
5109
0 commit comments