@@ -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,43 @@ 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 (var_type== json_object .or. &
4937
+ var_type== json_array .or. &
4938
+ (i> 1 .and. var_type/= var_type_prev)) then
4939
+ is_vector = .false.
4940
+ exit
4941
+ end if
4942
+ var_type_prev = var_type
4943
+ ! get the next child the list:
4944
+ element = > element% next
4945
+ end do
4946
+ else
4947
+ is_vector = .false.
4948
+ end if
4949
+
4893
4950
if (count== 0 ) then ! special case for empty array
4894
4951
4895
4952
call write_it( s// start_array// end_array, comma= print_comma )
4896
4953
4897
4954
else
4898
4955
4899
- call write_it( s// start_array )
4956
+ call write_it( s// start_array, advance = ( .not. is_vector) )
4900
4957
4901
4958
! if an array is in an array, there is an extra tab:
4902
4959
if (is_array) then
@@ -4915,30 +4972,44 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4915
4972
end if
4916
4973
4917
4974
! 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
-
4975
+ if (is_vector) then
4976
+ call json% json_value_print(element, iunit= iunit, indent= 0 ,&
4977
+ need_comma= i< count, is_array_element= .false. , str= str,&
4978
+ is_compressed_vector = .true. )
4979
+ else
4980
+ call json% json_value_print(element, iunit= iunit, indent= tab,&
4981
+ need_comma= i< count, is_array_element= .true. , str= str)
4982
+ end if
4921
4983
! get the next child the list:
4922
4984
element = > element% next
4923
4985
4924
4986
end do
4925
4987
4926
4988
! indent the closing array character:
4927
- call write_it( repeat (space, max (0 ,spaces- json% spaces_per_tab))// end_array,&
4928
- comma= print_comma )
4989
+ if (is_vector) then
4990
+ call write_it( end_array,comma= print_comma )
4991
+ else
4992
+ call write_it( repeat (space, max (0 ,spaces- json% spaces_per_tab))// end_array,&
4993
+ comma= print_comma )
4994
+ end if
4929
4995
nullify(element)
4930
4996
4931
4997
end if
4932
4998
4933
4999
case (json_null)
4934
5000
4935
- call write_it( s// null_str, comma= print_comma )
5001
+ call write_it( s// null_str, comma= print_comma, &
5002
+ advance= (.not. is_vector),&
5003
+ space_after_comma= is_vector )
4936
5004
4937
5005
case (json_string)
4938
5006
4939
5007
if (allocated (p% str_value)) then
4940
5008
call write_it( s// quotation_mark// &
4941
- p% str_value// quotation_mark, comma= print_comma )
5009
+ p% str_value// quotation_mark, &
5010
+ comma= print_comma, &
5011
+ advance= (.not. is_vector),&
5012
+ space_after_comma= is_vector )
4942
5013
else
4943
5014
call json% throw_exception(' Error in json_value_print:' // &
4944
5015
' p%value_string not allocated' )
@@ -4948,16 +5019,22 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4948
5019
case (json_logical)
4949
5020
4950
5021
if (p% log_value) then
4951
- call write_it( s// true_str, comma= print_comma )
5022
+ call write_it( s// true_str, comma= print_comma, &
5023
+ advance= (.not. is_vector),&
5024
+ space_after_comma= is_vector )
4952
5025
else
4953
- call write_it( s// false_str, comma= print_comma )
5026
+ call write_it( s// false_str, comma= print_comma, &
5027
+ advance= (.not. is_vector),&
5028
+ space_after_comma= is_vector )
4954
5029
end if
4955
5030
4956
5031
case (json_integer)
4957
5032
4958
5033
call integer_to_string(p% int_value,int_fmt,tmp)
4959
5034
4960
- call write_it( s// trim (tmp), comma= print_comma )
5035
+ call write_it( s// trim (tmp), comma= print_comma, &
5036
+ advance= (.not. is_vector),&
5037
+ space_after_comma= is_vector )
4961
5038
4962
5039
case (json_double)
4963
5040
@@ -4968,7 +5045,9 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4968
5045
call real_to_string(p% dbl_value,default_real_fmt,json% compact_real,tmp)
4969
5046
end if
4970
5047
4971
- call write_it( s// trim (tmp), comma= print_comma )
5048
+ call write_it( s// trim (tmp), comma= print_comma, &
5049
+ advance= (.not. is_vector),&
5050
+ space_after_comma= is_vector )
4972
5051
4973
5052
case default
4974
5053
@@ -4983,26 +5062,36 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
4983
5062
4984
5063
contains
4985
5064
4986
- subroutine write_it (s ,advance ,comma )
5065
+ subroutine write_it (s ,advance ,comma , space_after_comma )
4987
5066
4988
5067
! ! write the string to the file (or the output string)
4989
5068
4990
5069
implicit none
4991
5070
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
5071
+ character (kind= CK,len=* ),intent (in ) :: s ! ! string to print
5072
+ logical (LK),intent (in ),optional :: advance ! ! to add line break or not
5073
+ logical (LK),intent (in ),optional :: comma ! ! print comma after the string
5074
+ logical (LK),intent (in ),optional :: space_after_comma ! ! print a space after the comma
4995
5075
4996
5076
logical (LK) :: add_comma ! ! if a delimiter is to be added after string
4997
5077
logical (LK) :: add_line_break ! ! if a line break is to be added after string
5078
+ logical (LK) :: add_space ! ! if a space is to be added after the comma
4998
5079
character (kind= CK,len= :),allocatable :: s2 ! ! temporary string
4999
5080
5000
5081
if (present (comma)) then
5001
5082
add_comma = comma
5002
5083
else
5003
5084
add_comma = .false. ! default is not to add comma
5004
5085
end if
5005
-
5086
+ if (json% no_whitespace) then
5087
+ add_space = .false.
5088
+ else
5089
+ if (present (space_after_comma)) then
5090
+ add_space = space_after_comma
5091
+ else
5092
+ add_space = .false. ! default is not to add space
5093
+ end if
5094
+ end if
5006
5095
if (present (advance)) then
5007
5096
add_line_break = advance
5008
5097
else
@@ -5012,7 +5101,10 @@ subroutine write_it(s,advance,comma)
5012
5101
5013
5102
! string to print:
5014
5103
s2 = s
5015
- if (add_comma) s2 = s2 // delimiter
5104
+ if (add_comma) then
5105
+ s2 = s2 // delimiter
5106
+ if (add_space) s2 = s2 // space
5107
+ end if
5016
5108
5017
5109
if (write_file) then
5018
5110
0 commit comments