1+ ! to use this include file, the following variables must be defined :
2+ ! * value : the scalar value to add
3+ ! * json_type : the JSON type of the value to add
4+ !
5+ ! also, the following subroutines must be defined :
6+ ! * assign () : assigns 'value' to p
7+ ! * create() : creates a new json_value of type 'json_type' with
8+ ! 'value' and name 'name', and returns it in 'tmp'
9+
10+ class(json_core),intent(inout) :: json
11+ type(json_value),pointer :: me !! the JSON structure
12+ character(kind=CK ,len=*),intent(in) :: path !! the path to the variable
13+ logical(LK ),intent(out),optional :: found !! if the variable was found
14+ logical(LK ),intent(out),optional :: was_created !! if the variable had to be created
15+
16+ type(json_value),pointer :: p
17+ type(json_value),pointer :: tmp
18+ character(kind=CK ,len=:),allocatable :: name !! variable name
19+
20+ if ( .not . json%exception_thrown ) then
21+
22+ nullify(p)
23+
24+ ! return a pointer to the path (possibly creating it)
25+ ! If the variable had to be created, then
26+ ! it will be a json_null variable.
27+ call json%create(me,path,p,found,was_created)
28+
29+ if (.not . associated(p)) then
30+
31+ call json%throw_exception(' Error in json_add_scalar_by_path:' // &
32+ ' Unable to resolve path: ' // trim(path),found)
33+ if (present(found)) then
34+ found = .false.
35+ call json%clear_exceptions()
36+ end if
37+
38+ else
39+
40+ !NOTE: a new object is created, and the old one
41+ ! is replaced and destroyed. This is to
42+ ! prevent memory leaks if the type is
43+ ! being changed (for example, if an array
44+ ! is being replaced with a scalar).
45+
46+ if (p%var_type==json_type) then
47+ call assign()
48+ else
49+ call json%info(p,name=name)
50+ call create()
51+ call json%replace(p,tmp,destroy=.true .)
52+ end if
53+ end if
54+
55+ else
56+ if ( present(found) ) found = .false.
57+ if ( present(was_created) ) was_created = .false.
58+ end if
0 commit comments