Skip to content

Commit b079b87

Browse files
committed
added new routine to insert by index.
1 parent 9b66eda commit b079b87

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

src/json_value_module.F90

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/tests/jf_test_20.f90

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ subroutine test_20(error_cnt)
7373
error_cnt = error_cnt + 1
7474
else
7575
call json%create_integer(new,44,'') ! create a new element
76-
call json%insert_after(element,new) ! insert new element after x(3)
76+
call json%insert_after(element,new) ! insert new element after x(5)
7777
if (json%failed()) then
7878
call json%print_error_message(error_unit)
7979
error_cnt = error_cnt + 1
@@ -88,12 +88,41 @@ subroutine test_20(error_cnt)
8888
end if
8989
end if
9090

91+
!now, insert by index:
92+
nullify(element)
93+
call json%get(p,'x',element) ! get pointer to the array itself
94+
if (json%failed()) then
95+
call json%print_error_message(error_unit)
96+
error_cnt = error_cnt + 1
97+
else
98+
call json%create_integer(new,22,'') ! create a new element
99+
call json%insert_after(element,2,new) ! insert new element after x(2)
100+
if (json%failed()) then
101+
call json%print_error_message(error_unit)
102+
error_cnt = error_cnt + 1
103+
else
104+
call json%get(p,'x',iarray)
105+
if (.not. all(iarray==[1,2,22,3,33,4,44])) then
106+
write(error_unit,'(A,1x,*(I2,1X))') 'Error: unexpected output:',iarray
107+
error_cnt = error_cnt + 1
108+
else
109+
write(error_unit,'(A,1x,*(I2,1X))') 'Success:',iarray
110+
end if
111+
end if
112+
end if
113+
91114
call json%validate(p,is_valid,error_msg)
92115
if (.not. is_valid) then
93116
write(error_unit,'(A)') trim(error_msg)
94117
error_cnt = error_cnt + 1
95118
end if
96119

120+
!just in case:
121+
if (json%failed()) then
122+
call json%print_error_message(error_unit)
123+
error_cnt = error_cnt + 1
124+
end if
125+
97126
end if
98127

99128
! cleanup:

0 commit comments

Comments
 (0)