json_clone Subroutine

private subroutine json_clone(json, from, to, use_nonrecursive)

Create a deep copy of a json_value linked-list structure.

Notes

  • If from has children, then they are also cloned.
  • The parent of from is not linked to to.
  • If from is an element of an array, then the previous and next entries are not cloned (only that element and it’s children, if any).
  • The use_nonrecursive argument enables a non-recursive clone function, which may be useful for very large JSON structures that would otherwise cause a stack overflow with the recursive function on some systems.

Example

    program test
     use json_module
     implicit none
     type(json_core) :: json
     type(json_value),pointer :: j1, j2
     call json%load('files/inputs/test1.json',j1)
     call json%clone(j1,j2) !now have two independent copies
     call json%destroy(j1)  !destroys j1, but j2 remains
     call json%print(j2,'j2.json')
     call json%destroy(j2)
    end program test

History

  • 1/4/2026 : added non-recursive clone function option.

Type Bound

json_core

Arguments

Type IntentOptional Attributes Name
class(json_core), intent(inout) :: json
type(json_value), pointer :: from

this is the structure to clone

type(json_value), pointer :: to

the clone is put here (it must not already be associated)

logical(kind=LK), intent(in), optional :: use_nonrecursive

if true, use the non-recursive clone function [Default is False]


Calls

proc~~json_clone~~CallsGraph proc~json_clone json_core%json_clone proc~json_value_clone_func json_core%json_value_clone_func proc~json_clone->proc~json_value_clone_func proc~json_value_clone_func_nonrecursive json_core%json_value_clone_func_nonrecursive proc~json_clone->proc~json_value_clone_func_nonrecursive proc~json_value_clone_func->proc~json_value_clone_func

Called by

proc~~json_clone~~CalledByGraph proc~json_clone json_core%json_clone proc~assign_json_file json_file%assign_json_file proc~assign_json_file->proc~json_clone

Source Code

    subroutine json_clone(json,from,to,use_nonrecursive)

    implicit none

    class(json_core),intent(inout) :: json
    type(json_value),pointer :: from  !! this is the structure to clone
    type(json_value),pointer :: to    !! the clone is put here
                                      !! (it must not already be associated)
    logical(LK),intent(in),optional :: use_nonrecursive
                                      !! if true, use the non-recursive
                                      !! clone function [Default is False]

    logical(LK) :: use_nonrec !! local copy of `use_nonrecursive`

    !determine which clone function to use:
    if (present(use_nonrecursive)) then
        use_nonrec = use_nonrecursive
    else
        use_nonrec = .false.
    end if

    !call the main function:
    if (use_nonrec) then
        call json%json_value_clone_func_nonrecursive(from,to)
    else
        call json%json_value_clone_func(from,to)
    end if

    end subroutine json_clone