@@ -1289,25 +1289,58 @@ end subroutine json_value_destroy
1289
1289
! json_value_destroy
1290
1290
!
1291
1291
! 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
1295
1314
!
1296
1315
! 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.
1298
1320
!
1299
1321
! SOURCE
1300
1322
1301
- subroutine json_value_remove (me )
1323
+ subroutine json_value_remove (me , destroy )
1302
1324
1303
1325
implicit none
1304
1326
1305
- type (json_value),pointer :: me
1327
+ type (json_value),pointer :: me
1328
+ logical ,intent (in ),optional :: destroy
1306
1329
1307
1330
type (json_value),pointer :: parent,previous,next
1331
+ logical :: destroy_it
1308
1332
1309
1333
if (associated (me)) then
1334
+
1310
1335
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
+
1311
1344
if (associated (me% next)) then
1312
1345
1313
1346
! there are later items in the list:
@@ -1343,7 +1376,7 @@ subroutine json_value_remove(me)
1343
1376
1344
1377
end if
1345
1378
1346
- call json_value_destroy(me)
1379
+ if (destroy_it) call json_value_destroy(me)
1347
1380
1348
1381
end if
1349
1382
@@ -2603,7 +2636,7 @@ subroutine json_get_by_path(this, path, p, found)
2603
2636
cycle
2604
2637
end if
2605
2638
2606
- if (.not. associated (p)) then
2639
+ if (.not. associated (p)) then
2607
2640
call throw_exception(' Error in json_get_by_path:' // &
2608
2641
' Error getting child member.' )
2609
2642
exit
@@ -2629,7 +2662,7 @@ subroutine json_get_by_path(this, path, p, found)
2629
2662
child_i = i + 1
2630
2663
cycle
2631
2664
end if
2632
- if (.not. associated (p)) then
2665
+ if (.not. associated (p)) then
2633
2666
call throw_exception(' Error in json_get_by_path:' // &
2634
2667
' Error getting array element' )
2635
2668
exit
@@ -2802,7 +2835,7 @@ subroutine json_get_integer(this, path, value, found)
2802
2835
p = > this
2803
2836
end if
2804
2837
2805
- if (.not. associated (p)) then
2838
+ if (.not. associated (p)) then
2806
2839
2807
2840
call throw_exception(' Error in json_get_integer:' // &
2808
2841
' Unable to resolve path: ' // trim (path))
@@ -2939,7 +2972,7 @@ subroutine json_get_double(this, path, value, found)
2939
2972
p = > this
2940
2973
end if
2941
2974
2942
- if (.not. associated (p)) then
2975
+ if (.not. associated (p)) then
2943
2976
2944
2977
call throw_exception(' Error in json_get_double:' // &
2945
2978
' Unable to resolve path: ' // trim (path))
@@ -3076,7 +3109,7 @@ subroutine json_get_logical(this, path, value, found)
3076
3109
p = > this
3077
3110
end if
3078
3111
3079
- if (.not. associated (p)) then
3112
+ if (.not. associated (p)) then
3080
3113
3081
3114
call throw_exception(' Error in json_get_logical:' // &
3082
3115
' Unable to resolve path: ' // trim (path))
@@ -3210,7 +3243,7 @@ subroutine json_get_chars(this, path, value, found)
3210
3243
p = > this
3211
3244
end if
3212
3245
3213
- if (.not. associated (p)) then
3246
+ if (.not. associated (p)) then
3214
3247
3215
3248
call throw_exception(' Error in json_get_chars:' // &
3216
3249
' Unable to resolve path: ' // trim (path))
@@ -3487,7 +3520,7 @@ subroutine json_get_array(this, path, array_callback, found)
3487
3520
p = > this
3488
3521
end if
3489
3522
3490
- if (.not. associated (p)) then
3523
+ if (.not. associated (p)) then
3491
3524
3492
3525
call throw_exception(' Error in json_get_array:' // &
3493
3526
' Unable to resolve path: ' // trim (path))
@@ -4014,7 +4047,7 @@ subroutine to_object(me,name)
4014
4047
type (json_value),intent (inout ) :: me
4015
4048
! type(json_value),pointer,intent(inout) :: me !this causes crash in gfortran (compiler bug?)
4016
4049
character (len=* ),intent (in ),optional :: name
4017
-
4050
+
4018
4051
! set type and value:
4019
4052
! associate (d => me%data)
4020
4053
call me% data % destroy()
0 commit comments