@@ -1305,6 +1305,12 @@ end subroutine json_value_remove
1305
1305
! Only the simple cases where p1/p2 or p2/p1 are parent/child
1306
1306
! are currently checked.
1307
1307
!
1308
+ ! @warning There are also other situations where using this routine would
1309
+ ! produce a malformed JSON structure, such as swapping an array
1310
+ ! with one of its children. This is not checked for.
1311
+ !
1312
+ ! @note If `p1` and `p2` have a common parent, it is always safe to swap them.
1313
+ !
1308
1314
! @warning This is a work-in-progress and has not yet been fully validated.
1309
1315
1310
1316
subroutine json_value_swap (json ,p1 ,p2 )
@@ -1315,10 +1321,8 @@ subroutine json_value_swap(json,p1,p2)
1315
1321
type (json_value),pointer :: p1
1316
1322
type (json_value),pointer :: p2
1317
1323
1318
- logical :: same_parent,first_last
1319
- type (json_value),pointer :: p1_par_first,p1_par_last,p2_par_first,p2_par_last
1324
+ logical :: same_parent,first_last,adjacent
1320
1325
type (json_value),pointer :: a,b
1321
- logical :: adjacent
1322
1326
1323
1327
if (json% exception_thrown) return
1324
1328
@@ -1331,7 +1335,6 @@ subroutine json_value_swap(json,p1,p2)
1331
1335
1332
1336
! TODO Need to check *all* the `children` pointers, so make sure
1333
1337
! cases like p1%child%...%child => p2 don't occur...
1334
- ! .... or is that case OK? ....
1335
1338
if (associated (p1% parent,p2) .or. associated (p2% parent,p1)) then
1336
1339
call json% throw_exception(' Error in json_value_swap: ' // &
1337
1340
' cannot swap a parent/child pair' )
@@ -1341,6 +1344,7 @@ subroutine json_value_swap(json,p1,p2)
1341
1344
associated (p2% parent) .and. &
1342
1345
associated (p1% parent,p2% parent) )
1343
1346
if (same_parent) then
1347
+ ! if p1,p2 are the first,last or last,first children of a common parent
1344
1348
first_last = (associated (p1% parent% children,p1) .and. &
1345
1349
associated (p2% parent% tail,p2)) .or. &
1346
1350
(associated (p1% parent% tail,p1) .and. &
@@ -1356,67 +1360,29 @@ subroutine json_value_swap(json,p1,p2)
1356
1360
! this is all we have to do for the parent in this case:
1357
1361
call swap_pointers(p1% parent% children,p2% parent% tail)
1358
1362
1359
- elseif (same_parent .and. .not. first_last) then
1363
+ else if (same_parent .and. .not. first_last) then
1360
1364
1361
1365
if (associated (p1% parent% children,p1)) then
1362
- ! if p1 is the first child of its parent:
1363
- p1% parent% children = > p2
1364
- elseif (associated (p1% parent% children,p2)) then
1365
- ! if p2 is the first child of its parent:
1366
- p1% parent% children = > p1
1366
+ p1% parent% children = > p2 ! p1 is the first child of the parent
1367
+ else if (associated (p1% parent% children,p2)) then
1368
+ p1% parent% children = > p1 ! p2 is the first child of the parent
1367
1369
end if
1368
1370
if (associated (p1% parent% tail,p1)) then
1369
- ! if p1 is the last child of its parent:
1370
- p1% parent% tail = > p2
1371
- elseif (associated (p1% parent% tail,p2)) then
1372
- p1% parent% tail = > p1
1371
+ p1% parent% tail = > p2 ! p1 is the last child of the parent
1372
+ else if (associated (p1% parent% tail,p2)) then
1373
+ p1% parent% tail = > p1 ! p2 is the last child of the parent
1373
1374
end if
1374
1375
1375
1376
else ! general case: different parents
1376
1377
1377
- ! ... see if we can clean this up ...
1378
-
1379
- p1_par_first = > null ()
1380
- p1_par_last = > null ()
1381
- p2_par_first = > null ()
1382
- p2_par_last = > null ()
1383
- if (associated (p1% parent)) then
1384
- if (associated (p1% parent% children,p1)) then
1385
- ! if p1 is the first child of its parent:
1386
- p1_par_first = > p2
1387
- else
1388
- p1_par_first = > p1% parent% children ! no change
1389
- end if
1390
- if (associated (p1% parent% tail,p1)) then
1391
- ! if p1 is the last child of its parent:
1392
- p1_par_last = > p2
1393
- else
1394
- p1_par_last = > p1% parent% tail ! no change
1395
- end if
1396
- end if
1397
- if (associated (p2% parent)) then
1398
- if (associated (p2% parent% children,p2)) then
1399
- ! if p2 is the first child of its parent:
1400
- p2_par_first = > p1
1401
- else
1402
- p2_par_first = > p2% parent% children ! no change
1403
- end if
1404
- if (associated (p2% parent% tail,p2)) then
1405
- ! if p2 is the last child of its parent:
1406
- p2_par_last = > p1
1407
- else
1408
- p2_par_last = > p2% parent% tail ! no change
1409
- end if
1410
- end if
1411
1378
if (associated (p1% parent)) then
1412
- p1% parent% children = > p1_par_first
1413
- p1% parent% tail = > p1_par_last
1379
+ if ( associated ( p1% parent% children,p1)) p1 % parent % children = > p2
1380
+ if ( associated ( p1% parent% tail,p1)) p1 % parent % tail = > p2
1414
1381
end if
1415
1382
if (associated (p2% parent)) then
1416
- p2% parent% children = > p2_par_first
1417
- p2% parent% tail = > p2_par_last
1383
+ if ( associated ( p2% parent% children,p2)) p2 % parent % children = > p1
1384
+ if ( associated ( p2% parent% tail,p2)) p2 % parent % tail = > p1
1418
1385
end if
1419
-
1420
1386
call swap_pointers(p1% parent, p2% parent)
1421
1387
1422
1388
end if
@@ -5743,7 +5709,7 @@ recursive function pop_char(json, unit, str, eof, skip_ws) result(popped)
5743
5709
eof = .true.
5744
5710
exit
5745
5711
5746
- elseif (IS_IOSTAT_EOR(ios) .or. c== newline) then ! end of record
5712
+ else if (IS_IOSTAT_EOR(ios) .or. c== newline) then ! end of record
5747
5713
5748
5714
json% char_count = 0
5749
5715
json% line_count = json% line_count + 1
0 commit comments