Skip to content

Commit 1ba5d31

Browse files
committed
new option to json_value_destroy.
1 parent 2a9ce5c commit 1ba5d31

File tree

1 file changed

+48
-15
lines changed

1 file changed

+48
-15
lines changed

src/json_module.f90

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,25 +1289,58 @@ end subroutine json_value_destroy
12891289
! json_value_destroy
12901290
!
12911291
! DESCRIPTION
1292-
! Remove and destroy a json_value (and all its children)
1293-
! from a linked-list structure.
1294-
! The rest of the structure is preserved.
1292+
! Remove a json_value (and all its children)
1293+
! from a linked-list structure, preserving the rest of the structure.
1294+
!
1295+
! If destroy is not present, it is also destroyed.
1296+
! If destroy is present and false, it is not destroyed.
1297+
!
1298+
! EXAMPLE
1299+
!
1300+
! !to extract an object from one json structure, and add it to another:
1301+
! type(json_value),pointer :: json1,json2,p
1302+
! logical :: found
1303+
! ...create json1 and json2
1304+
! call json_get(json1,'name',p,found) ! get pointer to name element of json1
1305+
! call json_remove(p,destroy=.false.) ! remove it from json1 (don't destroy)
1306+
! call json_value_add(json2,p) ! add it to json2
1307+
!
1308+
! !to remove an object from a json structure (and destroy it)
1309+
! type(json_value),pointer :: json1,p
1310+
! logical :: found
1311+
! ...create json1
1312+
! call json_get(json1,'name',p,found) ! get pointer to name element of json1
1313+
! call json_remove(p) ! remove and destroy it
12951314
!
12961315
! AUTHOR
1297-
! Jacob Williams : 9/9/2014
1316+
! Jacob Williams : 9/9/2014
1317+
!
1318+
! HISTORY
1319+
! JW : 12/28/2014 : added destroy optional argument.
12981320
!
12991321
! SOURCE
13001322

1301-
subroutine json_value_remove(me)
1323+
subroutine json_value_remove(me,destroy)
13021324

13031325
implicit none
13041326

1305-
type(json_value),pointer :: me
1327+
type(json_value),pointer :: me
1328+
logical,intent(in),optional :: destroy
13061329

13071330
type(json_value),pointer :: parent,previous,next
1331+
logical :: destroy_it
13081332

13091333
if (associated(me)) then
1334+
13101335
if (associated(me%parent)) then
1336+
1337+
!optional input argument:
1338+
if (present(destroy)) then
1339+
destroy_it = destroy
1340+
else
1341+
destroy_it = .true.
1342+
end if
1343+
13111344
if (associated(me%next)) then
13121345

13131346
!there are later items in the list:
@@ -1343,7 +1376,7 @@ subroutine json_value_remove(me)
13431376

13441377
end if
13451378

1346-
call json_value_destroy(me)
1379+
if (destroy_it) call json_value_destroy(me)
13471380

13481381
end if
13491382

@@ -2603,7 +2636,7 @@ subroutine json_get_by_path(this, path, p, found)
26032636
cycle
26042637
end if
26052638

2606-
if (.not.associated(p)) then
2639+
if (.not. associated(p)) then
26072640
call throw_exception('Error in json_get_by_path:'//&
26082641
' Error getting child member.')
26092642
exit
@@ -2629,7 +2662,7 @@ subroutine json_get_by_path(this, path, p, found)
26292662
child_i = i + 1
26302663
cycle
26312664
end if
2632-
if (.not.associated(p)) then
2665+
if (.not. associated(p)) then
26332666
call throw_exception('Error in json_get_by_path:'//&
26342667
' Error getting array element')
26352668
exit
@@ -2802,7 +2835,7 @@ subroutine json_get_integer(this, path, value, found)
28022835
p => this
28032836
end if
28042837

2805-
if (.not.associated(p)) then
2838+
if (.not. associated(p)) then
28062839

28072840
call throw_exception('Error in json_get_integer:'//&
28082841
' Unable to resolve path: '// trim(path))
@@ -2939,7 +2972,7 @@ subroutine json_get_double(this, path, value, found)
29392972
p => this
29402973
end if
29412974

2942-
if (.not.associated(p)) then
2975+
if (.not. associated(p)) then
29432976

29442977
call throw_exception('Error in json_get_double:'//&
29452978
' Unable to resolve path: '//trim(path))
@@ -3076,7 +3109,7 @@ subroutine json_get_logical(this, path, value, found)
30763109
p => this
30773110
end if
30783111

3079-
if (.not.associated(p)) then
3112+
if (.not. associated(p)) then
30803113

30813114
call throw_exception('Error in json_get_logical:'//&
30823115
' Unable to resolve path: '//trim(path))
@@ -3210,7 +3243,7 @@ subroutine json_get_chars(this, path, value, found)
32103243
p => this
32113244
end if
32123245

3213-
if (.not.associated(p)) then
3246+
if (.not. associated(p)) then
32143247

32153248
call throw_exception('Error in json_get_chars:'//&
32163249
' Unable to resolve path: '//trim(path))
@@ -3487,7 +3520,7 @@ subroutine json_get_array(this, path, array_callback, found)
34873520
p => this
34883521
end if
34893522

3490-
if (.not.associated(p)) then
3523+
if (.not. associated(p)) then
34913524

34923525
call throw_exception('Error in json_get_array:'//&
34933526
' Unable to resolve path: '//trim(path))
@@ -4014,7 +4047,7 @@ subroutine to_object(me,name)
40144047
type(json_value),intent(inout) :: me
40154048
!type(json_value),pointer,intent(inout) :: me !this causes crash in gfortran (compiler bug?)
40164049
character(len=*),intent(in),optional :: name
4017-
4050+
40184051
!set type and value:
40194052
!associate (d => me%data)
40204053
call me%data%destroy()

0 commit comments

Comments
 (0)