Skip to content

Commit 81ea21f

Browse files
committed
Merge pull request #176 from jacobwilliams/core
json_core updates
2 parents 979e950 + 007020b commit 81ea21f

File tree

2 files changed

+259
-151
lines changed

2 files changed

+259
-151
lines changed

src/json_file_module.F90

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,28 @@ module json_file_module
150150
! date: 07/23/2015
151151
!
152152
! Structure constructor to initialize a [[json_file(type)]] object
153-
! with an existing [[json_value]] object
153+
! with an existing [[json_value]] object, and either the [[json_core]]
154+
! settings or a [[json_core]] instance.
154155
!
155156
!# Example
156157
!
157158
!```fortran
158159
! ...
159160
! type(json_file) :: my_file
160-
! type(json_value) :: json_object
161+
! type(json_value),pointer :: json_object
162+
! type(json_core) :: json_core_object
161163
! ...
162-
! ! Construct a json_object
163-
! my_file = json_file(json_object)
164+
! ! Construct a json_object:
165+
! !could do this:
166+
! my_file = json_file(json_object)
167+
! !or:
168+
! my_file = json_file(json_object,verbose=.true.)
169+
! !or:
170+
! my_file = json_file(json_object,json_core_object)
164171
!```
165172
interface json_file
166-
module procedure initialize_json_file
173+
module procedure :: initialize_json_file
174+
module procedure :: initialize_json_file_v2
167175
end interface
168176
!*************************************************************************************
169177

@@ -240,6 +248,10 @@ end subroutine json_file_print_error_message
240248
! This is just a wrapper for [[json_initialize]].
241249
!
242250
!@note: This does not destroy the data in the file.
251+
!
252+
!@note [[initialize_json_core]], [[json_initialize]],
253+
! [[initialize_json_core_in_file]], and [[initialize_json_file]]
254+
! all have a similar interface.
243255

244256
subroutine initialize_json_core_in_file(me,verbose,compact_reals,&
245257
print_signs,real_format,spaces_per_tab,&
@@ -270,6 +282,10 @@ end subroutine initialize_json_core_in_file
270282
!
271283
! Cast a [[json_value]] object as a [[json_file(type)]] object.
272284
! It also calls the `initialize()` method.
285+
!
286+
!@note [[initialize_json_core]], [[json_initialize]],
287+
! [[initialize_json_core_in_file]], and [[initialize_json_file]]
288+
! all have a similar interface.
273289

274290
function initialize_json_file(p,verbose,compact_reals,&
275291
print_signs,real_format,spaces_per_tab,&
@@ -300,18 +316,56 @@ end function initialize_json_file
300316

301317
!*****************************************************************************************
302318
!> author: Jacob Williams
303-
! date: 12/9/2013
319+
! date: 4/26/2016
320+
!
321+
! Cast a [[json_value]] pointer and a [[json_core]] object
322+
! as a [[json_file(type)]] object.
323+
324+
function initialize_json_file_v2(json_value_object, json_core_object) &
325+
result(file_object)
326+
327+
implicit none
328+
329+
type(json_file) :: file_object
330+
type(json_value),pointer,intent(in) :: json_value_object
331+
type(json_core),intent(in) :: json_core_object
332+
333+
file_object%p => json_value_object
334+
file_object%json = json_core_object
335+
336+
end function initialize_json_file_v2
337+
!*****************************************************************************************
338+
339+
!*****************************************************************************************
340+
!> author: Jacob Williams
304341
!
305-
! Destroy the [[json_file(type)]].
342+
! Destroy the [[json_value]] data in a [[json_file(type)]].
343+
! This must be done when the variable is no longer needed,
344+
! or will be reused to open a different file.
345+
! Otherwise a memory leak will occur.
346+
!
347+
! Optionally, also destroy the [[json_core]] instance (this
348+
! is not necessary to prevent memory leaks, since a [[json_core]]
349+
! does not use pointers).
350+
!
351+
!### History
352+
! * 12/9/2013 : Created
353+
! * 4/26/2016 : Added optional `destroy_core` argument
306354

307-
subroutine json_file_destroy(me)
355+
subroutine json_file_destroy(me,destroy_core)
308356

309357
implicit none
310358

311359
class(json_file),intent(inout) :: me
360+
logical,intent(in),optional :: destroy_core !! to also destroy the [[json_core]].
361+
!! default is to leave it as is.
312362

313363
if (associated(me%p)) call me%json%destroy(me%p)
314364

365+
if (present(destroy_core)) then
366+
if (destroy_core) call me%json%destroy()
367+
end if
368+
315369
end subroutine json_file_destroy
316370
!*****************************************************************************************
317371

0 commit comments

Comments
 (0)