@@ -213,6 +213,11 @@ module json_value_module
213
213
logical (LK) :: case_sensitive_keys = .true. ! ! for name and path comparisons, are they
214
214
! ! case sensitive.
215
215
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
+
216
221
contains
217
222
218
223
private
@@ -635,7 +640,8 @@ function initialize_json_core(verbose,compact_reals,&
635
640
print_signs ,real_format ,spaces_per_tab ,&
636
641
strict_type_checking ,&
637
642
trailing_spaces_significant ,&
638
- case_sensitive_keys ) result(json_core_object)
643
+ case_sensitive_keys ,&
644
+ no_whitespace ) result(json_core_object)
639
645
640
646
implicit none
641
647
@@ -650,14 +656,20 @@ function initialize_json_core(verbose,compact_reals,&
650
656
! ! (default is false)
651
657
logical (LK),intent (in ),optional :: trailing_spaces_significant ! ! for name and path comparisons, is trailing
652
658
! ! space to be considered significant.
659
+ ! ! (default is false)
653
660
logical (LK),intent (in ),optional :: case_sensitive_keys ! ! for name and path comparisons, are they
654
661
! ! 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)
655
666
656
667
call json_core_object% initialize(verbose,compact_reals,&
657
668
print_signs,real_format,spaces_per_tab,&
658
669
strict_type_checking,&
659
670
trailing_spaces_significant,&
660
- case_sensitive_keys)
671
+ case_sensitive_keys,&
672
+ no_whitespace)
661
673
662
674
end function initialize_json_core
663
675
! *****************************************************************************************
@@ -685,7 +697,8 @@ subroutine json_initialize(json,verbose,compact_reals,&
685
697
print_signs ,real_format ,spaces_per_tab ,&
686
698
strict_type_checking ,&
687
699
trailing_spaces_significant ,&
688
- case_sensitive_keys )
700
+ case_sensitive_keys ,&
701
+ no_whitespace )
689
702
690
703
implicit none
691
704
@@ -700,8 +713,12 @@ subroutine json_initialize(json,verbose,compact_reals,&
700
713
! ! (default is false)
701
714
logical (LK),intent (in ),optional :: trailing_spaces_significant ! ! for name and path comparisons, is trailing
702
715
! ! space to be considered significant.
716
+ ! ! (default is false)
703
717
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)
705
722
706
723
character (kind= CDK,len= 10 ) :: w,d,e
707
724
character (kind= CDK,len= 2 ) :: sgn, rl_edit_desc
@@ -735,6 +752,8 @@ subroutine json_initialize(json,verbose,compact_reals,&
735
752
json% trailing_spaces_significant = trailing_spaces_significant
736
753
if (present (case_sensitive_keys)) &
737
754
json% case_sensitive_keys = case_sensitive_keys
755
+ if (present (no_whitespace)) &
756
+ json% no_whitespace = no_whitespace
738
757
739
758
! Set the format for real numbers:
740
759
! [if not changing it, then it remains the same]
@@ -3347,7 +3366,8 @@ subroutine json_print_1(json,p,iunit)
3347
3366
3348
3367
class(json_core),intent (inout ) :: json
3349
3368
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).
3351
3371
3352
3372
character (kind= CK,len= :),allocatable :: dummy
3353
3373
@@ -3372,7 +3392,8 @@ subroutine json_print_2(json,p,filename)
3372
3392
3373
3393
class(json_core),intent (inout ) :: json
3374
3394
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)
3376
3397
3377
3398
integer (IK) :: iunit,istat
3378
3399
@@ -3438,7 +3459,7 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
3438
3459
end if
3439
3460
3440
3461
! number of "tabs" to indent:
3441
- if (present (indent)) then
3462
+ if (present (indent) .and. .not. json % no_whitespace ) then
3442
3463
tab = indent
3443
3464
else
3444
3465
tab = 0
@@ -3476,8 +3497,8 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
3476
3497
3477
3498
! if an object is in an array, there is an extra tab:
3478
3499
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
3481
3502
end if
3482
3503
3483
3504
nullify(element)
@@ -3492,9 +3513,16 @@ recursive subroutine json_value_print(json,p,iunit,str,indent,&
3492
3513
3493
3514
! print the name
3494
3515
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
3498
3526
else
3499
3527
call json% throw_exception(' Error in json_value_print:' // &
3500
3528
' element%name not allocated' )
@@ -3619,8 +3647,9 @@ subroutine write_it(s,advance,comma)
3619
3647
logical (LK),intent (in ),optional :: advance ! ! to add line break or not
3620
3648
logical (LK),intent (in ),optional :: comma ! ! print comma after the string
3621
3649
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
3624
3653
3625
3654
if (present (comma)) then
3626
3655
add_comma = comma
@@ -3631,7 +3660,8 @@ subroutine write_it(s,advance,comma)
3631
3660
if (present (advance)) then
3632
3661
add_line_break = advance
3633
3662
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
3635
3665
end if
3636
3666
3637
3667
! string to print:
@@ -3757,7 +3787,7 @@ subroutine json_get_by_path(json, me, path, p, found)
3757
3787
3758
3788
if (.not. associated (p)) then
3759
3789
call json% throw_exception(' Error in json_get_by_path:' // &
3760
- ' Error getting child member.' )
3790
+ ' Error getting child member.' )
3761
3791
exit
3762
3792
end if
3763
3793
@@ -3897,8 +3927,8 @@ function string_to_integer(json,str) result(ival)
3897
3927
if (ierr/= 0 ) then ! if there was an error
3898
3928
ival = 0
3899
3929
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))
3902
3932
end if
3903
3933
3904
3934
else
0 commit comments