@@ -148,7 +148,6 @@ module json_value_module
148148 ! call json%print(p,'test.json') !write it to a file
149149 ! call json%destroy(p) !cleanup
150150 ! end program test
151- ! type,public :: json_core
152151 ! ````
153152 type,public :: json_core
154153
@@ -503,6 +502,14 @@ module json_value_module
503502 procedure :: json_matrix_info
504503 procedure :: MAYBEWRAP(json_matrix_info_by_path)
505504
505+ ! >
506+ ! insert a new element after an existing one,
507+ ! updating the JSON structure accordingly
508+ generic,public :: insert_after = > json_value_insert_after, &
509+ json_value_insert_after_child_by_index
510+ procedure :: json_value_insert_after
511+ procedure :: json_value_insert_after_child_by_index
512+
506513 procedure ,public :: remove = > json_value_remove ! ! Remove a [[json_value]] from a linked-list structure.
507514 procedure ,public :: check_for_errors = > json_check_for_errors ! ! check for error and get error message
508515 procedure ,public :: clear_exceptions = > json_clear_exceptions ! ! clear exceptions
@@ -522,8 +529,6 @@ module json_value_module
522529 procedure ,public :: validate = > json_value_validate ! ! Check that a [[json_value]] linked list is valid
523530 ! ! (i.e., is properly constructed). This may be
524531 ! ! useful if it has been constructed externally.
525- procedure ,public :: insert_after = > json_value_insert_after ! ! insert a new element after an existing one,
526- ! ! updating the JSON structure accordingly
527532
528533 ! other private routines:
529534 procedure :: name_equal
@@ -2485,7 +2490,7 @@ end subroutine json_value_add_member
24852490
24862491! *****************************************************************************************
24872492! >
2488- ! Inserts `new ` after `p`, and updates the JSON structure accordingly.
2493+ ! Inserts `element ` after `p`, and updates the JSON structure accordingly.
24892494!
24902495! ### Example
24912496!
@@ -2533,7 +2538,7 @@ subroutine json_value_insert_after(json,p,element)
25332538 element% parent = > null ()
25342539 end if
25352540
2536- ! if are there any in the list after p:
2541+ ! if there are any in the list after p:
25372542 if (associated (p% next)) then
25382543 element% next = > p% next
25392544 element% next% previous = > element
@@ -2549,6 +2554,37 @@ subroutine json_value_insert_after(json,p,element)
25492554 end subroutine json_value_insert_after
25502555! *****************************************************************************************
25512556
2557+ ! *****************************************************************************************
2558+ ! >
2559+ ! Inserts `element` after the `idx`-th child of `p`,
2560+ ! and updates the JSON structure accordingly. It is just
2561+ ! a wrapper for [[json_value_insert_after]].
2562+
2563+ subroutine json_value_insert_after_child_by_index (json ,p ,idx ,element )
2564+
2565+ implicit none
2566+
2567+ class(json_core),intent (inout ) :: json
2568+ type (json_value),pointer :: p ! ! a JSON object or array.
2569+ integer (IK),intent (in ) :: idx ! ! the index of the child of `p` to
2570+ ! ! insert the new element after
2571+ type (json_value),pointer :: element ! ! the element to insert
2572+
2573+ type (json_value),pointer :: tmp ! ! for getting the `idx`-th child of `p`
2574+
2575+ if (.not. json% exception_thrown) then
2576+
2577+ ! get the idx-th child of p:
2578+ call json% get_child(p,idx,tmp)
2579+
2580+ ! call json_value_insert_after:
2581+ if (.not. json% failed()) call json% insert_after(tmp,element)
2582+
2583+ end if
2584+
2585+ end subroutine json_value_insert_after_child_by_index
2586+ ! *****************************************************************************************
2587+
25522588! *****************************************************************************************
25532589! > author: Jacob Williams
25542590! date: 1/19/2014
0 commit comments