@@ -213,6 +213,11 @@ module json_value_module
213213 logical (LK) :: case_sensitive_keys = .true. ! ! for name and path comparisons, are they
214214 ! ! case sensitive.
215215
216+ logical (LK) :: no_whitespace = .false. ! ! when printing a JSON string, don't include
217+ ! ! non-significant spaces or line breaks.
218+ ! ! If true, the entire structure will be
219+ ! ! printed on one line.
220+
216221 contains
217222
218223 private
@@ -635,7 +640,8 @@ function initialize_json_core(verbose,compact_reals,&
635640 print_signs ,real_format ,spaces_per_tab ,&
636641 strict_type_checking ,&
637642 trailing_spaces_significant ,&
638- case_sensitive_keys ) result(json_core_object)
643+ case_sensitive_keys ,&
644+ no_whitespace ) result(json_core_object)
639645
640646 implicit none
641647
@@ -650,14 +656,20 @@ function initialize_json_core(verbose,compact_reals,&
650656 ! ! (default is false)
651657 logical (LK),intent (in ),optional :: trailing_spaces_significant ! ! for name and path comparisons, is trailing
652658 ! ! space to be considered significant.
659+ ! ! (default is false)
653660 logical (LK),intent (in ),optional :: case_sensitive_keys ! ! for name and path comparisons, are they
654661 ! ! case sensitive.
662+ ! ! (default is true)
663+ logical (LK),intent (in ),optional :: no_whitespace ! ! if true, printing the JSON structure is
664+ ! ! done without adding any non-significant
665+ ! ! spaces or linebreaks (default is false)
655666
656667 call json_core_object% initialize(verbose,compact_reals,&
657668 print_signs,real_format,spaces_per_tab,&
658669 strict_type_checking,&
659670 trailing_spaces_significant,&
660- case_sensitive_keys)
671+ case_sensitive_keys,&
672+ no_whitespace)
661673
662674 end function initialize_json_core
663675! *****************************************************************************************
@@ -685,7 +697,8 @@ subroutine json_initialize(json,verbose,compact_reals,&
685697 print_signs ,real_format ,spaces_per_tab ,&
686698 strict_type_checking ,&
687699 trailing_spaces_significant ,&
688- case_sensitive_keys )
700+ case_sensitive_keys ,&
701+ no_whitespace )
689702
690703 implicit none
691704
@@ -700,8 +713,12 @@ subroutine json_initialize(json,verbose,compact_reals,&
700713 ! ! (default is false)
701714 logical (LK),intent (in ),optional :: trailing_spaces_significant ! ! for name and path comparisons, is trailing
702715 ! ! space to be considered significant.
716+ ! ! (default is false)
703717 logical (LK),intent (in ),optional :: case_sensitive_keys ! ! for name and path comparisons, are they
704- ! ! case sensitive.
718+ ! ! case sensitive. (default is true)
719+ logical (LK),intent (in ),optional :: no_whitespace ! ! if true, printing the JSON structure is
720+ ! ! done without adding any non-significant
721+ ! ! spaces or linebreaks (default is false)
705722
706723 character (kind= CDK,len= 10 ) :: w,d,e
707724 character (kind= CDK,len= 2 ) :: sgn, rl_edit_desc
@@ -735,6 +752,8 @@ subroutine json_initialize(json,verbose,compact_reals,&
735752 json% trailing_spaces_significant = trailing_spaces_significant
736753 if (present (case_sensitive_keys)) &
737754 json% case_sensitive_keys = case_sensitive_keys
755+ if (present (no_whitespace)) &
756+ json% no_whitespace = no_whitespace
738757
739758 ! Set the format for real numbers:
740759 ! [if not changing it, then it remains the same]
@@ -3347,7 +3366,8 @@ subroutine json_print_1(json,p,iunit)
33473366
33483367 class(json_core),intent (inout ) :: json
33493368 type (json_value),pointer ,intent (in ) :: p
3350- integer (IK),intent (in ) :: iunit ! ! the file unit (the file must already have been opened, can't be -1).
3369+ integer (IK),intent (in ) :: iunit ! ! the file unit (the file must
3370+ ! ! already have been opened, can't be -1).
33513371
33523372 character (kind= CK,len= :),allocatable :: dummy
33533373
@@ -3372,7 +3392,8 @@ subroutine json_print_2(json,p,filename)
33723392
33733393 class(json_core),intent (inout ) :: json
33743394 type (json_value),pointer ,intent (in ) :: p
3375- character (kind= CDK,len=* ),intent (in ) :: filename ! ! the filename to print to (should not already be open)
3395+ character (kind= CDK,len=* ),intent (in ) :: filename ! ! the filename to print to
3396+ ! ! (should not already be open)
33763397
33773398 integer (IK) :: iunit,istat
33783399
@@ -3438,7 +3459,7 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
34383459 end if
34393460
34403461 ! number of "tabs" to indent:
3441- if (present (indent)) then
3462+ if (present (indent) .and. .not. json % no_whitespace ) then
34423463 tab = indent
34433464 else
34443465 tab = 0
@@ -3476,8 +3497,8 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
34763497
34773498 ! if an object is in an array, there is an extra tab:
34783499 if (is_array) then
3479- tab = tab+1
3480- spaces = tab* json% spaces_per_tab
3500+ if ( .not. json % no_whitespace) tab = tab+1
3501+ spaces = tab* json% spaces_per_tab
34813502 end if
34823503
34833504 nullify(element)
@@ -3492,9 +3513,16 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
34923513
34933514 ! print the name
34943515 if (allocated (element% name)) then
3495- call write_it(repeat (space, spaces)// quotation_mark// &
3496- element% name// quotation_mark// colon_char// space,&
3497- advance= .false. )
3516+ if (json% no_whitespace) then
3517+ ! compact printing - no extra space
3518+ call write_it(repeat (space, spaces)// quotation_mark// &
3519+ element% name// quotation_mark// colon_char,&
3520+ advance= .false. )
3521+ else
3522+ call write_it(repeat (space, spaces)// quotation_mark// &
3523+ element% name// quotation_mark// colon_char// space,&
3524+ advance= .false. )
3525+ end if
34983526 else
34993527 call json% throw_exception(' Error in json_value_print:' // &
35003528 ' element%name not allocated' )
@@ -3619,8 +3647,9 @@ subroutine write_it(s,advance,comma)
36193647 logical (LK),intent (in ),optional :: advance ! ! to add line break or not
36203648 logical (LK),intent (in ),optional :: comma ! ! print comma after the string
36213649
3622- logical (LK) :: add_line_break, add_comma
3623- character (kind= CK,len= :),allocatable :: s2
3650+ logical (LK) :: add_comma ! ! if a delimiter is to be added after string
3651+ logical (LK) :: add_line_break ! ! if a line break is to be added after string
3652+ character (kind= CK,len= :),allocatable :: s2 ! ! temporary string
36243653
36253654 if (present (comma)) then
36263655 add_comma = comma
@@ -3631,7 +3660,8 @@ subroutine write_it(s,advance,comma)
36313660 if (present (advance)) then
36323661 add_line_break = advance
36333662 else
3634- add_line_break = .true. ! default is to advance
3663+ add_line_break = .not. json% no_whitespace ! default is to advance if
3664+ ! we are printing whitespace
36353665 end if
36363666
36373667 ! string to print:
@@ -3757,7 +3787,7 @@ subroutine json_get_by_path(json, me, path, p, found)
37573787
37583788 if (.not. associated (p)) then
37593789 call json% throw_exception(' Error in json_get_by_path:' // &
3760- ' Error getting child member.' )
3790+ ' Error getting child member.' )
37613791 exit
37623792 end if
37633793
@@ -3897,8 +3927,8 @@ function string_to_integer(json,str) result(ival)
38973927 if (ierr/= 0 ) then ! if there was an error
38983928 ival = 0
38993929 call json% throw_exception(' Error in string_to_integer: ' // &
3900- ' string cannot be converted to an integer: ' // &
3901- trim (str))
3930+ ' string cannot be converted to an integer: ' // &
3931+ trim (str))
39023932 end if
39033933
39043934 else
0 commit comments