json_parse_string Subroutine

private subroutine json_parse_string(json, p, str)

Parse the JSON string and populate the json_value tree.

See also

Type Bound

json_core

Arguments

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

output structure

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

string with JSON data


Source Code

    subroutine json_parse_string(json, p, str)

    implicit none

    class(json_core),intent(inout)      :: json
    type(json_value),pointer            :: p     !! output structure
    character(kind=CK,len=*),intent(in) :: str   !! string with JSON data

    integer(IK),parameter :: iunit = 0 !! indicates that json data will be read from buffer

    logical(LK) :: has_duplicate  !! if checking for duplicate keys
    character(kind=CK,len=:),allocatable :: path !! path to any duplicate key

    ! clear any exceptions and initialize:
    call json%initialize()
    call json%prepare_parser()

    ! create the value and associate the pointer
    call json_value_create(p)

    ! Note: the name of the root json_value doesn't really matter,
    !  but we'll allocate something here just in case.
    p%name = CK_''

    ! parse as a value
    call json%parse_value(unit=iunit, str=str, value=p)
    call json%parse_end(unit=iunit, str=str)

    if (json%exception_thrown) then
        call json%annotate_invalid_json(iunit,str)
    else
        if (.not. json%allow_duplicate_keys) then
            call json%check_for_duplicate_keys(p,has_duplicate,path=path)
            if (.not. json%exception_thrown) then
                if (has_duplicate) then
                    call json%throw_exception('Error in json_parse_string: '//&
                                              'Duplicate key found: '//path)
                end if
            end if
        end if
    end if

    end subroutine json_parse_string