json_update_string Subroutine

private subroutine json_update_string(json, p, path, val, found, trim_str, adjustl_str)

Given the path string, if the variable is present, and is a scalar, then update its value. If it is not present, then create it and set its value.

Arguments

TypeIntentOptionalAttributesName
class(json_core), intent(inout) :: json
type(json_value), pointer:: p
character(kind=CK,len=*), intent(in) :: path

path to the variable in the structure

character(kind=CK,len=*), intent(in) :: val

the new value

logical(kind=LK), intent(out) :: found

if the variable was found and was a scalar.

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

if TRIM() should be called for the val (only used if val is present)

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

if ADJUSTL() should be called for the val (only used if val is present) (note that ADJUSTL is done before TRIM)


Contents

Source Code


Source Code

    subroutine json_update_string(json,p,path,val,found,trim_str,adjustl_str)

    implicit none

    class(json_core),intent(inout)      :: json
    type(json_value),pointer            :: p
    character(kind=CK,len=*),intent(in) :: path  !! path to the variable in the structure
    character(kind=CK,len=*),intent(in) :: val   !! the new value
    logical(LK),intent(out)             :: found !! if the variable was found and was a scalar.
    logical(LK),intent(in),optional     :: trim_str    !! if TRIM() should be called for the `val`
                                                       !! (only used if `val` is present)
    logical(LK),intent(in),optional     :: adjustl_str !! if ADJUSTL() should be called for the `val`
                                                       !! (only used if `val` is present)
                                                       !! (note that ADJUSTL is done before TRIM)

    type(json_value),pointer :: p_var
    integer(IK) :: var_type

    call json%get(p,path,p_var,found)
    if (found) then

        call json%info(p_var,var_type)
        select case (var_type)
        case (json_null,json_logical,json_integer,json_real,json_string)
            call json%to_string(p_var,val,trim_str=trim_str,adjustl_str=adjustl_str) ! update the value
        case default
            found = .false.
            call json%throw_exception('Error in json_update_string: '//&
                                      'the variable is not a scalar value',found)
        end select

    else
        call json%add_by_path(p,path,val)   !add the new element
    end if

    end subroutine json_update_string