Skip to content

Commit c6b02e7

Browse files
committed
Add constructor to cast json_value as json_file
- Add further tests to boost testing to 100% procedure coverage
1 parent a89328d commit c6b02e7

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

src/json_module.F90

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,14 @@ module json_module
338338
!
339339
!```fortran
340340
! program test
341-
! use json_module
342-
! implicit none
341+
! use json_module
342+
! implicit none
343343
! type(json_file) :: json
344344
! integer :: ival
345345
! real(real64) :: rval
346346
! character(len=:),allocatable :: cval
347347
! logical :: found
348-
! call json_initialize()
348+
! call json_initialize()
349349
! call json%load_file(filename='myfile.json')
350350
! call json%print_file() !print to the console
351351
! call json%get('var.i',ival,found)
@@ -431,6 +431,28 @@ module json_module
431431
end type json_file
432432
!*********************************************************
433433

434+
!*********************************************************
435+
!> author: Izaak Beekman
436+
! date: 07/23/2015
437+
!
438+
! Structure constructor to initialize a [[json_file(type)]] object
439+
! with an existing [[json_value]] object
440+
!
441+
! # Example
442+
!
443+
! ```fortran
444+
! ...
445+
! type(json_file) :: my_file
446+
! type(json_value) :: json_object
447+
! ...
448+
! ! Construct a json_object
449+
! my_file = json_file(json_object)
450+
451+
interface json_file
452+
module procedure initialize_json_file
453+
end interface
454+
!*************************************************************************************
455+
434456
!*************************************************************************************
435457
!>
436458
! Array element callback function. Used by [[json_get_array]].
@@ -893,6 +915,22 @@ end subroutine array_callback_func
893915
contains
894916
!*****************************************************************************************
895917

918+
!*****************************************************************************************
919+
!> author: Izaak Beekman
920+
! date: 07/23/2015
921+
!
922+
! Cast a [[json_value]] object as a [[json_file(type)]] object
923+
924+
function initialize_json_file(p) result(file_object)
925+
type(json_value), pointer, optional, intent(in) :: p
926+
!! `json_value` object to cast as a `json_file` object
927+
type(json_file) :: file_object
928+
929+
if (present(p)) file_object%p => p
930+
931+
end function initialize_json_file
932+
!*****************************************************************************************
933+
896934
!*****************************************************************************************
897935
!> author: Jacob Williams
898936
!
@@ -918,7 +956,7 @@ end subroutine destroy_json_data
918956
!> author: Jacob Williams
919957
! date: 12/9/2013
920958
!
921-
! Destroy the [[json_file]].
959+
! Destroy the [[json_file(type)]].
922960

923961
subroutine json_file_destroy(me)
924962

@@ -935,7 +973,7 @@ end subroutine json_file_destroy
935973
!> author: Jacob Williams
936974
! date: 12/5/2014
937975
!
938-
! Move the [[json_value]] pointer from one [[json_file]] to another.
976+
! Move the [[json_value]] pointer from one [[json_file(type)]] to another.
939977
! The "from" pointer is then nullified, but not destroyed.
940978
!
941979
!@note If "from%p" is not associated, then an error is thrown.
@@ -1141,7 +1179,7 @@ end subroutine json_file_print_to_string
11411179
!> author: Jacob Williams
11421180
! date: 2/3/2014
11431181
!
1144-
! Returns information about a variable in a [[json_file]].
1182+
! Returns information about a variable in a [[json_file(type)]].
11451183

11461184
subroutine json_file_variable_info(me,path,found,var_type,n_children)
11471185

@@ -4720,7 +4758,7 @@ end subroutine wrap_json_get_string_with_path
47204758
!> author: Jacob Williams
47214759
! date: 5/14/2014
47224760
!
4723-
! Get a string vector from a [[json_file]].
4761+
! Get a string vector from a [[json_file(type)]].
47244762

47254763
subroutine json_get_string_vec(me, vec)
47264764

@@ -4773,7 +4811,7 @@ end subroutine json_get_string_vec
47734811

47744812
!*****************************************************************************************
47754813
!>
4776-
! Get a string vector from a [[json_file]], given the path.
4814+
! Get a string vector from a [[json_file(type)]], given the path.
47774815

47784816
subroutine json_get_string_vec_with_path(me, path, vec, found)
47794817

src/tests/jf_test_12.f90

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ subroutine test_12(error_cnt)
138138
call json_get(me=root,path='array data.data',array_callback=get_3D_from_array)
139139
call check_errors(all(fetched_array == reshape(raw_array,[size(raw_array)])))
140140

141+
my_file = json_file(root)
142+
143+
call my_file%update('array data.description',CK_'vector data',found=existed)
144+
call check_errors(existed)
145+
146+
call my_file%update(CK_'array data.description','Vector data',found=existed)
147+
call check_errors(existed)
148+
149+
call my_file%get('SOS',sos)
150+
call check_errors()
151+
152+
call my_file%get('$array data.data',fetched_array)
153+
call check_errors(all(fetched_array == reshape(raw_array,[size(raw_array)])))
154+
141155
contains
142156
subroutine check_errors(assertion)
143157
logical, optional, intent(in) :: assertion

0 commit comments

Comments
 (0)