Skip to content

Commit cddb8c9

Browse files
Merge pull request #217 from jacobwilliams/whitespace
Added new option to print JSON without extra whitespace
2 parents 55fd927 + b53a2c0 commit cddb8c9

File tree

3 files changed

+79
-28
lines changed

3 files changed

+79
-28
lines changed

src/json_file_module.F90

+18-6
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
271271
print_signs,real_format,spaces_per_tab,&
272272
strict_type_checking,&
273273
trailing_spaces_significant,&
274-
case_sensitive_keys)
274+
case_sensitive_keys,&
275+
no_whitespace)
275276

276277
implicit none
277278

@@ -286,14 +287,19 @@ subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
286287
!! (default is false)
287288
logical(LK),intent(in),optional :: trailing_spaces_significant !! for name and path comparisons, is trailing
288289
!! space to be considered significant.
290+
!! (default is false)
289291
logical(LK),intent(in),optional :: case_sensitive_keys !! for name and path comparisons, are they
290-
!! case sensitive.
292+
!! case sensitive. (default is true)
293+
logical(LK),intent(in),optional :: no_whitespace !! if true, printing the JSON structure is
294+
!! done without adding any non-significant
295+
!! spaces or linebreaks (default is false)
291296

292297
call me%core%initialize(verbose,compact_reals,&
293298
print_signs,real_format,spaces_per_tab,&
294299
strict_type_checking,&
295300
trailing_spaces_significant,&
296-
case_sensitive_keys)
301+
case_sensitive_keys,&
302+
no_whitespace)
297303

298304
end subroutine initialize_json_core_in_file
299305
!*****************************************************************************************
@@ -351,7 +357,8 @@ function initialize_json_file(p,verbose,compact_reals,&
351357
print_signs,real_format,spaces_per_tab,&
352358
strict_type_checking,&
353359
trailing_spaces_significant,&
354-
case_sensitive_keys) result(file_object)
360+
case_sensitive_keys,&
361+
no_whitespace) result(file_object)
355362

356363
implicit none
357364

@@ -368,14 +375,19 @@ function initialize_json_file(p,verbose,compact_reals,&
368375
!! (default is false)
369376
logical(LK),intent(in),optional :: trailing_spaces_significant !! for name and path comparisons, is trailing
370377
!! space to be considered significant.
378+
!! (default is false)
371379
logical(LK),intent(in),optional :: case_sensitive_keys !! for name and path comparisons, are they
372-
!! case sensitive.
380+
!! case sensitive. (default is true)
381+
logical(LK),intent(in),optional :: no_whitespace !! if true, printing the JSON structure is
382+
!! done without adding any non-significant
383+
!! spaces or linebreaks (default is false)
373384

374385
call file_object%initialize(verbose,compact_reals,&
375386
print_signs,real_format,spaces_per_tab,&
376387
strict_type_checking,&
377388
trailing_spaces_significant,&
378-
case_sensitive_keys)
389+
case_sensitive_keys,&
390+
no_whitespace)
379391

380392
if (present(p)) file_object%p => p
381393

src/json_value_module.F90

+48-18
Original file line numberDiff line numberDiff line change
@@ -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

src/tests/jf_test_1.f90

+13-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ subroutine test_1(error_cnt)
6161
! print the parsed data to the console
6262
write(error_unit,'(A)') ''
6363
write(error_unit,'(A)') 'printing the file...'
64-
write(output_unit,'(A)') '{ "part a" :' !Wrap 3 outputs to make stdout valid json
64+
!write(output_unit,'(A)') '{ "part a" :' !Wrap 3 outputs to make stdout valid json
6565
call json%print_file()
6666
if (json%failed()) then
6767
call json%print_error_message(error_unit)
@@ -211,7 +211,7 @@ subroutine test_1(error_cnt)
211211

212212
write(error_unit,'(A)') ''
213213
write(error_unit,'(A)') 'printing the modified structure...'
214-
write(output_unit,'(A)') ', "part b" : '
214+
!write(output_unit,'(A)') ', "part b" : '
215215
call json%print_file()
216216
if (json%failed()) then
217217
call json%print_error_message(error_unit)
@@ -246,9 +246,18 @@ subroutine test_1(error_cnt)
246246

247247
write(error_unit,'(A)') ''
248248
write(error_unit,'(A)') 'printing the modified structure...'
249-
write(output_unit,'(A)') ', "part c" : '
249+
!write(output_unit,'(A)') ', "part c" : '
250+
call json%print_file()
251+
!write(output_unit,'(A)') '}'
252+
if (json%failed()) then
253+
call json%print_error_message(error_unit)
254+
error_cnt = error_cnt + 1
255+
end if
256+
257+
write(error_unit,'(A)') ''
258+
write(error_unit,'(A)') 'printing the modified structure (compact mode)...'
259+
call json%initialize(no_whitespace=.true.)
250260
call json%print_file()
251-
write(output_unit,'(A)') '}'
252261
if (json%failed()) then
253262
call json%print_error_message(error_unit)
254263
error_cnt = error_cnt + 1

0 commit comments

Comments
 (0)