Skip to content

Commit 8c631d9

Browse files
committed
cleanup of read/write API names (now have print, load, serialize, and deserialize for both json_core and json_file). Fixes #397. The old ones are still there for backward compatibility.
added a json_core print to console routine. Fixes #408
1 parent 8867398 commit 8c631d9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+210
-154
lines changed

src/json_file_module.F90

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,18 @@ module json_file_module
4848
! character(len=:),allocatable :: cval
4949
! logical :: found
5050
! call json%initialize(compact_reals=.true.)
51-
! call json%load_file(filename='myfile.json')
52-
! call json%print_file() !print to the console
51+
! call json%load(filename='myfile.json')
52+
! call json%print() !print to the console
5353
! call json%get('var.i',ival,found)
5454
! call json%get('var.r(3)',rval,found)
5555
! call json%get('var.c',cval,found)
5656
! call json%destroy()
5757
! end program test
5858
!```
5959
!
60-
!@warning The `destroy()` method must be called before the variable
61-
! goes out of scope or a memory leak will occur.
60+
!@note The `destroy()` method may be called to free the memory, but
61+
! [[json_file(type)]] includes a finalizer that also calls
62+
! `destroy()` when the variable goes out of scope.
6263

6364
type,public :: json_file
6465

@@ -75,8 +76,16 @@ module json_file_module
7576

7677
procedure,public :: get_core => get_json_core_in_file
7778

79+
procedure,public :: load => json_file_load
80+
81+
!>
82+
! The same as `load`, but only here for backward compatibility
7883
procedure,public :: load_file => json_file_load
7984

85+
generic,public :: serialize => MAYBEWRAP(json_file_load_from_string)
86+
87+
!>
88+
! The same as `serialize`, but only here for backward compatibility
8089
generic,public :: load_from_string => MAYBEWRAP(json_file_load_from_string)
8190

8291
procedure,public :: destroy => json_file_destroy
@@ -91,11 +100,23 @@ module json_file_module
91100
procedure,public :: check_for_errors => json_file_check_for_errors
92101
procedure,public :: clear_exceptions => json_file_clear_exceptions
93102

103+
!>
104+
! Print the [[json_value]] structure to an allocatable string
105+
procedure,public :: deserialize => json_file_print_to_string
106+
107+
!>
108+
! The same as `deserialize`, but only here for backward compatibility
94109
procedure,public :: print_to_string => json_file_print_to_string
95110

111+
generic,public :: print => json_file_print_to_console, &
112+
json_file_print_to_unit, &
113+
json_file_print_to_filename
114+
115+
!>
116+
! The same as `print`, but only here for backward compatibility
96117
generic,public :: print_file => json_file_print_to_console, &
97-
json_file_print_1, &
98-
json_file_print_2
118+
json_file_print_to_unit, &
119+
json_file_print_to_filename
99120

100121
!>
101122
! Rename a variable, specifying it by path
@@ -150,7 +171,7 @@ module json_file_module
150171
! call f%add('inputs.t', 0.0_rk)
151172
! call f%add('inputs.x', [1.0_rk,2.0_rk,3.0_rk])
152173
! call f%add('inputs.flag', .true.)
153-
! call f%print_file()
174+
! call f%print() ! print to the console
154175
! end program test
155176
!```
156177
generic,public :: add => json_file_add, &
@@ -321,10 +342,10 @@ module json_file_module
321342
!remove:
322343
procedure :: MAYBEWRAP(json_file_remove)
323344

324-
!print_file:
345+
!print:
325346
procedure :: json_file_print_to_console
326-
procedure :: json_file_print_1
327-
procedure :: json_file_print_2
347+
procedure :: json_file_print_to_unit
348+
procedure :: json_file_print_to_filename
328349

329350
final :: finalize_json_file
330351

@@ -620,7 +641,7 @@ function initialize_json_file_from_string(str,&
620641
call file_object%initialize(&
621642
#include "json_initialize_dummy_arguments.inc"
622643
)
623-
call file_object%load_from_string(str)
644+
call file_object%serialize(str)
624645

625646
end function initialize_json_file_from_string
626647
!*****************************************************************************************
@@ -664,7 +685,7 @@ function initialize_json_file_from_string_v2(str, json_core_object) &
664685
type(json_core),intent(in) :: json_core_object
665686

666687
file_object%core = json_core_object
667-
call file_object%load_from_string(str)
688+
call file_object%serialize(str)
668689

669690
end function initialize_json_file_from_string_v2
670691
!*****************************************************************************************
@@ -719,7 +740,7 @@ end subroutine json_file_nullify
719740
!> author: Jacob Williams
720741
!
721742
! Destroy the [[json_value]] data in a [[json_file(type)]].
722-
! This must be done when the variable is no longer needed,
743+
! This may be done when the variable is no longer needed,
723744
! or will be reused to open a different file.
724745
! Otherwise a memory leak will occur.
725746
!
@@ -804,7 +825,7 @@ end subroutine json_file_move_pointer
804825
! use json_module
805826
! implicit none
806827
! type(json_file) :: f
807-
! call f%load_file('my_file.json')
828+
! call f%load('my_file.json')
808829
! !...
809830
! call f%destroy()
810831
! end program main
@@ -820,7 +841,7 @@ subroutine json_file_load(me, filename, unit)
820841
!! (if not present, a newunit
821842
!! is used)
822843

823-
call me%core%parse(file=filename, p=me%p, unit=unit)
844+
call me%core%load(file=filename, p=me%p, unit=unit)
824845

825846
end subroutine json_file_load
826847
!*****************************************************************************************
@@ -836,7 +857,7 @@ end subroutine json_file_load
836857
! Load JSON from a string:
837858
!```fortran
838859
! type(json_file) :: f
839-
! call f%load_from_string('{ "name": "Leonidas" }')
860+
! call f%serialize('{ "name": "Leonidas" }')
840861
!```
841862

842863
subroutine json_file_load_from_string(me, str)
@@ -846,7 +867,7 @@ subroutine json_file_load_from_string(me, str)
846867
class(json_file),intent(inout) :: me
847868
character(kind=CK,len=*),intent(in) :: str !! string to load JSON data from
848869

849-
call me%core%parse(str=str, p=me%p)
870+
call me%core%load(str=str, p=me%p)
850871

851872
end subroutine json_file_load_from_string
852873
!*****************************************************************************************
@@ -862,7 +883,7 @@ subroutine wrap_json_file_load_from_string(me, str)
862883
class(json_file),intent(inout) :: me
863884
character(kind=CDK,len=*),intent(in) :: str
864885

865-
call me%load_from_string(to_unicode(str))
886+
call me%serialize(to_unicode(str))
866887

867888
end subroutine wrap_json_file_load_from_string
868889
!*****************************************************************************************
@@ -890,7 +911,7 @@ end subroutine json_file_print_to_console
890911
!
891912
! Prints the JSON file to the specified file unit number.
892913

893-
subroutine json_file_print_1(me, iunit)
914+
subroutine json_file_print_to_unit(me, iunit)
894915

895916
implicit none
896917

@@ -900,10 +921,10 @@ subroutine json_file_print_1(me, iunit)
900921
if (iunit/=unit2str) then
901922
call me%core%print(me%p,iunit=iunit)
902923
else
903-
call me%core%throw_exception('Error in json_file_print_1: iunit must not be -1.')
924+
call me%core%throw_exception('Error in json_file_print_to_unit: iunit must not be -1.')
904925
end if
905926

906-
end subroutine json_file_print_1
927+
end subroutine json_file_print_to_unit
907928
!*****************************************************************************************
908929

909930
!*****************************************************************************************
@@ -919,12 +940,12 @@ end subroutine json_file_print_1
919940
!```fortran
920941
! type(json_file) :: f
921942
! logical :: found
922-
! call f%load_file('my_file.json') !open the original file
923-
! call f%update('version',4,found) !change the value of a variable
924-
! call f%print_file('my_file_2.json') !save file as new name
943+
! call f%load('my_file.json') !open the original file
944+
! call f%update('version',4,found) !change the value of a variable
945+
! call f%print('my_file_2.json') !save file as new name
925946
!```
926947

927-
subroutine json_file_print_2(me,filename)
948+
subroutine json_file_print_to_filename(me,filename)
928949

929950
implicit none
930951

@@ -933,7 +954,7 @@ subroutine json_file_print_2(me,filename)
933954

934955
call me%core%print(me%p,filename)
935956

936-
end subroutine json_file_print_2
957+
end subroutine json_file_print_to_filename
937958
!*****************************************************************************************
938959

939960
!*****************************************************************************************
@@ -948,8 +969,8 @@ end subroutine json_file_print_2
948969
!```fortran
949970
! type(json_file) :: f
950971
! character(kind=CK,len=:),allocatable :: str
951-
! call f%load_file('my_file.json')
952-
! call f%print_file(str)
972+
! call f%load('my_file.json')
973+
! call f%deserialize(str)
953974
!```
954975

955976
subroutine json_file_print_to_string(me,str)
@@ -959,7 +980,7 @@ subroutine json_file_print_to_string(me,str)
959980
class(json_file),intent(inout) :: me
960981
character(kind=CK,len=:),allocatable,intent(out) :: str !! string to print JSON data to
961982

962-
call me%core%print_to_string(me%p,str)
983+
call me%core%deserialize(me%p,str)
963984

964985
end subroutine json_file_print_to_string
965986
!*****************************************************************************************

0 commit comments

Comments
 (0)